Updates for the fix paragraph feature/fixImages
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 15 Oct 2015 14:07:45 +0000 (10:07 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 15 Oct 2015 14:07:45 +0000 (10:07 -0400)
Building in how to redo images with captions and correct the images that
were inserted more than once.

controllers/ToolboxImport.php

index aec01a9..d143efa 100644 (file)
                     $sql = "
                     SELECT *
                       FROM {$wpdb->prefix}posts
-                     WHERE post_content like '%{$attachment_guid}%'";
+                     WHERE post_content like '%{$baseImage}%'";
                     $page_results = $wpdb->get_row($sql, ARRAY_A);
                     if ( count( $page_results ) > 0 ) {
                         $page_id      = $page_results['ID'];
                         $post_content = $page_results['post_content'];
                         //echo '<pre>' . htmlspecialchars($post_content) . '</pre>';
                         // get the img tag for this image
-                        $imgPattern = '%<img class="([^"]+)" src="' . $attachment_guid . '"'
-                            . '( ?[^>]*) ?/?>%si';
+                        $imgPattern = '%(?:\[caption id="attachment_(\d+)" align="align(?:left|right)" width="(\d+)"\])?'
+                        . '<img class="([^"]+)" src="([^"]+' . $baseImage . '[^"]+)"'
+                        . '( ?[^>]*) ?/?>(?:[^\[]+\[/caption\])?%si';
                         //echo '<pre>pattern '.htmlspecialchars($imgPattern).'</pre>';
                         if (preg_match($imgPattern, $post_content, $matches)) {
-                            $matched_string = $matches[0];
-                            //echo '<pre>matches: '."\n";
-                            //foreach ($matches as $key => $match) {
-                                //echo "$key = " . htmlspecialchars($match)."\n";
-                            //}
-                            //echo '</pre>';
-                            $new_image_tag = preg_replace('%width="\d+"%', '', $matched_string);
-                            $new_image_tag = preg_replace('%height="\d+"%', '', $new_image_tag);
-                            $new_image_tag = str_replace('size-full', 'size-medium', $new_image_tag);
-                            //echo '<p>$new_image_tag = ' . htmlspecialchars($new_image_tag).'</p>';
+                            $matched_string  = $matches[0];
+                            $matched_img_src = $matches[4];
+                            // get the wp-image-# from this match if they
+                            // aren't the real # then delete the attachment
+                            if (preg_match_all('%wp-image-(\d+)%', $matched_string, $wp_image_matches)) {
+                                //echo '<pre>$wp_image_matches: ' . print_r($wp_image_matches, true) . '</pre>';
+                                foreach ($wp_image_matches[1] as $wp_image_id) {
+                                    if ($wp_image_id != $attachment_id) {
+                                        wp_delete_attachment($wp_image_id, true);
+                                    }
+                                    //echo '<pre>$wp_image_matches: ' . print_r($wp_image_id, true) . '</pre>';
+                                }
+                            }
+                            echo '<pre>matches: '."\n";
+                            foreach ($matches as $key => $match) {
+                                echo "$key = " . htmlspecialchars($match)."\n";
+                            }
+                            echo '</pre>';
+                            $new_image_tag = preg_replace('%class="[^"]+"%', 'class="{classes}"', $matched_string);
+                            // remove all size
+                            $new_image_tag = preg_replace('%attachment_\d+%', 'attachment_' . $attachment_id, $new_image_tag);
+                            $new_image_tag = preg_replace('%size-\w+%', '', $new_image_tag);
+                            $new_image_tag = preg_replace('%width="\d+"%', 'width="{width}"', $new_image_tag);
+                            $new_image_tag = preg_replace('%height="\d+"%', 'height="{height}"', $new_image_tag);
+                            $classes = array();
+                            $classes[] = 'wp-image-' . $attachment_id;
+                            // Image processing part
+                            $this->_refetchImage(
+                                $page_id,
+                                $attachment_id,
+                                $attachment_guid
+                            );
+                            $new_image_url = wp_get_attachment_image_src($attachment_id, 'medium');
+                            echo '<pre>' . print_r($matched_img_src, true) . '</pre>';
+                            echo '<pre>' . print_r($new_image_url, true) . '</pre>';
+                            $classes[] = 'size-medium';
+                            $new_image_tag = str_replace('{classes}', implode(' ', $classes), $new_image_tag);
+                            $new_image_tag = str_replace('{width}', $new_image_url[1], $new_image_tag);
+                            $new_image_tag = str_replace('{height}', $new_image_url[2], $new_image_tag);
+                            $new_image_tag = str_replace($matched_img_src, $new_image_url[0], $new_image_tag);
+                            echo '<p>$new_image_tag = ' . htmlspecialchars($new_image_tag).'</p>';
                             $post_content = str_replace($matched_string, $new_image_tag, $post_content);
                         } else {
-                            echo '<p>No Matches</p>';
+                            echo '<p>No Matches for ' . $baseImage . '</p>';
                         }
                         echo '<pre>page_id: ' . print_r($page_id, true) . '</pre>';
                     } else {
                         continue;
                     }
 
-                    // Image processing part
-
-                    $this->_refetchImage(
-                        $page_id,
-                        $attachment_id,
-                        $attachment_guid
-                    );
-                    // get the image size. If it is too large then use the medium size
-                    $new_image_url = wp_get_attachment_image_src($attachment_id, 'medium');
-
-                    //echo '<p>$new_image_url = ' . $new_image_url[0] . '</p>';
-                    //echo '<pre>$new_image_url: ' . print_r($new_image_url, true) . '</pre>';
-
-                    // replace url for image src in the page
-
-                    // update the attachment posts with the parent page id
-                    $post_content = str_replace($attachment_guid, $new_image_url[0], $post_content);
                     $updatePost = array(
                         'ID'           => $page_id,
                         'post_content' => $post_content