update for blog images feature/fixBlogImages
authorSteve Sutton <steve@gaslightmedia.com>
Mon, 19 Oct 2015 18:13:09 +0000 (14:13 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Mon, 19 Oct 2015 18:13:09 +0000 (14:13 -0400)
controllers/Admin.php
controllers/Import.php

index 354072e..db0824d 100644 (file)
@@ -92,6 +92,11 @@ class GlmWPImporter_Admin_Controller
                 'label' => 'Site URL',
                 'type'  => 'text'
             ),
+            array(
+                'name'  => 'blog_url',
+                'label' => 'Blog URL',
+                'type'  => 'text'
+            ),
             array(
                 'name'  => 'db_host',
                 'label' => 'Database Host',
index 43a77fb..6a19a2f 100644 (file)
             }
         }
 
+        private function replaceBlogUrls()
+        {
+            global $wpdb;
+            $this->_connect();
+            echo '<p>Replace Blog url\'s</p>';
+            $searchUrl = 'http://blog.mountpleasantwow.com';
+            // find all pages with links to site pages
+            $sql = "
+            SELECT *
+              FROM {$wpdb->prefix}posts
+             WHERE post_content LIKE '%{$searchUrl}%'
+               AND post_type = 'post'";
+            echo '<pre>'.$sql.'</pre>';
+            $results = $wpdb->get_results($sql, OBJECT);
+            echo '<pre>' . count($results) . '</pre>';
+
+            if (count($results) > 0) {
+                //echo '<pre>' . print_r($results, true) . '</pre>';
+                $pattern = ';(http://blog.mountpleasantwow.com/[^"]+);si';
+                foreach ($results as $post) {
+                    $content = $post->post_content;
+                    preg_match_all($pattern, $post->post_content, $matches);
+                    $matches = array_unique($matches[0]);
+                    echo '<pre>' . print_r($post->ID, true) . '</pre>';
+                    echo '<pre>' . print_r($matches, true) . '</pre>';
+                    foreach ($matches as $match) {
+                        // process the file as media library
+                        $parsed   = parse_url($match);
+                        $filename = basename($parsed['path']);
+                        $rootUrl  = str_replace('/' . $filename, '', $match);
+                        echo '<pre>' . print_r($parsed, true) . '</pre>';
+                        echo '<pre>' . print_r($filename, true) . '</pre>';
+                        echo '<pre>' . print_r($rootUrl, true) . '</pre>';
+                        $img_id = $this->_handleMediaFile(
+                            $filename,
+                            '',
+                            '',
+                            $post->ID,
+                            $rootUrl . '/'
+                        );
+                        echo '<pre>' . print_r($img_id, true) . '</pre>';
+                        $replaceUrl = wp_get_attachment_url($img_id);
+                        echo '<pre>' . print_r($image, true) . '</pre>';
+                        $content = str_replace($match, $replaceUrl, $content);
+                     }
+                    $updatePost = array(
+                        'ID'           => $post->ID,
+                        'post_content' => $content
+                    );
+                    wp_update_post($updatePost);
+                }
+            }
+        }
         private function _getCkImages()
         {
             global $wpdb;
                 }
                 break;
             case 1:
-                echo '<p>Reset</p>';
-                $this->_post = $this->_images = $this->_files = array();
-                $this->_storePost();
-                $this->_storeImages();
-                $this->_storeFiles();
-                $numPagesImported = $this->_import();
-                echo '<p>' . $numPagesImported . ' Pages Imported</p>';
-                echo '<p><a href="admin.php?import=toolbox&amp;step=2">Step Two (Importing Images)</a></p>';
+                echo '<p>Updating Blog Images</p>';
+                $this->replaceBlogUrls();
                 break;
+            //case 1:
+                //echo '<p>Reset</p>';
+                //$this->_post = $this->_images = $this->_files = array();
+                //$this->_storePost();
+                //$this->_storeImages();
+                //$this->_storeFiles();
+                //$numPagesImported = $this->_import();
+                //echo '<p>' . $numPagesImported . ' Pages Imported</p>';
+                //echo '<p><a href="admin.php?import=toolbox&amp;step=2">Step Two (Importing Images)</a></p>';
+                //break;
             case 2:
                 echo '<p>Done</p>';
                 echo '<p>Now processing the images from old database tables</p>';