g_action( 'wp' )` here, but the performance gain lays // in the micro-optimization area while exposing the Analytics to certain edge-cases. self::update_registered_customer( $user_id ); } } /** * Check if a user ID is a valid customer or other user role with past orders. * * @param int $user_id User ID. * @return bool */ protected static function is_valid_customer( $user_id ) { $user = new \WP_User( $user_id ); if ( (int) $user_id !== $user->ID ) { return false; } /** * Filter the customer roles, used to check if the user is a customer. * * @param array List of customer roles. * @since 4.0.0 */ $customer_roles = (array) apply_filters( 'woocommerce_analytics_customer_roles', array( 'customer' ) ); if ( empty( $user->roles ) || empty( array_intersect( $user->roles, $customer_roles ) ) ) { return false; } return true; } /** * Delete a customer lookup row. * * @param int $customer_id Customer ID. */ public static function delete_customer( $customer_id ) { global $wpdb; $customer_id = (int) $customer_id; $num_deleted = $wpdb->delete( self::get_db_table_name(), array( 'customer_id' => $customer_id ) ); if ( $num_deleted ) { /** * Fires when a customer is deleted. * * @param int $order_id Order ID. * @since 4.0.0 */ do_action( 'woocommerce_analytics_delete_customer', $customer_id ); ReportsCache::invalidate(); } } /** * Delete a customer lookup row by WordPress User ID. * * @param int $user_id WordPress User ID. */ public static function delete_customer_by_user_id( $user_id ) { global $wpdb; if ( (int) $user_id < 1 || doing_action( 'wp_uninitialize_site' ) ) { // Skip the deletion. return; } $user_id = (int) $user_id; $num_deleted = $wpdb->delete( self::get_db_table_name(), array( 'user_id' => $user_id ) ); if ( $num_deleted ) { ReportsCache::invalidate(); } } /** * Anonymize the customer data for a single order. * * @internal * @param int|WC_Order $order Order instance or ID. * @return void */ public static function anonymize_customer( $order ) { global $wpdb; if ( ! is_object( $order ) ) { $order = wc_get_order( absint( $order ) ); } $customer_id = $wpdb->get_var( $wpdb->prepare( "SELECT customer_id FROM {$wpdb->prefix}wc_order_stats WHERE order_id = %d", $order->get_id() ) ); if ( ! $customer_id ) { return; } // Long form query because $wpdb->update rejects [deleted]. $deleted_text = __( '[deleted]', 'woocommerce' ); $updated = $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}wc_customer_lookup SET user_id = NULL, username = %s, first_name = %s, last_name = %s, email = %s, country = '', postcode = %s, city = %s, state = %s WHERE customer_id = %d", array( $deleted_text, $deleted_text, $deleted_text, 'deleted@site.invalid', $deleted_text, $deleted_text, $deleted_text, $customer_id, ) ) ); // If the customer row was anonymized, flush the cache. if ( $updated ) { ReportsCache::invalidate(); } } /** * Build location filter SQL clause for includes or excludes. * * @since 10.5.0 * @param string $locations_string Comma-separated list of locations (e.g., "US:CA,US:NY,GB"). * @param bool $is_include True for IN clause, false for NOT IN clause. * @return string SQL WHERE clause condition. */ protected function build_location_filter_clause( $locations_string, $is_include = true ) { $customer_lookup_table = self::get_db_table_name(); $locations_array = explode( ',', $locations_string ); $country_state_pairs = array(); $countries = array(); foreach ( $locations_array as $location ) { $location = trim( $location ); if ( empty( $location ) ) { continue; } if ( false !== strpos( $location, ':' ) ) { $parts = explode( ':', $location ); if ( 2 === count( $parts ) ) { $country_state_pairs[] = array( 'country' => esc_sql( $parts[0] ), 'state' => esc_sql( $parts[1] ), ); } } else { $countries[] = esc_sql( $location ); } } $conditions = array(); // Build country:state pair conditions. if ( ! empty( $country_state_pairs ) ) { $pair_conditions = array(); foreach ( $country_state_pairs as $pair ) { if ( $is_include ) { $pair_conditions[] = "({$customer_lookup_table}.country = '{$pair['country']}' AND {$customer_lookup_table}.state = '{$pair['state']}')"; } else { $pair_conditions[] = "({$customer_lookup_table}.country != '{$pair['country']}' OR {$customer_lookup_table}.state != '{$pair['state']}')"; } } $pair_connector = $is_include ? ' OR ' : ' AND '; $conditions[] = '(' . implode( $pair_connector, $pair_conditions ) . ')'; } // Build country-only conditions. if ( ! empty( $countries ) ) { $operator = $is_include ? 'IN' : 'NOT IN'; $conditions[] = "{$customer_lookup_table}.country {$operator} ('" . implode( "','", $countries ) . "')"; } if ( empty( $conditions ) ) { return ''; } // Combine conditions with OR for includes, AND for excludes. $connector = $is_include ? ' OR ' : ' AND '; return '(' . implode( $connector, $conditions ) . ')'; } /** * Initialize query objects. */ protected function initialize_queries() { $this->clear_all_clauses(); $table_name = self::get_db_table_name(); $this->subquery = new SqlQuery( $this->context . '_subquery' ); $this->subquery->add_sql_clause( 'from', $table_name ); $this->subquery->add_sql_clause( 'select', "{$table_name}.customer_id" ); $this->subquery->add_sql_clause( 'group_by', "{$table_name}.customer_id" ); } }