Now importing content images.
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 14 Jul 2015 18:07:52 +0000 (14:07 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 14 Jul 2015 18:07:52 +0000 (14:07 -0400)
Goes thru the page content sections and imports images into media
library.

controllers/Admin.php
controllers/Import.php

index dd21ff3..3bbf822 100644 (file)
@@ -148,8 +148,13 @@ class GlmWPImporter_Admin_Controller
                 'type'  => 'text'
             ),
             array(
-                'name'  => 'member_page_id',
-                'label' => 'Ignore Pages',
+                'name'  => 'exclude_pages',
+                'label' => 'Exclude Pages',
+                'type'  => 'text'
+            ),
+            array(
+                'name'  => 'include_pages',
+                'label' => 'Include Pages',
                 'type'  => 'text'
             ),
         );
index eb5f72e..7db6030 100644 (file)
             return $content;
         }
 
+        private function _getDescriptionImage($src)
+        {
+            $parsed = parse_url($src);
+            $fileName = basename($parsed['path']);
+            $rootUrl = str_replace('/' . $fileName, '', $src);
+            $img_id = $this->_handleMediaFile(
+                basename($parsed['path']),
+                '',
+                0,
+                $rootUrl . '/'
+            );
+            $image = wp_get_attachment_image_src($img_id, 'full');
+            return $image[0];
+        }
+
         private function _displayFile($data)
         {
             $file_id = $this->_handleMediaFile($data['filename']);
          * @access private
          * @return void
          */
-        private function _handleMediaFile($file, $caption = '', $post_id = 0)
+        private function _handleMediaFile($file, $caption = '', $post_id = 0, $baseUrl = null)
         {
             $id = array_search($file, $this->_files);
             if ($id === false) {
                 if (!(($uploads = wp_upload_dir($time)) && false === $uploads['error'])) {
                     return new WP_Error('upload_error', $uploads['error']);
                 }
-                //echo '<pre>'.print_r($uploads, true).'</pre>';
                 $filename = $this->_fetchRemoteImage($file, $uploads['path']);
                 $new_file = $uploads['path'] . '/' . $filename;
                 $url      = $uploads['url'] . '/' . $filename;
                     'post_date'      => $post_date,
                     'post_date_gmt'  => $post_date_gmt
                  );
-                //echo '<pre>'.print_r($attachment, true).'</pre>';
-                //exit;
                 // Insert attachment
                 $id = wp_insert_attachment($attachment, $new_file, $post_id);
                 if (!is_wp_error($id)) {
          * @access private
          * @return void
          */
-        private function _fetchRemoteImage($file, $path)
+        private function _fetchRemoteImage($file, $path, $baseUrl = null)
         {
             $filename = wp_unique_filename($path, $file);
 
             $fp = fopen($path . '/' . $filename, 'w+');
-            $ch = curl_init($this->_options['toolbox_image_url'] . $file);
+            $fileUrl = ($baseUrl) ? $baseUrl . $file : $this->_options['toolbox_image_url'] . $file;
+            $ch = curl_init($fileUrl);
             curl_setopt($ch, CURLOPT_TIMEOUT, 50);
             curl_setopt($ch, CURLOPT_FILE, $fp);
             curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
         private function _fetchAllPages()
         {
             $this->_connect();
-            $where = '';
-            if ($this->_options['member_page_id']) {
-                $where = "WHERE id NOT IN ({$this->_options['member_page_id']})
-                    AND parent NOT IN ({$this->_options['member_page_id']}) ";
+            $where = array();
+            if ($this->_options['include_pages']) {
+                $where[] = "id IN ({$this->_options['include_pages']})
+                    OR parent IN ({$this->_options['include_pages']}) ";
+            }
+            if ($this->_options['exclude_pages']) {
+                $where[] = "id NOT IN ({$this->_options['exclude_pages']})
+                    AND parent NOT IN ({$this->_options['exclude_pages']}) ";
+            }
+            if (!empty($where)) {
+                $WHERE = ' WHERE ' . implode(' AND ', $where);
             }
             $pageSql = "
               SELECT *
                 FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_page_table']}
-                $where
+                $WHERE
             ORDER BY parent,pos";
             $pageData = $this->_dbh->query($pageSql)->fetchAll(PDO::FETCH_ASSOC);
             $paragraphSql = "
             $fileStmt = $this->_dbh->prepare($fileSql);
             $data = $this->_dbh->query($pageSql)->fetchAll();
             foreach ($data as &$page) {
+                $page['srcs'] = array();
                 $paraStmt->bindParam(':page', $page['id'], PDO::PARAM_INT);
                 $paraStmt->execute();
                 $paragraphs     = $paraStmt->fetchAll(PDO::FETCH_ASSOC);
                         break;
                     }
                     $page['pageContent'] .= $paragraph['description'];
+                    preg_match_all("/<img .*?(?=src)src=\"([^\"]+)\"/si", $paragraph['description'], $matches);
+                    for ($i = 0; $i < count($matches[0]); $i++) {
+                        $newSrc  = $this->_getDescriptionImage($matches[1][$i]);
+                        $parsUrl = parse_url($newSrc);
+                        $imgName = basename($parsUrl['path']);
+                        $page['pageContent'] = str_replace($matches[1][$i], $newSrc, $page['pageContent']);
+                        $page['srcs'][] = $imgName;
+                    }
                     $fileStmt->bindParam(':pid', $paragraph['id'], PDO::PARAM_INT);
                     $fileStmt->execute();
                     while ($file = $fileStmt->fetch()) {
             // grab all pages and build the post array
             $pages = $this->_fetchAllPages();
 
-            echo '<pre>' . print_r($this->_files, true) . '</pre>';
-            //exit;
             foreach ($pages as $page) {
                 $post = array(
                     'post_content'   => $page['pageContent'],
                 $ID = wp_insert_post($post, true);
                 $newPost = get_post($ID, ARRAY_A);
                 $this->_post[$page['id']] = $newPost;
+                if (isset($page['srcs']) && !empty($page['srcs'])) {
+                    foreach ($page['srcs'] as $image) {
+                        $img_id = array_search($image, $this->_files);
+                        if ($img_id) {
+                            wp_update_post(array('ID' => $img_id, 'post_parent' => $ID));
+                        }
+                    }
+                }
                 // update images so they are attached to the proper page
                 if (isset($page['images']) && !empty($page['images'])) {
-                    echo '<pre>' . print_r($page['images'], true) . '</pre>';
-                    //exit;
                     foreach ($page['images'] as $image) {
                         $img_id = array_search($image, $this->_files);
                         if ($img_id) {
                             wp_update_post(array('ID' => $img_id, 'post_parent' => $ID));
                         }
                     }
+                }
+                if (isset($page['files']) && !empty($page['files'])) {
                     foreach ($page['files'] as $file) {
                         $file_id = array_search($file, $this->_files);
                         if ($file_id) {
                         }
                     }
                 }
-
             }
-            //var_dump($pages);
         }
 
         /**