Plugin can now import the pages.
authorSteve Sutton <ssutton@gmail.com>
Sun, 12 Jul 2015 13:25:11 +0000 (09:25 -0400)
committerSteve Sutton <ssutton@gmail.com>
Sun, 12 Jul 2015 13:25:11 +0000 (09:25 -0400)
It also handles setting up the proper parent id for each post.
Added Notes.

NOTES.md
controllers/Admin.php
controllers/Import.php

index bd35942..04764b6 100644 (file)
--- a/NOTES.md
+++ b/NOTES.md
@@ -1,3 +1,4 @@
+#Notes
 Resources for the image importing:
 
 Plugins that work well with importing the images into the Media Library.
@@ -11,14 +12,14 @@ I'm planning on using the same thing here.
 3. Fetch list of all images, files and import them into Media Library
 4. Update all pages with new references of the images.
 
-##REFERENCES:
+## REFERENCES:
 For Wordpress Plugin Development
 https://developer.wordpress.org/plugins/the-basics/header-requirements/
 
-##Importing pages
+## Importing pages
 https://codex.wordpress.org/Function_Reference/wp_insert_post
 ```
-IMPORTANT: Setting a value for $post['ID'] WILL NOT create a post with that ID number. Setting this value will cause the function to update the post with that ID number with the other values specified in $post. In short, to insert a new post, $post['ID'] must be blank or not set at all. 
+IMPORTANT: Setting a value for $post['ID'] WILL NOT create a post with that ID number. Setting this value will cause the function to update the post with that ID number with the other values specified in $post. In short, to insert a new post, $post['ID'] must be blank or not set at all.
 
 The contents of the post array can depend on how much (or little) you want to trust the defaults. Here is a list with a short description of all the keys you can set for a post:
 
@@ -49,5 +50,7 @@ $post = array(
 );
 ```
 
-##Settings
-Will also need to add the site url to the setting and this will need to be required. I need to build the urls for the images.
+## Todo
+Need to pull all image url’s out of the post_content sections and replace them with images that are imported into the Media Library.
+Need to replace any ‘keywords’ with the correct url’s.
+
index b2597e3..2303fa0 100644 (file)
@@ -87,6 +87,11 @@ class GlmWPImporter_Admin_Controller
         );
 
         $fieldNames = array(
+            array(
+                'name'  => 'site_url',
+                'label' => 'Site URL',
+                'type'  => 'text'
+            ),
             array(
                 'name'  => 'db_host',
                 'label' => 'Database Host',
@@ -137,6 +142,11 @@ class GlmWPImporter_Admin_Controller
                 'label' => 'CKEditor Images Table',
                 'type'  => 'text'
             ),
+            array(
+                'name'  => 'member_page_id',
+                'label' => 'Page ID for Members Only',
+                'type'  => 'text'
+            ),
         );
 
         foreach ($fieldNames as $field) {
index c17725d..064a8a4 100644 (file)
  }
 
  if (class_exists('WP_Importer')) {
-     class Toolbox_Import extends WP_Importer
-     {
-         private $_dbh;
-         private $_post;
-         function __construct()
-         {
-         }
-
-        function connect()
+    class Toolbox_Import extends WP_Importer
+    {
+        private $_dbh;
+        private $_post;
+        private $_options;
+
+
+        public function __construct()
+        {
+            $this->_getOptions();
+        }
+
+        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);
+        }
+
+        private function _connect()
         {
             if ($this->_dbh) {
                 return;
             }
-             // read options for the import and if they aren't set then give a warning.
-             $options = get_option(GLM_WP_IMPORT_SETTINGS);
-             // try making a postgres connection to the database
-             $connString  = 'dbname=' . $options['db_name'];
-             $connString .= ' host=' . $options['db_host'];
-             $connString .= ' user=' . $options['db_user'];
-             $driverOptions = array(
-                 PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
-             );
-             $this->_dbh = new PDO('pgsql:'.$connString, null, null, $driverOptions);
-             $this->_dbh->setAttribute(
-                 PDO::ATTR_ERRMODE,
-                 PDO::ERRMODE_EXCEPTION
-             );
+            // try making a postgres connection to the database
+            $connString  = 'dbname=' . $this->_options['db_name'];
+            $connString .= ' host=' . $this->_options['db_host'];
+            $connString .= ' user=' . $this->_options['db_user'];
+            $driverOptions = array(
+                PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
+            );
+            $this->_dbh = new PDO('pgsql:'.$connString, null, null, $driverOptions);
+            $this->_dbh->setAttribute(
+                PDO::ATTR_ERRMODE,
+                PDO::ERRMODE_EXCEPTION
+            );
+        }
+
+        private function _header()
+        {
+            echo '<div class="wrap">';
+            echo '<h2>'.__('Toolbox Importer').'</h2>';
+        }
+
+        private function _footer()
+        {
+            echo '</div>';
+        }
+
+        private function _greet()
+        {
+            echo '<h2>Hello there person!</h2>';
+            $this->_connect();
+            $sql = "SELECT * FROM toolbox.pages";
+            $data = $this->_dbh->query($sql)->fetchAll();
+            printf('<p>Found %d Pages</p>', count($data));
+            echo '<a href="admin.php?import=toolbox&amp;step=1">Start the Import Now</a>';
         }
 
-         function header()
-         {
-             echo '<div class="wrap">';
-             echo '<h2>'.__('Toolbox Importer').'</h2>';
-         }
-
-         function footer()
-         {
-             echo '</div>';
-         }
-
-         function greet()
-         {
-             echo '<h2>Hello there person!</h2>';
-             $this->connect();
-             $sql = "SELECT * FROM toolbox.pages";
-             $data = $this->_dbh->query($sql)->fetchAll();
-             printf('<p>Found %d Pages</p>', count($data));
-             echo '<a href="admin.php?import=toolbox&amp;step=1">Start the Import Now</a>';
-         }
-
-        function displayImage($data, $alignment)
+        private function _displayImage($data, $alignment)
         {
             if ($data['caption']) {
                 $content .= '[caption id="attachment_'.$data['id'].'" align="align'.$alignment.'"]'
             return $content;
         }
 
-         function fetchAllPages()
-         {
-             $this->connect();
+        private function _fetchAllPages()
+        {
+            $this->_connect();
+            $where = '';
+            if ($this->_options['member_page_id']) {
+                $where = "WHERE id NOT IN ({$this->_options['member_page_id']})
+                    AND parent != {$this->_options['member_page_id']} ";
+            }
             $pageSql = "
               SELECT *
-                FROM toolbox.pages
+                FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_page_table']}
+                $where
             ORDER BY parent,pos";
             $pageData = $this->_dbh->query($pageSql)->fetchAll(PDO::FETCH_ASSOC);
             $paragraphSql = "
               SELECT *
-                FROM toolbox.paragraphs
+                FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_paragraphs_table']}
                WHERE page = :page
             ORDER BY pos";
             $paraStmt = $this->_dbh->prepare($paragraphSql);
-             $data = $this->_dbh->query($pageSql)->fetchAll();
+            $data = $this->_dbh->query($pageSql)->fetchAll();
             foreach ($data as &$page) {
                 $paraStmt->bindParam(':page', $page['id'], PDO::PARAM_INT);
                 $paraStmt->execute();
                     break;
                 }
                 $iterator = 1;
-            $page['pageContent'] = '';
-            foreach ($paragraphs as $paragraph) {
-                switch ($iterator) {
-                case 1:
-                    if ($paragraph['title']) {
-                        $page['pageContent'] .= '<h1>'.$paragraph['title'].'</h1>';
-                    }
-                    if ($paragraph['image']) {
-                        $page['pageContent'] .= $this->displayImage($paragraph, $primaryAlign);
-                    }
-                    break;
-                case 2:
-                    if ($paragraph['title']) {
-                        $page['pageContent'] .= '<h2>'.$paragraph['title'].'</h2>';
+                $page['pageContent'] = '';
+                foreach ($paragraphs as $paragraph) {
+                    switch ($iterator) {
+                    case 1:
+                        if ($paragraph['title']) {
+                            $page['pageContent'] .= '<h1>'.$paragraph['title'].'</h1>';
+                        }
+                        if ($paragraph['image']) {
+                            $page['pageContent'] .= $this->_displayImage($paragraph, $primaryAlign);
+                        }
+                        break;
+                    case 2:
+                        if ($paragraph['title']) {
+                            $page['pageContent'] .= '<h2>'.$paragraph['title'].'</h2>';
+                        }
+                        if ($paragraph['image']) {
+                            $page['pageContent'] .= $this->_displayImage($paragraph, $secondaryAlign);
+                        }
+                        break;
+                    default:
+                        if ($paragraph['title']) {
+                            $page['pageContent'] .= '<h2>'.$paragraph['title'].'</h2>';
+                        }
+                        if ($alternateImg && $page['template'] == 3) {
+                            $align = ($iterator%2 == 0) ? 'left' : 'right';
+                        } else {
+                            $align = $secondaryAlign;
+                        }
+                        if ($paragraph['image']) {
+                            $page['pageContent'] .= $this->_displayImage($paragraph, $align);
+                        }
+                        break;
                     }
-                    if ($paragraph['image']) {
-                        $page['pageContent'] .= $this->displayImage($paragraph, $secondaryAlign);
-                    }
-                    break;
-                default:
-                    if ($paragraph['title']) {
-                        $page['pageContent'] .= '<h2>'.$paragraph['title'].'</h2>';
-                    }
-                    if ($alternateImg && $page['template'] == 3) {
-                        $align = ($iterator%2 == 0) ? 'left' : 'right';
-                    } else {
-                        $align = $secondaryAlign;
-                    }
-                    if ($paragraph['image']) {
-                        $page['pageContent'] .= $this->displayImage($paragraph, $align);
-                    }
-                    break;
-                }
-                $page['pageContent'] .= $paragraph['description'];
-                ++$iterator;
+                    $page['pageContent'] .= $paragraph['description'];
+                    ++$iterator;
                 }
             }
             return $data;
-         }
+        }
+
+        private function _readOptions()
+        {
+            var_dump($this->_options);
+        }
 
-         function import()
-         {
+        private function _import()
+        {
             echo '<p>Fetching Pages</p>';
             // grab all pages and build the post array
-            $pages = $this->fetchAllPages();
-            echo '<pre>'.print_r($pages, true).'</pre>';
-         }
-
-         function dispatch()
-         {
-             if (empty($_GET['step'])) {
-                 $step = 0;
-             } else {
-                 $step = filter_var($_GET['step'], FILTER_VALIDATE_INT);
-             }
-             switch($step) {
-             case 0:
-                 $this->greet();
-                 break;
-             case 1:
-                 $this->import();
-                 break;
-             }
-         }
-     }
- }
+            $pages = $this->_fetchAllPages();
+            foreach ($pages as $page) {
+                $post = array(
+                    'post_content'   => $page['pageContent'],
+                    'post_name'      => $page['navigation_name'],// slug ?
+                    'post_title'     => $page['navigation_name'],
+                    'post_status'    => 'publish',
+                    'post_type'      => 'page',
+                    'post_author'    => 'steve',
+                    'ping_status'    => 'closed',
+                    'post_parent'    => $this->_post[$page['parent']]['ID'],
+                    'menu_order'     => $page['pos'],
+                    'comment_status' => 'closed',
+                );
+                $ID = wp_insert_post($post, true);
+                $newPost = get_post($ID, ARRAY_A);
+                $this->_post[$page['id']] = $newPost;
+            }
+            var_dump($pages);
+        }
+
+        public function dispatch()
+        {
+            $this->_header();
+
+            if (empty($_GET['step'])) {
+                $step = 0;
+            } else {
+                $step = filter_var($_GET['step'], FILTER_VALIDATE_INT);
+            }
+            switch($step) {
+            case 0:
+                $this->_greet();
+                $this->_readOptions();
+                break;
+            case 1:
+                $this->_import();
+                break;
+            }
+
+            $this->_footer();
+        }
+    }
+}
 
- $toolbox_import = new Toolbox_Import();
+$toolbox_import = new Toolbox_Import();
 
- register_importer(
+register_importer(
      'toolbox',
      __('Toolbox', 'import-toolbox-pages'),
      sprintf(
          'options-general.php?page=glmwpimporter'
      ),
      array($toolbox_import, 'dispatch')
- );
+);