' . esc_html__( 'Merging contacts is not switched on for you. Ask an administrator (Settings → Team).', 'nodebook' ) . '
';
Nodebook_Admin::footer();
return;
}
$pairs = Nodebook_Merge::find( 50 );
if ( Nodebook_Team::restricted() ) {
// Only pairs where both are theirs to see.
$pairs = array_values(
array_filter(
$pairs,
static function ( $p ) {
return Nodebook_Team::can_see( $p['a'] ) && Nodebook_Team::can_see( $p['b'] );
}
)
);
} else {
set_transient( 'nodebook_dupe_count', count( $pairs ), HOUR_IN_SECONDS );
}
Nodebook_Admin::header( __( 'Duplicates', 'nodebook' ) );
echo '' . esc_html__( 'People often reach you twice: a work and a private email, a phone call logged by hand, a typo. Merging keeps one contact with the complete journey. The address of the merged contact is remembered, so future forms and orders from it land on the same contact.', 'nodebook' ) . '
';
if ( ! $pairs ) {
echo '' . esc_html__( 'No likely duplicates found. Nodebook checks for the same email (ignoring Gmail dots and +tags), phone number, WordPress user, and full name combined with the same company.', 'nodebook' ) . '
';
Nodebook_Admin::footer();
return;
}
foreach ( $pairs as $p ) {
$a = $p['a'];
$b = $p['b'];
echo '' . esc_html( implode( ' · ', $p['reasons'] ) ) . '
';
foreach ( array( array( $a, $b ), array( $b, $a ) ) as $side ) {
list( $keep, $other ) = $side;
echo '
';
echo '
';
$rows = array(
__( 'Email', 'nodebook' ) => $keep['email'],
__( 'Phone', 'nodebook' ) => $keep['phone'],
__( 'Status', 'nodebook' ) => Nodebook_Fields::statuses()[ $keep['status'] ] ?? $keep['status'],
__( 'Captured via', 'nodebook' ) => $keep['source'],
__( 'Visits', 'nodebook' ) => number_format_i18n( (int) $keep['visits_count'] ),
__( 'Orders', 'nodebook' ) => number_format_i18n( (int) $keep['orders_count'] ),
);
foreach ( $rows as $k => $v ) {
echo '- ' . esc_html( $k ) . '
- ' . ( '' !== (string) $v ? esc_html( $v ) : '—' ) . '
';
}
echo '- ' . esc_html__( 'Created', 'nodebook' ) . '
- ' . wp_kses( Nodebook_Admin::ago( $keep['created_at'] ), Nodebook_Admin::KSES ) . '
';
echo '
';
echo '
';
}
echo '
';
}
Nodebook_Admin::footer();
}
public static function merge() {
Nodebook_Admin::require_cap();
check_admin_referer( 'nodebook_merge' );
$keep = isset( $_POST['keep'] ) ? absint( $_POST['keep'] ) : 0;
$other = isset( $_POST['other'] ) ? trim( sanitize_text_field( wp_unslash( $_POST['other'] ) ) ) : '';
$back = isset( $_POST['back'] ) && 'duplicates' === $_POST['back'];
$other_id = 0;
if ( ctype_digit( $other ) ) {
$other_id = (int) $other;
} elseif ( is_email( $other ) ) {
$found = Nodebook_Contacts::find_by_email( $other );
$other_id = $found ? (int) $found['id'] : 0;
}
$ok = $other_id && Nodebook_Merge::merge( $keep, $other_id );
delete_transient( 'nodebook_dupe_count' );
if ( $back && $ok ) {
Nodebook_Admin::redirect( Nodebook_Admin::url( 'nodebook-duplicates' ), 'merged' );
}
Nodebook_Admin::redirect( Nodebook_Admin::url( 'nodebook', array( 'action' => 'view', 'id' => $keep ) ), $ok ? 'merged' : 'mergefail' );
}
public static function dismiss() {
Nodebook_Admin::require_cap();
check_admin_referer( 'nodebook_dismiss_dupe' );
$a = isset( $_POST['a'] ) ? absint( $_POST['a'] ) : 0;
$b = isset( $_POST['b'] ) ? absint( $_POST['b'] ) : 0;
if ( $a && $b ) {
Nodebook_Merge::dismiss( $a, $b );
}
delete_transient( 'nodebook_dupe_count' );
Nodebook_Admin::redirect( Nodebook_Admin::url( 'nodebook-duplicates' ), 'saved' );
}
}