From: Steve Sutton Date: Tue, 14 Jul 2015 11:38:43 +0000 (-0400) Subject: Setup processing for images X-Git-Tag: v0.0.2^2~39 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=57e9c4f39e53716e44a24c178a6a1a96c0adfcc3;p=WP-Plugins%2Fglm-wp-importer.git Setup processing for images Create an array of images when looping through the paragraphs. Then right after adding the page to wordpress assign the images that were created as media to their page. --- diff --git a/NOTES.md b/NOTES.md index 04764b6..6b693f9 100644 --- a/NOTES.md +++ b/NOTES.md @@ -51,6 +51,9 @@ $post = array( ``` ## Todo +Need to import all the paragraph images into Media Library and assign them to their page. +Getting Errors on some of the images +Also need to fix the file permissions for the fetched images. Need to pull all image url’s out of the post_content sections and replace them with images that are imported into the Media Library. Need to replace any ‘keywords’ with the correct url’s. diff --git a/controllers/Admin.php b/controllers/Admin.php index 2303fa0..98f45d5 100644 --- a/controllers/Admin.php +++ b/controllers/Admin.php @@ -142,6 +142,11 @@ class GlmWPImporter_Admin_Controller 'label' => 'CKEditor Images Table', 'type' => 'text' ), + array( + 'name' => 'toolbox_image_url', + 'label' => 'Toolbox Image Url', + 'type' => 'text' + ), array( 'name' => 'member_page_id', 'label' => 'Page ID for Members Only', diff --git a/controllers/Import.php b/controllers/Import.php index ef0f1f6..33ced49 100644 --- a/controllers/Import.php +++ b/controllers/Import.php @@ -56,6 +56,8 @@ private $_dbh; private $_post; private $_options; + private $_files = array(); + private $_images = array(); /** @@ -212,23 +214,126 @@ * * @param mixed $data * @param mixed $alignment + * * @access private * @return void */ private function _displayImage($data, $alignment) { + $img_id = $this->_handleMediaFile($data['image'], $data['caption']); + if (!$img_id) { + return false; + } + $img_url = wp_get_attachment_url($img_id); if ($data['caption']) { - $content .= '[caption id="attachment_'.$data['id'].'" align="align'.$alignment.'"]' - .' ' + $content .= '[caption id="attachment_'.$img_id.'" align="align'.$alignment.'"]' + .' ' . $data['caption'] . '[/caption]'; } else { - $content .= ''; + $content .= ''; } return $content; } + /** + * _handleMediaFile + * + * @param mixed $file The file name + * @param int $post_id Parent Post id if given + * + * @access private + * @return void + */ + private function _handleMediaFile($file, $caption = '', $post_id = 0) + { + $id = array_search($file, $this->_files); + if ($id === false) { + set_time_limit(120); + if ($post_id) { + $post = get_post($post_id); + } + if (!(($uploads = wp_upload_dir($time)) && false === $uploads['error'])) { + return new WP_Error('upload_error', $uploads['error']); + } + //echo '
'.print_r($uploads, true).'
'; + $filename = $this->_fetchRemoteImage($file, $uploads['path']); + $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' => $title, + 'post_name' => $title, + 'post_content' => $content, + 'post_excerpt' => $caption, + 'post_date' => $post_date, + 'post_date_gmt' => $post_date_gmt + ); + //echo '
'.print_r($attachment, true).'
'; + //exit; + // 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; + } + + /** + * _fetchRemoteImage + * + * @param mixed $file File name + * @param mixed $filename Path of new file + * + * @access private + * @return void + */ + private function _fetchRemoteImage($file, $path) + { + $filename = wp_unique_filename($path, $file); + + $fp = fopen($path . '/' . $filename, 'w+'); + $ch = curl_init($this->_options['toolbox_image_url'] . $file); + 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 + $stat = stat( dirname( $path . '/' . $filename ) ); + $perms = $stat['mode'] & 0000666; + @chmod( $path . '/' . $filename, $perms ); + return $filename; + } + /** * _fetchAllPages * @@ -297,6 +402,7 @@ $page['pageContent'] .= '

'.$paragraph['title'].'

'; } if ($paragraph['image']) { + $page['images'][] = $paragraph['image']; $page['pageContent'] .= $this->_displayImage($paragraph, $primaryAlign); } break; @@ -305,6 +411,7 @@ $page['pageContent'] .= '

'.$paragraph['title'].'

'; } if ($paragraph['image']) { + $page['images'][] = $paragraph['image']; $page['pageContent'] .= $this->_displayImage($paragraph, $secondaryAlign); } break; @@ -318,6 +425,7 @@ $align = $secondaryAlign; } if ($paragraph['image']) { + $page['images'][] = $paragraph['image']; $page['pageContent'] .= $this->_displayImage($paragraph, $align); } break; @@ -339,7 +447,7 @@ */ private function _readOptions() { - var_dump($this->_options); + echo '
' . print_r($this->_options, true) . '
'; } /** @@ -355,6 +463,9 @@ echo '

Fetching Pages

'; // grab all pages and build the post array $pages = $this->_fetchAllPages(); + + echo '
' . print_r($this->_files, true) . '
'; + //exit; foreach ($pages as $page) { $post = array( 'post_content' => $page['pageContent'], @@ -371,8 +482,20 @@ $ID = wp_insert_post($post, true); $newPost = get_post($ID, ARRAY_A); $this->_post[$page['id']] = $newPost; + // update images so they are attached to the proper page + if (isset($page['images']) && !empty($page['images'])) { + echo '
' . print_r($page['images'], true) . '
'; + //exit; + foreach ($page['images'] as $image) { + $img_id = array_search($image, $this->_files); + if ($img_id) { + wp_update_post(array('ID' => $img_id, 'post_parent' => $ID)); + } + } + } + } - var_dump($pages); + //var_dump($pages); } /** @@ -396,7 +519,7 @@ switch($step) { case 0: $this->_greet(); - //$this->_readOptions(); + $this->_readOptions(); break; case 1: $this->_import();