diff options
-rw-r--r-- | composer.json | 2 | ||||
-rw-r--r-- | src/Metabox.php | 39 | ||||
-rw-r--r-- | tests/MetaboxTest.php | 19 |
3 files changed, 57 insertions, 3 deletions
diff --git a/composer.json b/composer.json index c23a035..e5c651b 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "drogueronin/metaboxes", - "version": "1.0.2", + "version": "1.1.0", "type": "library", "license": "MIT", "authors": [ diff --git a/src/Metabox.php b/src/Metabox.php index b2ae2e4..b04ef00 100644 --- a/src/Metabox.php +++ b/src/Metabox.php @@ -27,6 +27,11 @@ class Metabox public array $posttypes = []; /** + * The Taxonomies/s to add the Metabox to + */ + public array $taxonomies = []; + + /** * The Box's fields to add * * @var MetaboxField[] @@ -73,6 +78,24 @@ class Metabox } /** + * Assign a Taxonomy to add the Metabox to + * + * @param string|array $taxonomies + * + * @return $this + */ + public function taxonomy($taxonomies) + { + $taxonomies = (array)$taxonomies; + + foreach ($taxonomies as $taxonomy) { + $this->taxonomies[$taxonomy] = $taxonomy; + } + + return $this; + } + + /** * @param MetaboxField|array $fields * * @return $this @@ -110,7 +133,7 @@ class Metabox */ public function add() { - // build the rule groups for the location + // build the post type rule groups $postTypeRuleGroups = array_map(function ($posttype) { return [[ 'param' => 'post_type', @@ -119,11 +142,23 @@ class Metabox ]]; }, $this->posttypes); + // build the taxonomy rule groups + $taxonomyRuleGroups = array_map(function ($taxonomy) { + return [[ + 'param' => 'taxonomy', + 'operator' => '==', + 'value' => $taxonomy, + ]]; + }, $this->taxonomies); + + // merge the rules groups for the location + $ruleGroups = array_merge($postTypeRuleGroups, $taxonomyRuleGroups); + // merge with extra options $fieldGroup = array_merge_recursive([ 'key' => $this->key, 'title' => $this->title, - 'location' => $postTypeRuleGroups, + 'location' => $ruleGroups, ], $this->options); // add field group diff --git a/tests/MetaboxTest.php b/tests/MetaboxTest.php index 1a1353e..e26c9f2 100644 --- a/tests/MetaboxTest.php +++ b/tests/MetaboxTest.php @@ -55,6 +55,25 @@ class MetaboxTest extends TestCase /** * @test */ + public function assignsTaxonomyOnce() + { + $this->metabox->taxonomy(['category', 'post_tag']); + + $expectedTaxonomies = [ + 'category' => 'category', + 'post_tag' => 'post_tag', + ]; + + $this->assertEquals($expectedTaxonomies, $this->metabox->taxonomies); + + $this->metabox->taxonomy('category'); + + $this->assertEquals($expectedTaxonomies, $this->metabox->taxonomies); + } + + /** + * @test + */ public function modifiesAndHoldsPassedField() { $this->metabox->field($this->field); |