Updating the plugin for all steps
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 11 Aug 2015 19:50:01 +0000 (15:50 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 11 Aug 2015 19:50:01 +0000 (15:50 -0400)
Step 1 pull and build out pages.
Step 2 pull and setup all media (images and files)
Step 3 replace all image src tags and update caption elements
Step 4 replace keyword and site url links and href for is0

NOTES.md
controllers/Import.php

index cdd64cf..71c51e3 100644 (file)
--- a/NOTES.md
+++ b/NOTES.md
@@ -1,4 +1,4 @@
-#Notes
+# Notes #
 Resources for the image importing:
 
 Plugins that work well with importing the images into the Media Library.
@@ -12,11 +12,11 @@ I'm planning on using the same thing here.
 3. Fetch list of all images, files and import them into Media Library
 4. Update all pages with new references of the images.
 
-## REFERENCES:
+## REFERENCES: ##
 For Wordpress Plugin Development
 https://developer.wordpress.org/plugins/the-basics/header-requirements/
 
-## Importing pages
+## Importing pages ##
 https://codex.wordpress.org/Function_Reference/wp_insert_post
 ```
 IMPORTANT: Setting a value for $post['ID'] WILL NOT create a post with that ID number. Setting this value will cause the function to update the post with that ID number with the other values specified in $post. In short, to insert a new post, $post['ID'] must be blank or not set at all.
@@ -50,20 +50,19 @@ $post = array(
 );
 ```
 
-## Todo
+### Todo ###
 ac|todo item
 -|-----
 X|Need to import all the paragraph images into Media Library and assign them to their page.
 X|Getting Errors on some of the images
 X|Also need to fix the file permissions for the fetched images.
 X|Need to pull all image url’s out of the post_content sections and replace them with images that are imported into the Media Library.
- |Need to replace any ‘keywords’ with the correct url’s.
- |Need to replace any of the old site url's from their name-number/ to the correct wordpress ones.
- |Redo the import so if builds the pages without importing media then make that a second step
- |Have to use DOMDocument php functions to parse out all images src tags for their sizes and create correct size replacement for the page content.
+x|Need to replace any ‘keywords’ with the correct url’s.
+x|Need to replace any of the old site url's from their name-number/ to the correct wordpress ones.
+x|Redo the import so if builds the pages without importing media then make that a second step
 
 
-#Process For Import:
+#### 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, paragraph images)
 3. Update pages with the media id's
@@ -71,9 +70,9 @@ X|Need to pull all image url’s out of the post_content sections and replace th
 5. Update pages with old keywords (keyword replace)
 
 
-### For 08/10/2015 ###
+#### 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.
+Step 3 Updates all media src tags and caption tags
+Step 4 update keywords , site urls links inside, and update file refs
 
index dde40a9..5191137 100644 (file)
             }
         }
 
+        private function _updateFilesHref()
+        {
+            global $wpdb;
+            $files = get_option(GLM_WP_IMPORT_FILES_OPTION, array());
+            echo '<pre>' . print_r($files, true) . '</pre>';
+            //$images = get_option(GLM_WP_IMPORT_IMAGES_OPTION, array());
+            //$media = array_merge($images, $files);
+            //echo '<pre>' . print_r($media, true) . '</pre>';
+            $sql = "
+            SELECT *
+              FROM {$wpdb->prefix}posts
+             WHERE post_content LIKE '%http://is0.gaslightmedia.com%'
+               AND post_type = 'page'";
+            $results = $wpdb->get_results($sql, OBJECT);
+            if (count($results) > 0) {
+                foreach ($results as $post) {
+                    $content = $post->post_content;
+                    echo '<div style="border: 1px solid green; padding: 9px; margin: 10px;">';
+                    if (preg_match_all('%href="([^"]+)"%', $content, $isMatch)) {
+                        $isMatchCount = count($isMatch[0]);
+                        echo '<p>Matches is0 ' . $isMatchCount . '</p>';
+                        for ($index = 0; $index < $isMatchCount; ++$index) {
+                            echo '<pre>'.htmlspecialchars($isMatch[1][$index]).'</pre>';
+                            //echo '<pre>'.htmlspecialchars(basename($isMatch[1][$index])).'</pre>';
+                            //echo '<pre>'.print_r(pathinfo($isMatch[1][$index]), true).'</pre>';
+                            $key = array_search(basename($isMatch[1][$index]), $files);
+                            if ($key !== false) {
+                                var_dump($key);
+                                $newSrc = wp_get_attachment_url($key);
+                                $content = str_replace($isMatch[1][$index], $newSrc, $content);
+                            }
+                        }
+                    }
+                    echo '<pre>'.htmlspecialchars($content).'</pre>';
+                    echo '</div>';
+                    $updatePost = array(
+                        'ID'           => $post->ID,
+                        'post_content' => $content
+                    );
+                    //wp_update_post($updatePost);
+                }
+            }
+        }
+
         private function _updateMediaSrc()
         {
             global $wpdb;
             if (count($results) > 0) {
                 foreach ($results as $post) {
                     $content = $post->post_content;
-                    echo '<pre>Page Title: ' . print_r($post->post_title, true)  . '</pre>';
+                    echo '<pre>Updating Media Src\'s: ' . 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);
+                    if (preg_match_all('%\[caption [^\]]+\]<img ([^>]+)>([^\[]*)\[/caption\]%', $content, $cMatch)) {
+                        $captionCount = count($cMatch[0]);
+                        echo '<p>Matches in Caption ' . $captionCount . '</p>';
+                        //echo '<pre>' . print_r($cMatch, true)  . '</pre>';
+                        for ($index = 0; $index < $captionCount; ++$index) {
+                            //echo '<pre>cMatch: ' . print_r($cMatch, true)  . '</pre>';
+                            echo '<pre>'.htmlspecialchars($cMatch[0][$index]).'</pre>';
+                            echo '<pre>Caption Match: ' . print_r($cMatch[1][$index], true)  . '</pre>';
+                            echo '<pre>Caption Text: ' . print_r($cMatch[2][$index], true)  . '</pre>';
+                            if (preg_match('%src="([^"]+)"%', $cMatch[1][$index], $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(
+                                        'src="' . $srcMatch[1] . '"',
+                                        'src="' . $newImage[0] . '" width="' . $newImage[1] . '" height="' . $newImage[1] . '"',
+                                        $cMatch[0][$index]);
+                                    $caption = str_replace('<img', '<img class="wp-image-' . $imagesKey . ' size-medium"', $caption);
+                                    $caption = str_replace('[caption',
+                                        '[caption id="attachment_' . $imagesKey . '" width="' . $newImage[1] . '"',
+                                        $caption);
+                                    echo '<pre>'.htmlspecialchars($caption).'</pre>';
+                                    $content = str_replace($cMatch[0][$index], $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);
+                    }
+                    if (preg_match_all('%<img ([^>]+)>%', $content, $matches)) {
+                        $imageCount = count($matches[0]);
+                        for ($index = 0; $index < $imageCount; ++$index) {
+                            echo '<pre>Image Match: ' . $imageCount  . '</pre>';
+                            echo '<pre>'.htmlspecialchars($matches[0][$index]).'</pre>';
+                            if (preg_match('%src="([^"]+)"%', $matches[1][$index], $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][$index]);
+                                    if (preg_match('%class="([^"]+)"%', $matches[1][$index], $classMatch)) {
+                                        $imageTag = preg_replace('%class="([^"]+)"%', 'class="wp-image-' . $imagesKey . ' size-medium $1"', $imageTag);
+                                    } else {
+                                        $imageTag = str_replace('<img', '<img class="wp-image-' . $imagesKey . ' size-medium"', $imageTag);
+                                    }
+                                    //echo '<pre>'.htmlspecialchars($imageTag).'</pre>';
+                                    $content = str_replace($matches[0][$index], $imageTag, $content);
+                                }
                             }
                         }
                     }
                 $numPagesImported = $this->_import();
                 $start = filter_var($_REQUEST['start'], FILTER_VALIDATE_INT);
                 echo '<p>' . $numPagesImported . ' Pages Imported</p>';
-                echo '<p><a href="admin.php?import=toolbox&amp;step=2">Part 2</a></p>';
+                echo '<p><a href="admin.php?import=toolbox&amp;step=2">Step Two (Importing Images)</a></p>';
                 break;
             case 2:
                 echo '<p>Now processing the images from old database tables</p>';
                 //$this->_replaceKeywords();
                 //$this->replaceIsoUrls();
                 echo '<p>Done</p>';
+                echo '<p><a href="admin.php?import=toolbox&amp;step=3">Step Three (Updating Image Ref)</a></p>';
                 //echo '<p><a href="admin.php?import=toolbox&amp;step=3">Part 3</a></p>';
                 break;
             case 3:
                 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>';
+                echo '<p>Files</p>';
+                $this->_updateFilesHref();
+                echo '<p>Site Url</p>';
+                $this->_replaceUrls();
+                $this->_replaceKeywords();
+                echo '<p>Done</p>';
                 break;
             case 5:
                 echo '<p>Reset</p>';