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); }
}
}
function remove_all_categories() {
foreach (get_all_category_ids() as $id) {
wp_delete_category($id);
}
}
function process_data($data) {
$posts = array();
$categories = array();
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;
}
}
$categories = array_unique($categories);
return compact('posts', 'categories');
}
function create_categories($categories) {
$category_ids_by_slug = array();
if (is_array($categories)) {
foreach ($categories as $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;
}
function create_posts($posts, $categories) {
$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;
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) {
update_post_meta($id, $key, $value);
}
}
}
}
}
return $post_ids_created;
}
function create($data) {
$categories_by_slug = $this->create_categories($data['categories']);
$this->create_posts($data['posts'], $categories_by_slug);
}
function remove() {
$this->remove_all_posts();
$this->remove_all_categories();
}
}