From 85635ab26e3408f05bb2d04777672c108240db7e Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Fri, 31 Jul 2015 13:54:47 -0400 Subject: [PATCH] Fix src for images 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 | 123 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 122 insertions(+), 1 deletion(-) diff --git a/controllers/Import.php b/controllers/Import.php index 0695eeb..635459a 100644 --- a/controllers/Import.php +++ b/controllers/Import.php @@ -817,6 +817,126 @@ } } + private function _replaceSrc() + { + global $wpdb; + $this->_connect(); + echo '

Replace page src\'s

'; + // find all pages with links to site pages + $sql = " + SELECT * + FROM {$wpdb->prefix}posts + WHERE post_content LIKE '%get_results($sql, OBJECT); + echo '

'.count($results).'

'; + if (count($results) > 0) { + $pattern = ';(]>);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 '
' . print_r($image, true)  . '
'; + if ($image->hasAttribute('class')) { + //echo '
' . print_r($image->getAttribute('class'), true)  . '
'; + } + if ($image->hasAttribute('style')) { + $style = $image->getAttribute('style'); + //echo '
' . print_r($style, true)  . '
'; + if (preg_match(';width[: ]+(\d+)px;', $style, $wMatch)) { + $width = $wMatch[1]; + } + if (preg_match(';height[: ]+(\d+)px;', $style, $hMatch)) { + $height = $hMatch[1]; + } + //echo '
' . print_r($width, true)  . '
'; + //echo '
' . print_r($height, true)  . '
'; + } + if ($image->hasAttribute('src')) { + //echo '
' . print_r($image->getAttribute('src'), true)  . '
'; + $imgName = basename($image->getAttribute('src')); + //echo '
' . print_r($imgName, true)  . '
'; + $imgPathInfo = pathinfo($imgName); + $wpImage = $this->_getAttachmentByName($imgPathInfo['filename']); + //echo '
' . print_r($wpImage, true) . '
'; + if ($wpImage) { + echo '

Image found: id '.$wpImage->ID.'

'; + //$strToReplace = $dom->saveHTML($image); + //echo '
'.htmlspecialchars($strToReplace).'
'; + if ($width && $height) { + //echo '

width: ' . $width . '

'; + //echo '

height: ' . $height . '

'; + if ($width > 300 || $height > 300) { + echo '

Found Large

'; + $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 '

Found Thumbnail

'; + $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 '

Found Medium

'; + $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 '

no media found

'; + } + } + } + // remove the extra stuff Dom put in + $content = preg_replace( + '/^/', + '', + str_replace( + array('', '', '', ''), + array('', '', '', ''), + $dom->saveHTML() + ) + ); + //echo '
'.print_r(htmlspecialchars($content), true).'
'; + $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; @@ -914,8 +1034,9 @@ echo '

Part 2

'; break; case 2: + $this->_replaceSrc(); //$this->_replaceKeywords(); - $this->replaceIsoUrls(); + //$this->replaceIsoUrls(); echo '

Done

'; //echo '

Part 3

'; break; -- 2.17.1