wpseek.com
Un motore di ricerca WordPress per sviluppatori e autori di temi
wp_random_content_posts_pre_query › WordPress Function
Da7.2.0
Deprecaton/a
› wp_random_content_posts_pre_query ( $posts, $query )
| Parametri: (2) |
|
| Ritorna: |
|
| Definito a: |
|
| Codex: |
Selects the random post from the eligible posts among random post IDs.
The random content query is limited to 50 random post IDs, see wp_get_random_content_sample_ids, and one of the posts it selects is chosen at random. If none of the IDs are eligible posts, the query is run again with other random post IDs, up to three times in total. On sites where most post IDs are eligible posts, a post is nearly always selected by the first query. If no post is selected, eligible posts are too sparse among the post IDs to be found this way, for example a small category on a site with many posts. A pool of up to 1,000 randomly ordered eligible posts is then selected, which is the whole archive if it has no more posts than that, and their IDs are stored in the object cache for 10 minutes. While the pool is cached, the post is selected from it without querying the database for the eligible posts. Selecting the pool requires the database to read every eligible post, so sites with a persistent object cache do so at most once every 10 minutes for each such archive. The pool is selected without excluding password protected posts, which are skipped when the post is selected from it, so the database can read the eligible posts from an index rather than reading each post in full. Only one request selects the pool at a time. Once the pool is 10 minutes old, the first request to need it selects the next pool and, until it has, other requests select from the previous pool, which is kept for an hour. If there is no previous pool, as the archive has not been requested recently, concurrent requests each select a pool. The pool is keyed by the SQL of the random content query, excluding the random post IDs, so each archive, and each variation of an archive produced by {@see 'pre_get_posts'} callbacks or query filters, has its own pool. As the pool may be up to 10 minutes old, posts that have since been unpublished, deleted, password protected or changed to another post type are skipped. If none of its posts are eligible, a post is selected as if there was no pool. Other changes, such as a post being published or removed from a term, are reflected once the pool expires. This includes the empty pool of an archive without eligible posts, which is cached so the archive is not queried for every request. Added to the 'posts_pre_query'} filter by {@see WP::handle_random while the random content query runs. The database is only read from, never written to.Sorgenti
function wp_random_content_posts_pre_query( $posts, WP_Query $query ) {
global $wpdb;
if ( null !== $posts || ! wp_is_random_content_query( $query ) || ! is_string( $query->request ) ) {
return $posts;
}
// The placeholder escape string is unique to each request, see wpdb::placeholder_escape().
$request = $wpdb->remove_placeholder_escape( $query->request );
$sample_ids = $query->get( 'random_sample_ids' );
$sample_clause = is_array( $sample_ids ) && $sample_ids ? _wp_random_content_sample_clause( $sample_ids ) : '';
/*
* The random post IDs can only be replaced, to look up other IDs or to select the
* pool, if another callback has not modified their clause.
*/
$can_resample = '' !== $sample_clause && 1 === substr_count( $request, $sample_clause );
$cache_key = $can_resample ? 'pool:' . md5( str_replace( $sample_clause, '', $request ) ) : '';
$post_types = (array) $query->get( 'post_type' );
/**
* Selects a random eligible post from post IDs.
*
* @param array<int|string> $post_ids Post IDs.
* @return WP_Post|null The selected post, or null if none of the posts are eligible.
*/
$select_post = static function ( array $post_ids ) use ( $post_types ): ?WP_Post {
shuffle( $post_ids );
foreach ( $post_ids as $post_id ) {
$post = get_post( (int) $post_id );
if (
$post instanceof WP_Post
&& 'publish' === $post->post_status
&& '' === $post->post_password
&& in_array( $post->post_type, $post_types, true )
) {
return $post;
}
}
return null;
};
// The pool selected by an earlier request, once it is more than 10 minutes old.
$stale_pool = null;
if ( $can_resample ) {
$cached = wp_cache_get( $cache_key, 'random-content' );
if ( is_array( $cached ) && isset( $cached['post_ids'], $cached['expires'] ) && is_array( $cached['post_ids'] ) ) {
if ( $cached['expires'] > time() ) {
// The archive had no eligible posts when the pool was selected.
if ( array() === $cached['post_ids'] ) {
return array();
}
$post = $select_post( $cached['post_ids'] );
if ( null !== $post ) {
return array( $post );
}
} else {
$stale_pool = $cached['post_ids'];
}
}
}
// Look up random post IDs until one of them is an eligible post.
for ( $attempt = 1; $attempt <= 3; $attempt++ ) {
if ( $attempt > 1 ) {
$request = str_replace(
$sample_clause,
_wp_random_content_sample_clause( wp_get_random_content_sample_ids() ),
$wpdb->remove_placeholder_escape( $query->request )
);
}
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- The request is prepared by WP_Query::get_posts().
$post = $select_post( $wpdb->get_col( $request ) );
if ( null !== $post ) {
return array( $post );
}
if ( ! $can_resample ) {
return array();
}
}
/*
* Eligible posts are sparse among the post IDs, so select a pool of them.
*
* Only one request selects the pool at a time. While it does, other requests select
* from the previous pool, if there is one, rather than each reading every eligible
* post. wp_cache_add() only stores the lock if another request does not hold it.
*/
$lock_key = $cache_key . ':lock';
$has_lock = wp_cache_add( $lock_key, 1, 'random-content', MINUTE_IN_SECONDS );
if ( ! $has_lock && null !== $stale_pool ) {
$post = $select_post( $stale_pool );
if ( null !== $post ) {
return array( $post );
}
if ( array() === $stale_pool ) {
return array();
}
}
$request = str_replace( $sample_clause, '1=1', $wpdb->remove_placeholder_escape( $query->request ) );
/*
* The post password is not indexed, so excluding password protected posts in SQL
* requires the database to read the full row of every eligible post. They are
* excluded when the post is selected from the pool instead, which allows the pool
* to be selected from an index.
*/
$password_clause = " AND {$wpdb->posts}.post_password = ''";
if ( 1 === substr_count( $request, $password_clause ) ) {
$request = str_replace( $password_clause, '', $request );
}
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- The request is prepared by WP_Query::get_posts().
$pool = array_map( 'intval', $wpdb->get_col( $request . ' ORDER BY RAND() LIMIT 1000' ) );
$post = $select_post( $pool );
/*
* A pool of fewer than 1,000 posts is the whole archive. If none of its posts are
* eligible, as they are all password protected, the archive has no eligible posts.
*/
if ( null === $post && count( $pool ) < 1000 ) {
$pool = array();
}
/*
* The pool is used for 10 minutes. It is stored for an hour, so that requests can
* select from it while the next pool is being selected.
*/
wp_cache_set(
$cache_key,
array(
'post_ids' => $pool,
'expires' => time() + 10 * MINUTE_IN_SECONDS,
),
'random-content',
HOUR_IN_SECONDS
);
if ( $has_lock ) {
wp_cache_delete( $lock_key, 'random-content' );
}
return null === $post ? array() : array( $post );
}