From 633e6641e72e66f391148d288e8e8abd3f0d0c45 Mon Sep 17 00:00:00 2001 From: Scott Kingsley Clark Date: Sat, 23 Sep 2023 09:50:24 -0500 Subject: [PATCH 01/11] Resolve bidirectional removal issue from Pods 2.x --- classes/fields/pick.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/classes/fields/pick.php b/classes/fields/pick.php index c0837753b9..5f9aa4738a 100644 --- a/classes/fields/pick.php +++ b/classes/fields/pick.php @@ -1949,7 +1949,10 @@ public function save( $value, $id = null, $name = null, $options = null, $fields if ( empty( $value_ids ) ) { // Remove all bidirectional relationships. if ( ! empty( $remove_ids ) ) { + // Remove this ID from the related IDS. self::$api->delete_relationships( $remove_ids, $id, $related_pod, $related_field ); + + // Remove the related IDs from this ID. self::$api->delete_relationships( $id, $remove_ids, $pod, $options ); } @@ -1991,7 +1994,7 @@ public function save( $value, $id = null, $name = null, $options = null, $fields if ( ! in_array( $id, $bidirectional_ids, true ) ) { // Add to related items. $bidirectional_ids[] = $id; - } elseif ( empty( $remove_ids ) ) { + } elseif ( empty( $bidirectional_remove_ids ) ) { // Nothing to change. continue; } @@ -2003,6 +2006,11 @@ public function save( $value, $id = null, $name = null, $options = null, $fields } }//end foreach + // Remove this ID from the related IDs. + if ( ! empty( $remove_ids ) ) { + self::$api->delete_relationships( $remove_ids, $id, $related_pod, $related_field ); + } + if ( ! $no_conflict ) { pods_no_conflict_off( $related_pod['type'] ); } From a7973d172d4045256e0eb0684e53006495b0a44f Mon Sep 17 00:00:00 2001 From: Scott Kingsley Clark Date: Sat, 23 Sep 2023 09:51:42 -0500 Subject: [PATCH 02/11] Pods 3.0.4 --- changelog.txt | 4 ++++ init.php | 4 ++-- package.json | 2 +- readme.txt | 6 +++++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/changelog.txt b/changelog.txt index d09f567444..59d9bc33d5 100644 --- a/changelog.txt +++ b/changelog.txt @@ -2,6 +2,10 @@ Found a bug? Have a great feature idea? Get on GitHub and tell us about it and w Our GitHub has the full list of all prior releases of Pods: https://github.com/pods-framework/pods/releases += 3.0.4 - September 23rd, 2023 = + +* Fixed: Resolve bidirectional removal issue from Pods 2.x where bidirectional relationships would not have the current item removed when you removed that related item. (@sc0ttkclark) + = 3.0.3 - September 22nd, 2023 = * Fixed: Settings pods handling for relationships for Pods that are registered by DB / Code / File now normalize as necessary for relationships stored in the `wp_podsrel` index table for which item ID is used to relate from. (@sc0ttkclark) diff --git a/init.php b/init.php index c17ef8591a..5bf09bd48b 100644 --- a/init.php +++ b/init.php @@ -10,7 +10,7 @@ * Plugin Name: Pods - Custom Content Types and Fields * Plugin URI: https://pods.io/ * Description: Pods is a framework for creating, managing, and deploying customized content types and fields - * Version: 3.0.3 + * Version: 3.0.4 * Author: Pods Framework Team * Author URI: https://pods.io/about/ * Text Domain: pods @@ -43,7 +43,7 @@ add_action( 'init', 'pods_deactivate_pods_ui' ); } else { // Current version. - define( 'PODS_VERSION', '3.0.3' ); + define( 'PODS_VERSION', '3.0.4' ); // Current database version, this is the last version the database changed. define( 'PODS_DB_VERSION', '2.3.5' ); diff --git a/package.json b/package.json index 2f9844716f..79da908540 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pods", - "version": "3.0.3", + "version": "3.0.4", "description": "Pods is a development framework for creating, extending, managing, and deploying customized content types in WordPress.", "author": "Pods Foundation, Inc", "homepage": "https://pods.io/", diff --git a/readme.txt b/readme.txt index 183f01055e..98b8e91a3c 100644 --- a/readme.txt +++ b/readme.txt @@ -5,7 +5,7 @@ Tags: pods, custom post types, custom taxonomies, content types, custom fields, Requires at least: 6.0 Tested up to: 6.3 Requires PHP: 7.2 -Stable tag: 3.0.3 +Stable tag: 3.0.4 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -177,6 +177,10 @@ Pods really wouldn't be where it is without all the contributions from our [dono == Changelog == += 3.0.4 - September 23rd, 2023 = + +* Fixed: Resolve bidirectional removal issue from Pods 2.x where bidirectional relationships would not have the current item removed when you removed that related item. (@sc0ttkclark) + = 3.0.3 - September 22nd, 2023 = * Fixed: Settings pods handling for relationships for Pods that are registered by DB / Code / File now normalize as necessary for relationships stored in the `wp_podsrel` index table for which item ID is used to relate from. (@sc0ttkclark) From 57aeb7eb1f4ef83a4dce89b8076808bbd4b536f7 Mon Sep 17 00:00:00 2001 From: Scott Kingsley Clark Date: Sat, 23 Sep 2023 21:03:41 -0500 Subject: [PATCH 03/11] Additional checks and prevention of serialized data getting saved from the old syntax --- src/Pods/Data/Conditional_Logic.php | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Pods/Data/Conditional_Logic.php b/src/Pods/Data/Conditional_Logic.php index 0a61cf4446..5ce26f5b93 100644 --- a/src/Pods/Data/Conditional_Logic.php +++ b/src/Pods/Data/Conditional_Logic.php @@ -117,13 +117,27 @@ public static function maybe_migrate_field_name( $field_name ): string { */ public static function maybe_setup_from_old_syntax( $object ): ?Conditional_Logic { $old_syntax = [ - 'depends-on' => (array) pods_v( 'depends-on', $object, [], true ), - 'depends-on-any' => (array) pods_v( 'depends-on-any', $object, [], true ), - 'depends-on-multi' => (array) pods_v( 'depends-on-multi', $object, [], true ), - 'excludes-on' => (array) pods_v( 'excludes-on', $object, [], true ), - 'wildcard-on' => (array) pods_v( 'wildcard-on', $object, [], true ), + 'depends-on' => pods_v( 'depends-on', $object, [], true ), + 'depends-on-any' => pods_v( 'depends-on-any', $object, [], true ), + 'depends-on-multi' => pods_v( 'depends-on-multi', $object, [], true ), + 'excludes-on' => pods_v( 'excludes-on', $object, [], true ), + 'wildcard-on' => pods_v( 'wildcard-on', $object, [], true ), ]; + foreach ( $old_syntax as $old_syntax_key => $old_syntax_value ) { + if ( empty( $old_syntax_value ) ) { + $old_syntax_value = []; + } elseif ( ! is_array( $old_syntax_value ) ) { + $old_syntax_value = maybe_unserialize( $old_syntax_value ); + + if ( ! is_array( $old_syntax_value ) ) { + $old_syntax_value = []; + } + } + + $old_syntax[ $old_syntax_key ] = $old_syntax_value; + } + $action = 'show'; $logic = 'any'; $rules = []; From 3314027af38a93d2ac895eddfb9cb77a19c466df Mon Sep 17 00:00:00 2001 From: Scott Kingsley Clark Date: Sat, 23 Sep 2023 22:19:30 -0500 Subject: [PATCH 04/11] Add another arg to exclude when saving pod fields --- classes/PodsAPI.php | 1 + 1 file changed, 1 insertion(+) diff --git a/classes/PodsAPI.php b/classes/PodsAPI.php index b086f5756f..92f0863bac 100644 --- a/classes/PodsAPI.php +++ b/classes/PodsAPI.php @@ -3724,6 +3724,7 @@ public function save_field( $params, $table_operation = true, $sanitized = false 'group_id', 'attributes', 'depends-on', + 'depends-on-any', 'depends-on-multi', 'excludes-on', 'wildcard-on', From 9e34a03cd04f9943663e17e55e90fe160d5bdc5f Mon Sep 17 00:00:00 2001 From: Scott Kingsley Clark Date: Sat, 23 Sep 2023 22:20:03 -0500 Subject: [PATCH 05/11] Add new repair tool for fields with bugged conditional logic --- src/Pods/Tools/Repair.php | 124 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) diff --git a/src/Pods/Tools/Repair.php b/src/Pods/Tools/Repair.php index f4f992d1ca..4d0b594720 100644 --- a/src/Pods/Tools/Repair.php +++ b/src/Pods/Tools/Repair.php @@ -84,6 +84,9 @@ public function repair_groups_and_fields_for_pod( Pod $pod, $mode ) { // Maybe fix fields with invalid field type. $results[ __( 'Fixed fields with invalid field type', 'pods' ) ] = $this->maybe_fix_fields_with_invalid_field_type( $pod, $mode ); + // Maybe fix fields with invalid conditional logic. + $results[ __( 'Fixed fields with invalid conditional logic', 'pods' ) ] = $this->maybe_fix_fields_with_invalid_conditional_logic( $pod, $mode ); + // Check if changes were made to the Pod. $changes_made = [] !== array_filter( $results ); @@ -738,4 +741,125 @@ protected function maybe_fix_fields_with_invalid_field_type( Pod $pod, $mode ) { return $fixed_fields; } + /** + * Maybe fix fields with invalid conditional logic. + * + * @since 3.0.4 + * + * @param Pod $pod The Pod object. + * @param string $mode The repair mode (preview, upgrade, or full). + * + * @return string[] The label, name, and ID for each field fixed. + */ + protected function maybe_fix_fields_with_invalid_conditional_logic( Pod $pod, $mode ) { + $this->setup(); + + $fields = $pod->get_fields( [ + 'fallback_mode' => false, + 'meta_query' => [ + 'relation' => 'OR', + [ + 'key' => 'conditional_logic', + 'value' => 'a:0:{}', + 'compare' => 'LIKE', + ], + [ + 'key' => 'conditional_logic', + 'value' => 's:6:"a:0:{}"', + 'compare' => 'LIKE', + ], + [ + 'key' => 'depends-on', + 'compare' => 'EXISTS', + ], + [ + 'key' => 'depends-on-any', + 'compare' => 'EXISTS', + ], + [ + 'key' => 'depends-on-multi', + 'compare' => 'EXISTS', + ], + [ + 'key' => 'excludes-on', + 'compare' => 'EXISTS', + ], + [ + 'key' => 'wildcard-on', + 'compare' => 'EXISTS', + ], + ], + ] ); + + $fixed_fields = []; + + $invalid_args = [ + 'conditional_logic', + 'depends-on', + 'depends-on-any', + 'depends-on-multi', + 'excludes-on', + 'wildcard-on', + ]; + + foreach ( $fields as $field ) { + $field_id = $field->get_id(); + + if ( empty( $field_id ) ) { + continue; + } + + try { + $found_invalid_args = []; + + foreach ( $invalid_args as $invalid_arg ) { + $arg_value = $field->get_arg( $invalid_arg ); + + if ( null !== $arg_value ) { + $found_invalid_args[ $invalid_arg ] = $arg_value; + } + } + + if ( 'preview' !== $mode ) { + $field_args_to_save = [ + 'id' => $field_id, + 'pod_data' => $pod, + 'field' => $field, + ]; + + foreach ( $found_invalid_args as $invalid_arg => $arg_value ) { + if ( 'conditional_logic' === $invalid_arg ) { + $field_args_to_save['enable_conditional_logic'] = 0; + $field_args_to_save[ $invalid_arg ] = null; + + $field->set_arg( 'enable_conditional_logic', 0 ); + $field->set_arg( $invalid_arg, null ); + } else { + $field_args_to_save[ $invalid_arg ] = null; + + $field->set_arg( $invalid_arg, null ); + } + } + + $this->api->save_field( $field_args_to_save ); + } + + $fixed_fields[] = sprintf( + '%1$s (%2$s: [%3$s] | %4$s: %5$s | %6$s: %7$d)', + $field->get_label(), + __( 'Fixed invalid conditional logic args', 'pods' ), + implode( ', ', array_keys( $found_invalid_args ) ), + __( 'Name', 'pods' ), + $field->get_name(), + __( 'ID', 'pods' ), + $field->get_id() + ); + } catch ( Throwable $exception ) { + $this->errors[] = ucwords( str_replace( '_', ' ', __FUNCTION__ ) ) . ' > ' . $exception->getMessage() . ' (' . $field->get_name() . ' - #' . $field->get_id() . ')'; + } + } + + return $fixed_fields; + } + } From 688163b5c11882eeadbb80ff3ba154fe0e50e9cd Mon Sep 17 00:00:00 2001 From: Scott Kingsley Clark Date: Sat, 23 Sep 2023 22:35:50 -0500 Subject: [PATCH 06/11] Remove extra LIKE query --- src/Pods/Tools/Repair.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/Pods/Tools/Repair.php b/src/Pods/Tools/Repair.php index 4d0b594720..1ccab5f64b 100644 --- a/src/Pods/Tools/Repair.php +++ b/src/Pods/Tools/Repair.php @@ -763,11 +763,6 @@ protected function maybe_fix_fields_with_invalid_conditional_logic( Pod $pod, $m 'value' => 'a:0:{}', 'compare' => 'LIKE', ], - [ - 'key' => 'conditional_logic', - 'value' => 's:6:"a:0:{}"', - 'compare' => 'LIKE', - ], [ 'key' => 'depends-on', 'compare' => 'EXISTS', From edbad2725cb1dbaa02ecf4d7f0b8b212c26a0020 Mon Sep 17 00:00:00 2001 From: Scott Kingsley Clark Date: Sat, 23 Sep 2023 23:18:28 -0500 Subject: [PATCH 07/11] Update to repair to reference raw args for the fields --- src/Pods/Tools/Repair.php | 179 +++++++++++++++++------------ src/Pods/Whatsit.php | 7 +- src/Pods/Whatsit/Block_Field.php | 4 +- src/Pods/Whatsit/Field.php | 6 +- src/Pods/Whatsit/Group.php | 6 +- src/Pods/Whatsit/Legacy_Object.php | 6 +- src/Pods/Whatsit/Pod.php | 6 +- 7 files changed, 132 insertions(+), 82 deletions(-) diff --git a/src/Pods/Tools/Repair.php b/src/Pods/Tools/Repair.php index 1ccab5f64b..95e596ca66 100644 --- a/src/Pods/Tools/Repair.php +++ b/src/Pods/Tools/Repair.php @@ -754,40 +754,6 @@ protected function maybe_fix_fields_with_invalid_field_type( Pod $pod, $mode ) { protected function maybe_fix_fields_with_invalid_conditional_logic( Pod $pod, $mode ) { $this->setup(); - $fields = $pod->get_fields( [ - 'fallback_mode' => false, - 'meta_query' => [ - 'relation' => 'OR', - [ - 'key' => 'conditional_logic', - 'value' => 'a:0:{}', - 'compare' => 'LIKE', - ], - [ - 'key' => 'depends-on', - 'compare' => 'EXISTS', - ], - [ - 'key' => 'depends-on-any', - 'compare' => 'EXISTS', - ], - [ - 'key' => 'depends-on-multi', - 'compare' => 'EXISTS', - ], - [ - 'key' => 'excludes-on', - 'compare' => 'EXISTS', - ], - [ - 'key' => 'wildcard-on', - 'compare' => 'EXISTS', - ], - ], - ] ); - - $fixed_fields = []; - $invalid_args = [ 'conditional_logic', 'depends-on', @@ -797,64 +763,127 @@ protected function maybe_fix_fields_with_invalid_conditional_logic( Pod $pod, $m 'wildcard-on', ]; - foreach ( $fields as $field ) { - $field_id = $field->get_id(); + $fixed_fields = []; + + foreach ( $invalid_args as $invalid_arg ) { + $meta_query_check = [ + 'key' => $invalid_arg, + 'compare' => 'EXISTS', + ]; - if ( empty( $field_id ) ) { - continue; + if ( 'conditional_logic' === $invalid_arg ) { + $meta_query_check['value'] = 'a:0:{}'; + $meta_query_check['compare'] = 'LIKE'; } - try { - $found_invalid_args = []; + $fields = $pod->get_fields( [ + 'fallback_mode' => false, + 'meta_query' => [ + $meta_query_check, + ], + ] ); - foreach ( $invalid_args as $invalid_arg ) { - $arg_value = $field->get_arg( $invalid_arg ); + foreach ( $fields as $field ) { + $fixed_field = $this->maybe_fix_fields_with_invalid_conditional_logic_for_field( $pod, $field, $mode ); - if ( null !== $arg_value ) { - $found_invalid_args[ $invalid_arg ] = $arg_value; - } + if ( $fixed_field ) { + $fixed_fields[] = $fixed_field; } + } + } - if ( 'preview' !== $mode ) { - $field_args_to_save = [ - 'id' => $field_id, - 'pod_data' => $pod, - 'field' => $field, - ]; + return $fixed_fields; + } + + /** + * Maybe fix a field with invalid conditional logic. + * + * @since 3.0.4 + * + * @param Pod $pod The Pod object. + * @param Field $field The Field object. + * @param string $mode The repair mode (preview, upgrade, or full). + * + * @return string[]|false The label, name, and ID for the field fixed, or false if not fixed. + */ + protected function maybe_fix_fields_with_invalid_conditional_logic_for_field( Pod $pod, Field $field, $mode ) { + $this->setup(); - foreach ( $found_invalid_args as $invalid_arg => $arg_value ) { - if ( 'conditional_logic' === $invalid_arg ) { - $field_args_to_save['enable_conditional_logic'] = 0; - $field_args_to_save[ $invalid_arg ] = null; + $field_id = $field->get_id(); - $field->set_arg( 'enable_conditional_logic', 0 ); - $field->set_arg( $invalid_arg, null ); - } else { - $field_args_to_save[ $invalid_arg ] = null; + if ( empty( $field_id ) ) { + return false; + } + + $invalid_args = [ + 'conditional_logic', + 'depends-on', + 'depends-on-any', + 'depends-on-multi', + 'excludes-on', + 'wildcard-on', + ]; - $field->set_arg( $invalid_arg, null ); - } + try { + $found_invalid_args = []; + + foreach ( $invalid_args as $invalid_arg ) { + $arg_value = $field->get_arg( $invalid_arg, null, false, true ); + + if ( null !== $arg_value ) { + if ( + 'conditional_logic' === $invalid_arg + && ( + empty( $arg_value ) + || is_array( $arg_value ) + ) + ) { + continue; } - $this->api->save_field( $field_args_to_save ); + $found_invalid_args[ $invalid_arg ] = $arg_value; } + } - $fixed_fields[] = sprintf( - '%1$s (%2$s: [%3$s] | %4$s: %5$s | %6$s: %7$d)', - $field->get_label(), - __( 'Fixed invalid conditional logic args', 'pods' ), - implode( ', ', array_keys( $found_invalid_args ) ), - __( 'Name', 'pods' ), - $field->get_name(), - __( 'ID', 'pods' ), - $field->get_id() - ); - } catch ( Throwable $exception ) { - $this->errors[] = ucwords( str_replace( '_', ' ', __FUNCTION__ ) ) . ' > ' . $exception->getMessage() . ' (' . $field->get_name() . ' - #' . $field->get_id() . ')'; + if ( 'preview' !== $mode ) { + $field_args_to_save = [ + 'id' => $field_id, + 'pod_data' => $pod, + 'field' => $field, + ]; + + foreach ( $found_invalid_args as $invalid_arg => $arg_value ) { + if ( 'conditional_logic' === $invalid_arg ) { + $field_args_to_save['enable_conditional_logic'] = 0; + $field_args_to_save[ $invalid_arg ] = null; + + $field->set_arg( 'enable_conditional_logic', 0 ); + $field->set_arg( $invalid_arg, null ); + } else { + $field_args_to_save[ $invalid_arg ] = null; + + $field->set_arg( $invalid_arg, null ); + } + } + + $this->api->save_field( $field_args_to_save ); } + + return sprintf( + '%1$s (%2$s: [%3$s] | %4$s: %5$s | %6$s: %7$d)', + $field->get_label(), + __( 'Fixed invalid conditional logic args', 'pods' ), + implode( ', ', array_keys( $found_invalid_args ) ), + __( 'Name', 'pods' ), + $field->get_name(), + __( 'ID', 'pods' ), + $field->get_id() + ); + } catch ( Throwable $exception ) { + $this->errors[] = ucwords( str_replace( '_', ' ', __FUNCTION__ ) ) . ' > ' . $exception->getMessage() . ' (' . $field->get_name() . ' - #' . $field->get_id() . ')'; } - return $fixed_fields; + return false; } } diff --git a/src/Pods/Whatsit.php b/src/Pods/Whatsit.php index c4f8ca0b44..1fe7f3481c 100644 --- a/src/Pods/Whatsit.php +++ b/src/Pods/Whatsit.php @@ -619,12 +619,17 @@ public function set_args( $args, $replace = true ) { * @param string $arg Argument name. * @param mixed|null $default Default to use if not set. * @param bool $strict Whether to check only normal arguments and not special arguments. + * @param bool $raw Whether only get the raw value. * * @return null|mixed Argument value, or null if not set. */ - public function get_arg( $arg, $default = null, $strict = false ) { + public function get_arg( $arg, $default = null, $strict = false, $raw = false ) { $arg = (string) $arg; + if ( $raw ) { + return $this->args[ $arg ] ?? $default; + } + $special_args = [ 'identifier' => 'get_identifier', 'label' => 'get_label', diff --git a/src/Pods/Whatsit/Block_Field.php b/src/Pods/Whatsit/Block_Field.php index 4abd18c095..fd0cf7a42f 100644 --- a/src/Pods/Whatsit/Block_Field.php +++ b/src/Pods/Whatsit/Block_Field.php @@ -341,12 +341,12 @@ public function get_table_info() { /** * {@inheritdoc} */ - public function get_arg( $arg, $default = null, $strict = false ) { + public function get_arg( $arg, $default = null, $strict = false, $raw = false ) { if ( 'block' === $arg ) { return $this->get_parent_name(); } - return Whatsit::get_arg( $arg, $default ); + return Whatsit::get_arg( $arg, $default, $strict, $raw ); } /** diff --git a/src/Pods/Whatsit/Field.php b/src/Pods/Whatsit/Field.php index f0f77c24fa..730a126e01 100644 --- a/src/Pods/Whatsit/Field.php +++ b/src/Pods/Whatsit/Field.php @@ -64,7 +64,11 @@ public function get_type_arg( $arg, $default = null, $strict = false ) { /** * {@inheritdoc} */ - public function get_arg( $arg, $default = null, $strict = false ) { + public function get_arg( $arg, $default = null, $strict = false, $raw = false ) { + if ( $raw ) { + return parent::get_arg( $arg, $default, $strict, $raw ); + } + $arg = (string) $arg; $special_args = [ diff --git a/src/Pods/Whatsit/Group.php b/src/Pods/Whatsit/Group.php index b84c582510..67668a3cc7 100644 --- a/src/Pods/Whatsit/Group.php +++ b/src/Pods/Whatsit/Group.php @@ -108,7 +108,11 @@ public function get_groups( array $args = [] ) { /** * {@inheritdoc} */ - public function get_arg( $arg, $default = null, $strict = false ) { + public function get_arg( $arg, $default = null, $strict = false, $raw = false ) { + if ( $raw ) { + return parent::get_arg( $arg, $default, $strict, $raw ); + } + $arg = (string) $arg; $special_args = [ diff --git a/src/Pods/Whatsit/Legacy_Object.php b/src/Pods/Whatsit/Legacy_Object.php index 62fab88e43..93e7b297bc 100644 --- a/src/Pods/Whatsit/Legacy_Object.php +++ b/src/Pods/Whatsit/Legacy_Object.php @@ -41,7 +41,11 @@ public function get_clean_args() { /** * {@inheritdoc} */ - public function get_arg( $arg, $default = null, $strict = false ) { + public function get_arg( $arg, $default = null, $strict = false, $raw = false ) { + if ( $raw ) { + return parent::get_arg( $arg, $default, $strict, $raw ); + } + $old_mapping = [ 'name' => 'label', 'slug' => 'name', diff --git a/src/Pods/Whatsit/Pod.php b/src/Pods/Whatsit/Pod.php index 8d12055cf5..58bdf630a9 100644 --- a/src/Pods/Whatsit/Pod.php +++ b/src/Pods/Whatsit/Pod.php @@ -87,7 +87,11 @@ public function export( array $args = [] ) { /** * {@inheritdoc} */ - public function get_arg( $arg, $default = null, $strict = false ) { + public function get_arg( $arg, $default = null, $strict = false, $raw = false ) { + if ( $raw ) { + return parent::get_arg( $arg, $default, $strict, $raw ); + } + if ( 'storage' === $arg ) { return $this->get_storage(); } From c5422aeb20f068a173d92bdb04ca2608a3d05798 Mon Sep 17 00:00:00 2001 From: Scott Kingsley Clark Date: Sun, 24 Sep 2023 15:13:26 -0500 Subject: [PATCH 08/11] Update to repair for invalid conditional args and avoid group reassignment loops --- src/Pods/Tools/Repair.php | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/Pods/Tools/Repair.php b/src/Pods/Tools/Repair.php index 95e596ca66..eaf282d50e 100644 --- a/src/Pods/Tools/Repair.php +++ b/src/Pods/Tools/Repair.php @@ -588,7 +588,13 @@ protected function maybe_reassign_fields_with_invalid_groups( Pod $pod, $group_i ] ); $groups = wp_list_pluck( $groups, 'id' ); - $groups = array_filter( $groups ); + $groups = array_values( array_filter( $groups ) ); + + if ( $group_id ) { + $groups[] = $group_id; + } + + $groups = array_unique( $groups ); $fields = $pod->get_fields( [ 'fallback_mode' => false, @@ -643,6 +649,10 @@ protected function reassign_fields_to_group( $fields, $group_id, $pod, $mode ) { $reassigned_fields = []; foreach ( $fields as $field ) { + if ( $field->get_arg( 'group' ) === $group_id ) { + continue; + } + try { if ( 'preview' !== $mode ) { $this->api->save_field( [ @@ -756,6 +766,7 @@ protected function maybe_fix_fields_with_invalid_conditional_logic( Pod $pod, $m $invalid_args = [ 'conditional_logic', + 'attributes', 'depends-on', 'depends-on-any', 'depends-on-multi', @@ -817,6 +828,7 @@ protected function maybe_fix_fields_with_invalid_conditional_logic_for_field( Po $invalid_args = [ 'conditional_logic', + 'attributes', 'depends-on', 'depends-on-any', 'depends-on-multi', @@ -846,27 +858,17 @@ protected function maybe_fix_fields_with_invalid_conditional_logic_for_field( Po } if ( 'preview' !== $mode ) { - $field_args_to_save = [ - 'id' => $field_id, - 'pod_data' => $pod, - 'field' => $field, - ]; - foreach ( $found_invalid_args as $invalid_arg => $arg_value ) { if ( 'conditional_logic' === $invalid_arg ) { - $field_args_to_save['enable_conditional_logic'] = 0; - $field_args_to_save[ $invalid_arg ] = null; + update_post_meta( $field_id, 'enable_conditional_logic', 0 ); $field->set_arg( 'enable_conditional_logic', 0 ); - $field->set_arg( $invalid_arg, null ); - } else { - $field_args_to_save[ $invalid_arg ] = null; - - $field->set_arg( $invalid_arg, null ); } - } - $this->api->save_field( $field_args_to_save ); + delete_post_meta( $field_id, $invalid_arg ); + + $field->set_arg( $invalid_arg, null ); + } } return sprintf( From 86bb83030bfed7877a066867468fc3f59ba3140e Mon Sep 17 00:00:00 2001 From: Scott Kingsley Clark Date: Sun, 24 Sep 2023 22:39:45 -0500 Subject: [PATCH 09/11] More work for conditional logic repair tool --- src/Pods/Tools/Repair.php | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/Pods/Tools/Repair.php b/src/Pods/Tools/Repair.php index eaf282d50e..835a636e37 100644 --- a/src/Pods/Tools/Repair.php +++ b/src/Pods/Tools/Repair.php @@ -795,7 +795,7 @@ protected function maybe_fix_fields_with_invalid_conditional_logic( Pod $pod, $m ] ); foreach ( $fields as $field ) { - $fixed_field = $this->maybe_fix_fields_with_invalid_conditional_logic_for_field( $pod, $field, $mode ); + $fixed_field = $this->maybe_fix_fields_with_invalid_conditional_logic_for_field( $pod, $field, $invalid_arg, $mode ); if ( $fixed_field ) { $fixed_fields[] = $fixed_field; @@ -811,13 +811,14 @@ protected function maybe_fix_fields_with_invalid_conditional_logic( Pod $pod, $m * * @since 3.0.4 * - * @param Pod $pod The Pod object. - * @param Field $field The Field object. - * @param string $mode The repair mode (preview, upgrade, or full). + * @param Pod $pod The Pod object. + * @param Field $field The Field object. + * @param string $invalid_arg The invalid argument. + * @param string $mode The repair mode (preview, upgrade, or full). * * @return string[]|false The label, name, and ID for the field fixed, or false if not fixed. */ - protected function maybe_fix_fields_with_invalid_conditional_logic_for_field( Pod $pod, Field $field, $mode ) { + protected function maybe_fix_fields_with_invalid_conditional_logic_for_field( Pod $pod, Field $field, string $invalid_arg, $mode ) { $this->setup(); $field_id = $field->get_id(); @@ -839,12 +840,13 @@ protected function maybe_fix_fields_with_invalid_conditional_logic_for_field( Po try { $found_invalid_args = []; - foreach ( $invalid_args as $invalid_arg ) { - $arg_value = $field->get_arg( $invalid_arg, null, false, true ); + foreach ( $invalid_args as $other_invalid_arg ) { + $arg_value = $field->get_arg( $other_invalid_arg, null, false, true ); if ( null !== $arg_value ) { if ( - 'conditional_logic' === $invalid_arg + 'conditional_logic' !== $invalid_arg + && 'conditional_logic' === $other_invalid_arg && ( empty( $arg_value ) || is_array( $arg_value ) @@ -853,21 +855,21 @@ protected function maybe_fix_fields_with_invalid_conditional_logic_for_field( Po continue; } - $found_invalid_args[ $invalid_arg ] = $arg_value; + $found_invalid_args[ $other_invalid_arg ] = $arg_value; } } if ( 'preview' !== $mode ) { - foreach ( $found_invalid_args as $invalid_arg => $arg_value ) { - if ( 'conditional_logic' === $invalid_arg ) { + foreach ( $found_invalid_args as $found_invalid_arg => $arg_value ) { + if ( 'conditional_logic' === $found_invalid_arg ) { update_post_meta( $field_id, 'enable_conditional_logic', 0 ); $field->set_arg( 'enable_conditional_logic', 0 ); } - delete_post_meta( $field_id, $invalid_arg ); + delete_post_meta( $field_id, $found_invalid_arg ); - $field->set_arg( $invalid_arg, null ); + $field->set_arg( $found_invalid_arg, null ); } } From 91449d62867eb0f0468fb6e75377068963b89d39 Mon Sep 17 00:00:00 2001 From: Scott Kingsley Clark Date: Mon, 25 Sep 2023 09:08:52 -0500 Subject: [PATCH 10/11] Final adjustments to repair handling for invalid args --- src/Pods/Tools/Repair.php | 22 ++++++++++++++-------- ui/admin/settings-tools.php | 1 + 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/Pods/Tools/Repair.php b/src/Pods/Tools/Repair.php index 835a636e37..0fe579c2c9 100644 --- a/src/Pods/Tools/Repair.php +++ b/src/Pods/Tools/Repair.php @@ -84,8 +84,8 @@ public function repair_groups_and_fields_for_pod( Pod $pod, $mode ) { // Maybe fix fields with invalid field type. $results[ __( 'Fixed fields with invalid field type', 'pods' ) ] = $this->maybe_fix_fields_with_invalid_field_type( $pod, $mode ); - // Maybe fix fields with invalid conditional logic. - $results[ __( 'Fixed fields with invalid conditional logic', 'pods' ) ] = $this->maybe_fix_fields_with_invalid_conditional_logic( $pod, $mode ); + // Maybe fix fields with invalid arguments. + $results[ __( 'Fixed fields with invalid arguments', 'pods' ) ] = $this->maybe_fix_fields_with_invalid_args( $pod, $mode ); // Check if changes were made to the Pod. $changes_made = [] !== array_filter( $results ); @@ -752,7 +752,7 @@ protected function maybe_fix_fields_with_invalid_field_type( Pod $pod, $mode ) { } /** - * Maybe fix fields with invalid conditional logic. + * Maybe fix pod fields with invalid arguments. * * @since 3.0.4 * @@ -761,12 +761,13 @@ protected function maybe_fix_fields_with_invalid_field_type( Pod $pod, $mode ) { * * @return string[] The label, name, and ID for each field fixed. */ - protected function maybe_fix_fields_with_invalid_conditional_logic( Pod $pod, $mode ) { + protected function maybe_fix_fields_with_invalid_args( Pod $pod, $mode ) { $this->setup(); $invalid_args = [ 'conditional_logic', 'attributes', + 'grouped', 'depends-on', 'depends-on-any', 'depends-on-multi', @@ -795,7 +796,7 @@ protected function maybe_fix_fields_with_invalid_conditional_logic( Pod $pod, $m ] ); foreach ( $fields as $field ) { - $fixed_field = $this->maybe_fix_fields_with_invalid_conditional_logic_for_field( $pod, $field, $invalid_arg, $mode ); + $fixed_field = $this->maybe_fix_fields_with_invalid_args_for_field( $pod, $field, $invalid_arg, $mode ); if ( $fixed_field ) { $fixed_fields[] = $fixed_field; @@ -807,7 +808,7 @@ protected function maybe_fix_fields_with_invalid_conditional_logic( Pod $pod, $m } /** - * Maybe fix a field with invalid conditional logic. + * Maybe fix a field with invalid arguments. * * @since 3.0.4 * @@ -818,7 +819,7 @@ protected function maybe_fix_fields_with_invalid_conditional_logic( Pod $pod, $m * * @return string[]|false The label, name, and ID for the field fixed, or false if not fixed. */ - protected function maybe_fix_fields_with_invalid_conditional_logic_for_field( Pod $pod, Field $field, string $invalid_arg, $mode ) { + protected function maybe_fix_fields_with_invalid_args_for_field( Pod $pod, Field $field, string $invalid_arg, $mode ) { $this->setup(); $field_id = $field->get_id(); @@ -830,6 +831,7 @@ protected function maybe_fix_fields_with_invalid_conditional_logic_for_field( Po $invalid_args = [ 'conditional_logic', 'attributes', + 'grouped', 'depends-on', 'depends-on-any', 'depends-on-multi', @@ -838,7 +840,9 @@ protected function maybe_fix_fields_with_invalid_conditional_logic_for_field( Po ]; try { - $found_invalid_args = []; + $found_invalid_args = [ + $invalid_arg => null, + ]; foreach ( $invalid_args as $other_invalid_arg ) { $arg_value = $field->get_arg( $other_invalid_arg, null, false, true ); @@ -871,6 +875,8 @@ protected function maybe_fix_fields_with_invalid_conditional_logic_for_field( Po $field->set_arg( $found_invalid_arg, null ); } + + pods_api()->cache_flush_fields(); } return sprintf( diff --git a/ui/admin/settings-tools.php b/ui/admin/settings-tools.php index a0b7f01979..858812820f 100644 --- a/ui/admin/settings-tools.php +++ b/ui/admin/settings-tools.php @@ -78,6 +78,7 @@
  • +
  • Date: Mon, 25 Sep 2023 09:15:20 -0500 Subject: [PATCH 11/11] Update changelog --- changelog.txt | 3 ++- readme.txt | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/changelog.txt b/changelog.txt index 59d9bc33d5..6f8b33a105 100644 --- a/changelog.txt +++ b/changelog.txt @@ -2,9 +2,10 @@ Found a bug? Have a great feature idea? Get on GitHub and tell us about it and w Our GitHub has the full list of all prior releases of Pods: https://github.com/pods-framework/pods/releases -= 3.0.4 - September 23rd, 2023 = += 3.0.4 - September 25th, 2023 = * Fixed: Resolve bidirectional removal issue from Pods 2.x where bidirectional relationships would not have the current item removed when you removed that related item. (@sc0ttkclark) +* Fixed: Added repair tool to address the potential for invalid conditional logic saved to the DB in early Pods 3.0 releases or future cases, this is when conditional logic stores a rule that ends up being a long serialized PHP string that contains a serialized PHP array. (@sc0ttkclark) = 3.0.3 - September 22nd, 2023 = diff --git a/readme.txt b/readme.txt index 98b8e91a3c..e1d3061d33 100644 --- a/readme.txt +++ b/readme.txt @@ -177,9 +177,10 @@ Pods really wouldn't be where it is without all the contributions from our [dono == Changelog == -= 3.0.4 - September 23rd, 2023 = += 3.0.4 - September 25th, 2023 = * Fixed: Resolve bidirectional removal issue from Pods 2.x where bidirectional relationships would not have the current item removed when you removed that related item. (@sc0ttkclark) +* Fixed: Added repair tool to address the potential for invalid conditional logic saved to the DB in early Pods 3.0 releases or future cases, this is when conditional logic stores a rule that ends up being a long serialized PHP string that contains a serialized PHP array. (@sc0ttkclark) = 3.0.3 - September 22nd, 2023 =