Making WordPress.org


Ignore:
Timestamp:
10/25/2024 02:40:52 AM (2 years ago)
Author:
dd32
Message:

Registrations: Improve performance of the approval UI by limiting some requests to exact or anchored matches.
Additionally re-use/cache/skip getting counts for some searches.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-login/admin/class-user-registrations-list-table.php

    r13842 r14147  
    4040                ];
    4141
    42                 $default      = 'all';
    43                 $current_view = $_REQUEST['view'] ?? $default;
     42                $current_view = $this->get_current_view();
    4443
    4544                if ( ! empty( $_GET['s'] ) ) {
    46                         $default = 'search';
    4745                        $views[0] = [
    4846                                'search', 'All search results'
    … …  
    5048
    5149                        array_unshift( $views, [ 'all', 'All' ] );
    52 
    53                         if ( 'all' === $current_view ) {
    54                                 $current_view = 'search';
    55                         }
    5650                }
    5751
    … …  
    6054                                global $wpdb;
    6155
    62                                 $count = $wpdb->get_var(
    63                                         "SELECT count(*) FROM {$wpdb->base_prefix}user_pending_registrations registrations " .
    64                                         $this->get_join_where_sql( $item[0] )
    65                                 );
     56                                $view      = $item[0];
     57                                $is_search = ( ! empty( $_GET['s'] ) && 'all' != $view );
     58
     59                                // If we're searching, and the search didn't have any results, all the "sub views" are 0.
     60                                if ( $is_search && ! $this->get_view_total_count( 'search' ) ) {
     61                                        $count = 0;
     62                                } else {
     63                                        $count = $this->get_view_total_count( $view );
     64                                }
    6665
    6766                                $url = admin_url( 'admin.php?page=user-registrations' );
    68                                 if ( !empty( $_GET['s'] ) && 'all' != $item[0] ) {
    69                                         $url = add_query_arg( 's', urlencode( $_GET['s'] ), $url );
     67                                if ( $is_search ) {
     68                                        $url = add_query_arg( 's', urlencode( wp_unslash( $_GET['s'] ) ), $url );
    7069                                }
    7170
    72                                 if ( 'all' !== $item[0] ) {
    73                                         $url = add_query_arg( 'view', $item[0], $url );
     71                                if ( 'all' !== $view ) {
     72                                        $url = add_query_arg( 'view', $view, $url );
    7473                                }
    7574
    … …  
    7776                                        '<a href="%s" class="%s">%s <span class="count">(%s)</span></a>',
    7877                                        $url,
    79                                         $current_view === $item[0] ? 'current' : '',
     78                                        $current_view === $view ? 'current' : '',
    8079                                        $item[1],
    8180                                        number_format_i18n( $count ),
    … …  
    8584        }
    8685
     86        /**
     87         * Get the current view to display.
     88         *
     89         * @return string
     90         */
     91        protected function get_current_view() {
     92                $view = $_REQUEST['view'] ?? false;
     93                if ( ! $view ) {
     94                        $view = 'all';
     95                        if ( ! empty( $_GET['s'] ) ) {
     96                                $view = 'search';
     97                        }
     98                }
     99
     100                if ( 'all' === $view && ! empty( $_GET['s'] ) ) {
     101                        $view = 'search';
     102                }
     103
     104                return $view;
     105        }
     106
     107        /**
     108         * Get the total count for a given view.
     109         *
     110         * @param string $view The view to get the count for.
     111         * @return int
     112         */
     113        protected function get_view_total_count( $view ) {
     114                global $wpdb;
     115                static $counts = [];
     116
     117                // Search view has no results if no search term...
     118                if ( 'search' === $view && empty( $_GET['s'] ) ) {
     119                        return 0;
     120                }
     121
     122                $current_view = $this->get_current_view();
     123                if ( $view === $current_view ) {
     124                        return $this->get_pagination_arg( 'total_items' );
     125                }
     126
     127                $counts[ $view ] ??= $wpdb->get_var(
     128                        "SELECT count(*) FROM {$wpdb->base_prefix}user_pending_registrations registrations " .
     129                        $this->get_join_where_sql( $view )
     130                );
     131
     132                return $counts[ $view ];
     133        }
     134
     135        /**
     136         * Get the SQL WHERE clause for a given view.
     137         *
     138         * @param string $view The view to get the WHERE clause for.
     139         * @return string
     140         */
    87141        protected function get_view_sql_where( $view ) {
    88142                switch ( $view ) {
    … …  
    109163        }
    110164
     165        /**
     166         * Get the SQL JOIN and WHERE clause for a given view.
     167         *
     168         * @param string $view The view to get the JOIN and WHERE clause for.
     169         * @return string
     170         */
    111171        protected function get_join_where_sql( $view = null ) {
    112172                global $wpdb;
    … …  
    124184                        // Limit searches to where they're likely, for performance.
    125185                        if ( str_contains( $search_term, '@' ) ) {
    126                                 // Looks like an email, so just search the emails.
    127                                 $where .= $wpdb->prepare(
    128                                         "AND registrations.user_email LIKE %s",
    129                                         $search_like
    130                                 );
     186                                // If it looks like a full email, exact match.
     187                                if ( preg_match( '/^.{3,}@.+[.].+$/', $search_term ) ) {
     188                                        // Looks like an email, so just search the emails.
     189                                        $where .= $wpdb->prepare(
     190                                                "AND registrations.user_email = %s",
     191                                                $search_term
     192                                        );
     193                                } else {
     194                                        // Otherwise, a wildcard on the email.
     195                                        $where .= $wpdb->prepare(
     196                                                "AND registrations.user_email LIKE %s",
     197                                                $search_like
     198                                        );
     199                                }
    131200                        } elseif (
    132201                                // If it looks like an IP
    133202                                preg_match( '/^\d{1,3}\.[0-9.]*$/', $search_term ) ||
    134203                                // Or it looks like a country code,
    135                                 preg_match( '/^[A-Z]{2}/', $search_term )
     204                                preg_match( '/^[A-Z]{2}$/', $search_term )
    136205                        ) {
    137                                 // then only look in metadata.
     206                                // then only look in metadata, case sensitive.
    138207                                $where .= $wpdb->prepare(
    139                                         "AND registrations.meta LIKE %s",
    140                                         $search_like
     208                                        "AND registrations.meta LIKE BINARY %s",
     209                                        '%"' . $wpdb->esc_like( $search_term ) . '%' // Anchor it with a " at the start of the field.
    141210                                );
    142211                        } else {
Note: See TracChangeset for help on using the changeset viewer.