From 646f73e115471340571901738ec7c5f67942ca7c Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Tue, 29 Dec 2015 15:53:24 -0500 Subject: [PATCH] Fixes for the updating blog images. This was written for correcting images for the Staffords blogs. --- controllers/BlogImageImport.php | 220 +++++++++++++++++++++++++++++--- controllers/ToolboxImport.php | 4 +- 2 files changed, 206 insertions(+), 18 deletions(-) diff --git a/controllers/BlogImageImport.php b/controllers/BlogImageImport.php index 55430ff..b1d4e6c 100644 --- a/controllers/BlogImageImport.php +++ b/controllers/BlogImageImport.php @@ -182,9 +182,23 @@ return $id; } + private function _getAttachmentByName($name) + { + global $wpdb; + $sql = " + SELECT * + FROM {$wpdb->prefix}posts + WHERE post_type = 'attachment' + AND (post_name like '{$name}%' OR post_title like '{$name}%')"; + $results = $wpdb->get_results($sql, OBJECT); + return (!empty($results)) ? $results[0]->ID : false; + //return (!empty($results)) ? $results[0] : false; + } + private function _fetchRemoteImage($file, $path, $baseUrl = null) { - $filename = wp_unique_filename($path, $file); + $saveFileName = preg_replace( '/[?].*$/', '', $file ); + $filename = wp_unique_filename($path, $saveFileName); $fp = fopen($path . '/' . $filename, 'w+'); $fileUrl = ($baseUrl) ? $baseUrl . '/' . $file : $this->_options['toolbox_image_url'] . $file; @@ -203,12 +217,95 @@ return $filename; } + private function _updateMediaSrc() + { + global $wpdb; + + $sql = " + SELECT * + FROM {$wpdb->prefix}posts + WHERE post_content LIKE '%get_results($sql, OBJECT); + + echo count( $results ); + + if (count($results) > 0) { + foreach ($results as $post) { + $content = $post->post_content; + echo '
Updating Media Src\'s: ' . print_r($post->post_title, true)  . '
'; + echo '
'; + if (preg_match_all('%(:?\[caption [^\]]+\])?]+)>(:?([^\[]*)\[/caption\])?%', $content, $cMatch)) { + $captionCount = count($cMatch[0]); + //echo '

Matches in Caption ' . $captionCount . '

'; + //echo '
' . var_export($cMatch, true)  . '
'; + for ($index = 0; $index < $captionCount; ++$index) { + //echo '
cMatch: ' . print_r($cMatch[0][$index], true)  . '
'; + echo '
'.htmlspecialchars($cMatch[0][$index]).'
'; + //echo '
Caption Match: ' . print_r($cMatch[1][$index], true)  . '
'; + //echo '
Caption Text: ' . print_r($cMatch[2][$index], true)  . '/End Caption Text
'; + if (preg_match('%src="([^"]+)"%', $cMatch[0][$index], $srcMatch)) { + //echo '
' . print_r($srcMatch, true) . '
'; + echo '
Sub Src Match: ' . print_r($srcMatch[1], true)  . '
'; + $fileInfo = pathinfo($srcMatch[1]); + $fileName = basename($srcMatch[1]); + $fileBaseName = $fileInfo['filename']; + echo '
File Info: ' . print_r($fileInfo, true)  . '
'; + echo '
File Name: ' . print_r($fileName, true)  . '
'; + $path = str_replace($fileName, '', $srcMatch[1]); + $path = $fileInfo['dirname']; + //echo '
'.var_dump($path).'
'; + + $imagesKey = $this->_getAttachmentByName($fileBaseName); + var_dump($fileBaseName); + var_dump($imagesKey); + if (!$imagesKey) { + $imagesKey = $this->_handleMediaFile( + $fileName, + $fileBaseName, + '', + $post->ID, + $path + ); + } + + if ($imagesKey) { + var_dump($imagesKey); + $newImage = wp_get_attachment_image_src($imagesKey, 'full'); + // need to replace the src tags in content with the new image tags + $caption = str_replace( + 'src="' . $srcMatch[1] . '"', + 'src="' . $newImage[0] . '"', + $cMatch[0][$index]); + $caption = preg_replace('/wp-image-([0-9]*)/', 'wp-image-' . $imagesKey , $caption); + $caption = preg_replace('/attachment_([0-9]*)/', 'attachment_' . $imagesKey, $caption); + $content = str_replace($cMatch[0][$index], $caption, $content); + } + } + } + } + //echo '
'.htmlspecialchars($caption).'
'; + //exit; + echo '
'.htmlspecialchars($content).'
'; + echo '
'; + $updatePost = array( + 'ID' => $post->ID, + 'post_content' => $content + ); + wp_update_post($updatePost); + } + } + } + private function replaceBlogUrls() { if (!$this->_options['blog_url']) { return false; } global $wpdb; + $images = array(); $this->_connect(); echo '

Replace Blog url\'s

'; $searchUrl = $this->_options['blog_url']; @@ -229,34 +326,110 @@ $content = $post->post_content; preg_match_all($pattern, $post->post_content, $matches); $matches = array_unique($matches[0]); - echo '
' . print_r($post->ID, true) . '
'; + //echo '
' . print_r($post->ID, true) . '
'; echo '
' . print_r($matches, true) . '
'; foreach ($matches as $match) { // process the file as media library $parsed = parse_url($match); $filename = basename($parsed['path']); $rootUrl = str_replace('/' . $filename, '', $match); - echo '
' . print_r($parsed, true) . '
'; + $images[] = $filename; echo '
' . print_r($filename, true) . '
'; - echo '
' . print_r($rootUrl, true) . '
'; - $img_id = $this->_handleMediaFile( - $filename, - '', - '', - $post->ID, - $rootUrl . '/' - ); - echo '
' . print_r($img_id, true) . '
'; - $replaceUrl = wp_get_attachment_url($img_id); - echo '
' . print_r($image, true) . '
'; - $content = str_replace($match, $replaceUrl, $content); + $fileInfo = pathinfo($match); + $fileBaseName = $fileInfo['filename']; + $img_id = $this->_getAttachmentByName($fileBaseName); + if ( $img_id ) { + echo '
' . print_r($img_id, true) . '
'; + $replaceUrl = wp_get_attachment_url($img_id); + echo '
' . print_r($image, true) . '
'; + $content = str_replace($match, $replaceUrl, $content); + echo 'Replaced ' . $filename . ''; + } } + //echo '
'.htmlspecialchars($content).'
'; $updatePost = array( 'ID' => $post->ID, 'post_content' => $content ); wp_update_post($updatePost); } + echo '
' . print_r($images, true) . '
'; + } + } + + public function findLauryImages() + { + global $wpdb; + + $author_id = 4; // Laury + + $the_query = new WP_Query( + array( + 'posts_per_page' => -1, + 'post_type' => 'attachment', + 'post_status' => 'inherit', + 'author' => $author_id + ) + ); + if ( $the_query->have_posts() ) : + echo '

found ' . $the_query->found_posts . '

'; + while ( $the_query->have_posts() ) : $the_query->the_post(); + echo $the_query->post->ID . '
'; + echo get_the_title() . '
'; + echo wp_get_attachment_url( ) . '
'; + wp_delete_attachment( $the_query->post->ID, true ); + endwhile; + endif; + + } + + function checkForBlogImages() + { + global $wpdb; + + $sql = " + SELECT * + FROM {$wpdb->prefix}posts + WHERE post_content LIKE '%get_results($sql, OBJECT); + + echo count( $results ); + + if (count($results) > 0) { + foreach ($results as $post) { + $content = $post->post_content; + echo '
Updating Media Src\'s: ' . print_r($post->post_title, true)  . '
'; + echo '
'; + if (preg_match_all('%(:?\[caption [^\]]+\])?]+)>(:?([^\[]*)\[/caption\])?%', $content, $cMatch)) { + $captionCount = count($cMatch[0]); + //echo '

Matches in Caption ' . $captionCount . '

'; + //echo '
' . var_export($cMatch, true)  . '
'; + for ($index = 0; $index < $captionCount; ++$index) { + //echo '
cMatch: ' . print_r($cMatch[0][$index], true)  . '
'; + echo '
'.htmlspecialchars($cMatch[0][$index]).'
'; + //echo '
Caption Match: ' . print_r($cMatch[1][$index], true)  . '
'; + //echo '
Caption Text: ' . print_r($cMatch[2][$index], true)  . '/End Caption Text
'; + if (preg_match('%src="([^"]+)"%', $cMatch[0][$index], $srcMatch)) { + //echo '
' . print_r($srcMatch, true) . '
'; + echo '
Sub Src Match: ' . print_r($srcMatch[1], true)  . '
'; + $fileInfo = pathinfo($srcMatch[1]); + $fileName = basename($srcMatch[1]); + $fileBaseName = $fileInfo['filename']; + echo '
File Info: ' . print_r($fileInfo, true)  . '
'; + echo '
File Name: ' . print_r($fileName, true)  . '
'; + $path = str_replace($fileName, '', $srcMatch[1]); + $path = $fileInfo['dirname']; + //echo '
'.var_dump($path).'
'; + + } + } + } + + echo '
'.htmlspecialchars($content).'
'; + echo '
'; + } } } @@ -272,13 +445,28 @@ switch($step) { case 0: if ($this->_greet()) { - echo '

Import Images

'; + echo '

Update Blog Images

'; + echo '

Update Blog URL\'s

'; + echo '

Remove Laury\'s Images

'; } + $this->checkForBlogImages(); break; case 1: + echo '

Update Blog URL\'s

'; + echo '

Remove Laury\'s Images

'; echo '

Updating Blog Images

'; + $this->_updateMediaSrc(); + break; + case 2: + echo '

Update Blog Images

'; + echo '

Remove Laury\'s Images

'; $this->replaceBlogUrls(); break; + case 3: + echo '

Update Blog Images

'; + echo '

Update Blog URL\'s

'; + $this->findLauryImages(); + break; } $this->_footer(); diff --git a/controllers/ToolboxImport.php b/controllers/ToolboxImport.php index 10a4df9..6398cbc 100644 --- a/controllers/ToolboxImport.php +++ b/controllers/ToolboxImport.php @@ -153,7 +153,7 @@ SELECT count(*) FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_page_table']} $WHERE"; - //echo '
' . $sql . '
'; + echo '
' . $sql . '
'; $totalPages = $this->_dbh->query($sql)->fetchColumn(); printf('

Found %d Pages

', $totalPages); //echo '

Part 2

'; @@ -321,7 +321,7 @@ $pageIds = array(); $sql = " SELECT id - FROM {$this->_options['toolbox_page_table']} + FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_page_table']} WHERE parent = :parent"; $stmt = $this->_dbh->prepare($sql); $stmt->bindParam(':parent', $parent, PDO::PARAM_INT); -- 2.17.1