From 748e079281546c03502cf689160036bb179898bf Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Fri, 7 Oct 2016 13:11:45 -0400 Subject: [PATCH] Updating the image reference importing Adding option for site that is using ht_images in them. Updating how the images are fetch by using rawurlencode to the file name. This will help with file names that have spaces in them. --- controllers/Admin.php | 5 ++ controllers/ToolboxImport.php | 93 +++++++++++++++++++---------------- 2 files changed, 56 insertions(+), 42 deletions(-) diff --git a/controllers/Admin.php b/controllers/Admin.php index 8721dea..7f1cf27 100644 --- a/controllers/Admin.php +++ b/controllers/Admin.php @@ -92,6 +92,11 @@ class GlmWPImporter_Admin_Controller 'label' => 'Place the navigation title into page content', 'type' => 'checkbox' ), + array( + 'name' => 'site_uses_ht_images', + 'label' => 'Site uses old ht_images directory in images', + 'type' => 'checkbox', + ), array( 'name' => 'site_url', 'label' => 'Site URL', diff --git a/controllers/ToolboxImport.php b/controllers/ToolboxImport.php index 54a11c7..43e5d6d 100644 --- a/controllers/ToolboxImport.php +++ b/controllers/ToolboxImport.php @@ -227,13 +227,8 @@ return ($id) ? $this->_getAttachmentById($id) : false; } - private function _handleMediaFile( - $file, - $file_title, - $caption = '', - $post_id = 0, - $baseUrl = null - ) { + private function _handleMediaFile( $file, $file_title, $caption = '', $post_id = 0, $baseUrl = null) + { $id = array_search($file, $this->_files); if ($id === false) { set_time_limit(120); @@ -293,7 +288,7 @@ $filename = wp_unique_filename($path, $file); $fp = fopen($path . '/' . $filename, 'w+'); - $fileUrl = ($baseUrl) ? $baseUrl . '/' . $file : $this->_options['toolbox_image_url'] . $file; + $fileUrl = ($baseUrl) ? $baseUrl . '/' . rawurlencode($file) : $this->_options['toolbox_image_url'] . $file; $ch = curl_init($fileUrl); curl_setopt($ch, CURLOPT_TIMEOUT, 50); curl_setopt($ch, CURLOPT_FILE, $fp); @@ -707,7 +702,7 @@ } } - function _handleAccents($content) + private function _handleAccents($content) { // from: http://www.php.net/manual/en/domdocument.loadhtml.php#91513 if ( !empty( $content ) && function_exists( 'mb_convert_encoding' ) ) { @@ -1017,8 +1012,10 @@ $fileBaseName = $fileInfo['filename']; //echo '
File Info: ' . print_r($fileInfo, true)  . '
'; //echo '
File Name: ' . print_r($fileName, true)  . '
'; + //echo '
$fileBaseName: ' . print_r( $fileBaseName, true ) . '
'; $path = str_replace($fileName, '', $srcMatch[1]); $path = $fileInfo['dirname']; + //echo '
$path: ' . print_r( $path, true ) . '
'; //echo '
'.var_dump($path).'
'; $imagesKey = $this->_getAttachmentByName($fileBaseName); @@ -1114,6 +1111,7 @@ } echo '
Second: '.htmlspecialchars($content).'
'; echo ''; + //exit; $updatePost = array( 'ID' => $post->ID, 'post_content' => $content @@ -1255,14 +1253,18 @@ $stmt = $this->_dbh->query($sql); while ($row = $stmt->fetch()) { echo '
' . print_r($row, true) . '
'; - $fileName = $row['filename']; + $filePath = (isset($this->_options['site_uses_ht_images']) && $this->_options['site_uses_ht_images']) + ? $this->_options['toolbox_files_url'] + : $this->_options['toolbox_image_url'] . '/_ORIGINAL_/'; + echo '
$filePath: ' . print_r( $filePath, true ) . '
'; + $fileName = $row['filename']; $fileTitle = ($row['urltext']) ? $row['urltext'] : $fileName; - $wpImage = $this->_handleMediaFile( + $wpImage = $this->_handleMediaFile( $fileName, $fileTitle, '', 0, - $this->_options['toolbox_image_url'] . '/_ORIGINAL_/' + $filePath ); echo '
' . print_r($wpImage, true) . '
'; $this->_files[$wpImage] = $fileName; @@ -1377,6 +1379,7 @@ switch($step) { case 0: $this->_greet(); + echo '
$this->_options: ' . print_r( $this->_options, true ) . '
'; echo '

Begin Import

'; $currentPostArray = get_option(GLM_WP_IMPORT_POST_OPTION, array()); echo '
' . print_r($currentPostArray, true) . '
'; @@ -1409,49 +1412,55 @@ $imageLimit = 100; echo '

Done

'; echo '

Now processing the images from old database tables

'; - if (isset($_REQUEST['start'])) { - echo '

Second Part of the images

'; - $totalImages = $this->_getCkImageCount(); - echo '

Number of images in ckimage ' . $totalImages . '

'; - $start = filter_var( $_REQUEST['start'], FILTER_VALIDATE_INT ); - if ($start > $totalImages ) { - echo '

Done

'; - echo '

Step Three (Importing Files)

'; - } else { - $this->_getCkImages( $imageLimit, $start ); - $start += $imageLimit; - $endCount = $start + $imageLimit; - echo '

- Step Two [' . $start . ' - ' . $endCount . ']

'; - } + if ( isset($this->_options['site_uses_ht_images']) && $this->_options['site_uses_ht_images'] ) { + echo '

Site does not use ckimages

'; + echo '

Step Three (Importing Files)

'; } else { - $start = 0; - $this->_images = array(); - $this->_storeImages(); - if ($this->_options['toolbox_page_table'] == 'pages') { - $this->_getParagraphImages(); - echo '

Done with Paragraph images

'; - $endCount = $start + $imageLimit; - echo '

- Step Two [' . $start . ' - ' . $endCount . ']

'; + if (isset($_REQUEST['start'])) { + echo '

Second Part of the images

'; + $totalImages = $this->_getCkImageCount(); + echo '

Number of images in ckimage ' . $totalImages . '

'; + $start = filter_var( $_REQUEST['start'], FILTER_VALIDATE_INT ); + if ($start > $totalImages ) { + echo '

Done

'; + echo '

Step Three (Importing Files)

'; + } else { + $this->_getCkImages( $imageLimit, $start ); + $start += $imageLimit; + $endCount = $start + $imageLimit; + echo '

+ Step Two [' . $start . ' - ' . $endCount . ']

'; + } } else { - $this->_getBusCategoryImages(); - $this->_getBusImages(); - echo '

No import from images

'; - echo '

Step Three (Importing Files)

'; + $start = 0; + $this->_images = array(); + $this->_storeImages(); + if ($this->_options['toolbox_page_table'] == 'pages') { + $this->_getParagraphImages(); + echo '

Done with Paragraph images

'; + $endCount = $start + $imageLimit; + echo '

+ Step Two [' . $start . ' - ' . $endCount . ']

'; + } else { + $this->_getBusCategoryImages(); + $this->_getBusImages(); + echo '

No import from images

'; + echo '

Step Three (Importing Files)

'; + } } } + break; case 3: echo '

Done

'; echo '

Now processing the files from old database tables

'; $this->_files = array(); $this->_storeFiles(); - if ($this->_options['toolbox_page_table'] == 'pages') { + if ( $this->_options['toolbox_files_table'] ) { $this->_getToolboxFiles(); echo '

Done

'; } else { - echo '

No import from images

'; + echo '

No import from files

'; } echo '

Step Four (Updating Image Ref)

'; break; -- 2.17.1