2010-01-16 17:23:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class FixtureBuilder {
|
|
|
|
public $deferred_builds;
|
|
|
|
private $current_object;
|
|
|
|
|
|
|
|
function __construct() {
|
|
|
|
$this->deferred_builds = array();
|
|
|
|
$this->current_object = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ensure_type($type) {
|
|
|
|
if (!isset($this->deferred_builds[$type])) {
|
|
|
|
$this->deferred_builds[$type] = array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function check_correct_type($name) {
|
|
|
|
$ok = false;
|
|
|
|
if (!empty($this->current_object)) {
|
|
|
|
if ($name != $this->current_object['type']) {
|
|
|
|
throw new LogicException("You can't call ${name} at this point.");
|
|
|
|
}
|
|
|
|
$ok = true;
|
|
|
|
}
|
|
|
|
return $ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
function category($name) {
|
|
|
|
if ($this->check_correct_type('category')) {
|
|
|
|
$this->defer();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->current_object = array(
|
|
|
|
'type' => 'category', 'name' => $name
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
function post($title) {
|
|
|
|
if ($this->check_correct_type('post')) {
|
|
|
|
$this->defer();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->current_object = array(
|
|
|
|
'type' => 'post', 'post' => array(
|
|
|
|
'post_title' => $title,
|
|
|
|
'post_type' => 'post'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
function date($date) {
|
|
|
|
$this->check_correct_type('post');
|
|
|
|
|
|
|
|
$this->current_object['post']['post_date'] = $date;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2010-01-18 05:15:11 +00:00
|
|
|
function content($content) {
|
|
|
|
$this->check_correct_type('post');
|
|
|
|
|
|
|
|
$this->current_object['post']['post_content'] = $content;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2010-01-18 04:56:41 +00:00
|
|
|
function tags($tags) {
|
|
|
|
$this->check_correct_type('post');
|
|
|
|
|
|
|
|
$this->current_object['post']['tags'] = explode(',', $tags);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2010-01-16 17:23:15 +00:00
|
|
|
function defer() {
|
|
|
|
if (!empty($this->current_object)) {
|
|
|
|
$this->ensure_type($this->current_object['type']);
|
2010-01-18 00:41:10 +00:00
|
|
|
$this->{"defer_{$this->current_object['type']}"}($this->current_object);
|
2010-01-16 17:23:15 +00:00
|
|
|
}
|
2010-01-18 03:59:05 +00:00
|
|
|
unset($this->current_object);
|
2010-01-16 17:23:15 +00:00
|
|
|
}
|
|
|
|
|
2010-01-18 00:41:10 +00:00
|
|
|
function build() {
|
|
|
|
if (!empty($this->current_object)) {
|
|
|
|
$this->ensure_type($this->current_object['type']);
|
|
|
|
return $this->{"build_{$this->current_object['type']}"}($this->current_object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function categories($categories) {
|
|
|
|
$this->check_correct_type('post');
|
|
|
|
|
|
|
|
$this->current_object['post']['categories'] = explode(',', $categories);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
function metadata($key, $value) {
|
|
|
|
$this->check_correct_type('post');
|
|
|
|
|
|
|
|
$this->current_object['post']['metadata'][$key] = $value;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2010-01-18 05:15:11 +00:00
|
|
|
function option($name, $value) {
|
|
|
|
update_option($name, $value);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2010-01-18 00:41:10 +00:00
|
|
|
function defer_category($obj) {
|
|
|
|
$this->deferred_builds['category'] = array_merge($this->deferred_builds['category'], explode(',', $obj['name']));
|
|
|
|
}
|
|
|
|
|
|
|
|
function defer_post($obj) {
|
|
|
|
$this->deferred_builds['post'][] = $obj['post'];
|
|
|
|
}
|
|
|
|
|
2010-01-16 17:23:15 +00:00
|
|
|
function build_category($obj) {
|
2010-01-18 00:41:10 +00:00
|
|
|
return PostFixtures::create_category($obj['name']);
|
2010-01-16 17:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function build_post($obj) {
|
2010-01-18 00:41:10 +00:00
|
|
|
$post_obj = $obj['post'];
|
2010-01-18 04:56:41 +00:00
|
|
|
foreach (array('categories', 'metadata', 'tags') as $field) {
|
2010-01-18 00:41:10 +00:00
|
|
|
unset($post_obj[$field]);
|
|
|
|
}
|
|
|
|
$post_id = PostFixtures::create_post($post_obj);
|
|
|
|
if ($post_id != 0) {
|
|
|
|
$category_ids = array();
|
|
|
|
if (isset($obj['post']['categories'])) {
|
|
|
|
$category_ids_by_slug = array();
|
|
|
|
foreach ($obj['post']['categories'] as $category) {
|
|
|
|
$category_ids_by_slug = array_merge($category_ids_by_slug, PostFixtures::create_category($category));
|
|
|
|
}
|
|
|
|
$category_ids = array_values($category_ids_by_slug);
|
2010-01-18 03:59:05 +00:00
|
|
|
PostFixtures::set_post_categories($post_id, $category_ids);
|
2010-01-18 00:41:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$metadata = array();
|
|
|
|
if (isset($obj['post']['metadata'])) {
|
|
|
|
foreach ($obj['post']['metadata'] as $key => $value) {
|
|
|
|
PostFixtures::create_post_metadata($post_id, $key, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-18 04:56:41 +00:00
|
|
|
if (isset($obj['post']['tags'])) {
|
|
|
|
PostFixtures::create_post_tags($post_id, $obj['post']['tags']);
|
|
|
|
}
|
|
|
|
|
2010-01-18 00:41:10 +00:00
|
|
|
return get_post($post_id);
|
|
|
|
}
|
2010-01-18 02:28:26 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2010-01-18 00:41:10 +00:00
|
|
|
return false;
|
2010-01-18 02:28:26 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2010-01-16 17:23:15 +00:00
|
|
|
}
|
2010-01-18 03:59:05 +00:00
|
|
|
|
|
|
|
function finalize() {
|
|
|
|
$this->defer();
|
|
|
|
|
|
|
|
foreach ($this->deferred_builds as $type => $entries) {
|
|
|
|
foreach ($entries as $entry) {
|
|
|
|
$object = null;
|
|
|
|
switch ($type) {
|
|
|
|
case 'category':
|
|
|
|
$object = array('name' => $entry);
|
|
|
|
break;
|
|
|
|
case 'post':
|
|
|
|
$object = array('post' => $entry);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$this->{"build_${type}"}($object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-01-16 17:23:15 +00:00
|
|
|
}
|