0, 'imported' => 0, 'skipped_invalid' => 0, 'skipped_taken' => 0, 'skipped_existing' => 0, ); $country = (string) Options::get( 'general.default_country' ); $last_id = 0; do { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- One-off admin tool reading core ids. $ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->users} WHERE ID > %d ORDER BY ID ASC LIMIT %d", $last_id, self::BATCH ) ); $fetched = count( $ids ); foreach ( $ids as $id ) { $last_id = (int) $id; ++$result['scanned']; $raw = self::read_number( (int) $id, $source, $meta ); if ( '' === $raw ) { continue; } $phone = Phone_Normalizer::to_e164( $raw, $country ); if ( null === $phone ) { ++$result['skipped_invalid']; continue; } if ( array() !== Identity_Store::find_for_user( (int) $id, 'phone' ) ) { ++$result['skipped_existing']; continue; } $owner = Identity_Store::find( 'phone', $phone ); if ( $owner && (int) $owner->user_id > 0 && (int) $owner->user_id !== (int) $id ) { ++$result['skipped_taken']; continue; } if ( ! $dry_run ) { Identity_Store::save( (int) $id, 'phone', $phone ); } ++$result['imported']; } } while ( self::BATCH === $fetched ); return $result; } /** * Reads the stored phone number of one user. * * @param int $user_id User id. * @param string $source Source type. * @param string $meta Meta key for SOURCE_META. * @return string */ private static function read_number( $user_id, $source, $meta ) { if ( self::SOURCE_META === $source ) { return '' === $meta ? '' : trim( (string) get_user_meta( $user_id, $meta, true ) ); } // Digits keeps the full number in one key and, in some versions, splits it into country code and national number. $country_code = trim( (string) get_user_meta( $user_id, 'digt_countrycode', true ) ); $national = trim( (string) get_user_meta( $user_id, 'digits_phone_no', true ) ); if ( '' !== $country_code && '' !== $national ) { return '+' . ltrim( $country_code, '+' ) . ltrim( $national, '0' ); } return trim( (string) get_user_meta( $user_id, 'digits_phone', true ) ); } }