| 691 | |
| 692 | /** |
| 693 | * Generates the cache key to lookup query results for a specific set of arguments. |
| 694 | * |
| 695 | * @since 5.0.0 |
| 696 | * |
| 697 | * @param array $query_vars Array of WP_Site_Query arguments. See WP_Site_Query::__construct(). |
| 698 | * @return string Cache key to use for the lookup. |
| 699 | */ |
| 700 | protected function get_cache_key( $query_vars ) { |
| 701 | // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. |
| 702 | $_args = wp_array_slice_assoc( $query_vars, array_keys( $this->query_var_defaults ) ); |
| 703 | |
| 704 | // Ignore the $fields argument as the queried result will be the same regardless. |
| 705 | unset( $_args['fields'] ); |
| 706 | |
| 707 | // Ignore the $update_site_cache as it does not affect the query. |
| 708 | unset( $_args['update_site_cache'] ); |
| 709 | |
| 710 | // Use the same cache key for array lookups with one element and single value lookups. |
| 711 | $single_multi_mappings = array( |
| 712 | 'ID' => 'site__in', |
| 713 | 'network_id' => 'network__in', |
| 714 | 'domain' => 'domain__in', |
| 715 | 'path' => 'path__in', |
| 716 | 'lang_id' => 'lang__in', |
| 717 | ); |
| 718 | foreach ( $single_multi_mappings as $single_var => $multi_var ) { |
| 719 | if ( empty( $_args[ $single_var ] ) && is_array( $_args[ $multi_var ] ) && count( $_args[ $multi_var ] ) === 1 ) { |
| 720 | $_args[ $single_var ] = array_pop( $_args[ $multi_var ] ); |
| 721 | $_args[ $multi_var ] = ''; |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | $key = md5( serialize( $_args ) ); |
| 726 | |
| 727 | /* The following part of the code checks some common combinations of query vars. |
| 728 | * If all non-empty values that actually affect the last_changed date for the query |
| 729 | * belong to one of the special argument combinations, a more specific last_changed |
| 730 | * cache key is used instead of the global one. */ |
| 731 | |
| 732 | // Only consider arguments that are not empty and have thus been passed to the query. |
| 733 | $used_args = array_filter( $_args ); |
| 734 | |
| 735 | // The following arguments affect the query result, but not its last_changed value. |
| 736 | $ignore_args = array( 'number', 'offset', 'no_found_rows', 'orderby', 'order', 'count' ); |
| 737 | $used_args = array_diff_key( $used_args, array_flip( $ignore_args ) ); |
| 738 | |
| 739 | $last_changed_prefix = ''; |
| 740 | if ( count( $used_args ) === 3 && isset( $used_args['domain'] ) && isset( $used_args['path'] ) && isset( $used_args['network_id'] ) ) { |
| 741 | $last_changed_prefix = md5( $used_args['domain'] . $used_args['path'] . $used_args['network_id'] ) . '_'; |
| 742 | } elseif ( count( $used_args ) === 2 && isset( $used_args['domain'] ) && isset( $used_args['path'] ) ) { |
| 743 | $last_changed_prefix = md5( $used_args['domain'] . $used_args['path'] ) . '_'; |
| 744 | } elseif ( count( $used_args ) === 1 && isset( $used_args['domain'] ) ) { |
| 745 | $last_changed_prefix = md5( $used_args['domain'] ) . '_'; |
| 746 | } elseif ( count( $used_args ) === 1 && isset( $used_args['path'] ) ) { |
| 747 | $last_changed_prefix = md5( $used_args['path'] ) . '_'; |
| 748 | } |
| 749 | |
| 750 | $last_changed = wp_cache_get_last_changed( 'sites', $last_changed_prefix ); |
| 751 | |
| 752 | return "get_sites:$key:$last_changed"; |
| 753 | } |