WP_User_Query::generate_cache_keyprotectedWP 6.3.0

Generate cache key.

Method of the class: WP_User_Query{}

No Hooks.

Returns

String. Cache key.

Usage

// protected - for code of main (parent) or child class
$result = $this->generate_cache_key( $args, $sql );
$args(array) (required)
Query arguments.
$sql(string) (required)
SQL statement.

Notes

  • Global. wpdb. $wpdb WordPress database abstraction object.

Changelog

Since 6.3.0 Introduced.

WP_User_Query::generate_cache_key() code WP 6.8.1

protected function generate_cache_key( array $args, $sql ) {
	global $wpdb;

	// Replace wpdb placeholder in the SQL statement used by the cache key.
	$sql = $wpdb->remove_placeholder_escape( $sql );

	$key          = md5( $sql );
	$last_changed = wp_cache_get_last_changed( 'users' );

	if ( empty( $args['orderby'] ) ) {
		// Default order is by 'user_login'.
		$ordersby = array( 'user_login' => '' );
	} elseif ( is_array( $args['orderby'] ) ) {
		$ordersby = $args['orderby'];
	} else {
		// 'orderby' values may be a comma- or space-separated list.
		$ordersby = preg_split( '/[,\s]+/', $args['orderby'] );
	}

	$blog_id = 0;
	if ( isset( $args['blog_id'] ) ) {
		$blog_id = absint( $args['blog_id'] );
	}

	if ( $args['has_published_posts'] || in_array( 'post_count', $ordersby, true ) ) {
		$switch = $blog_id && get_current_blog_id() !== $blog_id;
		if ( $switch ) {
			switch_to_blog( $blog_id );
		}

		$last_changed .= wp_cache_get_last_changed( 'posts' );

		if ( $switch ) {
			restore_current_blog();
		}
	}

	return "get_users:$key:$last_changed";
}