From: Steve Sutton Date: Thu, 6 Aug 2015 21:08:52 +0000 (-0400) Subject: WIP for western UP X-Git-Tag: v0.0.2^2~13 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=d719f0a40f507ef64861c4e818e03f0416688886;p=WP-Plugins%2Fglm-wp-importer.git WIP for western UP --- diff --git a/NOTES.md b/NOTES.md index 3bf091c..8775d80 100644 --- a/NOTES.md +++ b/NOTES.md @@ -62,3 +62,9 @@ X|Need to pull all image url’s out of the post_content sections and replace th |Redo the import so if builds the pages without importing media then make that a second step |Have to use DOMDocument php functions to parse out all images src tags for their sizes and create correct size replacement for the page content. + +#Process For Import: +1. Build the html for the pages and import them into wordpress +2. Import all media types from old toolbox: (ckeditor images, files, paragraphs) +3. Update pages with the media id's + diff --git a/controllers/Import.php b/controllers/Import.php index 45a7286..c36b3a4 100644 --- a/controllers/Import.php +++ b/controllers/Import.php @@ -61,39 +61,17 @@ private $_images = array(); - /** - * __construct - * - * @access public - * @return void - */ public function __construct() { $this->_getOptions(); } - /** - * _getOptions - * - * Retrieve the Import options - * - * @access private - * @return void - */ 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); } - /** - * _connect - * - * Connect to the Toolbox Database - * - * @access private - * @return void - */ private function _connect() { if ($this->_dbh) { @@ -113,41 +91,17 @@ ); } - /** - * _header - * - * Build Admin Page Header - * - * @access private - * @return void - */ private function _header() { echo '
'; echo '

'.__('Toolbox Importer').'

'; } - /** - * _footer - * - * Build Admin Page Footer - * - * @access private - * @return void - */ private function _footer() { echo '
'; } - /** - * _checkRequiredOptions - * - * Verifies that important settings aren't empty - * - * @access private - * @return void - */ private function _checkRequiredOptions() { $errors = array(); @@ -178,16 +132,6 @@ return $errors; } - /** - * _greet - * - * Greet Screen when you first goto the Toolbox Import - * Runs checks on the settings. - * Won't allow you to pass unless all is O.K. - * - * @access private - * @return void - */ private function _greet() { // Verify that everything is setup @@ -213,48 +157,19 @@ } } - /** - * _displayImage - * - * Build the HTML code for the Images - * - * @param mixed $data - * @param mixed $alignment - * - * @access private - * @return void - */ private function _displayImage($data, $alignment) { if (!isset($data['image'])) { return false; } - if (isset($data['caption'])) { - $img_id = $this->_handleMediaFile($data['image'], $data['caption']); - } else if ($data['imagename']) { - $img_id = $this->_handleMediaFile($data['image'], $data['imagename']); - } else { - $img_id = $this->_handleMediaFile($data['image']); - } - if (!$img_id) { - return false; - } - $image = wp_get_attachment_image_src($img_id, 'medium'); - $img_url = $image[0]; + $img_url = $this->_options['toolbox_image_url'] . '/tbs1/' . $data['image']; $content = ''; if (isset($data['caption']) && $data['caption']) { - $content .= '[caption id="attachment_'.$img_id.'" align="align'.$alignment.'"]' - .' ' - . $data['caption'] . '[/caption]'; + $content .= '[caption align="align'.$alignment.'"] ' . $data['caption'] . '[/caption]'; } else if (isset($data['imagename']) && $data['imagename']) { - $content .= '[caption id="attachment_'.$img_id.'" align="align'.$alignment.'"]' - .' ' - . $data['imagename'] . '[/caption]'; + $content .= '[caption align="align'.$alignment.'"] ' . $data['imagename'] . '[/caption]'; } else { - $content .= ''; + $content .= ''; } return $content; } @@ -276,21 +191,22 @@ private function _displayFile($data) { - if ($this->_options['toolbox_paragraphs_table'] == 'pages') { - $file_id = $this->_handleMediaFile($data['filename']); - } else { - $file_id = $this->_handleMediaFile( - $data['filename'], - '', - 0, - $this->_options['site_url'] . 'uploads/' - ); - } + //if ($this->_options['toolbox_paragraphs_table'] == 'pages') { + //$file_id = $this->_handleMediaFile($data['filename']); + //} else { + //$file_id = $this->_handleMediaFile( + //$data['filename'], + //'', + //0, + //$this->_options['site_url'] . 'uploads/' + //); + //} - if (!$file_id) { - return false; - } - $fileUrl = wp_get_attachment_url($file_id); + //if (!$file_id) { + //return false; + //} + //$fileUrl = wp_get_attachment_url($file_id); + $fileUrl =$this->_options['toolbox_image_url'] . '/original/' . $data['filename']; $fileName = ($data['urltext']) ? $data['urltext'] : $data['filename']; return "\n".''.$fileName.''; } @@ -314,16 +230,6 @@ return ($id) ? $this->_getAttachmentById($id) : false; } - - /** - * _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, $baseUrl = null) { $id = array_search($file, $this->_files); @@ -380,15 +286,6 @@ return $id; } - /** - * _fetchRemoteImage - * - * @param mixed $file File name - * @param mixed $filename Path of new file - * - * @access private - * @return void - */ private function _fetchRemoteImage($file, $path, $baseUrl = null) { $filename = wp_unique_filename($path, $file); @@ -460,14 +357,6 @@ return $WHERE; } - /** - * _fetchAllPages - * - * Fetch all the toolbox pages - * - * @access private - * @return void - */ private function _fetchAllPages($limit = null, $offset = 0) { $this->_connect(); @@ -477,13 +366,14 @@ FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_page_table']} $WHERE ORDER BY parent,pos"; - $pageSql .= " LIMIT $limit OFFSET $offset"; + $pageSql .= ($limit) ? " LIMIT $limit OFFSET $offset" : ''; $pageData = $this->_dbh->query($pageSql)->fetchAll(PDO::FETCH_ASSOC); if ($this->_options['toolbox_page_table'] == 'pages') { $paragraphSql = " SELECT * FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_paragraphs_table']} WHERE page = :page + AND active = true ORDER BY pos"; $paraStmt = $this->_dbh->prepare($paragraphSql); $fileSql = " @@ -549,7 +439,7 @@ $page['pageContent'] .= '

'.$page['category'].'

'; ++$iterator; if ($page['image']) { - $page['images'][] = $page['image']; + //$page['images'][] = $page['image']; $page['pageContent'] .= $this->_displayImage($page, $primaryAlign); } $page['pageContent'] .= $page['description']; @@ -562,7 +452,7 @@ $page['pageContent'] .= '

'.$paragraph['title'].'

'; } if (isset($paragraph['image']) && $paragraph['image']) { - $page['images'][] = $paragraph['image']; + //$page['images'][] = $paragraph['image']; $page['pageContent'] .= $this->_displayImage($paragraph, $primaryAlign); } break; @@ -574,7 +464,7 @@ $page['pageContent'] .= '

'.$paragraph['title'].'

'; } if (isset($paragraph['image']) && $paragraph['image']) { - $page['images'][] = $paragraph['image']; + //$page['images'][] = $paragraph['image']; $page['pageContent'] .= $this->_displayImage($paragraph, $secondaryAlign); } break; @@ -591,15 +481,16 @@ $align = $secondaryAlign; } if (isset($paragraph['image']) && $paragraph['image']) { - $page['images'][] = $paragraph['image']; + //$page['images'][] = $paragraph['image']; $page['pageContent'] .= $this->_displayImage($paragraph, $align); } break; } $page['pageContent'] .= $paragraph['description']; - preg_match_all("/' . print_r($matches, true) . ''; + /* for ($i = 0; $i < count($matches[0]); $i++) { if ($matches[1][$i]) { $newSrc = $this->_getDescriptionImage($matches[1][$i]); @@ -609,12 +500,13 @@ $page['srcs'][] = $imgName; } } + */ //var_dump($paragraph['id']); $fileStmt->bindParam(':pid', $paragraph['id'], PDO::PARAM_INT); $fileStmt->execute(); while ($file = $fileStmt->fetch()) { //echo '
Files: ' . print_r($file, true) . '
'; - $page['files'][] = $file['filename']; + //$page['files'][] = $file['filename']; $page['pageContent'] .= $this->_displayFile($file); } ++$iterator; @@ -624,36 +516,22 @@ return $data; } - /** - * _readOptions - * - * Display all Import options - * - * @access private - * @return void - */ private function _readOptions() { echo '
' . print_r($this->_options, true) . '
'; echo '
' . print_r(get_option(GLM_WP_IMPORT_POST_OPTION, array()), true) . '
'; } - /** - * _import - * - * Handles the page import - * - * @access private - * @return void - */ private function _import() { echo '

Fetching Pages

'; $this->_post = get_option(GLM_WP_IMPORT_POST_OPTION, array()); // grab all pages and build the post array $start = filter_var($_REQUEST['start'], FILTER_VALIDATE_INT); - $pages = $this->_fetchAllPages(PAGES_PER_LOAD, $start); + //$pages = $this->_fetchAllPages(PAGES_PER_LOAD, $start); + $pages = $this->_fetchAllPages(); //echo '
' . print_r($pages, true) . '
'; + //return; //exit; $numPagesImported = count($pages); @@ -683,31 +561,6 @@ $ID = wp_insert_post($post, true); $newPost = get_post($ID, ARRAY_A); $this->_post[$page['id']] = $ID; - 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'])) { - 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) { - wp_update_post(array('ID' => $file_id, 'post_parent' => $ID)); - } - } - } } $this->_storePost(); return $numPagesImported; @@ -837,23 +690,26 @@ } } - function handle_accents($content) { - // from: http://www.php.net/manual/en/domdocument.loadhtml.php#91513 - if ( !empty( $content ) && function_exists( 'mb_convert_encoding' ) ) { - mb_detect_order( "ASCII,UTF-8,ISO-8859-1,windows-1252,iso-8859-15" ); - if ( empty( $encod ) ) - $encod = mb_detect_encoding( $content ); + function _handleAccents($content) + { + // from: http://www.php.net/manual/en/domdocument.loadhtml.php#91513 + if ( !empty( $content ) && function_exists( 'mb_convert_encoding' ) ) { + mb_detect_order( "ASCII,UTF-8,ISO-8859-1,windows-1252,iso-8859-15" ); + if ( empty( $encod ) ) { + $encod = mb_detect_encoding( $content ); + } $headpos = mb_strpos( $content,'' ); - if ( FALSE === $headpos ) - $headpos= mb_strpos( $content,'' ); - if ( FALSE !== $headpos ) { - $headpos+=6; - $content = mb_substr( $content,0,$headpos ) . '' .mb_substr( $content,$headpos ); + if ( FALSE === $headpos ) { + $headpos= mb_strpos( $content,'' ); + } + if ( FALSE !== $headpos ) { + $headpos+=6; + $content = mb_substr( $content,0,$headpos ) . '' .mb_substr( $content,$headpos ); + } + $content = mb_convert_encoding( $content, 'HTML-ENTITIES', $encod ); } - $content = mb_convert_encoding( $content, 'HTML-ENTITIES', $encod ); + return $content; } - return $content; - } private function _replaceSrc() { @@ -1121,15 +977,21 @@ } } - /** - * dispatch - * - * Dispatch Function. - * Checks which step admin user is on and calls appropriate methods. - * - * @access public - * @return void - */ + private function _getCkImages() + { + global $wpdb; + if ($this->_options['ckeditor_schema'] && $this->_options['ckeditor_images_table']) { + $this->_connect(); + $ckimagesSql = " + SELECT * + FROM {$this->_options['ckeditor_schema']}.{$this->_options['ckeditor_images_table']}"; + $ckData = $this->_dbh->query($ckimagesSql)->fetchAll(); + echo '
' . print_r($ckData, true) . '
'; + } else { + return false; + } + } + public function dispatch() { $this->_header(); @@ -1145,6 +1007,7 @@ //$this->_storePost(); $this->_greet(); $this->_readOptions(); + echo '

CkImages

'; echo '

Begin Import

'; break; case 1: @@ -1181,6 +1044,9 @@ $subPageIds = $this->_getSubPageIds($this->_options['include_pages']); echo '
'.print_r($subPageIds, true).'
'; break; + case 5: + $this->_getCkImages(); + break; } $this->_footer();