From 2e68d3222490b5d41e905e890f327507c5125202 Mon Sep 17 00:00:00 2001 From: Brent Vanwildemeersch Date: Thu, 14 Nov 2024 22:07:20 +0100 Subject: [PATCH] feat: Add support for MetaBox relationships (#17) --------- Co-authored-by: Brent Vanwildemeersch Co-authored-by: Nicolas Lemoine --- README.md | 43 +++++++++++++++++++++++++++++++++++++++++-- examples/fixtures.yml | 2 ++ src/Entity/Post.php | 9 +++++++++ src/Entity/Term.php | 10 ++++++++++ src/Entity/User.php | 10 ++++++++++ 5 files changed, 72 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e8fe70f..9fd65ef 100644 --- a/README.md +++ b/README.md @@ -344,15 +344,54 @@ Hellonico\Fixtures\Entity\Post: # repeater field features: - label: - value: - label: value: - label: value: ``` - Be careful with duplicate field keys, if you have multiple field with the same key, prefer using ACF field key (`field_948d1qj5mn4d3`). +### MetaBox Support + +#### MetaBox Custom Fields +MetaBox fields can be adressed using the `meta` key +```yaml +Hellonico\Fixtures\Entity\Post: + post{1..30}: + post_title: + post_date: + meta: + # number field + number: + meta_box_custom_field: +``` + +#### MetaBox Relationships (https://docs.metabox.io/extensions/mb-relationships/#using-code) +When using the MB Relationships extension, the relationships can be set/defined using the key `mb_relations`. For each relationship you want to create a fixture for, you use the relationship-ID which is used to register the MB-relation +```php + MB_Relationships_API::register( [ + 'id' => 'post_to_term', + 'from' => 'post', + 'to' => [ + 'object_type' => 'term', + 'taxonomy'=> 'custom_term' + ], + + ] ); +``` +and the post/term ID of the object you want it to have a relationship with. +```yaml +Hellonico\Fixtures\Entity\Post: + post{1..30}: + post_title: + post_date: + mb_relations: + post_to_term: '1x @custom_term*->term_id' + post_to_post: '1x @custom_post*->ID' +``` + + ### Custom formatters In addition to formatters provided by [fzaninotto/Faker](https://github.com/fzaninotto/Faker#formatters), you can use custom formatters below. diff --git a/examples/fixtures.yml b/examples/fixtures.yml index 0525356..0082dcc 100644 --- a/examples/fixtures.yml +++ b/examples/fixtures.yml @@ -97,6 +97,8 @@ Hellonico\Fixtures\Entity\Post: # CUSTOM POST TYPE product{1..15}: post_type: product + mb_relations: + post_to_term: '1x @places*->term_id' acf: # number field price: diff --git a/src/Entity/Post.php b/src/Entity/Post.php index e6e3a3b..0a7b9d2 100644 --- a/src/Entity/Post.php +++ b/src/Entity/Post.php @@ -30,6 +30,7 @@ class Post extends Entity { public $post_category; public $meta_input; public $acf; + public $mb_relations; private $extra = [ 'meta_input', 'acf' ]; /** @@ -126,6 +127,14 @@ public function persist() { } } + if ( defined( 'RWMB_VER' ) && class_exists( 'MB_Relationships_API' ) && ! empty( $this->mb_relations ) && is_array( $this->mb_relations ) ) { + foreach ( $this->mb_relations as $mb_rel_id => $object_ids ) { + foreach ( $object_ids as $id ) { + \MB_Relationships_API::add( $post_id, $id, $mb_rel_id ); + } + } + } + return true; } diff --git a/src/Entity/Term.php b/src/Entity/Term.php index ef1ecbb..08d0a8f 100644 --- a/src/Entity/Term.php +++ b/src/Entity/Term.php @@ -14,6 +14,7 @@ class Term extends Entity { public $description; public $parent; public $acf; + public $mb_relations; /** * Constructor. @@ -98,6 +99,15 @@ public function persist() { } } + // Save MetaBox Relationships + if ( defined( 'RWMB_VER' ) && class_exists( 'MB_Relationships_API' ) && ! empty( $this->mb_relations ) && is_array( $this->mb_relations ) ) { + foreach ( $this->mb_relations as $mb_rel_id => $object_ids ) { + foreach ( $object_ids as $id ) { + \MB_Relationships_API::add( $post_id, $id, $mb_rel_id ); + } + } + } + return true; } diff --git a/src/Entity/User.php b/src/Entity/User.php index d91c75e..140caae 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -29,6 +29,7 @@ class User extends Entity { public $use_ssl; public $show_admin_bar_front; public $acf; + public $mb_relations; /** * {@inheritdoc} @@ -88,6 +89,15 @@ public function persist() { } } + // Save MetaBox Relationships + if ( defined( 'RWMB_VER' ) && class_exists( 'MB_Relationships_API' ) && ! empty( $this->mb_relations ) && is_array( $this->mb_relations ) ) { + foreach ( $this->mb_relations as $mb_rel_id => $object_ids ) { + foreach ( $object_ids as $id ) { + \MB_Relationships_API::add( $post_id, $id, $mb_rel_id ); + } + } + } + return true; }