From df5c47e2d302ab117505f0888c6ca5ff08c3c485 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Mon, 23 Nov 2009 07:23:26 -0500 Subject: [PATCH] allow for parent/child categories --- classes/PostFixtures.inc | 8 +++++++- test/PostFixturesTest.php | 15 +++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/classes/PostFixtures.inc b/classes/PostFixtures.inc index ee944f9..529be79 100644 --- a/classes/PostFixtures.inc +++ b/classes/PostFixtures.inc @@ -129,7 +129,13 @@ class PostFixtures { $category_ids_by_slug = array(); if (is_array($categories)) { foreach ($categories as $category) { - $category_ids_by_slug[$category] = wp_insert_category(array('cat_name' => $category, 'slug' => $category)); + $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, 'slug' => $node, 'category_parent' => $parent)); + } } } return $category_ids_by_slug; diff --git a/test/PostFixturesTest.php b/test/PostFixturesTest.php index 661548d..a1662f1 100644 --- a/test/PostFixturesTest.php +++ b/test/PostFixturesTest.php @@ -111,7 +111,8 @@ class PostFixturesTest extends PHPUnit_Framework_TestCase { return array( array(false, array()), array(array(), array()), - array(array('test'), array('test' => 1)) + array(array('test'), array('test' => 1)), + array(array('test/test2'), array('test' => 1, 'test/test2' => 2)), ); } @@ -164,6 +165,16 @@ class PostFixturesTest extends PHPUnit_Framework_TestCase { 'test' => 'test2' )) ), + array( + array( + array( + 'post_title' => 'test', + 'categories' => array('test', 'test2/test3') + ) + ), + array(1), + array(1 => array(1,3)) + ), ); } @@ -174,7 +185,7 @@ class PostFixturesTest extends PHPUnit_Framework_TestCase { update_option('default_category', 1); wp_insert_category(array('slug' => 'test')); - $this->assertEquals($expected_post_ids, $this->pf->create_posts($posts, array('test' => 1, 'test2' => 2))); + $this->assertEquals($expected_post_ids, $this->pf->create_posts($posts, array('test' => 1, 'test2' => 2, 'test2/test3' => 3))); if (is_array($expected_post_categories)) { foreach ($expected_post_categories as $post_id => $categories) {