From: Steve Sutton Date: Mon, 19 Oct 2015 18:13:09 +0000 (-0400) Subject: update for blog images X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/index.cgi?a=commitdiff_plain;h=refs%2Fheads%2Ffeature%2FfixBlogImages;p=WP-Plugins%2Fglm-wp-importer.git update for blog images --- diff --git a/controllers/Admin.php b/controllers/Admin.php index 354072e..db0824d 100644 --- a/controllers/Admin.php +++ b/controllers/Admin.php @@ -92,6 +92,11 @@ class GlmWPImporter_Admin_Controller 'label' => 'Site URL', 'type' => 'text' ), + array( + 'name' => 'blog_url', + 'label' => 'Blog URL', + 'type' => 'text' + ), array( 'name' => 'db_host', 'label' => 'Database Host', diff --git a/controllers/Import.php b/controllers/Import.php index 43a77fb..6a19a2f 100644 --- a/controllers/Import.php +++ b/controllers/Import.php @@ -1187,6 +1187,59 @@ } } + private function replaceBlogUrls() + { + global $wpdb; + $this->_connect(); + echo '

Replace Blog url\'s

'; + $searchUrl = 'http://blog.mountpleasantwow.com'; + // find all pages with links to site pages + $sql = " + SELECT * + FROM {$wpdb->prefix}posts + WHERE post_content LIKE '%{$searchUrl}%' + AND post_type = 'post'"; + echo '
'.$sql.'
'; + $results = $wpdb->get_results($sql, OBJECT); + echo '
' . count($results) . '
'; + + if (count($results) > 0) { + //echo '
' . print_r($results, true) . '
'; + $pattern = ';(http://blog.mountpleasantwow.com/[^"]+);si'; + foreach ($results as $post) { + $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($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) . '
'; + 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); + } + $updatePost = array( + 'ID' => $post->ID, + 'post_content' => $content + ); + wp_update_post($updatePost); + } + } + } private function _getCkImages() { global $wpdb; @@ -1308,15 +1361,19 @@ } break; case 1: - echo '

Reset

'; - $this->_post = $this->_images = $this->_files = array(); - $this->_storePost(); - $this->_storeImages(); - $this->_storeFiles(); - $numPagesImported = $this->_import(); - echo '

' . $numPagesImported . ' Pages Imported

'; - echo '

Step Two (Importing Images)

'; + echo '

Updating Blog Images

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

Reset

'; + //$this->_post = $this->_images = $this->_files = array(); + //$this->_storePost(); + //$this->_storeImages(); + //$this->_storeFiles(); + //$numPagesImported = $this->_import(); + //echo '

' . $numPagesImported . ' Pages Imported

'; + //echo '

Step Two (Importing Images)

'; + //break; case 2: echo '

Done

'; echo '

Now processing the images from old database tables

';