Working on blog images
authorSteve Sutton <steve@gaslightmedia.com>
Mon, 12 Oct 2015 21:00:55 +0000 (17:00 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Mon, 12 Oct 2015 21:00:55 +0000 (17:00 -0400)
controllers/BlogImageImport.php

index 55430ff..3897754 100644 (file)
             return $filename;
         }
 
+        private function checkImageUrls()
+        {
+            global $wpdb;
+            echo '<p>Replace Blog url\'s</p>';
+            $searchUrl = 'uploads/';
+            // 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>';
+            //echo '<pre>' . print_r($results, true) . '</pre>';
+            $upload_dir = wp_upload_dir();
+//            echo '<pre>' . print_r($upload_dir, true) . '</pre>';
+
+            if (count($results) > 0) {
+                $pattern = ';src="(' . $upload_dir['baseurl'] . '/[^"]+);si';
+                foreach ($results as $post) {
+                    $content = $post->post_content;
+                    preg_match_all($pattern, $post->post_content, $matches);
+//                    echo '<pre>'.print_r($matches, true).'</pre>';
+                    $matches = array_unique($matches[1]);
+                    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>parsed: ' . print_r($parsed, true) . '</pre>';
+                        echo '<pre>filename: ' . print_r($filename, true) . '</pre>';
+                        echo '<pre>RootUrl: ' . 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 replaceBlogUrls()
         {
             if (!$this->_options['blog_url']) {
                 break;
             case 1:
                 echo '<p>Updating Blog Images</p>';
-                $this->replaceBlogUrls();
+                //$this->replaceBlogUrls();
+                $this->checkImageUrls();
                 break;
             }