From: Steve Sutton Date: Thu, 15 Oct 2015 14:07:45 +0000 (-0400) Subject: Updates for the fix paragraph X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/index.cgi?a=commitdiff_plain;h=refs%2Fheads%2Ffeature%2FfixImages;p=WP-Plugins%2Fglm-wp-importer.git Updates for the fix paragraph Building in how to redo images with captions and correct the images that were inserted more than once. --- diff --git a/controllers/ToolboxImport.php b/controllers/ToolboxImport.php index aec01a9..d143efa 100644 --- a/controllers/ToolboxImport.php +++ b/controllers/ToolboxImport.php @@ -1372,30 +1372,62 @@ $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 '
' . htmlspecialchars($post_content) . '
'; // get the img tag for this image - $imgPattern = '%]*) ?/?>%si'; + $imgPattern = '%(?:\[caption id="attachment_(\d+)" align="align(?:left|right)" width="(\d+)"\])?' + . ']*) ?/?>(?:[^\[]+\[/caption\])?%si'; //echo '
pattern '.htmlspecialchars($imgPattern).'
'; if (preg_match($imgPattern, $post_content, $matches)) { - $matched_string = $matches[0]; - //echo '
matches: '."\n";
-                            //foreach ($matches as $key => $match) {
-                                //echo "$key = " . htmlspecialchars($match)."\n";
-                            //}
-                            //echo '
'; - $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 '

$new_image_tag = ' . htmlspecialchars($new_image_tag).'

'; + $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 '
$wp_image_matches: ' . print_r($wp_image_matches, true) . '
'; + foreach ($wp_image_matches[1] as $wp_image_id) { + if ($wp_image_id != $attachment_id) { + wp_delete_attachment($wp_image_id, true); + } + //echo '
$wp_image_matches: ' . print_r($wp_image_id, true) . '
'; + } + } + echo '
matches: '."\n";
+                            foreach ($matches as $key => $match) {
+                                echo "$key = " . htmlspecialchars($match)."\n";
+                            }
+                            echo '
'; + $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 '
' . print_r($matched_img_src, true) . '
'; + echo '
' . print_r($new_image_url, true) . '
'; + $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 '

$new_image_tag = ' . htmlspecialchars($new_image_tag).'

'; $post_content = str_replace($matched_string, $new_image_tag, $post_content); } else { - echo '

No Matches

'; + echo '

No Matches for ' . $baseImage . '

'; } echo '
page_id: ' . print_r($page_id, true) . '
'; } else { @@ -1403,23 +1435,6 @@ 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 '

$new_image_url = ' . $new_image_url[0] . '

'; - //echo '
$new_image_url: ' . print_r($new_image_url, true) . '
'; - - // 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