WIP for western UP
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 6 Aug 2015 21:08:52 +0000 (17:08 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 6 Aug 2015 21:08:52 +0000 (17:08 -0400)
NOTES.md
controllers/Import.php

index 3bf091c..8775d80 100644 (file)
--- a/NOTES.md
+++ b/NOTES.md
@@ -62,3 +62,9 @@ X|Need to pull all image url’s out of the post_content sections and replace th
  |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.
 
+
+#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, paragraphs)
+3. Update pages with the media id's 
+
index 45a7286..c36b3a4 100644 (file)
         private $_images = array();
 
 
-        /**
-         * __construct
-         *
-         * @access public
-         * @return void
-         */
         public function __construct()
         {
             $this->_getOptions();
         }
 
-        /**
-         * _getOptions
-         *
-         * Retrieve the Import options
-         *
-         * @access private
-         * @return void
-         */
         private function _getOptions()
         {
             // read options for the import and if they aren't set then give a warning.
             $this->_options = get_option(GLM_WP_IMPORT_SETTINGS);
         }
 
-        /**
-         * _connect
-         *
-         * Connect to the Toolbox Database
-         *
-         * @access private
-         * @return void
-         */
         private function _connect()
         {
             if ($this->_dbh) {
             );
         }
 
-        /**
-         * _header
-         *
-         * Build Admin Page Header
-         *
-         * @access private
-         * @return void
-         */
         private function _header()
         {
             echo '<div class="wrap">';
             echo '<h2>'.__('Toolbox Importer').'</h2>';
         }
 
-        /**
-         * _footer
-         *
-         * Build Admin Page Footer
-         *
-         * @access private
-         * @return void
-         */
         private function _footer()
         {
             echo '</div>';
         }
 
-        /**
-         * _checkRequiredOptions
-         *
-         * Verifies that important settings aren't empty
-         *
-         * @access private
-         * @return void
-         */
         private function _checkRequiredOptions()
         {
             $errors = array();
             return $errors;
         }
 
-        /**
-         * _greet
-         *
-         * Greet Screen when you first goto the Toolbox Import
-         * Runs checks on the settings.
-         * Won't allow you to pass unless all is O.K.
-         *
-         * @access private
-         * @return void
-         */
         private function _greet()
         {
             // Verify that everything is setup
             }
         }
 
-        /**
-         * _displayImage
-         *
-         * Build the HTML code for the Images
-         *
-         * @param mixed $data
-         * @param mixed $alignment
-         *
-         * @access private
-         * @return void
-         */
         private function _displayImage($data, $alignment)
         {
             if (!isset($data['image'])) {
                 return false;
             }
-            if (isset($data['caption'])) {
-                $img_id  = $this->_handleMediaFile($data['image'], $data['caption']);
-            } else if ($data['imagename']) {
-                $img_id  = $this->_handleMediaFile($data['image'], $data['imagename']);
-            } else {
-                $img_id  = $this->_handleMediaFile($data['image']);
-            }
-            if (!$img_id) {
-                return false;
-            }
-            $image   = wp_get_attachment_image_src($img_id, 'medium');
-            $img_url = $image[0];
+            $img_url = $this->_options['toolbox_image_url'] . '/tbs1/' . $data['image'];
             $content = '';
             if (isset($data['caption']) && $data['caption']) {
-                $content .= '[caption id="attachment_'.$img_id.'" align="align'.$alignment.'"]'
-                    .'<img class="align'.$alignment.' size-medium wp-image-' . $img_id . '" '
-                    . ' src="' . $img_url . '"> '
-                    . $data['caption'] . '[/caption]';
+                $content .= '[caption align="align'.$alignment.'"]<img class="align'.$alignment.'" src="' . $img_url . '"> ' . $data['caption'] . '[/caption]';
             } else if (isset($data['imagename']) && $data['imagename']) {
-                $content .= '[caption id="attachment_'.$img_id.'" align="align'.$alignment.'"]'
-                    .'<img class="align'.$alignment.' size-medium wp-image-' . $img_id . '" '
-                    . ' src="' . $img_url . '"> '
-                    . $data['imagename'] . '[/caption]';
+                $content .= '[caption align="align'.$alignment.'"]<img class="align'.$alignment.'" src="' . $img_url . '"> ' . $data['imagename'] . '[/caption]';
             } else {
-                $content .= '<img class="align'.$alignment.' size-medium wp-image-' . $img_id . '" '
-                    . ' src="' . $img_url . '">';
+                $content .= '<img class="align'.$alignment.'" src="' . $img_url . '">';
             }
             return $content;
         }
 
         private function _displayFile($data)
         {
-            if ($this->_options['toolbox_paragraphs_table'] == 'pages') {
-                $file_id = $this->_handleMediaFile($data['filename']);
-            } else {
-                $file_id = $this->_handleMediaFile(
-                    $data['filename'],
-                    '',
-                    0,
-                    $this->_options['site_url'] . 'uploads/'
-                );
-            }
+            //if ($this->_options['toolbox_paragraphs_table'] == 'pages') {
+                //$file_id = $this->_handleMediaFile($data['filename']);
+            //} else {
+                //$file_id = $this->_handleMediaFile(
+                    //$data['filename'],
+                    //'',
+                    //0,
+                    //$this->_options['site_url'] . 'uploads/'
+                //);
+            //}
 
-            if (!$file_id) {
-                return false;
-            }
-            $fileUrl = wp_get_attachment_url($file_id);
+            //if (!$file_id) {
+                //return false;
+            //}
+            //$fileUrl = wp_get_attachment_url($file_id);
+            $fileUrl =$this->_options['toolbox_image_url'] . '/original/' . $data['filename'];
             $fileName = ($data['urltext']) ? $data['urltext'] : $data['filename'];
             return "\n".'<a href="'.$fileUrl.'">'.$fileName.'</a>';
         }
             return ($id) ? $this->_getAttachmentById($id) : false;
         }
 
-
-        /**
-         * _handleMediaFile
-         *
-         * @param mixed $file  The file name
-         * @param int $post_id Parent Post id if given
-         *
-         * @access private
-         * @return void
-         */
         private function _handleMediaFile($file, $caption = '', $post_id = 0, $baseUrl = null)
         {
             $id = array_search($file, $this->_files);
             return $id;
         }
 
-        /**
-         * _fetchRemoteImage
-         *
-         * @param mixed $file     File name
-         * @param mixed $filename Path of new file
-         *
-         * @access private
-         * @return void
-         */
         private function _fetchRemoteImage($file, $path, $baseUrl = null)
         {
             $filename = wp_unique_filename($path, $file);
             return $WHERE;
         }
 
-        /**
-         * _fetchAllPages
-         *
-         * Fetch all the toolbox pages
-         *
-         * @access private
-         * @return void
-         */
         private function _fetchAllPages($limit = null, $offset = 0)
         {
             $this->_connect();
                 FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_page_table']}
                 $WHERE
             ORDER BY parent,pos";
-            $pageSql .= " LIMIT $limit OFFSET $offset";
+            $pageSql .= ($limit) ? " LIMIT $limit OFFSET $offset" : '';
             $pageData = $this->_dbh->query($pageSql)->fetchAll(PDO::FETCH_ASSOC);
             if ($this->_options['toolbox_page_table'] == 'pages') {
                 $paragraphSql = "
                   SELECT *
                     FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_paragraphs_table']}
                    WHERE page = :page
+                     AND active = true
                 ORDER BY pos";
                 $paraStmt = $this->_dbh->prepare($paragraphSql);
                 $fileSql = "
                     $page['pageContent'] .= '<h1>'.$page['category'].'</h1>';
                     ++$iterator;
                     if ($page['image']) {
-                        $page['images'][]     = $page['image'];
+                        //$page['images'][]     = $page['image'];
                         $page['pageContent'] .= $this->_displayImage($page, $primaryAlign);
                     }
                     $page['pageContent'] .= $page['description'];
                             $page['pageContent'] .= '<h1>'.$paragraph['title'].'</h1>';
                         }
                         if (isset($paragraph['image']) && $paragraph['image']) {
-                            $page['images'][]     = $paragraph['image'];
+                            //$page['images'][]     = $paragraph['image'];
                             $page['pageContent'] .= $this->_displayImage($paragraph, $primaryAlign);
                         }
                         break;
                             $page['pageContent'] .= '<h2>'.$paragraph['title'].'</h2>';
                         }
                         if (isset($paragraph['image']) && $paragraph['image']) {
-                            $page['images'][]     = $paragraph['image'];
+                            //$page['images'][]     = $paragraph['image'];
                             $page['pageContent'] .= $this->_displayImage($paragraph, $secondaryAlign);
                         }
                         break;
                             $align = $secondaryAlign;
                         }
                         if (isset($paragraph['image']) && $paragraph['image']) {
-                            $page['images'][]     = $paragraph['image'];
+                            //$page['images'][]     = $paragraph['image'];
                             $page['pageContent'] .= $this->_displayImage($paragraph, $align);
                         }
                         break;
                     }
                     $page['pageContent'] .= $paragraph['description'];
-                    preg_match_all("/<img .*?(?=src)src=\"([^\"]+)\"/si", $paragraph['description'], $matches);
+                    //preg_match_all("/<img .*?(?=src)src=\"([^\"]+)\"/si", $paragraph['description'], $matches);
             //echo '<pre>' . print_r($matches, true) . '</pre>';
 
+                    /*
                     for ($i = 0; $i < count($matches[0]); $i++) {
                         if ($matches[1][$i]) {
                             $newSrc  = $this->_getDescriptionImage($matches[1][$i]);
                             $page['srcs'][] = $imgName;
                         }
                     }
+                     */
                     //var_dump($paragraph['id']);
                     $fileStmt->bindParam(':pid', $paragraph['id'], PDO::PARAM_INT);
                     $fileStmt->execute();
                     while ($file = $fileStmt->fetch()) {
             //echo '<pre>Files: ' . print_r($file, true) . '</pre>';
-                        $page['files'][] = $file['filename'];
+                        //$page['files'][] = $file['filename'];
                         $page['pageContent'] .= $this->_displayFile($file);
                     }
                     ++$iterator;
             return $data;
         }
 
-        /**
-         * _readOptions
-         *
-         * Display all Import options
-         *
-         * @access private
-         * @return void
-         */
         private function _readOptions()
         {
             echo '<pre>' . print_r($this->_options, true) . '</pre>';
             echo '<pre>' . print_r(get_option(GLM_WP_IMPORT_POST_OPTION, array()), true) . '</pre>';
         }
 
-        /**
-         * _import
-         *
-         * Handles the page import
-         *
-         * @access private
-         * @return void
-         */
         private function _import()
         {
             echo '<p>Fetching Pages</p>';
             $this->_post = get_option(GLM_WP_IMPORT_POST_OPTION, array());
             // grab all pages and build the post array
             $start = filter_var($_REQUEST['start'], FILTER_VALIDATE_INT);
-            $pages = $this->_fetchAllPages(PAGES_PER_LOAD, $start);
+            //$pages = $this->_fetchAllPages(PAGES_PER_LOAD, $start);
+            $pages = $this->_fetchAllPages();
             //echo '<pre>' . print_r($pages, true) . '</pre>';
+            //return;
             //exit;
             $numPagesImported = count($pages);
 
                 $ID = wp_insert_post($post, true);
                 $newPost = get_post($ID, ARRAY_A);
                 $this->_post[$page['id']] = $ID;
-                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'])) {
-                    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) {
-                            wp_update_post(array('ID' => $file_id, 'post_parent' => $ID));
-                        }
-                    }
-                }
             }
             $this->_storePost();
             return $numPagesImported;
             }
         }
 
-        function handle_accents($content) {
-        // from: http://www.php.net/manual/en/domdocument.loadhtml.php#91513
-        if ( !empty( $content ) && function_exists( 'mb_convert_encoding' ) ) {
-            mb_detect_order( "ASCII,UTF-8,ISO-8859-1,windows-1252,iso-8859-15" );
-            if ( empty( $encod ) )
-                $encod = mb_detect_encoding( $content );
+        function _handleAccents($content)
+        {
+            // from: http://www.php.net/manual/en/domdocument.loadhtml.php#91513
+            if ( !empty( $content ) && function_exists( 'mb_convert_encoding' ) ) {
+                mb_detect_order( "ASCII,UTF-8,ISO-8859-1,windows-1252,iso-8859-15" );
+                if ( empty( $encod ) ) {
+                    $encod = mb_detect_encoding( $content );
+                }
                 $headpos = mb_strpos( $content,'<head>' );
-            if ( FALSE === $headpos )
-                $headpos= mb_strpos( $content,'<HEAD>' );
-            if ( FALSE !== $headpos ) {
-                $headpos+=6;
-                $content = mb_substr( $content,0,$headpos ) . '<meta http-equiv="Content-Type" content="text/html; charset='.$encod.'">' .mb_substr( $content,$headpos );
+                if ( FALSE === $headpos ) {
+                    $headpos= mb_strpos( $content,'<HEAD>' );
+                }
+                if ( FALSE !== $headpos ) {
+                    $headpos+=6;
+                    $content = mb_substr( $content,0,$headpos ) . '<meta http-equiv="Content-Type" content="text/html; charset='.$encod.'">' .mb_substr( $content,$headpos );
+                }
+                $content = mb_convert_encoding( $content, 'HTML-ENTITIES', $encod );
             }
-            $content = mb_convert_encoding( $content, 'HTML-ENTITIES', $encod );
+            return $content;
         }
-        return $content;
-    }
 
         private function _replaceSrc()
         {
             }
         }
 
-        /**
-         * dispatch
-         *
-         * Dispatch Function.
-         * Checks which step admin user is on and calls appropriate methods.
-         *
-         * @access public
-         * @return void
-         */
+        private function _getCkImages()
+        {
+            global $wpdb;
+            if ($this->_options['ckeditor_schema'] && $this->_options['ckeditor_images_table']) {
+                $this->_connect();
+                $ckimagesSql = "
+                SELECT *
+                  FROM {$this->_options['ckeditor_schema']}.{$this->_options['ckeditor_images_table']}";
+                $ckData = $this->_dbh->query($ckimagesSql)->fetchAll();
+                echo '<pre>' . print_r($ckData, true) . '</pre>';
+            } else {
+                return false;
+            }
+        }
+
         public function dispatch()
         {
             $this->_header();
                 //$this->_storePost();
                 $this->_greet();
                 $this->_readOptions();
+                echo '<p><a href="admin.php?import=toolbox&amp;step=5&start=0">CkImages</a></p>';
                 echo '<p><a href="admin.php?import=toolbox&amp;step=1&start=0">Begin Import</a></p>';
                 break;
             case 1:
                 $subPageIds = $this->_getSubPageIds($this->_options['include_pages']);
                 echo '<pre>'.print_r($subPageIds, true).'</pre>';
                 break;
+            case 5:
+                $this->_getCkImages();
+                break;
             }
 
             $this->_footer();