diff options
author | Daniel Weipert <code@drogueronin.de> | 2020-12-17 00:01:06 +0100 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2020-12-17 00:01:06 +0100 |
commit | 131e542d013d6c1932bbf477d6eba031cebd0276 (patch) | |
tree | 1eb1026e91453a87dd100b8bc08d55fc92375d9d /src/Metabox.php | |
parent | e3c2639e80665e623e1f1f4ce93fe80babb59f09 (diff) |
Adds function for adding arbitrary locations and rewrites posttype and taxonomy to use this function as wellv1.2.0
Diffstat (limited to 'src/Metabox.php')
-rw-r--r-- | src/Metabox.php | 53 |
1 files changed, 24 insertions, 29 deletions
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,13 +84,29 @@ 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 * * @return $this @@ -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([ |