rules = $rules; $this->threshold = (int) $threshold; } /* ------------------------------------------------------------------ tables */ /** * Keys table name for this site. * * @since 1.0.0 * @return string */ public static function table() { global $wpdb; return $wpdb->prefix . 'apwc_dupe_keys'; } /** * People (scoring profiles) table name for this site. * * @since 1.0.0 * @return string */ public static function people_table() { global $wpdb; return $wpdb->prefix . 'apwc_dupe_people'; } /** * Pairs table name for this site. * * @since 1.0.0 * @return string */ public static function pairs_table() { global $wpdb; return $wpdb->prefix . 'apwc_dupe_pairs'; } /** * All work tables. * * @since 1.0.0 * @return string[] */ public static function tables() { return array( self::table(), self::people_table(), self::pairs_table() ); } /** * Whether the work tables have the current schema. * * @since 1.0.0 * @return bool */ public static function ready() { return (int) get_option( self::VERSION, 0 ) >= self::DB; } /** * Create the work tables (or rebuild them when their schema is old). They only ever hold * data of a scan in progress, so an old version is dropped, not migrated. * * Runs when the power-up is switched on and at the start of a scan (a dedicated admin * request): schema statements commit open transactions, so never elsewhere. * * @since 1.0.0 * @return void */ public static function install() { global $wpdb; if ( self::ready() ) { return; } self::drop_tables(); require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $collate = $wpdb->get_charset_collate(); dbDelta( array( 'CREATE TABLE ' . self::table() . " ( user_id bigint(20) unsigned NOT NULL, kind char(1) NOT NULL, keyval varchar(191) NOT NULL, PRIMARY KEY (kind,keyval,user_id), KEY user_id (user_id) ) {$collate};", 'CREATE TABLE ' . self::people_table() . " ( user_id bigint(20) unsigned NOT NULL, profile text NOT NULL, PRIMARY KEY (user_id) ) {$collate};", 'CREATE TABLE ' . self::pairs_table() . " ( user_a bigint(20) unsigned NOT NULL, user_b bigint(20) unsigned NOT NULL, score tinyint(3) unsigned NOT NULL DEFAULT 0, reasons tinyint(3) unsigned NOT NULL DEFAULT 0, PRIMARY KEY (user_a,user_b), KEY score (score) ) {$collate};", ) ); update_option( self::VERSION, self::DB, false ); } /** * Empty the work tables (DELETE, not TRUNCATE: no schema statement mid-request). * * @since 1.0.0 * @return void */ public static function reset() { global $wpdb; foreach ( self::tables() as $table ) { $wpdb->query( $wpdb->prepare( 'DELETE FROM %i', $table ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- our work table; nothing to cache. } } /** * Drop the work tables and forget their version (Data section and uninstall). * * @since 1.0.0 * @return void */ public static function drop() { self::drop_tables(); delete_option( self::VERSION ); } /** * Drop the work tables. * * @since 1.0.0 * @return void */ private static function drop_tables() { global $wpdb; foreach ( self::tables() as $table ) { $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $table ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- dropping our own work table (install and cleanup only). } } /* ------------------------------------------------------------------ state and lock */ /** * Start a scan: install/heal the tables, empty them and save a new state with a snapshot of * the settings. Call while holding the lock. * * @since 1.0.0 * @param string[] $rules Match rules. * @param int $threshold Fuzzy threshold. * @return array State. */ public static function begin( array $rules, $threshold ) { self::install(); self::reset(); $state = array( 'token' => wp_generate_password( 20, false ), 'phase' => 'index', 'after' => 0, 'read' => 0, 'total' => self::total(), 'cursor' => array( '', '' ), 'blocks_done' => 0, 'blocks' => -1, 'rules' => array_values( array_intersect( array( 'email', 'phone', 'address', 'name_address' ), array_map( 'strval', $rules ) ) ), 'threshold' => max( 0, min( 100, (int) $threshold ) ), 'started' => time(), ); update_option( self::TOKEN, $state, false ); return $state; } /** * The scan in progress, or null (none, or a state saved by an older version). * * @since 1.0.0 * @return array|null */ public static function state() { $state = get_option( self::TOKEN ); return is_array( $state ) && isset( $state['token'], $state['phase'] ) && is_string( $state['token'] ) ? $state : null; } /** * Whether a step belongs to the scan in progress. A token left by an older version (a plain * string) never matches. * * @since 1.0.0 * @param string $token Token sent by the step. * @return bool */ public static function is_current( $token ) { $state = self::state(); return $state && '' !== $state['token'] && hash_equals( $state['token'], (string) $token ); } /** * Take the step lock (shared `Support\Lock`, option `apwc_dupe_lock`, LOCK_TTL seconds). * Returns the lock value to release, or '' when another step holds it. * * @since 1.0.0 * @return string */ public static function lock() { return Lock::acquire( self::LOCK, self::LOCK_TTL ); } /** * Release the step lock (only our own). * * @since 1.0.0 * @param string $value Value returned by lock(). * @return void */ public static function unlock( $value ) { Lock::release( self::LOCK, (string) $value ); } /* ------------------------------------------------------------------ limits */ /** * Customers read per index step (10–5000). * * @since 1.0.0 * @return int */ public static function batch() { /** * Filter how many customers one scan step reads (lower it on slow hosts). Clamped to 10–5000. * * @since 1.0.0 * @param int $size Batch size. */ return max( 10, min( 5000, (int) apply_filters( 'apwc_duplicate_scan_batch', self::BATCH ) ) ); } /** * Blocks read per match step (1–1000). * * @since 1.0.0 * @return int */ public static function match_batch() { /** * Filter how many blocks (customers sharing a match key) one match step reads. Clamped to 1–1000. * * @since 1.0.0 * @param int $blocks Blocks per step. */ return max( 1, min( 1000, (int) apply_filters( 'apwc_duplicate_match_batch', self::MATCH_BATCH ) ) ); } /** * Pair comparisons per match step (100–50000). * * @since 1.0.0 * @return int */ public static function match_budget() { /** * Filter how many pairs one match step compares before it stops (it always finishes at least * one block). Clamped to 100–50000. * * @since 1.0.0 * @param int $pairs Comparisons per step. */ return max( 100, min( 50000, (int) apply_filters( 'apwc_duplicate_match_budget', self::MATCH_BUDGET ) ) ); } /** * Rows deleted per query when the work tables are emptied (1000–200000). * * @since 1.0.0 * @return int */ public static function cleanup_batch() { /** * Filter how many work-table rows one DELETE removes while a finished scan cleans up (lower * it when big deletes slow the database). Clamped to 1000–200000. * * @since 1.0.0 * @param int $rows Rows per DELETE. */ return max( 1000, min( 200000, (int) apply_filters( 'apwc_duplicate_cleanup_batch', self::CLEANUP_BATCH ) ) ); } /** * Seconds one cleanup step may spend deleting (0–30; it always runs one DELETE per table it * reaches, so 0 means one DELETE per step). * * @since 1.0.0 * @return float */ public static function cleanup_seconds() { /** * Filter how long one cleanup step keeps deleting work-table rows before it answers and the * next step carries on. Clamped to 0–30 seconds. * * @since 1.0.0 * @param float $seconds Seconds per step. */ return max( 0.0, min( 30.0, (float) apply_filters( 'apwc_duplicate_cleanup_seconds', self::CLEANUP_SECONDS ) ) ); } /** * Most pairs grouped (1–200000). With more, the strongest are kept and results say so. * * @since 1.0.0 * @return int */ public static function max_pairs() { /** * Filter how many matching pairs the last scan step groups. With more, the strongest pairs are * kept and the results are marked truncated. Clamped to 1–200000. * * @since 1.0.0 * @param int $pairs Most pairs. */ return max( 1, min( 200000, (int) apply_filters( 'apwc_duplicate_max_pairs', self::MAX_PAIRS ) ) ); } /** * Customer roles to scan. * * @since 1.0.0 * @return string[] */ public static function roles() { /** * Filter which roles the duplicate scan looks at. * * @since 1.0.0 * @param string[] $roles Roles. */ return (array) apply_filters( 'apwc_duplicate_customer_roles', array( 'customer' ) ); } /** * How many customers a scan covers. * * @since 1.0.0 * @return int */ public static function total() { $query = new WP_User_Query( array( 'role__in' => self::roles(), 'fields' => 'ID', 'number' => 1, 'count_total' => true, ) ); return (int) $query->get_total(); } /* ------------------------------------------------------------------ steps */ /** * Scanner for a scan in progress (its settings snapshot). * * @since 1.0.0 * @param array $state State. * @return self */ public static function for_state( array $state ) { return new self( (array) ( $state['rules'] ?? array() ), (int) ( $state['threshold'] ?? 80 ) ); } /** * Run one step of a scan and save where it stopped. Call while holding the lock. * * @since 1.0.0 * @param array $state State. * @return array{state: array|null, results: array|null} New state, or the results when the scan finished. */ public function run( array $state ) { switch ( $state['phase'] ) { case 'index': $state = $this->index_step( $state ); break; case 'match': $state = $this->match_step( $state ); break; case 'cleanup': if ( ! self::cleanup_step() ) { break; } delete_option( self::TOKEN ); return array( 'state' => null, 'results' => self::results(), ); default: $state = $this->group_step( $state ); } update_option( self::TOKEN, $state, false ); return array( 'state' => $state, 'results' => null, ); } /** * Index step: the next batch of customers after the last one indexed. * * @since 1.0.0 * @param array $state State. * @return array */ private function index_step( array $state ) { $batch = self::batch(); $ids = self::next_ids( (int) $state['after'], $batch ); if ( $ids ) { cache_users( $ids ); $keys = array(); $people = array(); foreach ( $ids as $id ) { $profile = self::profile( $id ); foreach ( $this->keys( $profile ) as $key ) { $keys[] = array( $id, $key[0], self::clip( $key[1], 191 ) ); } $people[] = array( $id, self::pack( $profile ) ); } // Retry-safe: the primary key ignores keys already indexed; profiles are replaced. self::insert( 'INSERT IGNORE INTO %i (user_id,kind,keyval) VALUES ', self::table(), '(%d,%s,%s)', $keys ); self::insert( 'INSERT INTO %i (user_id,profile) VALUES ', self::people_table(), '(%d,%s)', $people, ' ON DUPLICATE KEY UPDATE profile = VALUES(profile)' ); $state['after'] = max( $ids ); $state['read'] = (int) $state['read'] + count( $ids ); } if ( count( $ids ) < $batch ) { $state['phase'] = 'match'; } return $state; } /** * Customer IDs after a given ID, in ID order (keyset paging: no offset walk). * * @since 1.0.0 * @param int $after Last ID read. * @param int $limit Most IDs. * @return int[] */ private static function next_ids( $after, $limit ) { $after = max( 0, (int) $after ); $query = new WP_User_Query(); $where = static function ( $q ) use ( $query, $after ) { global $wpdb; if ( $q === $query ) { $q->query_where .= $wpdb->prepare( ' AND %i.ID > %d', $wpdb->users, $after ); } }; add_action( 'pre_user_query', $where ); try { $query->prepare_query( array( 'role__in' => self::roles(), 'fields' => 'ID', 'number' => $limit, 'orderby' => 'ID', 'order' => 'ASC', 'count_total' => false, 'apwc_dupe_after' => $after, ) ); $query->query(); } finally { remove_action( 'pre_user_query', $where ); } return array_map( 'intval', (array) $query->get_results() ); } /** * Match step: score the next blocks in key order, within the comparison budget. * * @since 1.0.0 * @param array $state State. * @return array */ private function match_step( array $state ) { global $wpdb; // phpcs:disable WordPress.DB.DirectDatabaseQuery -- our work tables; nothing to cache. if ( (int) $state['blocks'] < 0 ) { $state['blocks'] = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM ( SELECT 1 FROM %i GROUP BY kind, keyval HAVING COUNT(*) BETWEEN 2 AND %d ) blocks', self::table(), self::MAX_BLOCK ) ); } $cursor = array_values( (array) $state['cursor'] ) + array( '', '' ); $kind = in_array( $cursor[0], self::KINDS, true ) ? $cursor[0] : ''; $keyval = '' === $kind ? '' : (string) $cursor[1]; $limit = self::match_batch(); // Each key's users are unique (primary key kind, keyval, user_id), so COUNT(*) counts people. $rows = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT kind, keyval, COUNT(*) AS n, SUM( CHAR_LENGTH( user_id ) ) + COUNT(*) - 1 AS len, GROUP_CONCAT(user_id) AS ids FROM %i WHERE kind > %s OR ( kind = %s AND keyval > %s ) GROUP BY kind, keyval HAVING COUNT(*) BETWEEN 2 AND %d ORDER BY kind, keyval LIMIT %d', self::table(), $kind, $kind, $keyval, self::MAX_BLOCK, $limit ), ARRAY_A ); // phpcs:enable // Blocks this step finishes: within the budget, and always at least one. $budget = self::match_budget(); $spent = 0; $take = array(); foreach ( $rows as $row ) { $n = (int) $row['n']; $cost = intdiv( $n * ( $n - 1 ), 2 ); if ( $take && $spent + $cost > $budget ) { break; } $take[] = $row; $spent += $cost; } $profiles = $this->load_profiles( $take ); $pairs = array(); $seen = array(); foreach ( $take as $row ) { // GROUP_CONCAT cut short (group_concat_max_len): skip the block rather than score part of it. if ( strlen( (string) $row['ids'] ) !== (int) $row['len'] || ! in_array( $row['kind'], self::KINDS, true ) ) { continue; } $ids = array_map( 'intval', explode( ',', (string) $row['ids'] ) ); sort( $ids ); $this->score_block( (string) $row['kind'], $ids, $profiles, $pairs, $seen ); } $this->save_pairs( $pairs ); if ( $take ) { $last = end( $take ); $state['cursor'] = array( (string) $last['kind'], (string) $last['keyval'] ); $state['blocks_done'] = (int) $state['blocks_done'] + count( $take ); } if ( count( $take ) === count( $rows ) && count( $rows ) < $limit ) { $state['phase'] = 'group'; } return $state; } /** * Profiles for the customers in blocks that need them (address and fuzzy blocks). * * @since 1.0.0 * @param array> $blocks Block rows. * @return array> By user ID. */ private function load_profiles( array $blocks ) { global $wpdb; $ids = array(); foreach ( $blocks as $row ) { if ( in_array( $row['kind'], array( 'a', 'n', 'z' ), true ) ) { foreach ( explode( ',', (string) $row['ids'] ) as $id ) { $ids[ (int) $id ] = true; } } } $profiles = array(); foreach ( array_chunk( array_keys( $ids ), 1000 ) as $chunk ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- one %d placeholder built per ID; our work table. $rows = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT user_id, profile FROM %i WHERE user_id IN (' . implode( ',', array_fill( 0, count( $chunk ), '%d' ) ) . ')', self::people_table(), ...$chunk ) ); foreach ( $rows as $row ) { $profiles[ (int) $row->user_id ] = self::unpack( (string) $row->profile ); } } return $profiles; } /** * Score every pair in one block. * * @since 1.0.0 * @param string $kind Key kind. * @param int[] $ids Customers in the block, sorted. * @param array> $profiles Profiles by user ID. * @param array $pairs "a-b" => [ score, reason bits ] (added to). * @param array $seen Fuzzy pairs already scored this step. * @return void */ private function score_block( $kind, array $ids, array $profiles, array &$pairs, array &$seen ) { $count = count( $ids ); for ( $i = 0; $i < $count; $i++ ) { for ( $j = $i + 1; $j < $count; $j++ ) { $a = $ids[ $i ]; $b = $ids[ $j ]; $key = $a . '-' . $b; if ( 'e' === $kind ) { $hit = array( 100, self::EMAIL ); } elseif ( 'p' === $kind ) { $hit = array( 90, self::PHONE ); } elseif ( ! isset( $profiles[ $a ], $profiles[ $b ] ) ) { continue; // Deleted mid-scan: no profile. } elseif ( 'a' === $kind ) { // A shared address alone is often a household: needs the names to be close too. $names = self::person_score( $profiles[ $a ], $profiles[ $b ] ); if ( $names < $this->threshold - 20 ) { continue; } $hit = array( (int) round( 60 + $names * 0.35 ), self::ADDRESS ); } else { if ( isset( $seen[ $key ] ) ) { continue; } $seen[ $key ] = true; $score = $this->fuzzy( $profiles[ $a ], $profiles[ $b ] ); if ( $score < $this->threshold ) { continue; } $hit = array( $score, self::FUZZY ); } $pair = $pairs[ $key ] ?? array( 0, 0 ); $pairs[ $key ] = array( max( $pair[0], $hit[0] ), $pair[1] | $hit[1] ); } } } /** * Upsert scored pairs (a repeated step leaves the same result). * * @since 1.0.0 * @param array $pairs "a-b" => [ score, reason bits ]. * @return void */ private function save_pairs( array $pairs ) { $rows = array(); foreach ( $pairs as $key => $pair ) { list( $a, $b ) = array_map( 'intval', explode( '-', $key ) ); $rows[] = array( $a, $b, min( 100, $pair[0] ), $pair[1] ); } self::insert( 'INSERT INTO %i (user_a,user_b,score,reasons) VALUES ', self::pairs_table(), '(%d,%d,%d,%d)', $rows, ' ON DUPLICATE KEY UPDATE score = GREATEST(score, VALUES(score)), reasons = reasons | VALUES(reasons)' ); } /** * Insert rows into a work table, 500 per query, every value through prepare(). * * @since 1.0.0 * @param string $head Statement up to VALUES, with %i for the table. * @param string $table Table. * @param string $row Placeholders for one row, e.g. "(%d,%s)". * @param array> $rows Rows of values. * @param string $tail Constant SQL after the values. * @return void */ private static function insert( $head, $table, $row, array $rows, $tail = '' ) { global $wpdb; foreach ( array_chunk( $rows, 500 ) as $chunk ) { $sql = $head . implode( ',', array_fill( 0, count( $chunk ), $row ) ) . $tail; // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber, PluginCheck.Security.DirectDB.UnescapedDBParameter -- $sql is constant SQL plus one placeholder group per row; every value is passed to prepare(). $wpdb->query( $wpdb->prepare( $sql, $table, ...array_merge( ...$chunk ) ) ); } } /** * The saved results (an empty set when they were deleted meanwhile). * * @since 1.0.0 * @return array */ public static function results() { $results = get_option( self::RESULTS ); if ( ! is_array( $results ) ) { return array( 'scanned_at' => time(), 'scanned' => 0, 'groups' => array(), 'truncated' => false, ); } $results['groups'] = (array) ( $results['groups'] ?? array() ); return $results; } /** * Cleanup step: empty the work tables in chunks until they are empty or the step's time * (cleanup_seconds(), about a second) has passed. DELETE, never TRUNCATE (no schema statement mid-request). * * @since 1.0.0 * @return bool Whether the work tables are empty. */ private static function cleanup_step() { global $wpdb; $deadline = microtime( true ) + self::cleanup_seconds(); $chunk = self::cleanup_batch(); foreach ( self::tables() as $table ) { do { $deleted = (int) $wpdb->query( $wpdb->prepare( 'DELETE FROM %i LIMIT %d', $table, $chunk ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- our work table; nothing to cache. } while ( $deleted >= $chunk && microtime( true ) < $deadline ); if ( $deleted >= $chunk ) { return false; // Out of time: the next step carries on. } } return true; } /** * Group step: read the pairs, group them and save the results. The work tables are emptied * by the cleanup steps that follow. * * Dismissed pairs are dropped after the pair cap applies, so with more pairs than the cap a * few dismissed ones can take room from weaker matches. * * @since 1.0.0 * @param array $state State. * @return array New state (phase cleanup). */ private function group_step( array $state ) { global $wpdb; $table = self::pairs_table(); $max = self::max_pairs(); $dismissed = array_flip( array_map( 'strval', (array) get_option( self::DISMISSED, array() ) ) ); $pairs = array(); $keep = static function ( array $rows ) use ( &$pairs, $dismissed ) { foreach ( $rows as $row ) { $key = (int) $row->user_a . '-' . (int) $row->user_b; if ( ! isset( $dismissed[ $key ] ) ) { $pairs[ $key ] = array( (int) $row->score, self::reasons( (int) $row->reasons ) ); } } }; // phpcs:disable WordPress.DB.DirectDatabaseQuery -- our work table; nothing to cache. $count = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i', $table ) ); $truncated = $count > $max; if ( ! $truncated ) { $a = 0; $b = 0; do { $rows = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT user_a, user_b, score, reasons FROM %i WHERE user_a > %d OR ( user_a = %d AND user_b > %d ) ORDER BY user_a, user_b LIMIT %d', $table, $a, $a, $b, self::PAIR_CHUNK ) ); $keep( $rows ); $more = self::PAIR_CHUNK === count( $rows ); if ( $more ) { $last = end( $rows ); $a = (int) $last->user_a; $b = (int) $last->user_b; } } while ( $more ); } else { for ( $offset = 0; $offset < $max; $offset += self::PAIR_CHUNK ) { $rows = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT user_a, user_b, score, reasons FROM %i ORDER BY score DESC, user_a, user_b LIMIT %d, %d', $table, $offset, min( self::PAIR_CHUNK, $max - $offset ) ) ); $keep( $rows ); } } // phpcs:enable $results = array( 'scanned_at' => time(), 'scanned' => (int) $state['read'], 'groups' => self::group( $pairs ), 'truncated' => $truncated, ); update_option( self::RESULTS, $results, false ); $state['phase'] = 'cleanup'; return $state; } /* ------------------------------------------------------------------ profiles and keys */ /** * A customer's details, normalized for comparing. * * @since 1.0.0 * @param int $user_id User ID. * @return array{name: string, first: string, last: string, emails: string[], phones: string[], addresses: array} */ public static function profile( $user_id ) { $user = get_userdata( $user_id ); $meta = static fn( $key ) => (string) get_user_meta( $user_id, $key, true ); $name = trim( $meta( 'billing_first_name' ) . ' ' . $meta( 'billing_last_name' ) ); if ( '' === $name ) { $name = trim( $meta( 'first_name' ) . ' ' . $meta( 'last_name' ) ); } if ( '' === $name && $user ) { $name = (string) $user->display_name; } $addresses = array(); foreach ( array( 'billing', 'shipping' ) as $type ) { $line = trim( $meta( "{$type}_address_1" ) . ' ' . $meta( "{$type}_address_2" ) ); if ( '' !== $line ) { $addresses[] = array( 'line' => remove_accents( $line ), 'postcode' => $meta( "{$type}_postcode" ), 'city' => $meta( "{$type}_city" ), ); } } $first = $meta( 'billing_first_name' ) ? $meta( 'billing_first_name' ) : $meta( 'first_name' ); return array( 'name' => remove_accents( $name ), 'first' => remove_accents( $first ), 'last' => remove_accents( $meta( 'billing_last_name' ) ? $meta( 'billing_last_name' ) : $meta( 'last_name' ) ), 'emails' => array_values( array_unique( array_filter( array( Similarity::email( $user ? $user->user_email : '' ), Similarity::email( $meta( 'billing_email' ) ) ) ) ) ), 'phones' => array_values( array_unique( array_filter( array( Similarity::phone( $meta( 'billing_phone' ) ), Similarity::phone( $meta( 'shipping_phone' ) ) ) ) ) ), 'addresses' => $addresses, ); } /** * What scoring needs from a profile, as JSON for the people table. * * @since 1.0.0 * @param array $profile Profile from profile(). * @return string */ private static function pack( array $profile ) { $addresses = array(); foreach ( (array) $profile['addresses'] as $address ) { $addresses[] = array( self::clip( $address['line'], 200 ), self::clip( $address['postcode'], 40 ) ); } return (string) wp_json_encode( array( 'n' => self::clip( $profile['name'], 200 ), 'f' => self::clip( $profile['first'], 100 ), 'l' => self::clip( $profile['last'], 100 ), 'ad' => $addresses, ), JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE ); } /** * A profile read back from the people table. * * @since 1.0.0 * @param string $json JSON from pack(). * @return array{name: string, first: string, last: string, addresses: array} */ private static function unpack( $json ) { $data = json_decode( $json, true ); $data = is_array( $data ) ? $data : array(); $addresses = array(); foreach ( (array) ( $data['ad'] ?? array() ) as $address ) { $addresses[] = array( 'line' => (string) ( $address[0] ?? '' ), 'postcode' => (string) ( $address[1] ?? '' ), ); } return array( 'name' => (string) ( $data['n'] ?? '' ), 'first' => (string) ( $data['f'] ?? '' ), 'last' => (string) ( $data['l'] ?? '' ), 'addresses' => $addresses, ); } /** * Valid UTF-8, at most $length characters (never cuts a character in half). * * @since 1.0.0 * @param mixed $text Text. * @param int $length Most characters. * @return string */ private static function clip( $text, $length ) { return mb_substr( wp_check_invalid_utf8( (string) $text, true ), 0, $length, 'UTF-8' ); } /** * Match keys for a customer: [ kind, value ]. * * Kinds: e = email, p = phone, a = exact address, z = postcode block, n = city + surname sound block. * * @since 1.0.0 * @param array $p Profile from profile(). * @return array */ public function keys( array $p ) { $keys = array(); if ( in_array( 'email', $this->rules, true ) ) { foreach ( $p['emails'] as $email ) { $keys[] = array( 'e', $email ); } } if ( in_array( 'phone', $this->rules, true ) ) { foreach ( $p['phones'] as $phone ) { $keys[] = array( 'p', $phone ); } } foreach ( $p['addresses'] as $address ) { $street = Similarity::address( $address['line'] ); $postcode = Similarity::postcode( $address['postcode'] ); if ( in_array( 'address', $this->rules, true ) && '' !== $street ) { $keys[] = array( 'a', $street . '|' . $postcode ); } if ( in_array( 'name_address', $this->rules, true ) && '' !== $p['name'] ) { if ( '' !== $postcode ) { $keys[] = array( 'z', $postcode ); } $sound = Similarity::sound( $p['last'] ); $city = implode( ' ', Similarity::words( $address['city'] ) ); if ( '' !== $sound && '' !== $city ) { $keys[] = array( 'n', $city . '|' . $sound ); } } } return array_values( array_unique( $keys, SORT_REGULAR ) ); } /* ------------------------------------------------------------------ scoring */ /** * Name + address score: the best address pair, weighted 60/40 name/address. * * @since 1.0.0 * @param array $a Profile. * @param array $b Profile. * @return int */ public function fuzzy( array $a, array $b ) { $names = self::person_score( $a, $b ); if ( $names < 60 ) { return 0; } $best = 0; foreach ( $a['addresses'] as $x ) { foreach ( $b['addresses'] as $y ) { $best = max( $best, Similarity::address_score( $x['line'], $x['postcode'], $y['line'], $y['postcode'] ) ); } } return $best ? (int) round( $names * 0.6 + $best * 0.4 ) : 0; } /** * How alike two people's names are. When surnames sound alike, only first names count: * family members share a surname and an address, so the surname is no evidence. * * @since 1.0.0 * @param array $a Profile. * @param array $b Profile. * @return int */ public static function person_score( array $a, array $b ) { $same_surname = self::same_surname( (string) $a['last'], (string) $b['last'] ); if ( $same_surname && '' !== $a['first'] && '' !== $b['first'] ) { return Similarity::name_score( $a['first'], $b['first'] ); } return Similarity::name_score( $a['name'], $b['name'] ); } /** * Whether two surnames sound alike. metaphone() only knows Latin letters and returns '' for * Cyrillic, Greek, CJK and so on, so without a sound the words must match exactly instead. * * @since 1.0.0 * @param string $a Surname. * @param string $b Surname. * @return bool */ public static function same_surname( $a, $b ) { $sound = Similarity::sound( $a ); if ( '' !== $sound ) { return Similarity::sound( $b ) === $sound; } $lower = static fn( $text ) => function_exists( 'mb_strtolower' ) ? mb_strtolower( $text, 'UTF-8' ) : $text; $words = Similarity::words( $lower( $a ) ); return $words && Similarity::words( $lower( $b ) ) === $words; } /** * Reason labels for a pair's reason bits, in display order. * * @since 1.0.0 * @param int $bits Reason bits. * @return string[] */ public static function reasons( $bits ) { $labels = array( self::EMAIL => __( 'Same email', 'admin-powerups-for-woocommerce' ), self::PHONE => __( 'Same phone', 'admin-powerups-for-woocommerce' ), self::ADDRESS => __( 'Same address', 'admin-powerups-for-woocommerce' ), self::FUZZY => __( 'Similar name and address', 'admin-powerups-for-woocommerce' ), ); $out = array(); foreach ( $labels as $bit => $label ) { if ( $bits & $bit ) { $out[] = $label; } } return $out; } /** * Join pairs into groups (union-find); a group's score is its best pair. * * @since 1.0.0 * @param array $pairs "a-b" => [ score, reasons ]. * @return array Best first. */ public static function group( array $pairs ) { $parent = array(); $find = static function ( $x ) use ( &$parent, &$find ) { if ( ! isset( $parent[ $x ] ) ) { $parent[ $x ] = $x; } if ( $parent[ $x ] !== $x ) { $parent[ $x ] = $find( $parent[ $x ] ); } return $parent[ $x ]; }; foreach ( array_keys( $pairs ) as $key ) { list( $a, $b ) = array_map( 'intval', explode( '-', $key ) ); $parent[ $find( $a ) ] = $find( $b ); } // Members and reasons as keyed sets: no array_merge + array_unique per pair. $groups = array(); foreach ( $pairs as $key => $pair ) { list( $a, $b ) = array_map( 'intval', explode( '-', $key ) ); $root = $find( $a ); if ( ! isset( $groups[ $root ] ) ) { $groups[ $root ] = array( 'ids' => array(), 'score' => 0, 'reasons' => array(), ); } $groups[ $root ]['ids'][ $a ] = true; $groups[ $root ]['ids'][ $b ] = true; $reasons = array_unique( $pair[1] ); $groups[ $root ]['score'] = min( 100, max( $groups[ $root ]['score'], $pair[0] + 5 * ( count( $reasons ) - 1 ) ) ); foreach ( $reasons as $reason ) { $groups[ $root ]['reasons'][ $reason ] = true; } } $out = array(); foreach ( $groups as $group ) { $ids = array_keys( $group['ids'] ); sort( $ids ); $out[] = array( 'ids' => $ids, 'score' => (int) $group['score'], 'reasons' => array_map( 'strval', array_keys( $group['reasons'] ) ), ); } usort( $out, static fn( $x, $y ) => array( $y['score'], count( $y['ids'] ) ) <=> array( $x['score'], count( $x['ids'] ) ) ); return array_slice( $out, 0, self::MAX_GROUPS ); } /* ------------------------------------------------------------------ dismiss */ /** * Which of these user IDs are accounts of this site (one query per 1000 IDs). On multisite a * user removed from this site still exists in the network, so membership (this site's * capabilities meta, as is_user_member_of_blog() checks) is what counts. * * @since 1.0.0 * @param int[] $ids User IDs. * @return int[] */ public static function existing_ids( array $ids ) { global $wpdb; $ids = array_values( array_unique( array_filter( array_map( 'intval', $ids ) ) ) ); $found = array(); foreach ( array_chunk( $ids, 1000 ) as $chunk ) { $in = implode( ',', array_fill( 0, count( $chunk ), '%d' ) ); // phpcs:disable WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- $in is one %d placeholder per ID; one lookup instead of a get_userdata() per ID. $rows = is_multisite() ? $wpdb->get_col( $wpdb->prepare( "SELECT user_id FROM %i WHERE meta_key = %s AND user_id IN ({$in})", $wpdb->usermeta, $wpdb->get_blog_prefix() . 'capabilities', ...$chunk ) ) : $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM %i WHERE ID IN ({$in})", $wpdb->users, ...$chunk ) ); // phpcs:enable $found = array_merge( $found, (array) $rows ); } return array_values( array_unique( array_map( 'intval', $found ) ) ); } /** * Mark every pair in a saved group as "not a duplicate" and drop the group from the results. * * The IDs must be the existing members of one saved group (the tab leaves deleted accounts * out), at least 2 and at most MAX_DISMISS; otherwise nothing is stored. Call while holding * the lock (the REST route does): it reads and rewrites the dismissed list and the results. * * @since 1.0.0 * @param int[] $ids Customers in the group. * @return bool Whether the group was dismissed. */ public static function dismiss( array $ids ) { $ids = array_values( array_unique( array_filter( array_map( 'intval', $ids ), static fn( $id ) => $id > 0 ) ) ); sort( $ids ); $results = get_option( self::RESULTS ); if ( count( $ids ) < 2 || count( $ids ) > self::MAX_DISMISS || ! is_array( $results ) ) { return false; } $groups = (array) ( $results['groups'] ?? array() ); $candidates = array(); $members = $ids; foreach ( $groups as $i => $group ) { $group_ids = array_map( 'intval', (array) ( $group['ids'] ?? array() ) ); if ( ! array_diff( $ids, $group_ids ) ) { $candidates[ $i ] = $group_ids; $members = array_merge( $members, $group_ids ); } } if ( ! $candidates ) { return false; } $existing = array_flip( self::existing_ids( $members ) ); $match = null; foreach ( $candidates as $i => $group_ids ) { $left = array_values( array_filter( $group_ids, static fn( $id ) => isset( $existing[ $id ] ) ) ); sort( $left ); if ( $left === $ids ) { $match = $i; break; } } if ( null === $match ) { return false; } $dismissed = (array) get_option( self::DISMISSED, array() ); foreach ( $ids as $i => $a ) { foreach ( array_slice( $ids, $i + 1 ) as $b ) { $dismissed[] = $a . '-' . $b; } } update_option( self::DISMISSED, array_values( array_unique( $dismissed ) ), false ); unset( $groups[ $match ] ); $results['groups'] = array_values( $groups ); update_option( self::RESULTS, $results, false ); return true; } }