From 9093cd129ff45fd953c971acba08c878748bf0cd Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Tue, 11 Aug 2015 15:50:01 -0400 Subject: [PATCH] Updating the plugin for all steps Step 1 pull and build out pages. Step 2 pull and setup all media (images and files) Step 3 replace all image src tags and update caption elements Step 4 replace keyword and site url links and href for is0 --- NOTES.md | 23 +++---- controllers/Import.php | 147 +++++++++++++++++++++++++++++------------ 2 files changed, 115 insertions(+), 55 deletions(-) diff --git a/NOTES.md b/NOTES.md index cdd64cf..71c51e3 100644 --- a/NOTES.md +++ b/NOTES.md @@ -1,4 +1,4 @@ -#Notes +# Notes # Resources for the image importing: Plugins that work well with importing the images into the Media Library. @@ -12,11 +12,11 @@ I'm planning on using the same thing here. 3. Fetch list of all images, files and import them into Media Library 4. Update all pages with new references of the images. -## REFERENCES: +## REFERENCES: ## For Wordpress Plugin Development https://developer.wordpress.org/plugins/the-basics/header-requirements/ -## Importing pages +## Importing pages ## https://codex.wordpress.org/Function_Reference/wp_insert_post ``` IMPORTANT: Setting a value for $post['ID'] WILL NOT create a post with that ID number. Setting this value will cause the function to update the post with that ID number with the other values specified in $post. In short, to insert a new post, $post['ID'] must be blank or not set at all. @@ -50,20 +50,19 @@ $post = array( ); ``` -## Todo +### Todo ### ac|todo item -|----- X|Need to import all the paragraph images into Media Library and assign them to their page. X|Getting Errors on some of the images X|Also need to fix the file permissions for the fetched images. X|Need to pull all image url’s out of the post_content sections and replace them with images that are imported into the Media Library. - |Need to replace any ‘keywords’ with the correct url’s. - |Need to replace any of the old site url's from their name-number/ to the correct wordpress ones. - |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. +x|Need to replace any ‘keywords’ with the correct url’s. +x|Need to replace any of the old site url's from their name-number/ to the correct wordpress ones. +x|Redo the import so if builds the pages without importing media then make that a second step -#Process For Import: +#### 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, paragraph images) 3. Update pages with the media id's @@ -71,9 +70,9 @@ X|Need to pull all image url’s out of the post_content sections and replace th 5. Update pages with old keywords (keyword replace) -### For 08/10/2015 ### +#### 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. +Step 3 Updates all media src tags and caption tags +Step 4 update keywords , site urls links inside, and update file refs diff --git a/controllers/Import.php b/controllers/Import.php index dde40a9..5191137 100644 --- a/controllers/Import.php +++ b/controllers/Import.php @@ -917,6 +917,50 @@ } } + private function _updateFilesHref() + { + global $wpdb; + $files = get_option(GLM_WP_IMPORT_FILES_OPTION, array()); + echo '
' . print_r($files, true) . '
'; + //$images = get_option(GLM_WP_IMPORT_IMAGES_OPTION, array()); + //$media = array_merge($images, $files); + //echo '
' . print_r($media, true) . '
'; + $sql = " + SELECT * + FROM {$wpdb->prefix}posts + WHERE post_content LIKE '%http://is0.gaslightmedia.com%' + AND post_type = 'page'"; + $results = $wpdb->get_results($sql, OBJECT); + if (count($results) > 0) { + foreach ($results as $post) { + $content = $post->post_content; + echo '
'; + if (preg_match_all('%href="([^"]+)"%', $content, $isMatch)) { + $isMatchCount = count($isMatch[0]); + echo '

Matches is0 ' . $isMatchCount . '

'; + for ($index = 0; $index < $isMatchCount; ++$index) { + echo '
'.htmlspecialchars($isMatch[1][$index]).'
'; + //echo '
'.htmlspecialchars(basename($isMatch[1][$index])).'
'; + //echo '
'.print_r(pathinfo($isMatch[1][$index]), true).'
'; + $key = array_search(basename($isMatch[1][$index]), $files); + if ($key !== false) { + var_dump($key); + $newSrc = wp_get_attachment_url($key); + $content = str_replace($isMatch[1][$index], $newSrc, $content); + } + } + } + echo '
'.htmlspecialchars($content).'
'; + echo '
'; + $updatePost = array( + 'ID' => $post->ID, + 'post_content' => $content + ); + //wp_update_post($updatePost); + } + } + } + private function _updateMediaSrc() { global $wpdb; @@ -930,44 +974,61 @@ if (count($results) > 0) { foreach ($results as $post) { $content = $post->post_content; - echo '
Page Title: ' . print_r($post->post_title, true)  . '
'; + echo '
Updating Media Src\'s: ' . 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); + if (preg_match_all('%\[caption [^\]]+\]]+)>([^\[]*)\[/caption\]%', $content, $cMatch)) { + $captionCount = count($cMatch[0]); + echo '

Matches in Caption ' . $captionCount . '

'; + //echo '
' . print_r($cMatch, true)  . '
'; + for ($index = 0; $index < $captionCount; ++$index) { + //echo '
cMatch: ' . print_r($cMatch, true)  . '
'; + echo '
'.htmlspecialchars($cMatch[0][$index]).'
'; + echo '
Caption Match: ' . print_r($cMatch[1][$index], true)  . '
'; + echo '
Caption Text: ' . print_r($cMatch[2][$index], true)  . '
'; + if (preg_match('%src="([^"]+)"%', $cMatch[1][$index], $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( + 'src="' . $srcMatch[1] . '"', + 'src="' . $newImage[0] . '" width="' . $newImage[1] . '" height="' . $newImage[1] . '"', + $cMatch[0][$index]); + $caption = str_replace(''.htmlspecialchars($caption).''; + $content = str_replace($cMatch[0][$index], $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); + } + if (preg_match_all('%]+)>%', $content, $matches)) { + $imageCount = count($matches[0]); + for ($index = 0; $index < $imageCount; ++$index) { + echo '
Image Match: ' . $imageCount  . '
'; + echo '
'.htmlspecialchars($matches[0][$index]).'
'; + if (preg_match('%src="([^"]+)"%', $matches[1][$index], $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][$index]); + if (preg_match('%class="([^"]+)"%', $matches[1][$index], $classMatch)) { + $imageTag = preg_replace('%class="([^"]+)"%', 'class="wp-image-' . $imagesKey . ' size-medium $1"', $imageTag); + } else { + $imageTag = str_replace(''.htmlspecialchars($imageTag).''; + $content = str_replace($matches[0][$index], $imageTag, $content); + } } } } @@ -1181,7 +1242,7 @@ $numPagesImported = $this->_import(); $start = filter_var($_REQUEST['start'], FILTER_VALIDATE_INT); echo '

' . $numPagesImported . ' Pages Imported

'; - echo '

Part 2

'; + echo '

Step Two (Importing Images)

'; break; case 2: echo '

Now processing the images from old database tables

'; @@ -1196,22 +1257,22 @@ //$this->_replaceKeywords(); //$this->replaceIsoUrls(); echo '

Done

'; + echo '

Step Three (Updating Image Ref)

'; //echo '

Part 3

'; break; case 3: 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).'
'; + echo '

Files

'; + $this->_updateFilesHref(); + echo '

Site Url

'; + $this->_replaceUrls(); + $this->_replaceKeywords(); + echo '

Done

'; break; case 5: echo '

Reset

'; -- 2.17.1