Update WIP northernlakes
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 4 Aug 2015 21:01:02 +0000 (17:01 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 4 Aug 2015 21:01:02 +0000 (17:01 -0400)
controllers/Import.php

index e64490e..81a4053 100644 (file)
             return "\n".'<a href="'.$fileUrl.'">'.$fileName.'</a>';
         }
 
+        private function _addMedia($file, $postId)
+        {
+            $fileType = wp_check_filetype(basename($file), null);
+            $wpUploadDir = wp_upload_dir();
+            $attachment = array(
+                'guid'           => $wpUploadDir . '/' . basename($file),
+                'post_mime_type' => $fileType['type'],
+                'post_title'     => basename($file),
+                'post_content'   => '',
+                'post_status'    => 'inherit'
+            );
+            $id = wp_insert_attachment($attachment, $file, $postId);
+            if (!is_wp_error($id)) {
+                $data = wp_generate_attachment_metadata($id, $file);
+                wp_update_attachment_metadata($id, $data);
+            }
+            return ($id) ? $this->_getAttachmentById($id) : false;
+        }
+
+
         /**
          * _handleMediaFile
          *
               FROM {$wpdb->prefix}posts
              WHERE post_content LIKE '%<img%'
                AND post_type = 'page'";
-            $sql .= " AND post_title = 'Agriculture'";
+            //$sql .= " AND post_title = 'NLEA Chamber Council'";
+            $sql .= " AND ID = 2101";
+
             $results = $wpdb->get_results($sql, OBJECT);
-            echo '<p>'.count($results).'</p>';
+            echo '<p>Number of Pages'.count($results).'</p>';
             if (count($results) > 0) {
                 $pattern = ';(<img[^>]>);si';
                 foreach ($results as $post) {
                     $images = $dom->getElementsByTagName('img');
                     foreach ($images as $image) {
                         $width = $height = null;
+                        echo '<div style="border: 1px solid black; background-color: lightgrey; padding: 15px;width: 600px;margin: 0 10px">';
                         //echo '<pre>' . print_r($image, true)  . '</pre>';
                         if ($image->hasAttribute('class')) {
-                            //echo '<pre>' . print_r($image->getAttribute('class'), true)  . '</pre>';
+                            echo '<pre>' . print_r($image->getAttribute('class'), true)  . '</pre>';
+                            if (preg_match('%wp-image-\d+%', $image->getAttribute('class'))) {
+                                //echo 'Continue';
+                                //echo '</div>';
+                                //continue;
+                            }
                         }
                         if ($image->hasAttribute('style')) {
                             $style = $image->getAttribute('style');
+                            var_dump($style);
                             echo '<pre>' . print_r($style, true)  . '</pre>';
-                            if (preg_match(';[^-]width[: ]+(\d+)px;', $style, $wMatch)) {
+                            if (preg_match('%[^-]width[: ]+(\d+)%', $style, $wMatch)) {
                                 $width = $wMatch[1];
+                                echo '<pre>wMatch: ' . print_r($wMatch, true)  . '</pre>';
                             }
-                            if (preg_match(';[^-]height[: ]+(\d+)px;', $style, $hMatch)) {
+                            if (preg_match('%[^-]height[: ]+(\d+)%', $style, $hMatch)) {
                                 $height = $hMatch[1];
                             }
-                            echo '<pre>' . print_r($width, true)  . '</pre>';
-                            echo '<pre>' . print_r($height, true)  . '</pre>';
+                            echo '<pre>width: ' . print_r($width, true)  . '</pre>';
+                            echo '<pre>height: ' . print_r($height, true)  . '</pre>';
+                        }
+                        if ($image->hasAttribute('width')) {
+                            $width = $image->getAttribute('width');
+                        }
+                        if ($image->hasAttribute('height')) {
+                            $height = $image->getAttribute('height');
                         }
                         if ($image->hasAttribute('src')) {
                             //echo '<pre>' . print_r($image->getAttribute('src'), true)  . '</pre>';
                             $imgName = basename($image->getAttribute('src'));
                             echo '<pre>' . print_r($imgName, true)  . '</pre>';
                             $imgPathInfo = pathinfo($imgName);
+                            echo '<pre>' . print_r($imgPathInfo, true) . '</pre>';
                             $wpImage = $this->_getAttachmentByName($imgPathInfo['filename']);
+                            //echo '<pre>' . print_r($wpImage, true) . '</pre>';
+                            //exit;
+                            if (!$wpImage) {
+                                echo '<p>Image not found as attachment</p>';
+                                echo '<pre>' . print_r($imgPathInfo, true) . '</pre>';
+                                if (preg_match(';(is\d{2}-\d{10}-\d{5})(\d{1}|[-]\d+x\d+);', $imgPathInfo['filename'], $matches)) {
+                                    echo '<p>Duplicate Image found</p>';
+                                    echo '<pre>' . print_r($matches, true) . '</pre>';
+                                    $wpImage = $this->_getAttachmentByName($matches[1]);
+                                    if ($wpImage) {
+                                        echo '<p>Found Image</p>';
+                                    } else {
+                                        echo '<p>Image still not found</p>';
+                                        continue;
+                                    }
+                                }
+                            }
                             if (!$wpImage) {
-                                echo '<pre>' . print_r($image->getAttribute('src'), true) . '</pre>';
+                                echo '<p>Image not found as attachment 2</p>';
+                                //echo '<pre>' . print_r($image->getAttribute('src'), true) . '</pre>';
                                 $parsed   = parse_url($image->getAttribute('src'));
                                 $filename = basename($parsed['path']);
                                 if (preg_match(';is\d{2,}-\d{10,}-\d{5,};', $filename)) {
                                     echo '<pre>' . print_r($parsed, true) . '</pre>';
                                     echo '<pre>' . print_r($filename, true) . '</pre>';
                                     echo '<pre>' . print_r($rootUrl, true) . '</pre>';
-                                    $wpImage = $this->_handleMediaFile(
-                                        basename($parsed['path']),
-                                        '',
-                                        $post->ID,
-                                        $rootUrl . '/'
-                                    );
+                                    //if (preg_match('/(\d{4})\/(\d{2})$/', $rootUrl, $matches)) {
+                                        //echo '<pre>' . print_r($matches, true) . '</pre>';
+                                        //$uploadDir = wp_upload_dir();
+                                        //echo '<pre>' . print_r($uploadDir, true) . '</pre>';
+                                        //$absPath = $uploadDir['basedir'] . '/' . $matches[1] . '/' . $matches[2];
+                                        //echo '<pre>' . print_r($absPath, true) . '</pre>';
+                                        ////$wpImage = $this->_addMedia($absPath . '/' . $filename, $post->ID);
+                                        //echo '<pre>' . print_r($imageId, true) . '</pre>';
+                                    //}
+                                    //$fileExists = file_exists($upload_dir['baseurl'] . '/' . $filename);
+                                    //$wpImage = $this->_handleMediaFile(
+                                        //basename($parsed['path']),
+                                        //'',
+                                        //$post->ID,
+                                        //$rootUrl . '/'
+                                    //);
                                 }
                             }
                             if ($wpImage) {
                                 //$strToReplace = $dom->saveHTML($image);
                                 //echo '<pre>'.htmlspecialchars($strToReplace).'</pre>';
+                                echo '<p>Image found: id '.$wpImage->ID.'</p>';
                                 if ($width && $height) {
-                                    echo '<p>Image found: id '.$wpImage->ID.'</p>';
-                                    //echo '<p>width: ' . $width . '</p>';
-                                    //echo '<p>height: ' . $height . '</p>';
+                                    echo '<p>width: ' . $width . '</p>';
+                                    echo '<p>height: ' . $height . '</p>';
                                     if ($width > 300 || $height > 300) {
                                         echo '<p>Found Large</p>';
                                         $size = 'large';
                                             $image->setAttribute('src', $newImage[0]);
                                         }
                                     }
-                                    $image->removeAttribute('style');
-                                    $image->setAttribute('width', $width);
-                                    $image->setAttribute('height', $height);
-                                    //echo htmlspecialchars($dom->saveHTML($image));
+                                } else {
+                                    $size = 'large';
+                                    $newImage = wp_get_attachment_image_src($wpImage->ID, $size);
+                                    $image->setAttribute('class', 'alignnone size-' . $size . ' wp-image-' . $wpImage->ID);
+                                    $width    = $newImage[1];
+                                    $height   = $newImage[2];
+                                    if($newImage) {
+                                        $image->setAttribute('src', $newImage[0]);
+                                    }
                                 }
+                                $image->removeAttribute('style');
+                                $image->setAttribute('width', $width);
+                                $image->setAttribute('height', $height);
+                                //echo htmlspecialchars($dom->saveHTML($image));
                             } else {
                                 echo '<p>no media found</p>';
                             }
                         }
+                        echo '</div>';
                     }
                     // remove the extra stuff Dom put in
                     $content = preg_replace(
             SELECT *
               FROM {$wpdb->prefix}posts
              WHERE post_type = 'attachment'
-            AND post_title = '{$name}'";
+            AND post_name = '{$name}'";
+            $results = $wpdb->get_results($sql, OBJECT);
+            return (!empty($results)) ? $results[0] : false;
+        }
+
+        private function _getAttachmentById($id)
+        {
+            global $wpdb;
+            $sql = "
+            SELECT *
+              FROM {$wpdb->prefix}posts
+             WHERE post_type = 'attachment'
+            AND ID = {$id}";
             $results = $wpdb->get_results($sql, OBJECT);
             return (!empty($results)) ? $results[0] : false;
         }