Fix src for images
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 31 Jul 2015 17:54:47 +0000 (13:54 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 31 Jul 2015 17:54:47 +0000 (13:54 -0400)
This fixes why some images after the import aren't editable.
Finds all src tags in content and finds the images from the Media
Library to match it and places the class attribute with the wp-image
class with it's id so it can be referenced. Also fixes the size of these
images when they we're adjusted.style attribute part.

controllers/Import.php

index 0695eeb..635459a 100644 (file)
             }
         }
 
+        private function _replaceSrc()
+        {
+            global $wpdb;
+            $this->_connect();
+            echo '<p>Replace page src\'s</p>';
+            // find all pages with links to site pages
+            $sql = "
+            SELECT *
+              FROM {$wpdb->prefix}posts
+             WHERE post_content LIKE '%<img%'
+               AND post_type = 'page'";
+            //$sql .= " AND post_title = 'Business Assistance'";
+            $results = $wpdb->get_results($sql, OBJECT);
+            echo '<p>'.count($results).'</p>';
+            if (count($results) > 0) {
+                $pattern = ';(<img[^>]>);si';
+                foreach ($results as $post) {
+                    $dom = new DOMDocument();
+                    $test = $dom->loadHTML($post->post_content);
+                    //var_dump($test);
+                    $images = $dom->getElementsByTagName('img');
+                    foreach ($images as $image) {
+                        $width = $height = null;
+                        //echo '<pre>' . print_r($image, true)  . '</pre>';
+                        if ($image->hasAttribute('class')) {
+                            //echo '<pre>' . print_r($image->getAttribute('class'), true)  . '</pre>';
+                        }
+                        if ($image->hasAttribute('style')) {
+                            $style = $image->getAttribute('style');
+                            //echo '<pre>' . print_r($style, true)  . '</pre>';
+                            if (preg_match(';width[: ]+(\d+)px;', $style, $wMatch)) {
+                                $width = $wMatch[1];
+                            }
+                            if (preg_match(';height[: ]+(\d+)px;', $style, $hMatch)) {
+                                $height = $hMatch[1];
+                            }
+                            //echo '<pre>' . print_r($width, true)  . '</pre>';
+                            //echo '<pre>' . print_r($height, true)  . '</pre>';
+                        }
+                        if ($image->hasAttribute('src')) {
+                            //echo '<pre>' . print_r($image->getAttribute('src'), true)  . '</pre>';
+                            $imgName = basename($image->getAttribute('src'));
+                            //echo '<pre>' . print_r($imgName, true)  . '</pre>';
+                            $imgPathInfo = pathinfo($imgName);
+                            $wpImage = $this->_getAttachmentByName($imgPathInfo['filename']);
+                            //echo '<pre>' . print_r($wpImage, true) . '</pre>';
+                            if ($wpImage) {
+                                echo '<p>Image found: id '.$wpImage->ID.'</p>';
+                                //$strToReplace = $dom->saveHTML($image);
+                                //echo '<pre>'.htmlspecialchars($strToReplace).'</pre>';
+                                if ($width && $height) {
+                                    //echo '<p>width: ' . $width . '</p>';
+                                    //echo '<p>height: ' . $height . '</p>';
+                                    if ($width > 300 || $height > 300) {
+                                        echo '<p>Found Large</p>';
+                                        $size = 'large';
+                                        $newImage = wp_get_attachment_image_src($wpImage->ID, $size);
+                                        $image->setAttribute('class', 'alignnone wp-image-' . $wpImage->ID);
+                                        if($newImage) {
+                                            $image->setAttribute('src', $newImage[0]);
+                                        }
+                                    } else if ($width < 150 && $height < 150) {
+                                        echo '<p>Found Thumbnail</p>';
+                                        $size = 'thumbnail';
+                                        $newImage = wp_get_attachment_image_src($wpImage->ID, $size);
+                                        $image->setAttribute('class', 'alignnone size-' . $size . ' wp-image-' . $wpImage->ID);
+                                        if($newImage) {
+                                            $image->setAttribute('src', $newImage[0]);
+                                        }
+                                    } else {
+                                        echo '<p>Found Medium</p>';
+                                        $size = 'medium';
+                                        $newImage = wp_get_attachment_image_src($wpImage->ID, $size);
+                                        $image->setAttribute('class', 'alignnone size-' . $size . ' wp-image-' . $wpImage->ID);
+                                        if($newImage) {
+                                            $image->setAttribute('src', $newImage[0]);
+                                        }
+                                    }
+                                    $image->removeAttribute('style');
+                                    $image->setAttribute('width', $width);
+                                    $image->setAttribute('height', $height);
+                                    //echo htmlspecialchars($dom->saveHTML($image));
+                                }
+                            } else {
+                                //echo '<p>no media found</p>';
+                            }
+                        }
+                    }
+                    // remove the extra stuff Dom put in
+                    $content = preg_replace(
+                        '/^<!DOCTYPE.+?>/',
+                        '',
+                        str_replace(
+                            array('<html>', '</html>', '<body>', '</body>'),
+                            array('', '', '', ''),
+                            $dom->saveHTML()
+                        )
+                    );
+                    //echo '<pre>'.print_r(htmlspecialchars($content), true).'</pre>';
+                    $updatePost = array(
+                        'ID'           => $post->ID,
+                        'post_content' => $content
+                    );
+                    wp_update_post($updatePost);
+                }
+            }
+        }
+
+        private function _getAttachmentByName($name)
+        {
+            global $wpdb;
+            $sql = "
+            SELECT *
+              FROM {$wpdb->prefix}posts
+             WHERE post_type = 'attachment'
+            AND post_title = '{$name}'";
+            $results = $wpdb->get_results($sql, OBJECT);
+            return (!empty($results)) ? $results[0] : false;
+        }
+
         private function replaceIsoUrls()
         {
             global $wpdb;
                 echo '<p><a href="admin.php?import=toolbox&amp;step=2">Part 2</a></p>';
                 break;
             case 2:
+                $this->_replaceSrc();
                 //$this->_replaceKeywords();
-                $this->replaceIsoUrls();
+                //$this->replaceIsoUrls();
                 echo '<p>Done</p>';
                 //echo '<p><a href="admin.php?import=toolbox&amp;step=3">Part 3</a></p>';
                 break;