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 --- src/Metabox.php | 53 ++++++++++++++++++++++++----------------------------- 1 file changed, 24 insertions(+), 29 deletions(-) (limited to 'src/Metabox.php') 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([ -- cgit v1.2.3