WIP work on media and captions
authorSteve Sutton <steve@gaslightmedia.com>
Mon, 10 Aug 2015 21:03:55 +0000 (17:03 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Mon, 10 Aug 2015 21:03:55 +0000 (17:03 -0400)
Captions aren't saving correctly

NOTES.md
controllers/Import.php

index 8775d80..cdd64cf 100644 (file)
--- a/NOTES.md
+++ b/NOTES.md
@@ -65,6 +65,15 @@ X|Need to pull all image url’s out of the post_content sections and replace th
 
 #Process For Import:
 1. Build the html for the pages and import them into wordpress
-2. Import all media types from old toolbox: (ckeditor images, files, paragraphs)
-3. Update pages with the media id's 
+2. Import all media types from old toolbox: (ckeditor images, files, paragraph images)
+3. Update pages with the media id's
+4. Update pages with links to internal pages.
+5. Update pages with old keywords (keyword replace)
+
+
+### For 08/10/2015 ###
+I have the pages importing all in step 1.
+Step 2 I have all media being imported.
+Step Three I'm still working on getting the caption part updated correctly so it can see the image as editable and stay there when going from 
+'Visual' to 'Text' view. Currently it is loosing the caption when switching.
 
index cea8439..dde40a9 100644 (file)
             $img_url = $this->_options['toolbox_image_url'] . '/tbs1/' . $data['image'];
             $content = '';
             if (isset($data['caption']) && $data['caption']) {
-                $content .= '[caption align="align'.$alignment.'"]<img class="align'.$alignment.'" src="' . $img_url . '"> ' . $data['caption'] . '[/caption]';
+                $content .= '[caption align="align'.$alignment.'"]<img  src="' . $img_url . '"> ' . $data['caption'] . '[/caption]';
             } else if (isset($data['imagename']) && $data['imagename']) {
-                $content .= '[caption align="align'.$alignment.'"]<img class="align'.$alignment.'" src="' . $img_url . '"> ' . $data['imagename'] . '[/caption]';
+                $content .= '[caption align="align'.$alignment.'"]<img  src="' . $img_url . '"> ' . $data['imagename'] . '[/caption]';
             } else {
                 $content .= '<img class="align'.$alignment.'" src="' . $img_url . '">';
             }
             }
         }
 
+        private function _updateMediaSrc()
+        {
+            global $wpdb;
+            $images = get_option(GLM_WP_IMPORT_IMAGES_OPTION, array());
+            $sql = "
+            SELECT *
+              FROM {$wpdb->prefix}posts
+             WHERE post_content LIKE '%<img%'
+               AND post_type = 'page'";
+            $results = $wpdb->get_results($sql, OBJECT);
+            if (count($results) > 0) {
+                foreach ($results as $post) {
+                    $content = $post->post_content;
+                    echo '<pre>Page Title: ' . print_r($post->post_title, true)  . '</pre>';
+                    echo '<div style="border: 1px solid green; padding: 9px; margin: 10px;">';
+                    if (preg_match('%\[caption [^\]]+\]<img ([^>]+)>([^\[]*)\[/caption\]%', $post->post_content, $cMatch)) {
+                        echo '<pre>'.htmlspecialchars($cMatch[0]).'</pre>';
+                        echo '<pre>Caption Match: ' . print_r($cMatch[1], true)  . '</pre>';
+                        echo '<pre>Caption Text: ' . print_r($cMatch[2], true)  . '</pre>';
+                        if (preg_match('%src="([^"]+)"%', $cMatch[1], $srcMatch)) {
+                            echo '<pre>Sub Src Match: ' . print_r($srcMatch[1], true)  . '</pre>';
+                            $fileName = basename($srcMatch[1]);
+                            echo '<pre>File Name: ' . print_r($fileName, true)  . '</pre>';
+                            if ($imagesKey = array_search($fileName, $images)) {
+                                var_dump($imagesKey);
+                                $newImage = wp_get_attachment_image_src($imagesKey, 'medium');
+                                // need to replace the src tags in content with the new image tags
+                                $caption = str_replace($srcMatch[1], $newImage[0], $cMatch[0]);
+                                $caption = str_replace('<img', '<img class="size-medium wp-image-' . $imagesKey . '"', $caption);
+                                $caption = str_replace('[caption',
+                                    '[caption id="attachment_' . $imagesKey . '" width="' . $newImage[1] . '"',
+                                    $caption);
+                                echo '<pre>'.htmlspecialchars($caption).'</pre>';
+                                $content = str_replace($cMatch[0], $caption, $content);
+                            }
+                        }
+                    } else if (preg_match('%<img ([^>]+)>%', $post->post_content, $matches)) {
+                        echo '<pre>Image Match: ' . print_r($matches[1], true)  . '</pre>';
+                        echo '<pre>'.htmlspecialchars($matches[0]).'</pre>';
+                        if (preg_match('%src="([^"]+)"%', $matches[1], $srcMatch)) {
+                            echo '<pre>Sub Src Match: ' . print_r($srcMatch[1], true)  . '</pre>';
+                            $fileName = basename($srcMatch[1]);
+                            echo '<pre>File Name: ' . print_r($fileName, true)  . '</pre>';
+                            if ($imagesKey = array_search($fileName, $images)) {
+                                var_dump($imagesKey);
+                                $newUrl = wp_get_attachment_url($imagesKey);
+                                // need to replace the src tags in content with the new image tags
+                                $imageTag = str_replace($srcMatch[1], $newUrl, $matches[0]);
+                                $imageTag = str_replace('<img', '<img class="size-medium wp-image-' . $imagesKey . '"', $imageTag);
+                                echo '<pre>'.htmlspecialchars($imageTag).'</pre>';
+                                $content = str_replace($matches[0], $imageTag, $content);
+                            }
+                        }
+                    }
+                    echo '<pre>'.htmlspecialchars($content).'</pre>';
+                    echo '</div>';
+                    $updatePost = array(
+                        'ID'           => $post->ID,
+                        'post_content' => $content
+                    );
+                    wp_update_post($updatePost);
+                }
+            }
+        }
+
         private function _getAttachmentByName($name)
         {
             global $wpdb;
             }
             switch($step) {
             case 0:
-                //$this->post = array();
-                //$this->_storePost();
                 $this->_greet();
                 //$this->_readOptions();
                 //echo '<p><a href="admin.php?import=toolbox&amp;step=5&start=0">CkImages</a></p>';
                 echo '<pre>' . print_r($files, true) . '</pre>';
                 if (count($currentPostArray) > 0) {
                     echo '<p>Pages have been imported</p>';
-                    echo '<p><a href="admin.php?import=toolbox&amp;step=2&start=0">Step Two</a></p>';
+                    echo '<p><a href="admin.php?import=toolbox&amp;step=2">Step Two (Importing Images)</a></p>';
+                    echo '<p><a href="admin.php?import=toolbox&amp;step=3">Step Three (Updating Image Ref)</a></p>';
                 } else {
-                    echo '<p><a href="admin.php?import=toolbox&amp;step=1&start=0">Begin Import</a></p>';
+                    echo '<p><a href="admin.php?import=toolbox&amp;step=1">Begin Import</a></p>';
                 }
                 break;
             case 1:
                 $numPagesImported = $this->_import();
                 $start = filter_var($_REQUEST['start'], FILTER_VALIDATE_INT);
-                echo '<p>Start: ' . $start . '</p>';
-                if ($start === false) {
-                    $start = 0;
-                } else {
-                    $start += PAGES_PER_LOAD;
-                }
-                echo '<p>' . $numPagesImported . ' Pages Imported
-                    <form action="admin.php" method="get">
-                    <input type="hidden" name="import" value="toolbox">
-                    <input type="hidden" name="step" value="1">
-                    <input type="hidden" name="start" value="' . $start . '">
-                    <input type="submit" value="Continue">
-                    </form>
-                    </p>';
+                echo '<p>' . $numPagesImported . ' Pages Imported</p>';
                 echo '<p><a href="admin.php?import=toolbox&amp;step=2">Part 2</a></p>';
                 break;
             case 2:
                 //echo '<p><a href="admin.php?import=toolbox&amp;step=3">Part 3</a></p>';
                 break;
             case 3:
-                $this->_replaceUrls();
+                echo '<p>Phase Three</p>';
+                $this->_updateMediaSrc();
+                //$images = get_option(GLM_WP_IMPORT_IMAGES_OPTION, array());
+                //echo '<pre>' . print_r($images, true) . '</pre>';
+                //$files = get_option(GLM_WP_IMPORT_FILES_OPTION, array());
+                //echo '<pre>' . print_r($files, true) . '</pre>';
+                echo '<p>Done</p>';
                 break;
             case 4:
+                echo '<p>Phase Four</p>';
                 echo '<p>Test getting page sub ids</p>';
                 $subPageIds = $this->_getSubPageIds($this->_options['include_pages']);
                 echo '<pre>'.print_r($subPageIds, true).'</pre>';
                 break;
             case 5:
-                $this->_getCkImages();
+                echo '<p>Reset</p>';
+                $this->_post = $this->_images = $this->_files = array();
+                $this->_storePost();
+                $this->_storeImages();
+                $this->_storeFiles();
                 break;
             }