ok it's working

This commit is contained in:
John Bintz 2009-11-22 19:57:35 -05:00
parent 635ec15cab
commit 5b9a966c0e
3 changed files with 103 additions and 5 deletions

View File

@ -1,6 +1,96 @@
<?php <?php
class PostFixtures { class PostFixtures {
var $messages;
function init() {
add_action('admin_menu', array(&$this, 'admin_menu'));
add_action('admin_notices', array(&$this, 'admin_notices'));
}
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 }
}
function handle_update($info) {
if (isset($info['is_ok'])) {
$data = $this->parse_json(stripslashes($info['data']));
if (!empty($data)) {
$data = $this->process_data($data);
$this->remove();
$this->create($data);
$this->messages[] = "New data set loaded into WordPress.";
} else {
$this->messages[] = "Data is not valid JSON.";
}
}
}
function admin_menu() {
global $plugin_page;
add_submenu_page('tools.php', __('Post Fixtures', 'post-fixtures'), __('Post Fixtures', 'post-fixtures'), 'manage_options', 'post-fixtures', array(&$this, 'render_admin'));
if ($plugin_page == 'post-fixtures') {
wp_enqueue_script('jquery');
}
if (isset($_POST['pf'])) {
if (isset($_POST['pf']['_nonce'])) {
if (wp_verify_nonce($_POST['pf']['_nonce'], 'post-fixtures')) {
$this->handle_update($_POST['pf']);
}
}
}
}
function render_admin() { ?>
<div class="wrap">
<h2><?php _e('Post Fixtures', 'post-fixtures') ?></h2>
<p>
<?php _e('<strong>Post Fixtures</strong> is a developer tool for quickly setting up the WordPress database to test certain combinations of posts, categories, and meta data.', 'post-fixtures'); ?>
<?php _e('<strong>You should not have this installed on a live site!</strong>', 'post-fixtures'); ?>
<?php _e('This tool <strong>will delete all of your posts, categories, and metadata as necessary!</strong>', 'post-fixtures'); ?>
</p>
<form action="" method="post" id="post-fixtures-form">
<input type="hidden" name="pf[_nonce]" value="<?php echo wp_create_nonce('post-fixtures') ?>" />
<h3><?php _e('JSON data to load into the database:', 'post-fixtures') ?></h3>
<textarea name="pf[data]" rows="20" style="width: 100%"></textarea>
<label style="margin: 5px 0; display: block">
<input type="checkbox" name="pf[is_ok]" value="yes" /> <?php _e('Yes, I want Post Fixtures to <strong>delete all of my data and load this data instead.</strong>', 'post-fixtures') ?><br />
</label>
<input type="submit" value="Load Provided Data" />
</form>
<script type="text/javascript">
var form = jQuery('#post-fixtures-form').get(0);
if (form) {
jQuery(form).submit(function() {
var checkbox = jQuery('input[name*=is_ok]', form).get(0);
var ok = false;
if (checkbox) {
ok = checkbox.checked;
}
if (!ok) {
alert('<?php _e('You must check the checkbox to approve this operation.', 'post-fixtures') ?>');
return false;
}
return true;
});
}
</script>
</div>
<?php }
// data handling
function parse_json($input) { function parse_json($input) {
if (($data = json_decode($input)) !== false) { if (($data = json_decode($input)) !== false) {
if (is_array($data) || is_object($data)) { if (is_array($data) || is_object($data)) {
@ -11,7 +101,7 @@ class PostFixtures {
} }
function remove_all_posts() { function remove_all_posts() {
if (is_array($posts = get_posts('nopaging=1'))) { if (is_array($posts = get_posts(array('numberposts' => '-1', 'post_status' => 'draft,pending,future,inherit,private,publish')))) {
foreach ($posts as $post) { wp_delete_post($post->ID); } foreach ($posts as $post) { wp_delete_post($post->ID); }
} }
} }
@ -26,10 +116,12 @@ class PostFixtures {
$posts = $data; $posts = $data;
$categories = array(); $categories = array();
foreach ($data as $post) { foreach ($data as $post) {
$post = (array)$post;
if (isset($post['categories'])) { if (isset($post['categories'])) {
$categories = array_merge($categories, $post['categories']); $categories = array_merge($categories, $post['categories']);
} }
} }
$categories = array_unique($categories);
return compact('posts', 'categories'); return compact('posts', 'categories');
} }
@ -37,7 +129,7 @@ class PostFixtures {
$category_ids_by_slug = array(); $category_ids_by_slug = array();
if (is_array($categories)) { if (is_array($categories)) {
foreach ($categories as $category) { foreach ($categories as $category) {
$category_ids_by_slug[$category] = wp_insert_category(array('slug' => $category)); $category_ids_by_slug[$category] = wp_insert_category(array('cat_name' => $category, 'slug' => $category));
} }
} }
return $category_ids_by_slug; return $category_ids_by_slug;
@ -47,6 +139,10 @@ class PostFixtures {
$post_ids_created = array(); $post_ids_created = array();
if (is_array($posts)) { if (is_array($posts)) {
foreach ($posts as $post) { foreach ($posts as $post) {
$post = (array)$post;
if (!isset($post['post_status'])) {
$post['post_status'] = 'publish';
}
$id = wp_insert_post($post); $id = wp_insert_post($post);
if ($id != 0) { if ($id != 0) {
$post_ids_created[] = $id; $post_ids_created[] = $id;
@ -74,8 +170,8 @@ class PostFixtures {
} }
function create($data) { function create($data) {
$this->create_categories($data['categories']); $categories_by_slug = $this->create_categories($data['categories']);
$this->create_posts($data['posts'], $data['categories']); $this->create_posts($data['posts'], $categories_by_slug);
} }
function remove() { function remove() {

View File

@ -28,6 +28,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
function __post_fixtures_plugins_loaded() { function __post_fixtures_plugins_loaded() {
if (version_compare(PHP_VERSION, '5.0.0') === 1) { if (version_compare(PHP_VERSION, '5.0.0') === 1) {
require_once('classes/PostFixtures.inc'); require_once('classes/PostFixtures.inc');
$post_fixtures = new PostFixtures();
$post_fixtures->init();
} else { } else {
add_action('admin_notices', '__post_fixtures_admin_notices'); add_action('admin_notices', '__post_fixtures_admin_notices');
} }

View File

@ -42,7 +42,7 @@ class PostFixturesTest extends PHPUnit_Framework_TestCase {
update_post_meta($i, md5(rand()), md5(rand())); update_post_meta($i, md5(rand()), md5(rand()));
} }
_set_up_get_posts_response('nopaging=1', $posts); _set_up_get_posts_response(array('numberposts' => '-1', 'post_status' => 'draft,pending,future,inherit,private,publish'), $posts);
$this->assertEquals(5, count($wp_test_expectations['posts'])); $this->assertEquals(5, count($wp_test_expectations['posts']));
$this->assertEquals(5, count($wp_test_expectations['post_meta'])); $this->assertEquals(5, count($wp_test_expectations['post_meta']));