From c5422aeb20f068a173d92bdb04ca2608a3d05798 Mon Sep 17 00:00:00 2001
From: Scott Kingsley Clark <scott@skc.dev>
Date: Sun, 24 Sep 2023 15:13:26 -0500
Subject: [PATCH] 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(