--- /dev/null
+<?php
+
+/**
+ * EventImageImport.php
+ *
+ * PHP version 5.3
+ *
+ * @category Importer
+ * @package GLM WP Importer
+ * @author Steve Sutton <steve@gaslightmedia.com>
+ * @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 <steve@gaslightmedia.com>
+ * @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 <steve@gaslightmedia.com>
+ * @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 '<div class="wrap">';
+ echo '<h2>'.__('Blog Image Importer').'</h2>';
+ }
+
+ private function _footer()
+ {
+ echo '</div>';
+ }
+
+ 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 '<p>Confirming Settings...</p>';
+ if (isset($checkErrors) && is_array($checkErrors) && !empty($checkErrors)) {
+ printf("<pre>Please update empty GLM Import <a href=\"%s\">Settings</a>:\n%s</pre>",
+ 'options-general.php?page=glmwpimporter',
+ implode("\n", $checkErrors)
+ );
+ } else {
+ echo '<p>Blog Images</p>';
+ 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 '<p>Replace Blog url\'s</p>';
+ $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 '<pre>'.$sql.'</pre>';
+ $results = $wpdb->get_results($sql, OBJECT);
+ echo '<pre>' . count($results) . '</pre>';
+
+ if (count($results) > 0) {
+ //echo '<pre>' . print_r($results, true) . '</pre>';
+ $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 '<pre>' . print_r($post->ID, true) . '</pre>';
+ echo '<pre>' . print_r($matches, true) . '</pre>';
+ foreach ($matches as $match) {
+ // process the file as media library
+ $parsed = parse_url($match);
+ $filename = basename($parsed['path']);
+ $rootUrl = str_replace('/' . $filename, '', $match);
+ echo '<pre>' . print_r($parsed, true) . '</pre>';
+ echo '<pre>' . print_r($filename, true) . '</pre>';
+ echo '<pre>' . print_r($rootUrl, true) . '</pre>';
+ $img_id = $this->_handleMediaFile(
+ $filename,
+ '',
+ '',
+ $post->ID,
+ $rootUrl . '/'
+ );
+ echo '<pre>' . print_r($img_id, true) . '</pre>';
+ $replaceUrl = wp_get_attachment_url($img_id);
+ echo '<pre>' . print_r($image, true) . '</pre>';
+ $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 '<p><a href="admin.php?import=blogimages&step=1">Import Images</a></p>';
+ }
+ break;
+ case 1:
+ echo '<p>Updating Blog Images</p>';
+ $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 <a href="%s">setup</a> first to setup the database options.',
+ 'import-blog-images'),
+ 'options-general.php?page=glmwpimporter'
+ ),
+ array($toolbox_import, 'dispatch')
+);