Make WordPress Core

Ticket #42252: 42252.6.diff

File 42252.6.diff, 6.3 KB (added by flixos90, 6 years ago)
  • src/wp-includes/class-wp-site-query.php

     
    240240                 */
    241241                do_action_ref_array( 'pre_get_sites', array( &$this ) );
    242242
    243                 // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
    244                 $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
    245 
    246                 // Ignore the $fields argument as the queried result will be the same regardless.
    247                 unset( $_args['fields'] );
    248 
    249                 $key          = md5( serialize( $_args ) );
    250                 $last_changed = wp_cache_get_last_changed( 'sites' );
    251 
    252                 $cache_key   = "get_sites:$key:$last_changed";
     243                $cache_key   = $this->get_cache_key( $this->query_vars );
    253244                $cache_value = wp_cache_get( $cache_key, 'sites' );
    254245
    255246                if ( false === $cache_value ) {
     
    698689                        return 'DESC';
    699690                }
    700691        }
     692
     693        /**
     694         * Generates the cache key to lookup query results for a specific set of arguments.
     695         *
     696         * @since 5.0.0
     697         *
     698         * @param array $query_vars Array of WP_Site_Query arguments. See WP_Site_Query::__construct().
     699         * @return string Cache key to use for the lookup.
     700         */
     701        protected function get_cache_key( $query_vars ) {
     702                // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
     703                $_args = wp_array_slice_assoc( $query_vars, array_keys( $this->query_var_defaults ) );
     704
     705                // Ignore the $fields argument as the queried result will be the same regardless.
     706                unset( $_args['fields'] );
     707
     708                // Ignore the $update_site_cache as it does not affect the query.
     709                unset( $_args['update_site_cache'] );
     710
     711                // Use the same cache key for array lookups with one element and single value lookups.
     712                $single_multi_mappings = array(
     713                        'ID'         => 'site__in',
     714                        'network_id' => 'network__in',
     715                        'domain'     => 'domain__in',
     716                        'path'       => 'path__in',
     717                        'lang_id'    => 'lang__in',
     718                );
     719                foreach ( $single_multi_mappings as $single_var => $multi_var ) {
     720                        if ( empty( $_args[ $single_var ] ) && isset( $_args[ $multi_var ] ) && is_array( $_args[ $multi_var ] ) && count( $_args[ $multi_var ] ) === 1 ) {
     721                                $_args[ $single_var ] = array_pop( $_args[ $multi_var ] );
     722                                $_args[ $multi_var ] = '';
     723                        }
     724                }
     725
     726                $key = md5( serialize( $_args ) );
     727
     728                /* The following part of the code checks some common combinations of query vars.
     729                 * If all non-empty values that actually affect the last_changed date for the query
     730                 * belong to one of the special argument combinations, a more specific last_changed
     731                 * cache key is used instead of the global one. */
     732
     733                // Only consider arguments that are not empty and have thus been passed to the query.
     734                $used_args = array_filter( $_args );
     735
     736                // The following arguments affect the query result, but not its last_changed value.
     737                $ignore_args = array( 'number', 'offset', 'no_found_rows', 'orderby', 'order', 'count' );
     738                $used_args   = array_diff_key( $used_args, array_flip( $ignore_args ) );
     739
     740                // The following arguments are relevant for a special 'last_changed' prefix.
     741                $last_changed_keys = array( 'domain', 'path', 'network_id' );
     742
     743                $last_changed_args = array();
     744                foreach ( $last_changed_keys as $key ) {
     745                        if ( isset( $used_args[ $key ] ) ) {
     746                                $last_changed_args[ $key ] = (string) $used_args[ $key ];
     747                                unset( $used_args[ $key ] );
     748                        }
     749                }
     750
     751                // Only use a special 'last_changed' prefix if no other arguments are present.
     752                if ( ! empty( $last_changed_args ) && empty( $used_args ) ) {
     753                        $last_changed_prefix = md5( serialize( $last_changed_args ) ) . '_';
     754                } else {
     755                        $last_changed_prefix = '';
     756                }
     757
     758                $last_changed = wp_cache_get_last_changed( 'sites', $last_changed_prefix );
     759
     760                return "get_sites:$key:$last_changed";
     761        }
    701762}
  • src/wp-includes/functions.php

     
    59525952 * Get last changed date for the specified cache group.
    59535953 *
    59545954 * @since 4.7.0
     5955 * @since 5.0.0 Now supports the $prefix parameter.
    59555956 *
    5956  * @param string $group Where the cache contents are grouped.
     5957 * @param string $group  Where the cache contents are grouped.
     5958 * @param string $prefix Optional. Prefix to look for a specific last changed key. Default empty string.
    59575959 *
    59585960 * @return string $last_changed UNIX timestamp with microseconds representing when the group was last changed.
    59595961 */
    5960 function wp_cache_get_last_changed( $group ) {
    5961         $last_changed = wp_cache_get( 'last_changed', $group );
     5962function wp_cache_get_last_changed( $group, $prefix = '' ) {
     5963        $key = $prefix . 'last_changed';
     5964
     5965        $last_changed = wp_cache_get( $key, $group );
    59625966
    59635967        if ( ! $last_changed ) {
    59645968                $last_changed = microtime();
    5965                 wp_cache_set( 'last_changed', $last_changed, $group );
     5969                wp_cache_set( $key, $last_changed, $group );
    59665970        }
    59675971
    59685972        return $last_changed;
  • src/wp-includes/ms-blogs.php

     
    459459                                'blog_id' => $blog_id,
    460460                                'domain'  => null,
    461461                                'path'    => null,
     462                                'site_id' => get_current_network_id(),
    462463                        )
    463464                );
    464465        }
     
    486487         */
    487488        do_action( 'clean_site_cache', $blog_id, $blog, $domain_path_key );
    488489
    489         wp_cache_set( 'last_changed', microtime(), 'sites' );
     490        $current_time = microtime();
     491
     492        // The following arguments are relevant for a special 'last_changed' prefix.
     493        $last_changed_args = array(
     494                'domain'     => $blog->domain,
     495                'path'       => $blog->path,
     496                'network_id' => $blog->site_id,
     497        );
     498
     499        $last_changed_prefixes = array( '' );
     500
     501        // Generate all possible variations.
     502        $combinations = array( array() );
     503        foreach ( $last_changed_args as $key => $value ) {
     504                $value = (string) $value;
     505
     506                foreach ( $combinations as $combination ) {
     507                        $current_args = array_merge( $combination, array( $key => $value ) );
     508                        $last_changed_prefixes[] = md5( serialize( $current_args ) ) . '_';
     509
     510                        $combinations[] = $current_args;
     511                }
     512        }
     513
     514        foreach ( $last_changed_prefixes as $last_changed_prefix ) {
     515                wp_cache_set( $last_changed_prefix . 'last_changed', $current_time, 'sites' );
     516        }
    490517
    491518        /**
    492519         * Fires after the blog details cache is cleared.