From: Steve Sutton Date: Sun, 12 Jul 2015 13:25:11 +0000 (-0400) Subject: Plugin can now import the pages. X-Git-Tag: v0.0.2^2~43 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=b64cb124934fc0275871b817ec83df8726431cea;p=WP-Plugins%2Fglm-wp-importer.git Plugin can now import the pages. It also handles setting up the proper parent id for each post. Added Notes. --- diff --git a/NOTES.md b/NOTES.md index bd35942..04764b6 100644 --- 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. + diff --git a/controllers/Admin.php b/controllers/Admin.php index b2597e3..2303fa0 100644 --- a/controllers/Admin.php +++ b/controllers/Admin.php @@ -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) { diff --git a/controllers/Import.php b/controllers/Import.php index c17725d..064a8a4 100644 --- a/controllers/Import.php +++ b/controllers/Import.php @@ -40,57 +40,65 @@ } 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 '
'; + echo '

'.__('Toolbox Importer').'

'; + } + + private function _footer() + { + echo '
'; + } + + private function _greet() + { + echo '

Hello there person!

'; + $this->_connect(); + $sql = "SELECT * FROM toolbox.pages"; + $data = $this->_dbh->query($sql)->fetchAll(); + printf('

Found %d Pages

', count($data)); + echo 'Start the Import Now'; } - function header() - { - echo '
'; - echo '

'.__('Toolbox Importer').'

'; - } - - function footer() - { - echo '
'; - } - - function greet() - { - echo '

Hello there person!

'; - $this->connect(); - $sql = "SELECT * FROM toolbox.pages"; - $data = $this->_dbh->query($sql)->fetchAll(); - printf('

Found %d Pages

', count($data)); - echo 'Start the Import Now'; - } - - function displayImage($data, $alignment) + private function _displayImage($data, $alignment) { if ($data['caption']) { $content .= '[caption id="attachment_'.$data['id'].'" align="align'.$alignment.'"]' @@ -104,21 +112,27 @@ 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(); @@ -150,76 +164,103 @@ break; } $iterator = 1; - $page['pageContent'] = ''; - foreach ($paragraphs as $paragraph) { - switch ($iterator) { - case 1: - if ($paragraph['title']) { - $page['pageContent'] .= '

'.$paragraph['title'].'

'; - } - if ($paragraph['image']) { - $page['pageContent'] .= $this->displayImage($paragraph, $primaryAlign); - } - break; - case 2: - if ($paragraph['title']) { - $page['pageContent'] .= '

'.$paragraph['title'].'

'; + $page['pageContent'] = ''; + foreach ($paragraphs as $paragraph) { + switch ($iterator) { + case 1: + if ($paragraph['title']) { + $page['pageContent'] .= '

'.$paragraph['title'].'

'; + } + if ($paragraph['image']) { + $page['pageContent'] .= $this->_displayImage($paragraph, $primaryAlign); + } + break; + case 2: + if ($paragraph['title']) { + $page['pageContent'] .= '

'.$paragraph['title'].'

'; + } + if ($paragraph['image']) { + $page['pageContent'] .= $this->_displayImage($paragraph, $secondaryAlign); + } + break; + default: + if ($paragraph['title']) { + $page['pageContent'] .= '

'.$paragraph['title'].'

'; + } + 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'] .= '

'.$paragraph['title'].'

'; - } - 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 '

Fetching Pages

'; // grab all pages and build the post array - $pages = $this->fetchAllPages(); - echo '
'.print_r($pages, true).'
'; - } - - 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( @@ -228,4 +269,4 @@ 'options-general.php?page=glmwpimporter' ), array($toolbox_import, 'dispatch') - ); +);