From bee78768e09f94267ba07f0ea6924317e8008c97 Mon Sep 17 00:00:00 2001 From: Craig Morris Date: Tue, 17 Oct 2023 05:54:22 +0000 Subject: [PATCH 1/6] Allow specifying of rules manually When manually specifying keys because of migrating existing db / json fields, the ConditionalLogic class can't / won't use the manually specified key. This change allows the rules to be specified manually and bypass the ConditionalLogic class. Example: ```php TrueFalse::make('Display Webinar Chapters?') ->key('field_64c842b5238bc'), Text::make('Webinar Chapters') ->key('field_64c8422d238b7') ->conditionalLogic([ ['field' => 'field_64c842b5238bc', 'operator' => '==', 'value' => 1] ]) ``` --- src/Fields/Field.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Fields/Field.php b/src/Fields/Field.php index c4d676c4..f007be46 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -13,6 +13,7 @@ namespace Extended\ACF\Fields; +use Extended\ACF\ConditionalLogic; use Extended\ACF\Key; use InvalidArgumentException; @@ -110,7 +111,7 @@ public function get(string|null $parentKey = null): array if (isset($this->settings['conditional_logic'])) { $this->settings['conditional_logic'] = array_map( - fn ($rules) => $rules->get($parentKey), + fn ($rules) => $rules instanceof ConditionalLogic ? $rules->get($parentKey) : $rules, $this->settings['conditional_logic'] ); } From a252ef42f220f8197f4fca2953556fdee9773de9 Mon Sep 17 00:00:00 2001 From: Craig Morris Date: Tue, 17 Oct 2023 20:20:37 +1100 Subject: [PATCH 2/6] add test --- tests/ConditionalLogicTest.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/ConditionalLogicTest.php b/tests/ConditionalLogicTest.php index 656eca6f..af9eb7cc 100644 --- a/tests/ConditionalLogicTest.php +++ b/tests/ConditionalLogicTest.php @@ -64,9 +64,39 @@ public function testResolvedParentKey() 'location' => [] ]); + dump($settings); + $this->assertSame( $settings['fields'][0]['key'], $settings['fields'][1]['sub_fields'][0]['conditional_logic'][0][0]['field'] ); } + + public function testManualRules() + { + $settings = register_extended_field_group([ + 'title' => 'Manual Key', + 'fields' => [ + Select::make('Select') + ->key('field_manual') + ->choices([ + 'red' => 'Red', + ]) + ->defaultValue('red'), + Repeater::make('Repeater') + ->fields([ + Text::make('Red') + ->conditionalLogic([ + ['field' => 'field_manual', 'operator' => '==', 'value' => 'red'], + ]), + ]) + ], + 'location' => [] + ]); + + $this->assertSame( + $settings['fields'][0]['key'], + $settings['fields'][1]['sub_fields'][0]['conditional_logic'][0]['field'] + ); + } } From 19157c102bbb1af5221829265c7d3a323c7ea89c Mon Sep 17 00:00:00 2001 From: Craig Morris Date: Tue, 17 Oct 2023 20:21:24 +1100 Subject: [PATCH 3/6] rm dump --- tests/ConditionalLogicTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/ConditionalLogicTest.php b/tests/ConditionalLogicTest.php index af9eb7cc..eba295aa 100644 --- a/tests/ConditionalLogicTest.php +++ b/tests/ConditionalLogicTest.php @@ -64,8 +64,6 @@ public function testResolvedParentKey() 'location' => [] ]); - dump($settings); - $this->assertSame( $settings['fields'][0]['key'], $settings['fields'][1]['sub_fields'][0]['conditional_logic'][0][0]['field'] From 2b2eaaebf85e881e5f1cd21df7556d2d49c183e6 Mon Sep 17 00:00:00 2001 From: Craig Morris Date: Tue, 17 Oct 2023 20:22:09 +1100 Subject: [PATCH 4/6] add comment --- .php-cs-fixer.cache | 1 + src/Fields/Field.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 .php-cs-fixer.cache diff --git a/.php-cs-fixer.cache b/.php-cs-fixer.cache new file mode 100644 index 00000000..3433cec7 --- /dev/null +++ b/.php-cs-fixer.cache @@ -0,0 +1 @@ +{"php":"8.2.11","version":"3.35.1:v3.35.1#ec1ccc264994b6764882669973ca435cf05bab08","indent":" ","lineEnding":"\n","rules":{"concat_space":{"spacing":"one"},"function_declaration":{"closure_fn_spacing":"none"},"method_argument_space":true,"single_line_empty_body":true,"binary_operator_spaces":{"default":"at_least_single_space"},"blank_line_after_opening_tag":true,"blank_line_between_import_groups":true,"blank_lines_before_namespace":true,"braces_position":{"allow_single_line_empty_anonymous_classes":true},"class_definition":{"inline_constructor_arguments":false,"space_before_parenthesis":true},"compact_nullable_type_declaration":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_parentheses":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"imports_order":["class","function","const"],"sort_algorithm":"none"},"return_type_declaration":true,"short_scalar_cast":true,"single_import_per_statement":{"group_to_single_imports":false},"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"visibility_required":true,"blank_line_after_namespace":true,"constant_case":true,"control_structure_braces":true,"control_structure_continuation_position":true,"elseif":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"no_break_comment":true,"no_closing_tag":true,"no_multiple_statements_per_line":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_line_after_imports":true,"spaces_inside_parentheses":true,"statement_indentation":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true},"hashes":{"tests\/KeyTest.php":"22eb18bebbe0381d5816c3a39e40088b","tests\/LocationTest.php":"258d0a6dc3d6ae18d02a5be32204f292","tests\/HelpersTest.php":"e12536166910421f41f9d6a640d5904f","tests\/ConditionalLogicTest.php":"e966c04e591699d434757e83598ca420","tests\/Fields\/UserTest.php":"2a7183dc18da3bc8b50abb11926ae675","tests\/Fields\/LinkTest.php":"cdb79c7c0d464d06639952e58c75bb55","tests\/Fields\/TimePickerTest.php":"a582805ccdc15ae062e8d1b4f7609f1e","tests\/Fields\/SelectTest.php":"0176791d159abe09f466b23bb6db0201","tests\/Fields\/RelationshipTest.php":"74569da5eb2e02f83fc2fef963308a59","tests\/Fields\/RangeTest.php":"fda88a57b38459defb817d404c684b60","tests\/Fields\/AccordionTest.php":"2cd90890de995599345b1c21ff31e6e3","tests\/Fields\/FileTest.php":"1d5d85df44543cfef46b2530f3c239bf","tests\/Fields\/TabTest.php":"613b3d5af36ebbd3178ae8b97af20c24","tests\/Fields\/TextTest.php":"90f9621d2a276beca500bc15d9184880","tests\/Fields\/MessageTest.php":"2e49ba4855d039f3b6bba50752fc2487","tests\/Fields\/GroupTest.php":"f06385cec06774e7e3133ccdbac00e2d","tests\/Fields\/RadioButtonTest.php":"5beb9ed72470b2c85eb4e4cf0efa3cf8","tests\/Fields\/CheckboxTest.php":"a42901025d2968d125a36292790abb8b","tests\/Fields\/EmailTest.php":"9eac2c6c2351b57a9c2af2e3eafdd89e","tests\/Fields\/RepeaterTest.php":"7c4ed67dd482f4cb732e09a1aa02f214","tests\/Fields\/TrueFalseTest.php":"d2ec0c973ceb77980a4cd6c5a66d2e15","tests\/Fields\/GoogleMapTest.php":"1465daa1e21a08546dae10f8906bc619","tests\/Fields\/ImageTest.php":"17dd26b5c727d2282ce968decd77a6e5","tests\/Fields\/NumberTest.php":"396c04ca160e81e4bde2fa0fb5b049a2","tests\/Fields\/LayoutTest.php":"adf85c31ecc93e03b68c64c189026c5d","tests\/Fields\/DatePickerTest.php":"b117827b729f6c033e992fbb6fa2c224","tests\/Fields\/PageLinkTest.php":"dcf8f6099046242b59cdb53c3d081974","tests\/Fields\/ButtonGroupTest.php":"fd35e01d50a71c88d68f9400ae33bd92","tests\/Fields\/OembedTest.php":"fd2a277524edff66d0e42041a5bd1d77","tests\/Fields\/TextareaTest.php":"ddae64143b1af19bf235cce0ca872a2f","tests\/Fields\/WysiwygEditorTest.php":"75420c4ec7ec279027af0a146e812532","tests\/Fields\/FlexibleContentTest.php":"796a5dffee0f731b93a44fe3cac66250","tests\/Fields\/DateTimePickerTest.php":"b1ef6a7a9b6921fbf950380dce228d2b","tests\/Fields\/PostObjectTest.php":"b7864e3f163ffb873a8d49a3ea36597e","tests\/Fields\/GalleryTest.php":"a567ea022902fc82451610271dc0190c","tests\/Fields\/UrlTest.php":"ffa8892be91fd115161cc361326e8fb3","tests\/Fields\/ColorPickerTest.php":"cc457c9c683a85155ea321d5f7a881ac","tests\/Fields\/PasswordTest.php":"cb5744fe6e4d589d141d9e480f6a74da","tests\/Fields\/TaxonomyTest.php":"a6b66c938bb2a35d367322bccee50f66","tests\/helpers.php":"36f2e4bc21c1e53b840c4ee5bf03ffe2","examples\/options-page.php":"72efef2aec7008d1898d143dac9876d5","examples\/with-extended-cpts.php":"2bf9c272575aed992303fc47d1509eb2","examples\/custom-post-type.php":"15267b63e662f40041c949e47ba96207","examples\/gutenberg-block.php":"199262462520ddc74dce1bba0fac13d3","src\/Key.php":"6003e26e9ed7793e6e441e7f43c257a9","src\/ConditionalLogic.php":"b87a8966faa7a835cef5903cc5ca24d6","src\/Location.php":"b9a7342375aab8bb5e947589313179c1","src\/Fields\/Email.php":"a85be371325c823a4b3bc5ec16b63c8a","src\/Fields\/WysiwygEditor.php":"4e2a2d89185ac1279c26ab96d0e7ab45","src\/Fields\/Taxonomy.php":"662ffc2f7256a597c1d7a0c5e50e4693","src\/Fields\/Settings\/ReturnFormat.php":"9d44a3d101a489496d87123b55dfe88b","src\/Fields\/Settings\/DateTimeFormat.php":"441f5db12422ffc385ffcc7f15693471","src\/Fields\/Settings\/MimeTypes.php":"8e8f4aff03c9f43dd14d47f1e7454294","src\/Fields\/Settings\/PreviewSize.php":"fbd2efd28534d30ce87adee9049c01e1","src\/Fields\/Settings\/Instructions.php":"8ca33050ad7823d21e9d1d2873926e66","src\/Fields\/Settings\/Nullable.php":"7b2cfc1c5f4079dc296c6169c67e3457","src\/Fields\/Settings\/DirectionLayout.php":"ea05ff7b203d980830a161f197716458","src\/Fields\/Settings\/ButtonLabel.php":"a81bdf1a8b7ba258750f5f202a8118ed","src\/Fields\/Settings\/Dimensions.php":"bf9cc88114af3a3ddcb4e338308dbeb2","src\/Fields\/Settings\/Step.php":"1aca5145784b043478459c915d63d498","src\/Fields\/Settings\/Writable.php":"24cca1a8a97d314d574137eba5cb1731","src\/Fields\/Settings\/Library.php":"741457d67736c6206d3068672b4c58ed","src\/Fields\/Settings\/NewLines.php":"1d24a1a8f35b02f3aa4d720b7d9b315a","src\/Fields\/Settings\/Layout.php":"5832c03c299a0d0208ffc3ecca8c2592","src\/Fields\/Settings\/Disabled.php":"e69f197e22f36db823bcf824a348a67d","src\/Fields\/Settings\/Placeholder.php":"cd07492242693e2109a63f777e5a8ca4","src\/Fields\/Settings\/ConditionalLogic.php":"dcb02a91a8086ec4881853d6537fa85b","src\/Fields\/Settings\/Multiple.php":"819ce4f9c1e2d093dc2872432c1ab3f7","src\/Fields\/Settings\/CharacterLimit.php":"3bf3e4602b56fc54aab9111b148b0a90","src\/Fields\/Settings\/Choices.php":"a8db2da2ffb0734e525c7ee2a29477bd","src\/Fields\/Settings\/Height.php":"7d2b12524d0de3c37a412a9b9f04bb4a","src\/Fields\/Settings\/Endpoint.php":"a0a23dc5cfb184fe09a77848029c6d3f","src\/Fields\/Settings\/Wrapper.php":"6a52971a5da9f06929e4ac297a6e42e6","src\/Fields\/Settings\/WeekDay.php":"ab43d26c460d8dc4cd6c3be82b5e845e","src\/Fields\/Settings\/SubFields.php":"c3d7f2585e29effa21d74f92a5a73152","src\/Fields\/Settings\/Required.php":"4bc0161cfe1acca6646225f4eb811b6e","src\/Fields\/Settings\/MinMax.php":"3f07cccf6ec07ef6265e47f21799691d","src\/Fields\/Settings\/FilterBy.php":"bdf405686e43019529f39d14bde6c48d","src\/Fields\/Settings\/DefaultValue.php":"3a14b8e16d19df2c74e41abf682d4f45","src\/Fields\/Settings\/Message.php":"f2ff1bef4fcdd7835a75adb28fa8cc61","src\/Fields\/Settings\/FileSize.php":"ba58868e2129a04518a7985d432896ad","src\/Fields\/Settings\/Pending.php":"4076d2a167c61e629e3a2b6d8a724b3e","src\/Fields\/DatePicker.php":"a8500533d6a96f483d8670ad4943c30a","src\/Fields\/Gallery.php":"c32b7241f9d8ae8ac52e6be4427a795b","src\/Fields\/ButtonGroup.php":"583ef70d1133cfa416730e4e3bd3f6de","src\/Fields\/Number.php":"f5837e4c99e9321b2444c66a1af326ca","src\/Fields\/Group.php":"8d8ddf38a2fec442d129506ffa0b8d0b","src\/Fields\/PageLink.php":"b976ee2e046edfcdaee9068cf03dba03","src\/Fields\/Link.php":"733673a7ddf3874b35380853e34c8bdf","src\/Fields\/Accordion.php":"1d3df25fb48f4a54553816d1609f1e5b","src\/Fields\/DateTimePicker.php":"00e236d672b9b8a95a03dca8d7f62fd7","src\/Fields\/File.php":"9517c64278c03018c7a62bd758d9cc06","src\/Fields\/PostObject.php":"9c7f44041cb6959acdf9bd3622c851d9","src\/Fields\/TimePicker.php":"f169dfd4c775a1f40a6a2af9abef17ba","src\/Fields\/User.php":"611ea2dbf2ca553e8a391981c6e06936","src\/Fields\/Tab.php":"38589580a1ea9a9af9f24f7a7ef28226","src\/Fields\/TrueFalse.php":"d4019718acc1243423b886be59f76705","src\/Fields\/Repeater.php":"5d9a86fe99df44d39ce2ec2373f5c7aa","src\/Fields\/Field.php":"63e5de7601eeba0eb19ab8de88c6f424","src\/Fields\/FlexibleContent.php":"33e373749f1d0f543af2c96e0377b604","src\/Fields\/GoogleMap.php":"4d7327e69d94e72f8a2b69ac69696103","src\/Fields\/Url.php":"8f9d6260a025aabdb1ef259b29eee6bd","src\/Fields\/Layout.php":"cbbaf812ff6318fe1cc13fb785711e63","src\/Fields\/Password.php":"f0e7e9cddcd2db5550d4c38c4159aaf0","src\/Fields\/Relationship.php":"59c5b2537dd76353b372242739372bbd","src\/Fields\/RadioButton.php":"24b14dbb403892ee24d3fa4127eeb927","src\/Fields\/ColorPicker.php":"79b9f520eb25c07826490fb0dc65be05","src\/Fields\/Textarea.php":"a4f8bfccfdc69d4a04f4c7de565265c6","src\/Fields\/Select.php":"2b5e1bde291c773706c748e7cff3d5e7","src\/Fields\/Checkbox.php":"16ef825ef5a2f2f04d92f21185b234e4","src\/Fields\/Range.php":"024f37de35db791c924c334b051ec102","src\/Fields\/Oembed.php":"b981f419e6a6b56c76be3594d4a986b7","src\/Fields\/Message.php":"b80cd3fc7f8504111a409faadb8fded2","src\/Fields\/Text.php":"8a1c9d6b8f532da997d0569e34805899","src\/Fields\/Image.php":"79ae919bbd8f5eb3c9bca21593532a72","src\/helpers.php":"98d2633c456d78b6a73738ad88f4005a"}} \ No newline at end of file diff --git a/src/Fields/Field.php b/src/Fields/Field.php index f007be46..e95a5e44 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -111,7 +111,7 @@ public function get(string|null $parentKey = null): array if (isset($this->settings['conditional_logic'])) { $this->settings['conditional_logic'] = array_map( - fn ($rules) => $rules instanceof ConditionalLogic ? $rules->get($parentKey) : $rules, + fn ($rules) => $rules instanceof ConditionalLogic ? $rules->get($parentKey) : $rules, // allow specifying of rules manually with an array $this->settings['conditional_logic'] ); } From 784ada0445d67c1615f5f37a73406588e1761aa3 Mon Sep 17 00:00:00 2001 From: Vincent Klaiber Date: Tue, 17 Oct 2023 13:20:48 +0200 Subject: [PATCH 5/6] Added custom key support --- .php-cs-fixer.cache | 1 - src/ConditionalLogic.php | 31 +++++++++++++++++-------------- src/Fields/Field.php | 3 +-- tests/ConditionalLogicTest.php | 31 +++++++++++++++---------------- 4 files changed, 33 insertions(+), 33 deletions(-) delete mode 100644 .php-cs-fixer.cache diff --git a/.php-cs-fixer.cache b/.php-cs-fixer.cache deleted file mode 100644 index 3433cec7..00000000 --- a/.php-cs-fixer.cache +++ /dev/null @@ -1 +0,0 @@ -{"php":"8.2.11","version":"3.35.1:v3.35.1#ec1ccc264994b6764882669973ca435cf05bab08","indent":" ","lineEnding":"\n","rules":{"concat_space":{"spacing":"one"},"function_declaration":{"closure_fn_spacing":"none"},"method_argument_space":true,"single_line_empty_body":true,"binary_operator_spaces":{"default":"at_least_single_space"},"blank_line_after_opening_tag":true,"blank_line_between_import_groups":true,"blank_lines_before_namespace":true,"braces_position":{"allow_single_line_empty_anonymous_classes":true},"class_definition":{"inline_constructor_arguments":false,"space_before_parenthesis":true},"compact_nullable_type_declaration":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_parentheses":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"imports_order":["class","function","const"],"sort_algorithm":"none"},"return_type_declaration":true,"short_scalar_cast":true,"single_import_per_statement":{"group_to_single_imports":false},"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"visibility_required":true,"blank_line_after_namespace":true,"constant_case":true,"control_structure_braces":true,"control_structure_continuation_position":true,"elseif":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"no_break_comment":true,"no_closing_tag":true,"no_multiple_statements_per_line":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_line_after_imports":true,"spaces_inside_parentheses":true,"statement_indentation":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true},"hashes":{"tests\/KeyTest.php":"22eb18bebbe0381d5816c3a39e40088b","tests\/LocationTest.php":"258d0a6dc3d6ae18d02a5be32204f292","tests\/HelpersTest.php":"e12536166910421f41f9d6a640d5904f","tests\/ConditionalLogicTest.php":"e966c04e591699d434757e83598ca420","tests\/Fields\/UserTest.php":"2a7183dc18da3bc8b50abb11926ae675","tests\/Fields\/LinkTest.php":"cdb79c7c0d464d06639952e58c75bb55","tests\/Fields\/TimePickerTest.php":"a582805ccdc15ae062e8d1b4f7609f1e","tests\/Fields\/SelectTest.php":"0176791d159abe09f466b23bb6db0201","tests\/Fields\/RelationshipTest.php":"74569da5eb2e02f83fc2fef963308a59","tests\/Fields\/RangeTest.php":"fda88a57b38459defb817d404c684b60","tests\/Fields\/AccordionTest.php":"2cd90890de995599345b1c21ff31e6e3","tests\/Fields\/FileTest.php":"1d5d85df44543cfef46b2530f3c239bf","tests\/Fields\/TabTest.php":"613b3d5af36ebbd3178ae8b97af20c24","tests\/Fields\/TextTest.php":"90f9621d2a276beca500bc15d9184880","tests\/Fields\/MessageTest.php":"2e49ba4855d039f3b6bba50752fc2487","tests\/Fields\/GroupTest.php":"f06385cec06774e7e3133ccdbac00e2d","tests\/Fields\/RadioButtonTest.php":"5beb9ed72470b2c85eb4e4cf0efa3cf8","tests\/Fields\/CheckboxTest.php":"a42901025d2968d125a36292790abb8b","tests\/Fields\/EmailTest.php":"9eac2c6c2351b57a9c2af2e3eafdd89e","tests\/Fields\/RepeaterTest.php":"7c4ed67dd482f4cb732e09a1aa02f214","tests\/Fields\/TrueFalseTest.php":"d2ec0c973ceb77980a4cd6c5a66d2e15","tests\/Fields\/GoogleMapTest.php":"1465daa1e21a08546dae10f8906bc619","tests\/Fields\/ImageTest.php":"17dd26b5c727d2282ce968decd77a6e5","tests\/Fields\/NumberTest.php":"396c04ca160e81e4bde2fa0fb5b049a2","tests\/Fields\/LayoutTest.php":"adf85c31ecc93e03b68c64c189026c5d","tests\/Fields\/DatePickerTest.php":"b117827b729f6c033e992fbb6fa2c224","tests\/Fields\/PageLinkTest.php":"dcf8f6099046242b59cdb53c3d081974","tests\/Fields\/ButtonGroupTest.php":"fd35e01d50a71c88d68f9400ae33bd92","tests\/Fields\/OembedTest.php":"fd2a277524edff66d0e42041a5bd1d77","tests\/Fields\/TextareaTest.php":"ddae64143b1af19bf235cce0ca872a2f","tests\/Fields\/WysiwygEditorTest.php":"75420c4ec7ec279027af0a146e812532","tests\/Fields\/FlexibleContentTest.php":"796a5dffee0f731b93a44fe3cac66250","tests\/Fields\/DateTimePickerTest.php":"b1ef6a7a9b6921fbf950380dce228d2b","tests\/Fields\/PostObjectTest.php":"b7864e3f163ffb873a8d49a3ea36597e","tests\/Fields\/GalleryTest.php":"a567ea022902fc82451610271dc0190c","tests\/Fields\/UrlTest.php":"ffa8892be91fd115161cc361326e8fb3","tests\/Fields\/ColorPickerTest.php":"cc457c9c683a85155ea321d5f7a881ac","tests\/Fields\/PasswordTest.php":"cb5744fe6e4d589d141d9e480f6a74da","tests\/Fields\/TaxonomyTest.php":"a6b66c938bb2a35d367322bccee50f66","tests\/helpers.php":"36f2e4bc21c1e53b840c4ee5bf03ffe2","examples\/options-page.php":"72efef2aec7008d1898d143dac9876d5","examples\/with-extended-cpts.php":"2bf9c272575aed992303fc47d1509eb2","examples\/custom-post-type.php":"15267b63e662f40041c949e47ba96207","examples\/gutenberg-block.php":"199262462520ddc74dce1bba0fac13d3","src\/Key.php":"6003e26e9ed7793e6e441e7f43c257a9","src\/ConditionalLogic.php":"b87a8966faa7a835cef5903cc5ca24d6","src\/Location.php":"b9a7342375aab8bb5e947589313179c1","src\/Fields\/Email.php":"a85be371325c823a4b3bc5ec16b63c8a","src\/Fields\/WysiwygEditor.php":"4e2a2d89185ac1279c26ab96d0e7ab45","src\/Fields\/Taxonomy.php":"662ffc2f7256a597c1d7a0c5e50e4693","src\/Fields\/Settings\/ReturnFormat.php":"9d44a3d101a489496d87123b55dfe88b","src\/Fields\/Settings\/DateTimeFormat.php":"441f5db12422ffc385ffcc7f15693471","src\/Fields\/Settings\/MimeTypes.php":"8e8f4aff03c9f43dd14d47f1e7454294","src\/Fields\/Settings\/PreviewSize.php":"fbd2efd28534d30ce87adee9049c01e1","src\/Fields\/Settings\/Instructions.php":"8ca33050ad7823d21e9d1d2873926e66","src\/Fields\/Settings\/Nullable.php":"7b2cfc1c5f4079dc296c6169c67e3457","src\/Fields\/Settings\/DirectionLayout.php":"ea05ff7b203d980830a161f197716458","src\/Fields\/Settings\/ButtonLabel.php":"a81bdf1a8b7ba258750f5f202a8118ed","src\/Fields\/Settings\/Dimensions.php":"bf9cc88114af3a3ddcb4e338308dbeb2","src\/Fields\/Settings\/Step.php":"1aca5145784b043478459c915d63d498","src\/Fields\/Settings\/Writable.php":"24cca1a8a97d314d574137eba5cb1731","src\/Fields\/Settings\/Library.php":"741457d67736c6206d3068672b4c58ed","src\/Fields\/Settings\/NewLines.php":"1d24a1a8f35b02f3aa4d720b7d9b315a","src\/Fields\/Settings\/Layout.php":"5832c03c299a0d0208ffc3ecca8c2592","src\/Fields\/Settings\/Disabled.php":"e69f197e22f36db823bcf824a348a67d","src\/Fields\/Settings\/Placeholder.php":"cd07492242693e2109a63f777e5a8ca4","src\/Fields\/Settings\/ConditionalLogic.php":"dcb02a91a8086ec4881853d6537fa85b","src\/Fields\/Settings\/Multiple.php":"819ce4f9c1e2d093dc2872432c1ab3f7","src\/Fields\/Settings\/CharacterLimit.php":"3bf3e4602b56fc54aab9111b148b0a90","src\/Fields\/Settings\/Choices.php":"a8db2da2ffb0734e525c7ee2a29477bd","src\/Fields\/Settings\/Height.php":"7d2b12524d0de3c37a412a9b9f04bb4a","src\/Fields\/Settings\/Endpoint.php":"a0a23dc5cfb184fe09a77848029c6d3f","src\/Fields\/Settings\/Wrapper.php":"6a52971a5da9f06929e4ac297a6e42e6","src\/Fields\/Settings\/WeekDay.php":"ab43d26c460d8dc4cd6c3be82b5e845e","src\/Fields\/Settings\/SubFields.php":"c3d7f2585e29effa21d74f92a5a73152","src\/Fields\/Settings\/Required.php":"4bc0161cfe1acca6646225f4eb811b6e","src\/Fields\/Settings\/MinMax.php":"3f07cccf6ec07ef6265e47f21799691d","src\/Fields\/Settings\/FilterBy.php":"bdf405686e43019529f39d14bde6c48d","src\/Fields\/Settings\/DefaultValue.php":"3a14b8e16d19df2c74e41abf682d4f45","src\/Fields\/Settings\/Message.php":"f2ff1bef4fcdd7835a75adb28fa8cc61","src\/Fields\/Settings\/FileSize.php":"ba58868e2129a04518a7985d432896ad","src\/Fields\/Settings\/Pending.php":"4076d2a167c61e629e3a2b6d8a724b3e","src\/Fields\/DatePicker.php":"a8500533d6a96f483d8670ad4943c30a","src\/Fields\/Gallery.php":"c32b7241f9d8ae8ac52e6be4427a795b","src\/Fields\/ButtonGroup.php":"583ef70d1133cfa416730e4e3bd3f6de","src\/Fields\/Number.php":"f5837e4c99e9321b2444c66a1af326ca","src\/Fields\/Group.php":"8d8ddf38a2fec442d129506ffa0b8d0b","src\/Fields\/PageLink.php":"b976ee2e046edfcdaee9068cf03dba03","src\/Fields\/Link.php":"733673a7ddf3874b35380853e34c8bdf","src\/Fields\/Accordion.php":"1d3df25fb48f4a54553816d1609f1e5b","src\/Fields\/DateTimePicker.php":"00e236d672b9b8a95a03dca8d7f62fd7","src\/Fields\/File.php":"9517c64278c03018c7a62bd758d9cc06","src\/Fields\/PostObject.php":"9c7f44041cb6959acdf9bd3622c851d9","src\/Fields\/TimePicker.php":"f169dfd4c775a1f40a6a2af9abef17ba","src\/Fields\/User.php":"611ea2dbf2ca553e8a391981c6e06936","src\/Fields\/Tab.php":"38589580a1ea9a9af9f24f7a7ef28226","src\/Fields\/TrueFalse.php":"d4019718acc1243423b886be59f76705","src\/Fields\/Repeater.php":"5d9a86fe99df44d39ce2ec2373f5c7aa","src\/Fields\/Field.php":"63e5de7601eeba0eb19ab8de88c6f424","src\/Fields\/FlexibleContent.php":"33e373749f1d0f543af2c96e0377b604","src\/Fields\/GoogleMap.php":"4d7327e69d94e72f8a2b69ac69696103","src\/Fields\/Url.php":"8f9d6260a025aabdb1ef259b29eee6bd","src\/Fields\/Layout.php":"cbbaf812ff6318fe1cc13fb785711e63","src\/Fields\/Password.php":"f0e7e9cddcd2db5550d4c38c4159aaf0","src\/Fields\/Relationship.php":"59c5b2537dd76353b372242739372bbd","src\/Fields\/RadioButton.php":"24b14dbb403892ee24d3fa4127eeb927","src\/Fields\/ColorPicker.php":"79b9f520eb25c07826490fb0dc65be05","src\/Fields\/Textarea.php":"a4f8bfccfdc69d4a04f4c7de565265c6","src\/Fields\/Select.php":"2b5e1bde291c773706c748e7cff3d5e7","src\/Fields\/Checkbox.php":"16ef825ef5a2f2f04d92f21185b234e4","src\/Fields\/Range.php":"024f37de35db791c924c334b051ec102","src\/Fields\/Oembed.php":"b981f419e6a6b56c76be3594d4a986b7","src\/Fields\/Message.php":"b80cd3fc7f8504111a409faadb8fded2","src\/Fields\/Text.php":"8a1c9d6b8f532da997d0569e34805899","src\/Fields\/Image.php":"79ae919bbd8f5eb3c9bca21593532a72","src\/helpers.php":"98d2633c456d78b6a73738ad88f4005a"}} \ No newline at end of file diff --git a/src/ConditionalLogic.php b/src/ConditionalLogic.php index 306e7bd8..0632843c 100644 --- a/src/ConditionalLogic.php +++ b/src/ConditionalLogic.php @@ -23,16 +23,23 @@ public function __construct( string $name, string $operator, mixed $value = null, - string $group = null + string $group = null, + string $key = null, ) { - $this->rules[] = $this->rule($name, $operator, $value, $group); + $this->rules[] = [ + 'name' => $name, + 'operator' => $operator, + 'value' => $value, + 'group' => $group, + 'key' => $key, + ]; } /** * @param string $operator `==` is equal to, `!=` is not equal to, `>` is greater than, `<` is less than, `==pattern` matches pattern, `==contains` contains value, `==empty` has no value, `!=empty` has any value * @throws \InvalidArgumentException */ - public static function where(string $name, string $operator, mixed $value = null, string $group = null): static + public static function where(string $name, string $operator, mixed $value = null, string $group = null, string $key = null): static { $allowedOperators = [ '>', @@ -49,24 +56,20 @@ public static function where(string $name, string $operator, mixed $value = null throw new InvalidArgumentException("Invalid conditional logic operator [$operator]."); } - return new self($name, $operator, $value, $group); - } - - public function and(string|array $name, string $operator, mixed $value = null, string $group = null): static - { - $this->rules[] = $this->rule($name, $operator, $value, $group); - - return $this; + return new self($name, $operator, $value, $group, $key); } - private function rule(string|array $name, string $operator, mixed $value = null, string $group = null): array + public function and(string|array $name, string $operator, mixed $value = null, string $group = null, string $key = null): static { - return [ + $this->rules[] = [ 'name' => $name, 'operator' => $operator, 'value' => $value, 'group' => $group, + 'field' => $key, ]; + + return $this; } /** @internal */ @@ -79,7 +82,7 @@ public function get(string|null $parentKey = null): array $key = $resolvedParentKey . '_' . Key::sanitize($rule['name']); $newRule = [ - 'field' => 'field_' . Key::hash($key), + 'field' => $rule['key'] ?? 'field_' . Key::hash($key), 'operator' => $rule['operator'], ]; diff --git a/src/Fields/Field.php b/src/Fields/Field.php index e95a5e44..c4d676c4 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -13,7 +13,6 @@ namespace Extended\ACF\Fields; -use Extended\ACF\ConditionalLogic; use Extended\ACF\Key; use InvalidArgumentException; @@ -111,7 +110,7 @@ public function get(string|null $parentKey = null): array if (isset($this->settings['conditional_logic'])) { $this->settings['conditional_logic'] = array_map( - fn ($rules) => $rules instanceof ConditionalLogic ? $rules->get($parentKey) : $rules, // allow specifying of rules manually with an array + fn ($rules) => $rules->get($parentKey), $this->settings['conditional_logic'] ); } diff --git a/tests/ConditionalLogicTest.php b/tests/ConditionalLogicTest.php index eba295aa..872e3af1 100644 --- a/tests/ConditionalLogicTest.php +++ b/tests/ConditionalLogicTest.php @@ -70,31 +70,30 @@ public function testResolvedParentKey() ); } - public function testManualRules() + public function testFieldKey() { $settings = register_extended_field_group([ - 'title' => 'Manual Key', + 'title' => 'Field Key', 'fields' => [ Select::make('Select') - ->key('field_manual') - ->choices([ - 'red' => 'Red', - ]) - ->defaultValue('red'), - Repeater::make('Repeater') - ->fields([ - Text::make('Red') - ->conditionalLogic([ - ['field' => 'field_manual', 'operator' => '==', 'value' => 'red'], - ]), - ]) + ->key('field_123abc') + ->choices(['Red']), + Text::make('Text') + ->conditionalLogic([ + ConditionalLogic::where( + name: 'select', + operator: '==', + value: 'red', + key: 'field_123abc' + ), + ]), ], 'location' => [] ]); $this->assertSame( - $settings['fields'][0]['key'], - $settings['fields'][1]['sub_fields'][0]['conditional_logic'][0]['field'] + 'field_123abc', + $settings['fields'][1]['conditional_logic'][0][0]['field'] ); } } From 4f0229a8392b712830050754de1dc1a71a7d198e Mon Sep 17 00:00:00 2001 From: Vincent Klaiber Date: Tue, 17 Oct 2023 13:24:19 +0200 Subject: [PATCH 6/6] Fix syntax issues --- src/Fields/Field.php | 6 +++--- src/Fields/Settings/Choices.php | 2 +- src/helpers.php | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Fields/Field.php b/src/Fields/Field.php index c4d676c4..f792555d 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -110,21 +110,21 @@ public function get(string|null $parentKey = null): array if (isset($this->settings['conditional_logic'])) { $this->settings['conditional_logic'] = array_map( - fn ($rules) => $rules->get($parentKey), + fn($rules) => $rules->get($parentKey), $this->settings['conditional_logic'] ); } if (isset($this->settings['layouts'])) { $this->settings['layouts'] = array_map( - fn ($layout) => $layout->get($key), + fn($layout) => $layout->get($key), $this->settings['layouts'] ); } if (isset($this->settings['sub_fields'])) { $this->settings['sub_fields'] = array_map( - fn ($field) => $field->get($key), + fn($field) => $field->get($key), $this->settings['sub_fields'] ); } diff --git a/src/Fields/Settings/Choices.php b/src/Fields/Settings/Choices.php index d02f9a7c..5eb74956 100644 --- a/src/Fields/Settings/Choices.php +++ b/src/Fields/Settings/Choices.php @@ -23,7 +23,7 @@ trait Choices public function choices(array $choices): static { if (array_is_list($choices)) { - $choices = array_combine(array_map(fn ($key) => Key::sanitize($key), $choices), $choices); + $choices = array_combine(array_map(fn($key) => Key::sanitize($key), $choices), $choices); } $this->settings['choices'] = $choices; diff --git a/src/helpers.php b/src/helpers.php index d02b7169..20569411 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -32,9 +32,9 @@ function register_extended_field_group(array $settings): array $settings['style'] ??= 'seamless'; - $settings['fields'] = array_map(fn ($field) => $field->get($key), $settings['fields']); + $settings['fields'] = array_map(fn($field) => $field->get($key), $settings['fields']); - $settings['location'] = array_map(fn ($location) => $location->get(), $settings['location']); + $settings['location'] = array_map(fn($location) => $location->get(), $settings['location']); $settings['key'] = Key::generate($key, 'group');