From a9eb91d6434eb0dbbed078d8fbf9e12f789537bf Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Mon, 10 Aug 2015 17:03:55 -0400 Subject: [PATCH] WIP work on media and captions Captions aren't saving correctly --- NOTES.md | 13 ++++- controllers/Import.php | 106 ++++++++++++++++++++++++++++++++--------- 2 files changed, 95 insertions(+), 24 deletions(-) diff --git a/NOTES.md b/NOTES.md index 8775d80..cdd64cf 100644 --- a/NOTES.md +++ b/NOTES.md @@ -65,6 +65,15 @@ X|Need to pull all image url’s out of the post_content sections and replace th #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 +2. Import all media types from old toolbox: (ckeditor images, files, paragraph images) +3. Update pages with the media id's +4. Update pages with links to internal pages. +5. Update pages with old keywords (keyword replace) + + +### For 08/10/2015 ### +I have the pages importing all in step 1. +Step 2 I have all media being imported. +Step Three I'm still working on getting the caption part updated correctly so it can see the image as editable and stay there when going from +'Visual' to 'Text' view. Currently it is loosing the caption when switching. diff --git a/controllers/Import.php b/controllers/Import.php index cea8439..dde40a9 100644 --- a/controllers/Import.php +++ b/controllers/Import.php @@ -165,9 +165,9 @@ $img_url = $this->_options['toolbox_image_url'] . '/tbs1/' . $data['image']; $content = ''; if (isset($data['caption']) && $data['caption']) { - $content .= '[caption align="align'.$alignment.'"] ' . $data['caption'] . '[/caption]'; + $content .= '[caption align="align'.$alignment.'"] ' . $data['caption'] . '[/caption]'; } else if (isset($data['imagename']) && $data['imagename']) { - $content .= '[caption align="align'.$alignment.'"] ' . $data['imagename'] . '[/caption]'; + $content .= '[caption align="align'.$alignment.'"] ' . $data['imagename'] . '[/caption]'; } else { $content .= ''; } @@ -917,6 +917,71 @@ } } + private function _updateMediaSrc() + { + global $wpdb; + $images = get_option(GLM_WP_IMPORT_IMAGES_OPTION, array()); + $sql = " + SELECT * + FROM {$wpdb->prefix}posts + WHERE post_content LIKE '%get_results($sql, OBJECT); + if (count($results) > 0) { + foreach ($results as $post) { + $content = $post->post_content; + echo '
Page Title: ' . print_r($post->post_title, true)  . '
'; + echo '
'; + if (preg_match('%\[caption [^\]]+\]]+)>([^\[]*)\[/caption\]%', $post->post_content, $cMatch)) { + echo '
'.htmlspecialchars($cMatch[0]).'
'; + echo '
Caption Match: ' . print_r($cMatch[1], true)  . '
'; + echo '
Caption Text: ' . print_r($cMatch[2], true)  . '
'; + if (preg_match('%src="([^"]+)"%', $cMatch[1], $srcMatch)) { + echo '
Sub Src Match: ' . print_r($srcMatch[1], true)  . '
'; + $fileName = basename($srcMatch[1]); + echo '
File Name: ' . print_r($fileName, true)  . '
'; + if ($imagesKey = array_search($fileName, $images)) { + var_dump($imagesKey); + $newImage = wp_get_attachment_image_src($imagesKey, 'medium'); + // need to replace the src tags in content with the new image tags + $caption = str_replace($srcMatch[1], $newImage[0], $cMatch[0]); + $caption = str_replace(''.htmlspecialchars($caption).''; + $content = str_replace($cMatch[0], $caption, $content); + } + } + } else if (preg_match('%]+)>%', $post->post_content, $matches)) { + echo '
Image Match: ' . print_r($matches[1], true)  . '
'; + echo '
'.htmlspecialchars($matches[0]).'
'; + if (preg_match('%src="([^"]+)"%', $matches[1], $srcMatch)) { + echo '
Sub Src Match: ' . print_r($srcMatch[1], true)  . '
'; + $fileName = basename($srcMatch[1]); + echo '
File Name: ' . print_r($fileName, true)  . '
'; + if ($imagesKey = array_search($fileName, $images)) { + var_dump($imagesKey); + $newUrl = wp_get_attachment_url($imagesKey); + // need to replace the src tags in content with the new image tags + $imageTag = str_replace($srcMatch[1], $newUrl, $matches[0]); + $imageTag = str_replace(''.htmlspecialchars($imageTag).''; + $content = str_replace($matches[0], $imageTag, $content); + } + } + } + echo '
'.htmlspecialchars($content).'
'; + echo '
'; + $updatePost = array( + 'ID' => $post->ID, + 'post_content' => $content + ); + wp_update_post($updatePost); + } + } + } + private function _getAttachmentByName($name) { global $wpdb; @@ -1096,8 +1161,6 @@ } switch($step) { case 0: - //$this->post = array(); - //$this->_storePost(); $this->_greet(); //$this->_readOptions(); //echo '

CkImages

'; @@ -1108,28 +1171,16 @@ echo '
' . print_r($files, true) . '
'; if (count($currentPostArray) > 0) { echo '

Pages have been imported

'; - echo '

Step Two

'; + echo '

Step Two (Importing Images)

'; + echo '

Step Three (Updating Image Ref)

'; } else { - echo '

Begin Import

'; + echo '

Begin Import

'; } break; case 1: $numPagesImported = $this->_import(); $start = filter_var($_REQUEST['start'], FILTER_VALIDATE_INT); - echo '

Start: ' . $start . '

'; - if ($start === false) { - $start = 0; - } else { - $start += PAGES_PER_LOAD; - } - echo '

' . $numPagesImported . ' Pages Imported -

- - - - -
-

'; + echo '

' . $numPagesImported . ' Pages Imported

'; echo '

Part 2

'; break; case 2: @@ -1148,15 +1199,26 @@ //echo '

Part 3

'; break; case 3: - $this->_replaceUrls(); + echo '

Phase Three

'; + $this->_updateMediaSrc(); + //$images = get_option(GLM_WP_IMPORT_IMAGES_OPTION, array()); + //echo '
' . print_r($images, true) . '
'; + //$files = get_option(GLM_WP_IMPORT_FILES_OPTION, array()); + //echo '
' . print_r($files, true) . '
'; + echo '

Done

'; break; case 4: + echo '

Phase Four

'; echo '

Test getting page sub ids

'; $subPageIds = $this->_getSubPageIds($this->_options['include_pages']); echo '
'.print_r($subPageIds, true).'
'; break; case 5: - $this->_getCkImages(); + echo '

Reset

'; + $this->_post = $this->_images = $this->_files = array(); + $this->_storePost(); + $this->_storeImages(); + $this->_storeFiles(); break; } -- 2.17.1