Skip to content

Commit bd053eb

Browse files
committed
Support new dt-import non-overwrite setting
1 parent 56584c0 commit bd053eb

3 files changed

Lines changed: 115 additions & 3 deletions

File tree

dt-posts/dt-posts-endpoints.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,8 +590,10 @@ public function create_post( WP_REST_Request $request ) {
590590
$get_params = $request->get_query_params();
591591
$silent = isset( $get_params['silent'] ) && $get_params['silent'] === 'true';
592592
$check_dups = ! empty( $get_params['check_for_duplicates'] ) ? explode( ',', $get_params['check_for_duplicates'] ) : [];
593+
$overwrite_existing_fields = isset( $get_params['overwrite_existing_fields'] ) && $get_params['overwrite_existing_fields'] === 'true';
593594
$post = DT_Posts::create_post( $url_params['post_type'], $fields, $silent, true, [
594-
'check_for_duplicates' => $check_dups
595+
'check_for_duplicates' => $check_dups,
596+
'overwrite_existing_fields' => $overwrite_existing_fields
595597
] );
596598
return $post;
597599
}

dt-posts/dt-posts-hooks.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

dt-posts/dt-posts.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,25 @@ public static function create_post( string $post_type, array $fields, bool $sile
8484
if ( isset( $args['check_for_duplicates'] ) && is_array( $args['check_for_duplicates'] ) && ! empty( $args['check_for_duplicates'] ) ) {
8585
$duplicate_post_ids = apply_filters( 'dt_create_check_for_duplicate_posts', [], $post_type, $fields, $args['check_for_duplicates'], $check_permissions );
8686
if ( ! empty( $duplicate_post_ids ) && count( $duplicate_post_ids ) > 0 ) {
87+
$duplicate_post_id = $duplicate_post_ids[0];
8788

8889
$name = $fields['name'] ?? $fields['title'];
8990

9091
$fields['notes'] = isset( $fields['notes'] ) ? $fields['notes'] : [];
9192
//No need to update title or name.
92-
unset( $fields['title'], $fields['name'] );
93+
unset( $fields['title'], $fields['name'], $fields['ID'] );
94+
95+
/**
96+
* If field overwrite has been disabled for existing fields, then ensure
97+
* to have them removed from importing fields.
98+
*/
99+
100+
if ( isset( $args['overwrite_existing_fields'] ) && !$args['overwrite_existing_fields'] ) {
101+
$fields = apply_filters( 'dt_ignore_duplicated_post_fields', [], $fields, $post_type, $duplicate_post_id );
102+
}
93103

94104
//update most recently created matched post.
95-
$updated_post = self::update_post( $post_type, $duplicate_post_ids[0], $fields, $silent, false );
105+
$updated_post = self::update_post( $post_type, $duplicate_post_id, $fields, $silent, false );
96106
if ( is_wp_error( $updated_post ) ){
97107
return $updated_post;
98108
}

0 commit comments

Comments
 (0)