Skip to content

Commit dad1f61

Browse files
authored
Duplicates: don't show already closed duplicate contacts (#2750)
* Enhance duplicate checking by adding 'ignore_archived' parameter to API calls - Updated the API call in contacts.js to include an 'ignore_archived' option when fetching duplicates. - Modified the duplicates-merging.php to handle the new parameter, allowing for more refined duplicate searches by excluding archived records. * Refactor API call formatting in contacts.js for improved readability - Updated the formatting of the API call to get duplicates, enhancing code clarity by aligning parameters and removing unnecessary line breaks. * Refactor duplicate checking API calls to remove 'ignore_archived' parameter - Simplified the API call in contacts.js by removing the 'ignore_archived' parameter when fetching duplicates. - Updated duplicates-merging.php to reflect this change, ensuring consistent handling of duplicate searches without the archived status filter. * Refactor API call formatting in contacts.js for improved readability - Enhanced the formatting of the API call to get duplicates by aligning parameters and removing unnecessary line breaks for better clarity. - Updated the promise handling to improve code structure and maintainability. * Refactor duplicate search logic in duplicates-merging.php - Updated the method for querying duplicate searches to use get_post_field_settings for improved clarity. - Simplified the construction of search queries by directly assigning values to the search_query array. - Added logic to filter out already merged records from the duplicate checks, ensuring that only relevant duplicates are processed. - Enhanced SQL query to exclude posts marked as 'duplicate' in the reason_closed field, improving the accuracy of duplicate detection. * Refactor condition check in duplicates-merging.php for improved readability - Updated the condition for excluding merged records to enhance clarity by adjusting spacing in the array syntax. - Ensured consistent formatting in the logic that filters out already processed duplicates. * fix phpcs
1 parent 363c33e commit dad1f61

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

dt-contacts/duplicates-merging.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ public static function ids_of_non_dismissed_duplicates( $post_type, $post_id, $e
141141
if ( is_wp_error( $post ) ){
142142
return $post;
143143
}
144+
global $wpdb;
144145
$search_query = self::query_for_duplicate_searches( $post_type, $post_id, $exact );
145146
$res = DT_Posts::search_viewable_post( 'contacts', [ $search_query['query'] ] );
146147
if ( is_wp_error( $res ) ){
@@ -156,6 +157,30 @@ public static function ids_of_non_dismissed_duplicates( $post_type, $post_id, $e
156157
//exclude already dismissed duplicates and self
157158
$ids = array_values( array_diff( $ids, array_merge( $dismissed, [ $post_id ] ) ) );
158159

160+
//ignore already merged records
161+
if ( $post_type === 'contacts' && !empty( $ids ) ) {
162+
163+
$ids_sql = dt_array_to_sql( $ids );
164+
$sql = "
165+
SELECT DISTINCT p.ID as post_id
166+
FROM $wpdb->posts p
167+
WHERE p.ID IN ( $ids_sql )
168+
AND p.post_type = 'contacts'
169+
AND NOT EXISTS (
170+
SELECT 1
171+
FROM $wpdb->postmeta pm
172+
WHERE pm.post_id = p.ID
173+
AND pm.meta_key = 'reason_closed'
174+
AND pm.meta_value = 'duplicate'
175+
)
176+
";
177+
178+
$filtered_ids = $wpdb->get_results( $sql, ARRAY_A ); //phpcs:ignore
179+
180+
// Update the ids array with only the filtered results
181+
$ids = array_column( $filtered_ids, 'post_id' );
182+
}
183+
159184
return [
160185
'ids' => $ids
161186
];
@@ -199,6 +224,9 @@ public static function get_all_duplicates_on_post( $post_type, $post_id ){
199224
if ( $possible_duplicate['ID'] === $post_id || in_array( $possible_duplicate['ID'], $ids ) ){
200225
continue; // exclude self and records already processed
201226
}
227+
if ( isset( $possible_duplicate['overall_status']['key'], $possible_duplicate['reason_closed']['key'] ) && in_array( $possible_duplicate['overall_status']['key'], [ 'closed' ] ) && in_array( $possible_duplicate['reason_closed']['key'], [ 'duplicate' ] ) ) {
228+
continue; // exclude merged records
229+
}
202230
$ids[] = $possible_duplicate['ID'];
203231
$match_on = [];
204232
$points = 0;

0 commit comments

Comments
 (0)