From 5b9a966c0e331152df67e4a73e276d60d135fe1f Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sun, 22 Nov 2009 19:57:35 -0500 Subject: [PATCH] ok it's working --- classes/PostFixtures.inc | 104 ++++++++++++++++++++++++++++++++++++-- post-fixtures.php | 2 + test/PostFixturesTest.php | 2 +- 3 files changed, 103 insertions(+), 5 deletions(-) diff --git a/classes/PostFixtures.inc b/classes/PostFixtures.inc index 365f19b..ee944f9 100644 --- a/classes/PostFixtures.inc +++ b/classes/PostFixtures.inc @@ -1,6 +1,96 @@ messages)) { ?> +
+ messages as $message) { ?> +

+ +
+ 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() { ?> +
+

+ +

+ Post Fixtures is a developer tool for quickly setting up the WordPress database to test certain combinations of posts, categories, and meta data.', 'post-fixtures'); ?> + You should not have this installed on a live site!', 'post-fixtures'); ?> + will delete all of your posts, categories, and metadata as necessary!', 'post-fixtures'); ?> +

+ +
+ +

+ + + +
+ +
+ '-1', 'post_status' => 'draft,pending,future,inherit,private,publish')))) { foreach ($posts as $post) { wp_delete_post($post->ID); } } } @@ -26,10 +116,12 @@ class PostFixtures { $posts = $data; $categories = array(); foreach ($data as $post) { + $post = (array)$post; if (isset($post['categories'])) { $categories = array_merge($categories, $post['categories']); } } + $categories = array_unique($categories); return compact('posts', 'categories'); } @@ -37,7 +129,7 @@ class PostFixtures { $category_ids_by_slug = array(); if (is_array($categories)) { 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; @@ -47,6 +139,10 @@ class PostFixtures { $post_ids_created = array(); if (is_array($posts)) { foreach ($posts as $post) { + $post = (array)$post; + if (!isset($post['post_status'])) { + $post['post_status'] = 'publish'; + } $id = wp_insert_post($post); if ($id != 0) { $post_ids_created[] = $id; @@ -74,8 +170,8 @@ class PostFixtures { } function create($data) { - $this->create_categories($data['categories']); - $this->create_posts($data['posts'], $data['categories']); + $categories_by_slug = $this->create_categories($data['categories']); + $this->create_posts($data['posts'], $categories_by_slug); } function remove() { diff --git a/post-fixtures.php b/post-fixtures.php index e3a833c..192aa74 100644 --- a/post-fixtures.php +++ b/post-fixtures.php @@ -28,6 +28,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA function __post_fixtures_plugins_loaded() { if (version_compare(PHP_VERSION, '5.0.0') === 1) { require_once('classes/PostFixtures.inc'); + $post_fixtures = new PostFixtures(); + $post_fixtures->init(); } else { add_action('admin_notices', '__post_fixtures_admin_notices'); } diff --git a/test/PostFixturesTest.php b/test/PostFixturesTest.php index b3eeb39..661548d 100644 --- a/test/PostFixturesTest.php +++ b/test/PostFixturesTest.php @@ -42,7 +42,7 @@ class PostFixturesTest extends PHPUnit_Framework_TestCase { 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['post_meta']));