From: Steve Sutton Date: Tue, 14 Jul 2015 18:07:52 +0000 (-0400) Subject: Now importing content images. X-Git-Tag: v0.0.2^2~37 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=7dc6ae066a29f27a5fe79b69171fd77929a112a8;p=WP-Plugins%2Fglm-wp-importer.git Now importing content images. Goes thru the page content sections and imports images into media library. --- diff --git a/controllers/Admin.php b/controllers/Admin.php index dd21ff3..3bbf822 100644 --- a/controllers/Admin.php +++ b/controllers/Admin.php @@ -148,8 +148,13 @@ class GlmWPImporter_Admin_Controller 'type' => 'text' ), array( - 'name' => 'member_page_id', - 'label' => 'Ignore Pages', + 'name' => 'exclude_pages', + 'label' => 'Exclude Pages', + 'type' => 'text' + ), + array( + 'name' => 'include_pages', + 'label' => 'Include Pages', 'type' => 'text' ), ); diff --git a/controllers/Import.php b/controllers/Import.php index eb5f72e..7db6030 100644 --- a/controllers/Import.php +++ b/controllers/Import.php @@ -238,6 +238,21 @@ return $content; } + private function _getDescriptionImage($src) + { + $parsed = parse_url($src); + $fileName = basename($parsed['path']); + $rootUrl = str_replace('/' . $fileName, '', $src); + $img_id = $this->_handleMediaFile( + basename($parsed['path']), + '', + 0, + $rootUrl . '/' + ); + $image = wp_get_attachment_image_src($img_id, 'full'); + return $image[0]; + } + private function _displayFile($data) { $file_id = $this->_handleMediaFile($data['filename']); @@ -258,7 +273,7 @@ * @access private * @return void */ - private function _handleMediaFile($file, $caption = '', $post_id = 0) + private function _handleMediaFile($file, $caption = '', $post_id = 0, $baseUrl = null) { $id = array_search($file, $this->_files); if ($id === false) { @@ -269,7 +284,6 @@ 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; @@ -304,8 +318,6 @@ '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)) { @@ -326,12 +338,13 @@ * @access private * @return void */ - private function _fetchRemoteImage($file, $path) + private function _fetchRemoteImage($file, $path, $baseUrl = null) { $filename = wp_unique_filename($path, $file); $fp = fopen($path . '/' . $filename, 'w+'); - $ch = curl_init($this->_options['toolbox_image_url'] . $file); + $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); @@ -357,15 +370,22 @@ private function _fetchAllPages() { $this->_connect(); - $where = ''; - if ($this->_options['member_page_id']) { - $where = "WHERE id NOT IN ({$this->_options['member_page_id']}) - AND parent NOT IN ({$this->_options['member_page_id']}) "; + $where = array(); + if ($this->_options['include_pages']) { + $where[] = "id IN ({$this->_options['include_pages']}) + OR parent IN ({$this->_options['include_pages']}) "; + } + if ($this->_options['exclude_pages']) { + $where[] = "id NOT IN ({$this->_options['exclude_pages']}) + AND parent NOT IN ({$this->_options['exclude_pages']}) "; + } + if (!empty($where)) { + $WHERE = ' WHERE ' . implode(' AND ', $where); } $pageSql = " SELECT * FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_page_table']} - $where + $WHERE ORDER BY parent,pos"; $pageData = $this->_dbh->query($pageSql)->fetchAll(PDO::FETCH_ASSOC); $paragraphSql = " @@ -382,6 +402,7 @@ $fileStmt = $this->_dbh->prepare($fileSql); $data = $this->_dbh->query($pageSql)->fetchAll(); foreach ($data as &$page) { + $page['srcs'] = array(); $paraStmt->bindParam(':page', $page['id'], PDO::PARAM_INT); $paraStmt->execute(); $paragraphs = $paraStmt->fetchAll(PDO::FETCH_ASSOC); @@ -449,6 +470,14 @@ break; } $page['pageContent'] .= $paragraph['description']; + preg_match_all("/_getDescriptionImage($matches[1][$i]); + $parsUrl = parse_url($newSrc); + $imgName = basename($parsUrl['path']); + $page['pageContent'] = str_replace($matches[1][$i], $newSrc, $page['pageContent']); + $page['srcs'][] = $imgName; + } $fileStmt->bindParam(':pid', $paragraph['id'], PDO::PARAM_INT); $fileStmt->execute(); while ($file = $fileStmt->fetch()) { @@ -488,8 +517,6 @@ // 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'], @@ -506,16 +533,24 @@ $ID = wp_insert_post($post, true); $newPost = get_post($ID, ARRAY_A); $this->_post[$page['id']] = $newPost; + if (isset($page['srcs']) && !empty($page['srcs'])) { + foreach ($page['srcs'] as $image) { + $img_id = array_search($image, $this->_files); + if ($img_id) { + wp_update_post(array('ID' => $img_id, 'post_parent' => $ID)); + } + } + } // 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)); } } + } + if (isset($page['files']) && !empty($page['files'])) { foreach ($page['files'] as $file) { $file_id = array_search($file, $this->_files); if ($file_id) { @@ -523,9 +558,7 @@ } } } - } - //var_dump($pages); } /**