@@ -7,6 +7,7 @@ public function __construct() {
77 add_action ( 'post_connection_removed ' , [ $ this , 'post_connection_removed ' ], 10 , 4 );
88 add_action ( 'post_connection_added ' , [ $ this , 'post_connection_added ' ], 10 , 4 );
99 add_filter ( 'dt_create_check_for_duplicate_posts ' , [ $ this , 'dt_create_check_for_duplicate_posts ' ], 10 , 5 );
10+ add_filter ( 'dt_ignore_duplicated_post_fields ' , [ $ this , 'dt_ignore_duplicated_post_fields ' ], 10 , 4 );
1011 }
1112
1213 /**
@@ -219,4 +220,103 @@ public static function dt_create_check_for_duplicate_posts( $duplicates, $post_t
219220
220221 return $ duplicates ;
221222 }
223+
224+ /**
225+ * Remove duplicated field values from specified post record.
226+ *
227+ * @param $updated_fields
228+ * @param $fields
229+ * @param $post_type
230+ * @param $post_id
231+ */
232+ public static function dt_ignore_duplicated_post_fields ( $ updated_fields , $ fields , $ post_type , $ post_id ) {
233+ $ existing_fields = DT_Posts::get_post ( $ post_type , $ post_id );
234+ if ( !empty ( $ existing_fields ) && !is_wp_error ( $ existing_fields ) ) {
235+ $ field_settings = DT_Posts::get_post_field_settings ( $ post_type );
236+ foreach ( $ fields as $ field_key => $ field_value ) {
237+ if ( isset ( $ existing_fields [ $ field_key ], $ field_settings [ $ field_key ]['type ' ] ) ) {
238+ $ field_type = $ field_settings [ $ field_key ]['type ' ];
239+ switch ( $ field_type ) {
240+ case 'text ' :
241+ case 'textarea ' :
242+ case 'boolean ' :
243+ if ( $ field_value != $ existing_fields [ $ field_key ] ) {
244+ $ updated_fields [ $ field_key ] = $ field_value ;
245+ }
246+ break ;
247+ case 'date ' :
248+ case 'key_select ' :
249+ $ key = 'key ' ;
250+ if ( $ field_type == 'date ' ) {
251+ $ key = 'formatted ' ;
252+ }
253+ if ( !( isset ( $ existing_fields [ $ field_key ][ $ key ] ) && $ existing_fields [ $ field_key ][ $ key ] == $ field_value ) ) {
254+ $ updated_fields [ $ field_key ] = $ field_value ;
255+ }
256+ break ;
257+ case 'multi_select ' :
258+ $ values = [];
259+ foreach ( $ field_value ['values ' ] ?? [] as $ value ) {
260+ if ( !( isset ( $ value ['value ' ] ) && in_array ( $ value ['value ' ], $ existing_fields [ $ field_key ] ) ) ) {
261+ $ values [] = $ value ;
262+ }
263+ }
264+ if ( !empty ( $ values ) ) {
265+ $ updated_fields [ $ field_key ] = [
266+ 'values ' => $ values ,
267+ ];
268+ }
269+ break ;
270+ case 'location ' :
271+ case 'location_meta ' :
272+ $ values = [];
273+ foreach ( $ field_value ['values ' ] ?? [] as $ value ) {
274+ $ key = 'label ' ;
275+ $ found = array_filter ( $ existing_fields [ $ field_key ], function ( $ option ) use ( $ value , $ key ) {
276+ $ hit = isset ( $ option [$ key ] ) && $ option [$ key ] == $ value ['value ' ];
277+
278+ if ( !$ hit && isset ( $ option ['matched_search ' ] ) && $ option ['matched_search ' ] == $ value ['value ' ] ) {
279+ $ hit = true ;
280+ }
281+
282+ return $ hit ;
283+ } );
284+ if ( empty ( $ found ) || count ( $ found ) == 0 ) {
285+ $ values [] = $ value ;
286+ }
287+ }
288+ if ( !empty ( $ values ) ) {
289+ $ updated_fields [$ field_key ] = [
290+ 'values ' => $ values ,
291+ ];
292+ }
293+ break ;
294+ case 'communication_channel ' :
295+ $ values = [];
296+ foreach ( $ field_value ?? [] as $ value ) {
297+ $ key = 'value ' ;
298+ $ found = array_filter ( $ existing_fields [ $ field_key ], function ( $ option ) use ( $ value , $ key ) {
299+ return isset ( $ option [$ key ] ) && $ option [$ key ] == $ value ['value ' ];
300+ } );
301+
302+ if ( empty ( $ found ) || count ( $ found ) == 0 ) {
303+ $ values [] = $ value ;
304+ }
305+ }
306+ if ( !empty ( $ values ) ) {
307+ $ updated_fields [ $ field_key ] = $ values ;
308+ }
309+ break ;
310+ default :
311+ $ updated_fields [ $ field_key ] = $ field_value ;
312+ break ;
313+ }
314+ } else {
315+ $ updated_fields [ $ field_key ] = $ field_value ;
316+ }
317+ }
318+ }
319+
320+ return $ updated_fields ;
321+ }
222322}
0 commit comments