private $_dbh;
private $_post;
private $_options;
+ private $_files = array();
+ private $_images = array();
/**
*
* @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.'"]'
- .'<img class="align'.$alignment.' size-medium wp-image-'.$data['id'].'" '
- . ' src="' . TOOLBOX_IMAGE_URL . $data['image'].'"> '
+ $content .= '[caption id="attachment_'.$img_id.'" align="align'.$alignment.'"]'
+ .'<img class="align'.$alignment.' size-medium wp-image-' . $img_id . '" '
+ . ' src="' . $img_url . '"> '
. $data['caption'] . '[/caption]';
} else {
- $content .= '<img class="align'.$alignment.' size-medium wp-image-'.$data['id'].'" '
- . ' src="' . TOOLBOX_IMAGE_URL . $data['image'].'">';
+ $content .= '<img class="align'.$alignment.' size-medium wp-image-' . $img_id . '" '
+ . ' src="' . $img_url . '">';
}
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 '<pre>'.print_r($uploads, true).'</pre>';
+ $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 '<pre>'.print_r($attachment, true).'</pre>';
+ //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
*
$page['pageContent'] .= '<h1>'.$paragraph['title'].'</h1>';
}
if ($paragraph['image']) {
+ $page['images'][] = $paragraph['image'];
$page['pageContent'] .= $this->_displayImage($paragraph, $primaryAlign);
}
break;
$page['pageContent'] .= '<h2>'.$paragraph['title'].'</h2>';
}
if ($paragraph['image']) {
+ $page['images'][] = $paragraph['image'];
$page['pageContent'] .= $this->_displayImage($paragraph, $secondaryAlign);
}
break;
$align = $secondaryAlign;
}
if ($paragraph['image']) {
+ $page['images'][] = $paragraph['image'];
$page['pageContent'] .= $this->_displayImage($paragraph, $align);
}
break;
*/
private function _readOptions()
{
- var_dump($this->_options);
+ echo '<pre>' . print_r($this->_options, true) . '</pre>';
}
/**
echo '<p>Fetching Pages</p>';
// grab all pages and build the post array
$pages = $this->_fetchAllPages();
+
+ echo '<pre>' . print_r($this->_files, true) . '</pre>';
+ //exit;
foreach ($pages as $page) {
$post = array(
'post_content' => $page['pageContent'],
$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 '<pre>' . print_r($page['images'], true) . '</pre>';
+ //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);
}
/**
switch($step) {
case 0:
$this->_greet();
- //$this->_readOptions();
+ $this->_readOptions();
break;
case 1:
$this->_import();