Update for paragraph image fix
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 13 Oct 2015 19:30:51 +0000 (15:30 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 13 Oct 2015 19:30:51 +0000 (15:30 -0400)
controllers/ToolboxImport.php

index 1e4395f..aec01a9 100644 (file)
             }
         }
 
+        private function _refetchImage($post_id, $attach_id, $guid)
+        {
+            global $wpdb;
+            static $upload_dir;
+            if (!$upload_dir) {
+                $upload_dir = wp_upload_dir();
+            }
+            //echo '<pre>' . print_r($upload_dir, true) . '</pre>';
+            //echo '<p>$post_id = ' . $post_id . '</p>';
+            //echo '<p>$attach_id = ' . $attach_id . '</p>';
+            //echo '<p>$guid = ' . $guid . '</p>';
+            $file = pathinfo($guid);
+            //echo '<pre>' . print_r($file, true) . '</pre>';
+            if (preg_match('%uploads/(\d{4}/\d{2})%', $file['dirname'], $matches)) {
+                //echo '<pre>' . print_r($matches, true) . '</pre>';
+                $yr_mon = $matches[1];
+            }
+            $file_path = "{$upload_dir['basedir']}/{$yr_mon}/{$file['basename']}";
+            //echo '<p>$file_path = ' . $file_path . '</p>';
+
+            // update the attachment posts with the parent page id
+            $updatePost = array(
+                'ID'          => $attach_id,
+                'post_parent' => $post_id
+            );
+            //echo '<pre>' . print_r($updatePost, true) . '</pre>';
+            $updated_id = wp_update_post($updatePost);
+            //echo '<pre>' . print_r($updated_id, true) . '</pre>';
+            // 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
          *
         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 '<pre>image,caption: ' . print_r($row, true) . '</pre>';
-                    $fileName = $row['image'];
-                    $fileinfo = pathinfo($fileName);
-                    echo '<p>filename: ' . print_r($fileinfo['filename'], true) . '</p>';
+                    //echo '<pre>image,caption: ' . print_r($row, true) . '</pre>';
+                    $fileName  = $row['image'];
+                    $fileinfo  = pathinfo($fileName);
+                    $baseImage = $fileinfo['filename'];
+                    echo '<p>filename: ' . print_r($baseImage, true) . '</p>';
                     // find the attachment id
                     $sql = "
                     SELECT *
                         echo '<pre>attachment_guid: ' . print_r($attachment_guid, true) . '</pre>';
                     } else {
                         echo '<p style="color: red;">WARN: not finding attachment</p>';
+                        continue;
                     }
                     // find the post with that guid in it
                     $sql = "
                     $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 '<pre>' . htmlspecialchars($post_content) . '</pre>';
+                        // get the img tag for this image
+                        $imgPattern = '%<img class="([^"]+)" src="' . $attachment_guid . '"'
+                            . '( ?[^>]*) ?/?>%si';
+                        //echo '<pre>pattern '.htmlspecialchars($imgPattern).'</pre>';
+                        if (preg_match($imgPattern, $post_content, $matches)) {
+                            $matched_string = $matches[0];
+                            //echo '<pre>matches: '."\n";
+                            //foreach ($matches as $key => $match) {
+                                //echo "$key = " . htmlspecialchars($match)."\n";
+                            //}
+                            //echo '</pre>';
+                            $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 '<p>$new_image_tag = ' . htmlspecialchars($new_image_tag).'</p>';
+                            $post_content = str_replace($matched_string, $new_image_tag, $post_content);
+                        } else {
+                            echo '<p>No Matches</p>';
+                        }
                         echo '<pre>page_id: ' . print_r($page_id, true) . '</pre>';
                     } else {
                         echo '<p style="color: red;">WARN: not finding guid in any page</p>';
+                        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 '<p>$new_image_url = ' . $new_image_url[0] . '</p>';
+                    //echo '<pre>$new_image_url: ' . print_r($new_image_url, true) . '</pre>';
+
+                    // 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 '<pre>' . print_r($this->_images, true) . '</pre>';
             }
         }
 
                 break;
             case 0:
                 $this->_greet();
-                echo '<p><a href="admin.php?import=toolbox&amp;step=1">Begin Import</a></p>';
+                //echo '<p><a href="admin.php?import=toolbox&amp;step=1">Begin Import</a></p>';
                 $currentPostArray = get_option(GLM_WP_IMPORT_POST_OPTION, array());
-                echo '<pre>' . print_r($currentPostArray, true) . '</pre>';
+                //echo '<pre>' . print_r($currentPostArray, true) . '</pre>';
                 $images = get_option(GLM_WP_IMPORT_IMAGES_OPTION, array());
-                echo '<pre>' . print_r($images, true) . '</pre>';
+                //echo '<pre>' . print_r($images, true) . '</pre>';
                 $files = get_option(GLM_WP_IMPORT_FILES_OPTION, array());
-                echo '<pre>' . print_r($files, true) . '</pre>';
+                //echo '<pre>' . print_r($files, true) . '</pre>';
                 if (count($currentPostArray) > 0) {
-                    echo '<p>Pages have been imported</p>';
-                    echo '<p><a href="admin.php?import=toolbox&amp;step=2">Step Two (Importing Images)</a></p>';
-                    echo '<p><a href="admin.php?import=toolbox&amp;step=3">Step Three (Updating Image Ref)</a></p>';
+                    //echo '<p>Pages have been imported</p>';
+                    //echo '<p><a href="admin.php?import=toolbox&amp;step=2">Step Two (Importing Images)</a></p>';
+                    //echo '<p><a href="admin.php?import=toolbox&amp;step=3">Step Three (Updating Image Ref)</a></p>';
                 } else {
-                    echo '<p><a href="admin.php?import=toolbox&amp;step=1">Begin Import</a></p>';
+                    //echo '<p><a href="admin.php?import=toolbox&amp;step=1">Begin Import</a></p>';
                 }
                 echo '<p><a href="admin.php?import=toolbox&amp;step=6">Fix Paragraph Images</a></p>';
                 break;