Changeset 55657
- Timestamp:
- 04/18/2023 11:48:46 AM (19 months ago)
- Location:
- trunk
- Files:
-
- 1 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-user-query.php
r55654 r55657 120 120 'login__in' => array(), 121 121 'login__not_in' => array(), 122 'cache_results' => true, 122 123 ); 123 124 … … 141 142 * @since 5.3.0 Introduced the 'meta_type_key' parameter. 142 143 * @since 5.9.0 Added 'capability', 'capability__in', and 'capability__not_in' parameters. 144 * @since 6.3.0 Added 'cache_results' parameter. 143 145 * 144 146 * @global wpdb $wpdb WordPress database abstraction object. … … 255 257 * @type string[] $login__not_in An array of logins to exclude. Users matching one of these 256 258 * logins will not be included in results. Default empty array. 259 * @type bool $cache_results Whether to cache user information. Default true. 257 260 * } 258 261 */ … … 790 793 791 794 $qv =& $this->query_vars; 795 796 // Do not cache results if more than 3 fields are requested. 797 if ( is_array( $qv['fields'] ) && count( $qv['fields'] ) > 3 ) { 798 $qv['cache_results'] = false; 799 } 792 800 793 801 /** … … 817 825 {$this->query_limit} 818 826 "; 819 820 if ( is_array( $qv['fields'] ) ) { 821 $this->results = $wpdb->get_results( $this->request ); 827 $cache_value = false; 828 $cache_key = $this->generate_cache_key( $qv, $this->request ); 829 $cache_group = 'users-queries'; 830 if ( $qv['cache_results'] ) { 831 $cache_value = wp_cache_get( $cache_key, $cache_group ); 832 } 833 if ( false !== $cache_value ) { 834 $this->results = $cache_value['user_data']; 835 $this->total_users = $cache_value['total_users']; 822 836 } else { 823 $this->results = $wpdb->get_col( $this->request ); 824 } 825 826 if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { 827 /** 828 * Filters SELECT FOUND_ROWS() query for the current WP_User_Query instance. 829 * 830 * @since 3.2.0 831 * @since 5.1.0 Added the `$this` parameter. 832 * 833 * @global wpdb $wpdb WordPress database abstraction object. 834 * 835 * @param string $sql The SELECT FOUND_ROWS() query for the current WP_User_Query. 836 * @param WP_User_Query $query The current WP_User_Query instance. 837 */ 838 $found_users_query = apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()', $this ); 839 840 $this->total_users = (int) $wpdb->get_var( $found_users_query ); 837 838 if ( is_array( $qv['fields'] ) ) { 839 $this->results = $wpdb->get_results( $this->request ); 840 } else { 841 $this->results = $wpdb->get_col( $this->request ); 842 } 843 844 if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { 845 /** 846 * Filters SELECT FOUND_ROWS() query for the current WP_User_Query instance. 847 * 848 * @since 3.2.0 849 * @since 5.1.0 Added the `$this` parameter. 850 * 851 * @global wpdb $wpdb WordPress database abstraction object. 852 * 853 * @param string $sql The SELECT FOUND_ROWS() query for the current WP_User_Query. 854 * @param WP_User_Query $query The current WP_User_Query instance. 855 */ 856 $found_users_query = apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()', $this ); 857 858 $this->total_users = (int) $wpdb->get_var( $found_users_query ); 859 } 860 861 if ( $qv['cache_results'] ) { 862 $cache_value = array( 863 'user_data' => $this->results, 864 'total_users' => $this->total_users, 865 ); 866 wp_cache_add( $cache_key, $cache_value, $cache_group ); 867 } 841 868 } 842 869 } … … 1012 1039 1013 1040 /** 1041 * Generate cache key. 1042 * 1043 * @since 6.3.0 1044 * 1045 * @global wpdb $wpdb WordPress database abstraction object. 1046 * 1047 * @param array $args Query arguments. 1048 * @param string $sql SQL statement. 1049 * @return string Cache key. 1050 */ 1051 protected function generate_cache_key( array $args, $sql ) { 1052 global $wpdb; 1053 1054 // Replace wpdb placeholder in the SQL statement used by the cache key. 1055 $sql = $wpdb->remove_placeholder_escape( $sql ); 1056 1057 $key = md5( $sql ); 1058 $last_changed = wp_cache_get_last_changed( 'users' ); 1059 1060 if ( empty( $args['orderby'] ) ) { 1061 // Default order is by 'user_login'. 1062 $ordersby = array( 'user_login' => '' ); 1063 } elseif ( is_array( $args['orderby'] ) ) { 1064 $ordersby = $args['orderby']; 1065 } else { 1066 // 'orderby' values may be a comma- or space-separated list. 1067 $ordersby = preg_split( '/[,\s]+/', $args['orderby'] ); 1068 } 1069 1070 $blog_id = 0; 1071 if ( isset( $args['blog_id'] ) ) { 1072 $blog_id = absint( $args['blog_id'] ); 1073 } 1074 if ( ( $args['has_published_posts'] && $blog_id ) || in_array( 'post_count', $ordersby, true ) ) { 1075 $switch = get_current_blog_id() !== $blog_id; 1076 if ( $switch ) { 1077 switch_to_blog( $blog_id ); 1078 } 1079 $last_changed .= wp_cache_get_last_changed( 'posts' ); 1080 if ( $switch ) { 1081 restore_current_blog(); 1082 } 1083 } 1084 1085 return "get_users:$key:$last_changed"; 1086 } 1087 1088 /** 1014 1089 * Parses an 'order' query variable and casts it to ASC or DESC as necessary. 1015 1090 * -
trunk/src/wp-includes/default-filters.php
r55620 r55657 115 115 add_action( 'updated_post_meta', 'wp_cache_set_posts_last_changed' ); 116 116 add_action( 'deleted_post_meta', 'wp_cache_set_posts_last_changed' ); 117 118 // User meta. 119 add_action( 'added_user_meta', 'wp_cache_set_users_last_changed' ); 120 add_action( 'updated_user_meta', 'wp_cache_set_users_last_changed' ); 121 add_action( 'deleted_user_meta', 'wp_cache_set_users_last_changed' ); 122 add_action( 'add_user_role', 'wp_cache_set_users_last_changed' ); 123 add_action( 'set_user_role', 'wp_cache_set_users_last_changed' ); 124 add_action( 'remove_user_role', 'wp_cache_set_users_last_changed' ); 117 125 118 126 // Term meta. -
trunk/src/wp-includes/load.php
r55652 r55657 777 777 'user_meta', 778 778 'userslugs', 779 'users-queries', 779 780 ) 780 781 ); -
trunk/src/wp-includes/ms-blogs.php
r55537 r55657 574 574 'user_meta', 575 575 'userslugs', 576 'users-queries', 576 577 ) 577 578 ); … … 667 668 'user_meta', 668 669 'userslugs', 670 'users-queries', 669 671 ) 670 672 ); -
trunk/src/wp-includes/ms-functions.php
r55642 r55657 296 296 } 297 297 298 clean_user_cache( $user_id ); 298 299 restore_current_blog(); 299 300 -
trunk/src/wp-includes/user.php
r55584 r55657 1908 1908 1909 1909 wp_cache_delete( $user->ID, 'user_meta' ); 1910 wp_cache_set_users_last_changed(); 1910 1911 1911 1912 /** … … 5017 5018 ); 5018 5019 } 5020 5021 /** 5022 * Sets the last changed time for the 'users' cache group. 5023 * 5024 * @since 6.3.0 5025 */ 5026 function wp_cache_set_users_last_changed() { 5027 wp_cache_set( 'last_changed', microtime(), 'users' ); 5028 } -
trunk/tests/phpunit/includes/abstract-testcase.php
r55537 r55657 413 413 'user_meta', 414 414 'userslugs', 415 'users-queries', 415 416 ) 416 417 );
Note: See TracChangeset
for help on using the changeset viewer.