Skip to content

Support New DT-Import Non-Overwrite Setting #2668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion dt-posts/dt-posts-endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,10 @@ public function create_post( WP_REST_Request $request ) {
$get_params = $request->get_query_params();
$silent = isset( $get_params['silent'] ) && $get_params['silent'] === 'true';
$check_dups = ! empty( $get_params['check_for_duplicates'] ) ? explode( ',', $get_params['check_for_duplicates'] ) : [];
$overwrite_existing_fields = isset( $get_params['overwrite_existing_fields'] ) && $get_params['overwrite_existing_fields'] === 'true';
$post = DT_Posts::create_post( $url_params['post_type'], $fields, $silent, true, [
'check_for_duplicates' => $check_dups
'check_for_duplicates' => $check_dups,
'overwrite_existing_fields' => $overwrite_existing_fields
] );
return $post;
}
Expand Down
100 changes: 100 additions & 0 deletions dt-posts/dt-posts-hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public function __construct() {
add_action( 'post_connection_removed', [ $this, 'post_connection_removed' ], 10, 4 );
add_action( 'post_connection_added', [ $this, 'post_connection_added' ], 10, 4 );
add_filter( 'dt_create_check_for_duplicate_posts', [ $this, 'dt_create_check_for_duplicate_posts' ], 10, 5 );
add_filter( 'dt_ignore_duplicated_post_fields', [ $this, 'dt_ignore_duplicated_post_fields' ], 10, 4 );
}

/**
Expand Down Expand Up @@ -219,4 +220,103 @@ public static function dt_create_check_for_duplicate_posts( $duplicates, $post_t

return $duplicates;
}

/**
* Remove duplicated field values from specified post record.
*
* @param $updated_fields
* @param $fields
* @param $post_type
* @param $post_id
*/
public static function dt_ignore_duplicated_post_fields( $updated_fields, $fields, $post_type, $post_id ) {
$existing_fields = DT_Posts::get_post( $post_type, $post_id );
if ( !empty( $existing_fields ) && !is_wp_error( $existing_fields ) ) {
$field_settings = DT_Posts::get_post_field_settings( $post_type );
foreach ( $fields as $field_key => $field_value ) {
if ( isset( $existing_fields[ $field_key ], $field_settings[ $field_key ]['type'] ) ) {
$field_type = $field_settings[ $field_key ]['type'];
switch ( $field_type ) {
case 'text':
case 'textarea':
case 'boolean':
if ( $field_value != $existing_fields[ $field_key ] ) {
$updated_fields[ $field_key ] = $field_value;
}
break;
case 'date':
case 'key_select':
$key = 'key';
if ( $field_type == 'date' ) {
$key = 'formatted';
}
if ( !( isset( $existing_fields[ $field_key ][ $key ] ) && $existing_fields[ $field_key ][ $key ] == $field_value ) ) {
$updated_fields[ $field_key ] = $field_value;
}
break;
case 'multi_select':
$values = [];
foreach ( $field_value['values'] ?? [] as $value ) {
if ( !( isset( $value['value'] ) && in_array( $value['value'], $existing_fields[ $field_key ] ) ) ) {
$values[] = $value;
}
}
if ( !empty( $values ) ) {
$updated_fields[ $field_key ] = [
'values' => $values,
];
}
break;
case 'location':
case 'location_meta':
$values = [];
foreach ( $field_value['values'] ?? [] as $value ) {
$key = 'label';
$found = array_filter( $existing_fields[ $field_key ], function ( $option ) use ( $value, $key ) {
$hit = isset( $option[$key] ) && $option[$key] == $value['value'];

if ( !$hit && isset( $option['matched_search'] ) && $option['matched_search'] == $value['value'] ) {
$hit = true;
}

return $hit;
} );
if ( empty( $found ) || count( $found ) == 0 ) {
$values[] = $value;
}
}
if ( !empty( $values ) ) {
$updated_fields[$field_key] = [
'values' => $values,
];
}
break;
case 'communication_channel':
$values = [];
foreach ( $field_value ?? [] as $value ) {
$key = 'value';
$found = array_filter( $existing_fields[ $field_key ], function ( $option ) use ( $value, $key ) {
return isset( $option[$key] ) && $option[$key] == $value['value'];
} );

if ( empty( $found ) || count( $found ) == 0 ) {
$values[] = $value;
}
}
if ( !empty( $values ) ) {
$updated_fields[ $field_key ] = $values;
}
break;
default:
$updated_fields[ $field_key ] = $field_value;
break;
}
} else {
$updated_fields[ $field_key ] = $field_value;
}
}
}

return $updated_fields;
}
}
14 changes: 12 additions & 2 deletions dt-posts/dt-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,25 @@ public static function create_post( string $post_type, array $fields, bool $sile
if ( isset( $args['check_for_duplicates'] ) && is_array( $args['check_for_duplicates'] ) && ! empty( $args['check_for_duplicates'] ) ) {
$duplicate_post_ids = apply_filters( 'dt_create_check_for_duplicate_posts', [], $post_type, $fields, $args['check_for_duplicates'], $check_permissions );
if ( ! empty( $duplicate_post_ids ) && count( $duplicate_post_ids ) > 0 ) {
$duplicate_post_id = $duplicate_post_ids[0];

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

$fields['notes'] = isset( $fields['notes'] ) ? $fields['notes'] : [];
//No need to update title or name.
unset( $fields['title'], $fields['name'] );
unset( $fields['title'], $fields['name'], $fields['ID'] );

/**
* If field overwrite has been disabled for existing fields, then ensure
* to have them removed from importing fields.
*/

if ( isset( $args['overwrite_existing_fields'] ) && !$args['overwrite_existing_fields'] ) {
$fields = apply_filters( 'dt_ignore_duplicated_post_fields', [], $fields, $post_type, $duplicate_post_id );
}

//update most recently created matched post.
$updated_post = self::update_post( $post_type, $duplicate_post_ids[0], $fields, $silent, false );
$updated_post = self::update_post( $post_type, $duplicate_post_id, $fields, $silent, false );
if ( is_wp_error( $updated_post ) ){
return $updated_post;
}
Expand Down