diff --git a/classes/FixtureBuilder.inc b/classes/FixtureBuilder.inc index 1883689..c5114b1 100644 --- a/classes/FixtureBuilder.inc +++ b/classes/FixtureBuilder.inc @@ -64,6 +64,7 @@ class FixtureBuilder { $this->ensure_type($this->current_object['type']); $this->{"defer_{$this->current_object['type']}"}($this->current_object); } + unset($this->current_object); } function build() { @@ -113,6 +114,7 @@ class FixtureBuilder { $category_ids_by_slug = array_merge($category_ids_by_slug, PostFixtures::create_category($category)); } $category_ids = array_values($category_ids_by_slug); + PostFixtures::set_post_categories($post_id, $category_ids); } $metadata = array(); @@ -128,4 +130,23 @@ class FixtureBuilder { return false; // @codeCoverageIgnoreEnd } + + 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); + } + } + } } diff --git a/classes/PostFixtures.inc b/classes/PostFixtures.inc index 920661d..37c7af7 100644 --- a/classes/PostFixtures.inc +++ b/classes/PostFixtures.inc @@ -49,15 +49,40 @@ class PostFixtures { */ 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.", 'post-fixtures'); - } else { - $this->messages[] = __("Data is not valid JSON.", 'post-fixtures'); - } + if (strpos($info['data'], 'file:') === 0) { + $this->handle_load_file(substr($info['data'], 5)); + } else { + $this->handle_json($info['data']); + } + } + } + + function handle_load_file($file) { + $file = realpath($this->wpcontent_path() . '/' . $file); + if (file_exists($file)) { + switch (pathinfo($file, PATHINFO_EXTENSION)) { + case "json": + $this->handle_json(file_get_contents($file)); + break; + case "inc"; + $this->remove(); + $builder = new FixtureBuilder(); + include($file); + $builder->finalize(); + break; + } + } + } + + function handle_json($input) { + $data = $this->parse_json(stripslashes($input)); + if (!empty($data)) { + $data = $this->process_data($data); + $this->remove(); + $this->create($data); + $this->messages[] = __("New data set loaded into WordPress.", 'post-fixtures'); + } else { + $this->messages[] = __("Data is not valid JSON.", 'post-fixtures'); } } @@ -128,7 +153,7 @@ class PostFixtures { $dh = opendir($dir); $is_fixture_dir = (basename($dir) === 'fixtures'); while ($file = readdir($dh)) { - if ($file[0] != '.') { + if ($file[0] != '.') { $target = $dir . '/' . $file; if ($is_fixture_dir) { if (is_file($target)) { @@ -286,6 +311,10 @@ class PostFixtures { update_post_meta($post_id, $key, $value); } + function set_post_categories($post_id, $categories) { + wp_set_post_categories($post_id, $categories); + } + /** * Create everything from the provided data. * @param array $data The data to use in creation. @@ -339,9 +368,9 @@ class PostFixtures { if (count($parts) == 1) { $index = reset($parts); if (isset($this->{"_${source}"})) { - if (isset($this->{"_${source}"}[$index])) { - $value = $this->{"_${source}"}[$index]; - } + if (isset($this->{"_${source}"}[$index])) { + $value = $this->{"_${source}"}[$index]; + } } } } diff --git a/classes/partials/admin.inc b/classes/partials/admin.inc index c83bb80..0409d1b 100644 --- a/classes/partials/admin.inc +++ b/classes/partials/admin.inc @@ -9,8 +9,20 @@