From 131e542d013d6c1932bbf477d6eba031cebd0276 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Thu, 17 Dec 2020 00:01:06 +0100 Subject: Adds function for adding arbitrary locations and rewrites posttype and taxonomy to use this function as well --- composer.json | 2 +- composer.lock | 51 ++----------------------------------------------- src/Metabox.php | 53 +++++++++++++++++++++++---------------------------- tests/MetaboxTest.php | 44 +++++++++++++++++++++++++----------------- 4 files changed, 53 insertions(+), 97 deletions(-) diff --git a/composer.json b/composer.json index e5c651b..aa1ab4c 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "drogueronin/metaboxes", - "version": "1.1.0", + "version": "1.2.0", "type": "library", "license": "MIT", "authors": [ diff --git a/composer.lock b/composer.lock index 7eb67b4..c373d57 100644 --- a/composer.lock +++ b/composer.lock @@ -4,55 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "06a0492edddd04e15173a7208cec1c1c", - "packages": [ - { - "name": "jjgrainger/posttypes", - "version": "2.1", - "source": { - "type": "git", - "url": "https://github.com/jjgrainger/PostTypes.git", - "reference": "a87584606e9ef726f7621c1d71a184fdf7282172" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/jjgrainger/PostTypes/zipball/a87584606e9ef726f7621c1d71a184fdf7282172", - "reference": "a87584606e9ef726f7621c1d71a184fdf7282172", - "shasum": "" - }, - "require": { - "php": ">=7.2" - }, - "require-dev": { - "phpunit/phpunit": "8.*", - "squizlabs/php_codesniffer": "3.*" - }, - "type": "library", - "autoload": { - "psr-4": { - "PostTypes\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Joe Grainger", - "email": "hello@jjgrainger.co.uk", - "homepage": "https://jjgrainger.co.uk" - } - ], - "description": "Simple WordPress custom post types.", - "homepage": "https://github.com/jjgrainger/posttype", - "support": { - "issues": "https://github.com/jjgrainger/PostTypes/issues", - "source": "https://github.com/jjgrainger/PostTypes/tree/v2.1" - }, - "time": "2020-04-12T13:01:12+00:00" - } - ], + "content-hash": "c5fb66ad293216cf0d8d4e14b4679cd1", + "packages": [], "packages-dev": [ { "name": "doctrine/instantiator", diff --git a/src/Metabox.php b/src/Metabox.php index b04ef00..4da3c4c 100644 --- a/src/Metabox.php +++ b/src/Metabox.php @@ -22,14 +22,9 @@ class Metabox public string $title; /** - * The PostType/s to add the Metabox to + * ACF location rule groups */ - public array $posttypes = []; - - /** - * The Taxonomies/s to add the Metabox to - */ - public array $taxonomies = []; + public array $locations = []; /** * The Box's fields to add @@ -71,7 +66,7 @@ class Metabox $posttypes = (array)$posttypes; foreach ($posttypes as $posttype) { - $this->posttypes[$posttype] = $posttype; + $this->location('post_type', $posttype); } return $this; @@ -89,12 +84,28 @@ class Metabox $taxonomies = (array)$taxonomies; foreach ($taxonomies as $taxonomy) { - $this->taxonomies[$taxonomy] = $taxonomy; + $this->location('taxonomy', $taxonomy); } return $this; } + /** + * Add a rule group to the acf group location + * + * @param string $param The type to check against + * @param string $value The value to check against + * @param string $operator The operator to check with + * + * @return $this + */ + public function location(string $param, string $value, string $operator = '==') + { + $this->locations[] = compact('param', 'operator', 'value'); + + return $this; + } + /** * @param MetaboxField|array $fields * @@ -133,26 +144,10 @@ class Metabox */ public function add() { - // build the post type rule groups - $postTypeRuleGroups = array_map(function ($posttype) { - return [[ - 'param' => 'post_type', - 'operator' => '==', - 'value' => $posttype, - ]]; - }, $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); + // build the location rule groups + $ruleGroups = array_map(function ($location) { + return [$location]; + }, $this->locations); // merge with extra options $fieldGroup = array_merge_recursive([ diff --git a/tests/MetaboxTest.php b/tests/MetaboxTest.php index e26c9f2..319e4c0 100644 --- a/tests/MetaboxTest.php +++ b/tests/MetaboxTest.php @@ -36,39 +36,47 @@ class MetaboxTest extends TestCase /** * @test */ - public function assignsPostTypeOnce() + public function assignsPostType() { $this->metabox->posttype(['post', 'page']); - $expectedPostTypes = [ - 'post' => 'post', - 'page' => 'page', + $expectedLocations = [ + [ + 'param' => 'post_type', + 'operator' => '==', + 'value' => 'post' + ], + [ + 'param' => 'post_type', + 'operator' => '==', + 'value' => 'page' + ], ]; - $this->assertEquals($expectedPostTypes, $this->metabox->posttypes); - - $this->metabox->posttype('post'); - - $this->assertEquals($expectedPostTypes, $this->metabox->posttypes); + $this->assertEquals($expectedLocations, $this->metabox->locations); } /** * @test */ - public function assignsTaxonomyOnce() + public function assignsTaxonomy() { $this->metabox->taxonomy(['category', 'post_tag']); - $expectedTaxonomies = [ - 'category' => 'category', - 'post_tag' => 'post_tag', + $expectedLocations = [ + [ + 'param' => 'taxonomy', + 'operator' => '==', + 'value' => 'category' + ], + [ + 'param' => 'taxonomy', + 'operator' => '==', + 'value' => 'post_tag' + ], ]; - $this->assertEquals($expectedTaxonomies, $this->metabox->taxonomies); - - $this->metabox->taxonomy('category'); - - $this->assertEquals($expectedTaxonomies, $this->metabox->taxonomies); + $this->assertEquals($expectedLocations, $this->metabox->locations); } /** -- cgit v1.2.3