2009-11-22 20:35:39 +00:00
|
|
|
<?php
|
2009-11-22 20:50:16 +00:00
|
|
|
|
2010-01-14 20:24:17 +00:00
|
|
|
/**
|
|
|
|
* Set up fresh WordPress data quickly and easily.
|
|
|
|
*/
|
2009-11-22 20:50:16 +00:00
|
|
|
class PostFixtures {
|
2009-11-23 00:57:35 +00:00
|
|
|
var $messages;
|
|
|
|
|
2010-01-14 20:24:17 +00:00
|
|
|
/**
|
|
|
|
* Initialize the plugin.
|
|
|
|
*/
|
2010-01-14 21:19:58 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2009-11-23 00:57:35 +00:00
|
|
|
function init() {
|
2010-01-14 21:19:58 +00:00
|
|
|
foreach (array(
|
|
|
|
'admin_init' => array(),
|
|
|
|
'admin_menu' => array(),
|
|
|
|
'admin_notices' => array(),
|
|
|
|
'admin_enqueue_scripts' => array(10, 1),
|
|
|
|
) as $method => $additional_params) {
|
|
|
|
call_user_func_array(
|
|
|
|
'add_action',
|
|
|
|
array_merge(
|
|
|
|
array($method, array(&$this, $method)),
|
|
|
|
$additional_params
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2009-11-23 00:57:35 +00:00
|
|
|
}
|
2010-01-14 21:19:58 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2009-11-23 00:57:35 +00:00
|
|
|
|
2010-01-14 20:24:17 +00:00
|
|
|
/**
|
|
|
|
* Print any admin notices.
|
|
|
|
*/
|
2010-01-14 21:19:58 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2009-11-23 00:57:35 +00:00
|
|
|
function admin_notices() {
|
|
|
|
if (!empty($this->messages)) { ?>
|
|
|
|
<div class="fade updated">
|
|
|
|
<?php foreach ($this->messages as $message) { ?>
|
|
|
|
<p><?php echo $message ?></p>
|
|
|
|
<?php } ?>
|
|
|
|
</div>
|
|
|
|
<?php }
|
|
|
|
}
|
2010-01-14 21:19:58 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2009-11-23 00:57:35 +00:00
|
|
|
|
2010-01-14 20:24:17 +00:00
|
|
|
/**
|
|
|
|
* Handle an update.
|
|
|
|
* @param array $info The post info.
|
|
|
|
*/
|
2009-11-23 00:57:35 +00:00
|
|
|
function handle_update($info) {
|
|
|
|
if (isset($info['is_ok'])) {
|
2010-01-18 03:59:05 +00:00
|
|
|
if (strpos($info['data'], 'file:') === 0) {
|
|
|
|
$this->handle_load_file(substr($info['data'], 5));
|
|
|
|
} else {
|
|
|
|
$this->handle_json($info['data']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handle_load_file($file) {
|
|
|
|
$file = realpath($this->wpcontent_path() . '/' . $file);
|
|
|
|
if (file_exists($file)) {
|
|
|
|
switch (pathinfo($file, PATHINFO_EXTENSION)) {
|
|
|
|
case "json":
|
|
|
|
$this->handle_json(file_get_contents($file));
|
|
|
|
break;
|
|
|
|
case "inc";
|
|
|
|
$this->remove();
|
|
|
|
$builder = new FixtureBuilder();
|
|
|
|
include($file);
|
|
|
|
$builder->finalize();
|
2010-01-18 05:15:11 +00:00
|
|
|
|
|
|
|
$this->messages[] = __("New data set loaded into WordPress.", 'post-fixtures');
|
|
|
|
|
2010-01-18 03:59:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handle_json($input) {
|
|
|
|
$data = $this->parse_json(stripslashes($input));
|
|
|
|
if (!empty($data)) {
|
|
|
|
$data = $this->process_data($data);
|
|
|
|
$this->remove();
|
|
|
|
$this->create($data);
|
|
|
|
$this->messages[] = __("New data set loaded into WordPress.", 'post-fixtures');
|
|
|
|
} else {
|
|
|
|
$this->messages[] = __("Data is not valid JSON.", 'post-fixtures');
|
2009-11-23 00:57:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-14 20:24:17 +00:00
|
|
|
/**
|
|
|
|
* Handle the admin_menu action.
|
|
|
|
*/
|
2010-01-14 21:19:58 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2009-11-23 00:57:35 +00:00
|
|
|
function admin_menu() {
|
|
|
|
global $plugin_page;
|
|
|
|
|
2010-01-14 20:24:17 +00:00
|
|
|
$this->hook_suffix = add_submenu_page('tools.php', __('Post Fixtures', 'post-fixtures'), __('Post Fixtures', 'post-fixtures'), 'manage_options', 'post-fixtures', array(&$this, 'render_admin'));
|
2010-01-14 21:19:58 +00:00
|
|
|
}
|
|
|
|
// @codeCoverageIgnoreEnd
|
2009-11-23 00:57:35 +00:00
|
|
|
|
2010-01-14 21:19:58 +00:00
|
|
|
/**
|
|
|
|
* Handle the admin_init action.
|
|
|
|
* This is where the processing happens.
|
|
|
|
*/
|
|
|
|
function admin_init() {
|
|
|
|
if (isset($_POST['pf']['_nonce'])) {
|
|
|
|
if (wp_verify_nonce($_POST['pf']['_nonce'], 'post-fixtures')) {
|
|
|
|
$this->handle_update($_POST['pf']);
|
2009-11-23 00:57:35 +00:00
|
|
|
}
|
|
|
|
}
|
2010-01-14 21:19:58 +00:00
|
|
|
unset($_POST['pf']);
|
2009-11-23 00:57:35 +00:00
|
|
|
}
|
|
|
|
|
2010-01-14 20:24:17 +00:00
|
|
|
/**
|
|
|
|
* Handle the admin_enqueue_scripts action.
|
|
|
|
* @param string $hook_suffix The hook suffix for the page being loaded.
|
|
|
|
*/
|
2010-01-14 21:19:58 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2010-01-14 20:24:17 +00:00
|
|
|
function admin_enqueue_scripts($hook_suffix) {
|
|
|
|
if ($this->hook_suffix == $hook_suffix) {
|
|
|
|
wp_enqueue_script('jquery');
|
2010-01-14 21:03:28 +00:00
|
|
|
wp_enqueue_style('post-fixtures', plugin_dir_url(dirname(__FILE__)) . 'style.css');
|
2010-01-14 20:24:17 +00:00
|
|
|
}
|
|
|
|
}
|
2010-01-14 21:19:58 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2010-01-14 20:24:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Render the admin page.
|
|
|
|
*/
|
2010-01-18 02:28:26 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2010-01-14 20:41:55 +00:00
|
|
|
function render_admin() {
|
2010-01-18 02:34:24 +00:00
|
|
|
$fixtures = $this->find_fixtures();
|
2010-01-18 02:28:26 +00:00
|
|
|
|
2010-01-14 20:41:55 +00:00
|
|
|
include(dirname(__FILE__) . '/partials/admin.inc');
|
|
|
|
}
|
2009-11-23 00:57:35 +00:00
|
|
|
|
2010-01-18 02:28:26 +00:00
|
|
|
function wpcontent_path() { return ABSPATH . '/wp-content'; }
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
|
|
|
function find_fixtures() {
|
|
|
|
$this->fixtures = array();
|
|
|
|
|
|
|
|
$this->_find_fixtures_recurse($this->wpcontent_path());
|
|
|
|
|
2010-01-18 02:34:24 +00:00
|
|
|
foreach ($this->fixtures as &$path) {
|
|
|
|
$path = str_replace($this->wpcontent_path() . '/', '', $path);
|
|
|
|
}
|
|
|
|
|
2010-01-18 02:28:26 +00:00
|
|
|
return $this->fixtures;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _find_fixtures_recurse($dir) {
|
|
|
|
$queue = array();
|
|
|
|
$dh = opendir($dir);
|
|
|
|
$is_fixture_dir = (basename($dir) === 'fixtures');
|
|
|
|
while ($file = readdir($dh)) {
|
2010-01-18 03:59:05 +00:00
|
|
|
if ($file[0] != '.') {
|
2010-01-18 02:28:26 +00:00
|
|
|
$target = $dir . '/' . $file;
|
|
|
|
if ($is_fixture_dir) {
|
|
|
|
if (is_file($target)) {
|
|
|
|
if (preg_match('#\.(inc|json)$#', $target) > 0) {
|
|
|
|
$this->fixtures[] = $target;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (is_dir($target)) {
|
|
|
|
$queue[] = $target;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($dh);
|
|
|
|
|
|
|
|
foreach ($queue as $dir) {
|
|
|
|
$this->_find_fixtures_recurse($dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-23 00:57:35 +00:00
|
|
|
// data handling
|
|
|
|
|
2010-01-14 20:24:17 +00:00
|
|
|
/**
|
|
|
|
* Parse incoming JSON data.
|
|
|
|
* @param string $input The JSON data to parse.
|
|
|
|
* @return array|false An array of JSON data, or false if invalid.
|
|
|
|
*/
|
2009-11-22 20:50:16 +00:00
|
|
|
function parse_json($input) {
|
|
|
|
if (($data = json_decode($input)) !== false) {
|
|
|
|
if (is_array($data) || is_object($data)) {
|
2009-11-23 13:39:09 +00:00
|
|
|
return (array)$data;
|
2009-11-22 20:50:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-22 22:34:57 +00:00
|
|
|
|
2010-01-14 20:24:17 +00:00
|
|
|
/**
|
|
|
|
* Remove all posts in the database.
|
|
|
|
* This is done via the WP interface so that associated comments are also deleted.
|
|
|
|
*/
|
2009-11-22 22:34:57 +00:00
|
|
|
function remove_all_posts() {
|
2009-11-23 00:57:35 +00:00
|
|
|
if (is_array($posts = get_posts(array('numberposts' => '-1', 'post_status' => 'draft,pending,future,inherit,private,publish')))) {
|
2009-11-22 22:34:57 +00:00
|
|
|
foreach ($posts as $post) { wp_delete_post($post->ID); }
|
|
|
|
}
|
|
|
|
}
|
2009-11-22 22:58:34 +00:00
|
|
|
|
2010-01-14 20:24:17 +00:00
|
|
|
/**
|
|
|
|
* Remove all categories in the database.
|
|
|
|
*/
|
2009-11-22 22:58:34 +00:00
|
|
|
function remove_all_categories() {
|
|
|
|
foreach (get_all_category_ids() as $id) {
|
|
|
|
wp_delete_category($id);
|
|
|
|
}
|
|
|
|
}
|
2009-11-22 23:11:59 +00:00
|
|
|
|
2010-01-14 20:24:17 +00:00
|
|
|
/**
|
|
|
|
* Process the provided data and assemble a list of objects to create in the database.
|
|
|
|
* @param array $data The data to parse.
|
|
|
|
* @return array The list of objects to create.
|
|
|
|
*/
|
2009-11-22 23:11:59 +00:00
|
|
|
function process_data($data) {
|
2009-11-23 13:18:59 +00:00
|
|
|
$posts = $categories = $options = array();
|
2009-11-23 12:27:36 +00:00
|
|
|
|
|
|
|
foreach ($data as $type => $info) {
|
|
|
|
switch ($type) {
|
|
|
|
case 'posts':
|
|
|
|
$posts = $info;
|
|
|
|
foreach ($posts as $post) {
|
|
|
|
$post = (array)$post;
|
|
|
|
if (isset($post['categories'])) {
|
|
|
|
$categories = array_merge($categories, $post['categories']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2009-11-23 13:18:59 +00:00
|
|
|
case 'options':
|
|
|
|
$options = $info;
|
|
|
|
break;
|
2009-11-22 23:11:59 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-23 00:57:35 +00:00
|
|
|
$categories = array_unique($categories);
|
2009-11-23 13:18:59 +00:00
|
|
|
return compact('posts', 'categories', 'options');
|
2009-11-22 23:11:59 +00:00
|
|
|
}
|
2009-11-22 23:32:52 +00:00
|
|
|
|
2010-01-14 20:24:17 +00:00
|
|
|
/**
|
|
|
|
* The categories to create.
|
|
|
|
* Categories are passed as name/name/name, with parent -> child relationships being constructed as necessary.
|
|
|
|
*/
|
2009-11-22 23:32:52 +00:00
|
|
|
function create_categories($categories) {
|
|
|
|
$category_ids_by_slug = array();
|
|
|
|
if (is_array($categories)) {
|
|
|
|
foreach ($categories as $category) {
|
2010-01-18 00:41:10 +00:00
|
|
|
$category_ids_by_slug = array_merge($category_ids_by_slug, $this->create_category($category));
|
2009-11-22 23:32:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $category_ids_by_slug;
|
|
|
|
}
|
2009-11-22 23:57:23 +00:00
|
|
|
|
2010-01-18 00:41:10 +00:00
|
|
|
function create_category($category) {
|
|
|
|
$category_ids_by_slug = array();
|
|
|
|
$nodes = explode('/', $category);
|
|
|
|
$parent = 0;
|
|
|
|
$joined_nodes = array();
|
|
|
|
foreach ($nodes as $node) {
|
|
|
|
$joined_nodes[] = $node;
|
|
|
|
$parent = $category_ids_by_slug[implode('/', $joined_nodes)] = wp_insert_category(array('cat_name' => $node, 'category_nicename' => $node, 'category_parent' => $parent));
|
|
|
|
}
|
|
|
|
return $category_ids_by_slug;
|
|
|
|
}
|
|
|
|
|
2010-01-14 20:24:17 +00:00
|
|
|
/**
|
|
|
|
* The posts to create.
|
|
|
|
* Post data is passed in just as if you were using wp_insert_post().
|
|
|
|
* Categories are assigned using slug names separated by commas.
|
|
|
|
*/
|
2009-11-22 23:57:23 +00:00
|
|
|
function create_posts($posts, $categories) {
|
|
|
|
$post_ids_created = array();
|
|
|
|
if (is_array($posts)) {
|
|
|
|
foreach ($posts as $post) {
|
2010-01-18 00:41:10 +00:00
|
|
|
$id = $this->create_post($post);
|
2009-11-22 23:57:23 +00:00
|
|
|
if ($id != 0) {
|
|
|
|
$post_ids_created[] = $id;
|
|
|
|
if (isset($post['categories'])) {
|
|
|
|
$all_categories = array();
|
|
|
|
foreach ($post['categories'] as $slug) {
|
|
|
|
if (isset($categories[$slug])) {
|
|
|
|
$all_categories[] = $categories[$slug];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wp_set_post_categories($id, $all_categories);
|
|
|
|
} else {
|
|
|
|
wp_set_post_categories($id, array(get_option('default_category')));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($post['metadata'])) {
|
|
|
|
foreach ($post['metadata'] as $key => $value) {
|
2010-01-18 00:41:10 +00:00
|
|
|
$this->create_post_metadata($id, $key, $value);
|
2009-11-22 23:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
2010-01-18 04:56:41 +00:00
|
|
|
|
|
|
|
if (isset($post['tags'])) {
|
|
|
|
$this->create_post_tags($id, $post['tags']);
|
|
|
|
}
|
2009-11-22 23:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $post_ids_created;
|
|
|
|
}
|
2009-11-23 00:05:20 +00:00
|
|
|
|
2010-01-18 00:41:10 +00:00
|
|
|
function create_post($post) {
|
|
|
|
$post = (array)$post;
|
|
|
|
if (!isset($post['post_status'])) {
|
|
|
|
$post['post_status'] = 'publish';
|
|
|
|
}
|
|
|
|
return wp_insert_post($post);
|
|
|
|
}
|
|
|
|
|
|
|
|
function create_post_metadata($post_id, $key, $value) {
|
|
|
|
update_post_meta($post_id, $key, $value);
|
|
|
|
}
|
|
|
|
|
2010-01-18 04:56:41 +00:00
|
|
|
function create_post_tags($post_id, $tags) {
|
|
|
|
wp_set_post_tags($post_id, $tags);
|
|
|
|
}
|
|
|
|
|
2010-01-18 03:59:05 +00:00
|
|
|
function set_post_categories($post_id, $categories) {
|
|
|
|
wp_set_post_categories($post_id, $categories);
|
|
|
|
}
|
|
|
|
|
2010-01-14 20:24:17 +00:00
|
|
|
/**
|
|
|
|
* Create everything from the provided data.
|
|
|
|
* @param array $data The data to use in creation.
|
|
|
|
*/
|
2009-11-23 00:05:20 +00:00
|
|
|
function create($data) {
|
2009-11-23 00:57:35 +00:00
|
|
|
$categories_by_slug = $this->create_categories($data['categories']);
|
|
|
|
$this->create_posts($data['posts'], $categories_by_slug);
|
2009-11-23 13:20:52 +00:00
|
|
|
$this->process_blog_options($data['options'], $categories_by_slug);
|
2009-11-23 00:05:20 +00:00
|
|
|
}
|
2009-11-23 00:07:13 +00:00
|
|
|
|
2010-01-14 20:24:17 +00:00
|
|
|
/**
|
|
|
|
* Remove everything from the WordPress database that Post Fixures handles.
|
|
|
|
*/
|
2009-11-23 00:07:13 +00:00
|
|
|
function remove() {
|
|
|
|
$this->remove_all_posts();
|
|
|
|
$this->remove_all_categories();
|
|
|
|
}
|
2009-11-23 13:16:55 +00:00
|
|
|
|
2010-01-14 20:24:17 +00:00
|
|
|
/**
|
|
|
|
* Update the provided blog options.
|
|
|
|
* Option values can have other values injected into them. Currently only category slug names are available.
|
|
|
|
* @param array $options The options to set or unset. Pass in `false` to unset them.
|
|
|
|
* @param array $categories The list of categories to work with in string replacement.
|
|
|
|
*/
|
2009-11-23 13:16:55 +00:00
|
|
|
function process_blog_options($options, $categories) {
|
|
|
|
$this->_category = $categories;
|
|
|
|
foreach ($options as $option => $value) {
|
2010-01-14 20:42:45 +00:00
|
|
|
if ($value === false) {
|
|
|
|
delete_option($option);
|
|
|
|
} else {
|
|
|
|
$value = preg_replace_callback('#\$\{([^\}]+)\}#', array(&$this, '_process_blog_options_callback'), $value);
|
|
|
|
update_option($option, $value);
|
|
|
|
}
|
2009-11-23 13:16:55 +00:00
|
|
|
}
|
|
|
|
unset($this->_category);
|
|
|
|
}
|
|
|
|
|
2010-01-14 20:24:17 +00:00
|
|
|
/**
|
|
|
|
* Callback for process_blog_options
|
|
|
|
* @param array $matches Matches from preg_replace_callback
|
|
|
|
* @return string The replacement value for the match.
|
|
|
|
*/
|
2009-11-23 13:16:55 +00:00
|
|
|
function _process_blog_options_callback($matches) {
|
|
|
|
$value = $matches[0];
|
|
|
|
$parts = explode(':', $matches[1]);
|
|
|
|
if (count($parts) > 1) {
|
2009-11-23 13:24:00 +00:00
|
|
|
$source = strtolower(array_shift($parts));
|
|
|
|
switch ($source) {
|
|
|
|
case 'cat': $source = 'category'; break;
|
|
|
|
}
|
2009-11-23 13:16:55 +00:00
|
|
|
if (count($parts) == 1) {
|
|
|
|
$index = reset($parts);
|
|
|
|
if (isset($this->{"_${source}"})) {
|
2010-01-18 03:59:05 +00:00
|
|
|
if (isset($this->{"_${source}"}[$index])) {
|
|
|
|
$value = $this->{"_${source}"}[$index];
|
|
|
|
}
|
2009-11-23 13:16:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
2009-11-22 20:50:16 +00:00
|
|
|
}
|