<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[14147] sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-login: Registrations: Improve performance of the approval UI by limiting some requests to exact or anchored matches.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/14147">14147</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/14147","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>dd32</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2024-10-25 02:40:52 +0000 (Fri, 25 Oct 2024)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentthemespubwporgloginadminclassuserregistrationslisttablephp">sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-login/admin/class-user-registrations-list-table.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentthemespubwporgloginfunctionsregistrationphp">sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-login/functions-registration.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentthemespubwporgloginadminclassuserregistrationslisttablephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-login/admin/class-user-registrations-list-table.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-login/admin/class-user-registrations-list-table.php     2024-10-25 02:16:03 UTC (rev 14146)
+++ sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-login/admin/class-user-registrations-list-table.php       2024-10-25 02:40:52 UTC (rev 14147)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -39,20 +39,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        ]
</span><span class="cx" style="display: block; padding: 0 10px">                ];
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $default      = 'all';
-               $current_view = $_REQUEST['view'] ?? $default;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $current_view = $this->get_current_view();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! empty( $_GET['s'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $default = 'search';
</del><span class="cx" style="display: block; padding: 0 10px">                         $views[0] = [
</span><span class="cx" style="display: block; padding: 0 10px">                                'search', 'All search results'
</span><span class="cx" style="display: block; padding: 0 10px">                        ];
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        array_unshift( $views, [ 'all', 'All' ] );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-                       if ( 'all' === $current_view ) {
-                               $current_view = 'search';
-                       }
</del><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                return array_map(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -59,24 +53,29 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        function( $item ) use ( $current_view ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                global $wpdb;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $count = $wpdb->get_var(
-                                       "SELECT count(*) FROM {$wpdb->base_prefix}user_pending_registrations registrations " .
-                                       $this->get_join_where_sql( $item[0] )
-                               );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $view      = $item[0];
+                               $is_search = ( ! empty( $_GET['s'] ) && 'all' != $view );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                // If we're searching, and the search didn't have any results, all the "sub views" are 0.
+                               if ( $is_search && ! $this->get_view_total_count( 'search' ) ) {
+                                       $count = 0;
+                               } else {
+                                       $count = $this->get_view_total_count( $view );
+                               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                                 $url = admin_url( 'admin.php?page=user-registrations' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                if ( !empty( $_GET['s'] ) && 'all' != $item[0] ) {
-                                       $url = add_query_arg( 's', urlencode( $_GET['s'] ), $url );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         if ( $is_search ) {
+                                       $url = add_query_arg( 's', urlencode( wp_unslash( $_GET['s'] ) ), $url );
</ins><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                if ( 'all' !== $item[0] ) {
-                                       $url = add_query_arg( 'view', $item[0], $url );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         if ( 'all' !== $view ) {
+                                       $url = add_query_arg( 'view', $view, $url );
</ins><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                return sprintf(
</span><span class="cx" style="display: block; padding: 0 10px">                                        '<a href="%s" class="%s">%s <span class="count">(%s)</span></a>',
</span><span class="cx" style="display: block; padding: 0 10px">                                        $url,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        $current_view === $item[0] ? 'current' : '',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 $current_view === $view ? 'current' : '',
</ins><span class="cx" style="display: block; padding: 0 10px">                                         $item[1],
</span><span class="cx" style="display: block; padding: 0 10px">                                        number_format_i18n( $count ),
</span><span class="cx" style="display: block; padding: 0 10px">                                );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -84,6 +83,61 @@
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * Get the current view to display.
+        *
+        * @return string
+        */
+       protected function get_current_view() {
+               $view = $_REQUEST['view'] ?? false;
+               if ( ! $view ) {
+                       $view = 'all';
+                       if ( ! empty( $_GET['s'] ) ) {
+                               $view = 'search';
+                       }
+               }
+
+               if ( 'all' === $view && ! empty( $_GET['s'] ) ) {
+                       $view = 'search';
+               }
+
+               return $view;
+       }
+
+       /**
+        * Get the total count for a given view.
+        *
+        * @param string $view The view to get the count for.
+        * @return int
+        */
+       protected function get_view_total_count( $view ) {
+               global $wpdb;
+               static $counts = [];
+
+               // Search view has no results if no search term...
+               if ( 'search' === $view && empty( $_GET['s'] ) ) {
+                       return 0;
+               }
+
+               $current_view = $this->get_current_view();
+               if ( $view === $current_view ) {
+                       return $this->get_pagination_arg( 'total_items' );
+               }
+
+               $counts[ $view ] ??= $wpdb->get_var(
+                       "SELECT count(*) FROM {$wpdb->base_prefix}user_pending_registrations registrations " .
+                       $this->get_join_where_sql( $view )
+               );
+
+               return $counts[ $view ];
+       }
+
+       /**
+        * Get the SQL WHERE clause for a given view.
+        *
+        * @param string $view The view to get the WHERE clause for.
+        * @return string
+        */
</ins><span class="cx" style="display: block; padding: 0 10px">         protected function get_view_sql_where( $view ) {
</span><span class="cx" style="display: block; padding: 0 10px">                switch ( $view ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        case 'pending':
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -108,6 +162,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * Get the SQL JOIN and WHERE clause for a given view.
+        *
+        * @param string $view The view to get the JOIN and WHERE clause for.
+        * @return string
+        */
</ins><span class="cx" style="display: block; padding: 0 10px">         protected function get_join_where_sql( $view = null ) {
</span><span class="cx" style="display: block; padding: 0 10px">                global $wpdb;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -123,21 +183,30 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        // Limit searches to where they're likely, for performance.
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( str_contains( $search_term, '@' ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                // Looks like an email, so just search the emails.
-                               $where .= $wpdb->prepare(
-                                       "AND registrations.user_email LIKE %s",
-                                       $search_like
-                               );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         // If it looks like a full email, exact match.
+                               if ( preg_match( '/^.{3,}@.+[.].+$/', $search_term ) ) {
+                                       // Looks like an email, so just search the emails.
+                                       $where .= $wpdb->prepare(
+                                               "AND registrations.user_email = %s",
+                                               $search_term
+                                       );
+                               } else {
+                                       // Otherwise, a wildcard on the email.
+                                       $where .= $wpdb->prepare(
+                                               "AND registrations.user_email LIKE %s",
+                                               $search_like
+                                       );
+                               }
</ins><span class="cx" style="display: block; padding: 0 10px">                         } elseif (
</span><span class="cx" style="display: block; padding: 0 10px">                                // If it looks like an IP
</span><span class="cx" style="display: block; padding: 0 10px">                                preg_match( '/^\d{1,3}\.[0-9.]*$/', $search_term ) ||
</span><span class="cx" style="display: block; padding: 0 10px">                                // Or it looks like a country code, 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                preg_match( '/^[A-Z]{2}/', $search_term )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         preg_match( '/^[A-Z]{2}$/', $search_term )
</ins><span class="cx" style="display: block; padding: 0 10px">                         ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                // then only look in metadata.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         // then only look in metadata, case sensitive.
</ins><span class="cx" style="display: block; padding: 0 10px">                                 $where .= $wpdb->prepare(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        "AND registrations.meta LIKE %s",
-                                       $search_like
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 "AND registrations.meta LIKE BINARY %s",
+                                       '%"' . $wpdb->esc_like( $search_term ) . '%' // Anchor it with a " at the start of the field.
</ins><span class="cx" style="display: block; padding: 0 10px">                                 );
</span><span class="cx" style="display: block; padding: 0 10px">                        } else {
</span><span class="cx" style="display: block; padding: 0 10px">                                // Otherwise, search everything.
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentthemespubwporgloginfunctionsregistrationphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-login/functions-registration.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-login/functions-registration.php        2024-10-25 02:16:03 UTC (rev 14146)
+++ sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-login/functions-registration.php  2024-10-25 02:40:52 UTC (rev 14147)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -74,6 +74,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">        $profile_key        = wp_generate_password( 24, false, false );
</span><span class="cx" style="display: block; padding: 0 10px">        $hashed_profile_key = time() . ':' . wp_hash_password( $profile_key );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        $source = $_COOKIE['wporg_came_from'] ?? '';
+       if ( $source ) {
+               $source = remove_query_arg( [ 'SAMLRequest', 'RelayState' ], $source );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         $pending_user = array(
</span><span class="cx" style="display: block; padding: 0 10px">                'user_login'          => $user_login,
</span><span class="cx" style="display: block; padding: 0 10px">                'user_email'          => $user_email,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -83,7 +88,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                'meta'                => $meta + array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'registration_ip'         => $_SERVER['REMOTE_ADDR'], // Spam & fraud control. Will be discarded after the account is created.
</span><span class="cx" style="display: block; padding: 0 10px">                        'registration_ip_country' => ( is_callable( 'WordPressdotorg\GeoIP\query' ) ? \WordPressdotorg\GeoIP\query( $_SERVER['REMOTE_ADDR'], 'country_short' ) : '' ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'source'                  => $_COOKIE['wporg_came_from'] ?? '',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'source'                  => $source,
</ins><span class="cx" style="display: block; padding: 0 10px">                 ),
</span><span class="cx" style="display: block; padding: 0 10px">                'scores'              => array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'pending' => 1,
</span></span></pre>
</div>
</div>

</body>
</html>