From: Steve Sutton Date: Wed, 30 Sep 2015 15:19:37 +0000 (-0400) Subject: Add blog image importer X-Git-Tag: v0.0.2^2~3 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=69cb496be078a4df9a9309cbb2748ab099ab26d7;p=WP-Plugins%2Fglm-wp-importer.git Add blog image importer Fetches blog images from the blog url to transfer them into the dev site. --- 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/BlogImageImport.php b/controllers/BlogImageImport.php new file mode 100644 index 0000000..55430ff --- /dev/null +++ b/controllers/BlogImageImport.php @@ -0,0 +1,301 @@ + + * @copyright 2013 Gaslight Media + * @license Gaslight Media + * @version SVN: (0.0.1) + * @link <> + */ + +/** + * EventImageImport + * + * Main Controller for the Gaslight WP Importer Plugin + * + * @category Importer + * @package GLM WP Importer + * @author Steve Sutton + * @copyright 2013 Gaslight Media + * @license Gaslight Media + * @release Release: (0.0.1) + * @link <> + */ + if (!defined('WP_LOAD_IMPORTERS')) { + return; + } + require_once ABSPATH . 'wp-admin/includes/import.php'; + + if ( !class_exists( 'WP_Importer' ) ) { + $class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php'; + if ( file_exists( $class_wp_importer ) ) { + require_once $class_wp_importer; + } + } + + if (class_exists('WP_Importer')) { + + /** + * BlogImage_Import + * + * @uses WP_Importer + * @package Webdav + * @version //autogen// + * @copyright Copyright (c) 2010 All rights reserved. + * @author Steve Sutton + * @license PHP Version 3.0 {@link http://www.php.net/license/3_0.txt} + */ + class BlogImage_Import extends WP_Importer + { + private $_dbh; + private $_post; + private $_options; + + + public function __construct() + { + $this->_getOptions(); + } + + private function _getOptions() + { + // read options for the import and if they aren't set then give a warning. + $this->_options = get_option(GLM_WP_IMPORT_SETTINGS); + } + + private function _connect() + { + if ($this->_dbh) { + return; + } + // try making a postgres connection to the database + $connString = 'dbname=' . $this->_options['db_name']; + $connString .= ' host=' . $this->_options['db_host']; + $connString .= ' user=' . $this->_options['db_user']; + $driverOptions = array( + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC + ); + $this->_dbh = new PDO('pgsql:'.$connString, null, null, $driverOptions); + $this->_dbh->setAttribute( + PDO::ATTR_ERRMODE, + PDO::ERRMODE_EXCEPTION + ); + } + + private function _header() + { + echo '
'; + echo '

'.__('Blog Image Importer').'

'; + } + + private function _footer() + { + echo '
'; + } + + private function _checkRequiredOptions() + { + $errors = array(); + if (!$this->_options['blog_url']) { + $errors[] = 'Blog URL'; + } + return $errors; + } + + private function _greet() + { + // Verify that everything is setup + $checkErrors = $this->_checkRequiredOptions(); + echo '

Confirming Settings...

'; + if (isset($checkErrors) && is_array($checkErrors) && !empty($checkErrors)) { + printf("
Please update empty GLM Import Settings:\n%s
", + 'options-general.php?page=glmwpimporter', + implode("\n", $checkErrors) + ); + } else { + echo '

Blog Images

'; + return true; + } + } + + private function _handleMediaFile( + $file, + $file_title, + $caption = '', + $post_id = 0, + $baseUrl = null + ) { + set_time_limit(120); + if ($post_id) { + $post = get_post($post_id); + } + if (!(($uploads = wp_upload_dir()) && false === $uploads['error'])) { + return new WP_Error('upload_error', $uploads['error']); + } + $filename = $this->_fetchRemoteImage($file, $uploads['path'], $baseUrl); + $new_file = $uploads['path'] . '/' . $filename; + $url = $uploads['url'] . '/' . $filename; + $return = apply_filters('wp_handle_upload', array('file' => $new_file, 'url' => $url, 'type' => wp_check_filetype($file, null))); + $new_file = $return['file']; + $url = $return['url']; + $type = $return['type']; + + $title = preg_replace('!\.[^.]+$!', '', basename($file)); + $content = ''; + // use image exif/iptc data for title and caption defaults if possible + if ($image_meta = wp_read_image_metadata($new_file)) { + if ('' != trim( $image_meta['title'])) { + $title = trim( $image_meta['title']); + } + if ('' != trim( $image_meta['caption'])) { + $content = trim($image_meta['caption']); + } + } + $post_date = current_time('mysql'); + $post_date_gmt = current_time('mysql', 1); + // Construct the attachment array + $wp_filetype = wp_check_filetype(basename($filename), null); + $attachment = array( + 'post_mime_type' => $wp_filetype['type'], + 'guid' => $url, + 'post_parent' => $post_id, + 'post_title' => $file_title, + 'post_name' => $title, + 'post_content' => $content, + 'post_excerpt' => $caption, + 'post_date' => $post_date, + 'post_date_gmt' => $post_date_gmt + ); + // Insert attachment + $id = wp_insert_attachment($attachment, $new_file, $post_id); + if (!is_wp_error($id)) { + $data = wp_generate_attachment_metadata($id, $new_file); + wp_update_attachment_metadata($id, $data); + $this->_files[$id] = $file; + } + return $id; + } + + private function _fetchRemoteImage($file, $path, $baseUrl = null) + { + $filename = wp_unique_filename($path, $file); + + $fp = fopen($path . '/' . $filename, 'w+'); + $fileUrl = ($baseUrl) ? $baseUrl . '/' . $file : $this->_options['toolbox_image_url'] . $file; + $ch = curl_init($fileUrl); + curl_setopt($ch, CURLOPT_TIMEOUT, 50); + curl_setopt($ch, CURLOPT_FILE, $fp); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + $imgData = curl_exec($ch); + $httpCode = curl_getinfo($ch); + curl_close($ch); + fclose($fp); + // Set correct file permissions + $oldUmask = umask(0); + chmod( $path . '/' . $filename, 0660 ); + umask($oldUmask); + return $filename; + } + + private function replaceBlogUrls() + { + if (!$this->_options['blog_url']) { + return false; + } + global $wpdb; + $this->_connect(); + echo '

Replace Blog url\'s

'; + $searchUrl = $this->_options['blog_url']; + // 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 = ';(' . $this->_options['blog_url'] . '/[^"]+);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); + } + } + } + + public function dispatch() + { + $this->_header(); + + if (empty($_GET['step'])) { + $step = 0; + } else { + $step = filter_var($_GET['step'], FILTER_VALIDATE_INT); + } + switch($step) { + case 0: + if ($this->_greet()) { + echo '

Import Images

'; + } + break; + case 1: + echo '

Updating Blog Images

'; + $this->replaceBlogUrls(); + break; + } + + $this->_footer(); + } + } +} + +$toolbox_import = new BlogImage_Import(); + +register_importer( + 'blogimages', + __('Blog Images', 'import-blog-images'), + sprintf( + __('Import the images of Blog from blog.sitename. + Visit setup first to setup the database options.', + 'import-blog-images'), + 'options-general.php?page=glmwpimporter' + ), + array($toolbox_import, 'dispatch') +); diff --git a/index.php b/index.php index c8367b3..43c67cc 100644 --- a/index.php +++ b/index.php @@ -18,6 +18,7 @@ if (is_admin()) { require_once 'controllers/Admin.php'; require_once 'controllers/ToolboxImport.php'; require_once 'controllers/EventImageImport.php'; + require_once 'controllers/BlogImageImport.php'; register_activation_hook(__FILE__, array('GlmWPImporter_Admin_Controller', 'activate_plugin')); register_deactivation_hook(__FILE__, array('GlmWPImporter_Admin_Controller', 'deactivate_plugin')); register_uninstall_hook(__FILE__, array('GlmWPImporter_Admin_Controller', 'uninstall'));