Filter
uncanny-automator
automator_get_users_expiry_time
Filters the cache expiration time for user data before it's stored, allowing custom durations.
add_filter( 'automator_get_users_expiry_time', $callback, 10, 1 );
Description
Allows modification of the cache expiration time for fetched user lists. Developers can adjust the default caching duration of three minutes before user data is considered stale, influencing how frequently the user list is re-fetched from the database.
Usage
add_filter( 'automator_get_users_expiry_time', 'your_function_name', 10, 1 );
Parameters
-
$users(mixed) - This parameter represents the cache expiration time for the user data.
Return Value
The filtered value.
Examples
/**
* Modify the cache expiration time for user queries.
*
* This filter allows developers to dynamically adjust how long user query results
* are cached. For example, we might want to reduce the cache time during periods
* of high user churn or increase it for static user lists.
*
* @param int $expiration_time The current cache expiration time in seconds.
* @param array $users The array of user objects that were fetched.
*
* @return int The modified cache expiration time in seconds.
*/
add_filter( 'automator_get_users_expiry_time', function( $expiration_time, $users ) {
// If there are a very large number of users, we might want to reduce
// the cache expiration to ensure we're not serving stale data for
// a rapidly changing user base. Let's say for more than 1000 users,
// we halve the cache time.
if ( is_array( $users ) && count( $users ) > 1000 ) {
// Ensure we don't set a negative expiration time, though unlikely.
return max( HOUR_IN_SECONDS, floor( $expiration_time / 2 ) );
}
// Otherwise, return the original expiration time.
return $expiration_time;
}, 10, 2 );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/core/lib/helpers/class-automator-recipe-helpers.php:1003
public function wp_users( $limit = 99999 ) {
global $wpdb;
// prepare transient key.
$transient_key = 'automator_transient_users';
// attempt fetching options from transient.
$users = Automator()->cache->get( 'uap_transient_users' );
if ( empty( $users ) ) {
$query = apply_filters(
'automator_get_users_query',
"SELECT ID, display_name
FROM $wpdb->users
ORDER BY display_name
LIMIT 0, $limit"
);
$users = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
// save fetched posts in a transient for 3 minutes for performance gains.
$expiration_time = apply_filters( 'automator_get_users_expiry_time', Automator()->cache->expires, $users );
Automator()->cache->set( $transient_key, $users, 'automator', $expiration_time );
}
return apply_filters( 'automator_modify_user_results', $users );
}