From: Steve Sutton Date: Tue, 13 Oct 2015 19:30:51 +0000 (-0400) Subject: Update for paragraph image fix X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/index.cgi?a=commitdiff_plain;h=1bd5da1b03c850eeef7b136f3ba7d0a465ea8987;p=WP-Plugins%2Fglm-wp-importer.git Update for paragraph image fix --- diff --git a/controllers/ToolboxImport.php b/controllers/ToolboxImport.php index 1e4395f..aec01a9 100644 --- a/controllers/ToolboxImport.php +++ b/controllers/ToolboxImport.php @@ -1281,6 +1281,54 @@ } } + private function _refetchImage($post_id, $attach_id, $guid) + { + global $wpdb; + static $upload_dir; + if (!$upload_dir) { + $upload_dir = wp_upload_dir(); + } + //echo '
' . print_r($upload_dir, true) . '
'; + //echo '

$post_id = ' . $post_id . '

'; + //echo '

$attach_id = ' . $attach_id . '

'; + //echo '

$guid = ' . $guid . '

'; + $file = pathinfo($guid); + //echo '
' . print_r($file, true) . '
'; + if (preg_match('%uploads/(\d{4}/\d{2})%', $file['dirname'], $matches)) { + //echo '
' . print_r($matches, true) . '
'; + $yr_mon = $matches[1]; + } + $file_path = "{$upload_dir['basedir']}/{$yr_mon}/{$file['basename']}"; + //echo '

$file_path = ' . $file_path . '

'; + + // update the attachment posts with the parent page id + $updatePost = array( + 'ID' => $attach_id, + 'post_parent' => $post_id + ); + //echo '
' . print_r($updatePost, true) . '
'; + $updated_id = wp_update_post($updatePost); + //echo '
' . print_r($updated_id, true) . '
'; + // fetch the image + $fp = fopen( $file_path, 'w+' ); + $fileUrl = $this->_options['toolbox_image_url'] . '/CKImage/' . $file['basename']; + $ch = curl_init($fileUrl); + curl_setopt($ch, CURLOPT_TIMEOUT, 50); + curl_setopt($ch, CURLOPT_FILE, $fp); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + $imgData = curl_exec($ch); + $httpCode = curl_getinfo($ch); + curl_close($ch); + fclose($fp); + // Set correct file permissions + $oldUmask = umask(0); + chmod( $file_path, 0660 ); + umask($oldUmask); + + $attach_data = wp_generate_attachment_metadata( $attach_id, $file_path ); + wp_update_attachment_metadata( $attach_id, $attach_data ); + } + /** * Fix the paragraph images * @@ -1291,20 +1339,20 @@ private function _fixParagraphimages() { global $wpdb; - $this->_images = array(); if ($this->_options['toolbox_paragraphs_table']) { - $this->_images = get_option(GLM_WP_IMPORT_IMAGES_OPTION, array()); $this->_connect(); $sql = " SELECT image,caption FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_paragraphs_table']} WHERE image != ''"; + //$sql .= " LIMIT 1 OFFSET 0"; $stmt = $this->_dbh->query($sql); while ($row = $stmt->fetch()) { - echo '
image,caption: ' . print_r($row, true) . '
'; - $fileName = $row['image']; - $fileinfo = pathinfo($fileName); - echo '

filename: ' . print_r($fileinfo['filename'], true) . '

'; + //echo '
image,caption: ' . print_r($row, true) . '
'; + $fileName = $row['image']; + $fileinfo = pathinfo($fileName); + $baseImage = $fileinfo['filename']; + echo '

filename: ' . print_r($baseImage, true) . '

'; // find the attachment id $sql = " SELECT * @@ -1318,6 +1366,7 @@ echo '
attachment_guid: ' . print_r($attachment_guid, true) . '
'; } else { echo '

WARN: not finding attachment

'; + continue; } // find the post with that guid in it $sql = " @@ -1327,12 +1376,56 @@ $page_results = $wpdb->get_row($sql, ARRAY_A); if ( count( $page_results ) > 0 ) { $page_id = $page_results['ID']; + $post_content = $page_results['post_content']; + //echo '
' . htmlspecialchars($post_content) . '
'; + // get the img tag for this image + $imgPattern = '%]*) ?/?>%si'; + //echo '
pattern '.htmlspecialchars($imgPattern).'
'; + if (preg_match($imgPattern, $post_content, $matches)) { + $matched_string = $matches[0]; + //echo '
matches: '."\n";
+                            //foreach ($matches as $key => $match) {
+                                //echo "$key = " . htmlspecialchars($match)."\n";
+                            //}
+                            //echo '
'; + $new_image_tag = preg_replace('%width="\d+"%', '', $matched_string); + $new_image_tag = preg_replace('%height="\d+"%', '', $new_image_tag); + $new_image_tag = str_replace('size-full', 'size-medium', $new_image_tag); + //echo '

$new_image_tag = ' . htmlspecialchars($new_image_tag).'

'; + $post_content = str_replace($matched_string, $new_image_tag, $post_content); + } else { + echo '

No Matches

'; + } echo '
page_id: ' . print_r($page_id, true) . '
'; } else { echo '

WARN: not finding guid in any page

'; + continue; } + + // Image processing part + + $this->_refetchImage( + $page_id, + $attachment_id, + $attachment_guid + ); + // get the image size. If it is too large then use the medium size + $new_image_url = wp_get_attachment_image_src($attachment_id, 'medium'); + + //echo '

$new_image_url = ' . $new_image_url[0] . '

'; + //echo '
$new_image_url: ' . print_r($new_image_url, true) . '
'; + + // replace url for image src in the page + + // update the attachment posts with the parent page id + $post_content = str_replace($attachment_guid, $new_image_url[0], $post_content); + $updatePost = array( + 'ID' => $page_id, + 'post_content' => $post_content + ); + $updated_id = wp_update_post($updatePost); } - //echo '
' . print_r($this->_images, true) . '
'; } } @@ -1352,19 +1445,19 @@ break; case 0: $this->_greet(); - echo '

Begin Import

'; + //echo '

Begin Import

'; $currentPostArray = get_option(GLM_WP_IMPORT_POST_OPTION, array()); - echo '
' . print_r($currentPostArray, true) . '
'; + //echo '
' . print_r($currentPostArray, true) . '
'; $images = get_option(GLM_WP_IMPORT_IMAGES_OPTION, array()); - echo '
' . print_r($images, true) . '
'; + //echo '
' . print_r($images, true) . '
'; $files = get_option(GLM_WP_IMPORT_FILES_OPTION, array()); - echo '
' . print_r($files, true) . '
'; + //echo '
' . print_r($files, true) . '
'; if (count($currentPostArray) > 0) { - echo '

Pages have been imported

'; - echo '

Step Two (Importing Images)

'; - echo '

Step Three (Updating Image Ref)

'; + //echo '

Pages have been imported

'; + //echo '

Step Two (Importing Images)

'; + //echo '

Step Three (Updating Image Ref)

'; } else { - echo '

Begin Import

'; + //echo '

Begin Import

'; } echo '

Fix Paragraph Images

'; break;