-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Allow deleting expired transients by prefix #10830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
589a402
a674bce
a4a4c15
e5aa26e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1599,8 +1599,11 @@ function set_transient( $transient, $value, $expiration = 0 ) { | |
| * @since 6.8.0 | ||
| * | ||
| * @param string $transient The name of the transient. | ||
| * @since 6.8.0 | ||
| * @param mixed $value Transient value. | ||
| * @since 6.8.0 | ||
| * @param int $expiration Time until expiration in seconds. | ||
| * @since 6.8.0 | ||
| */ | ||
| do_action( 'set_transient', $transient, $value, $expiration ); | ||
|
|
||
|
|
@@ -1633,53 +1636,86 @@ function set_transient( $transient, $value, $expiration = 0 ) { | |
| * | ||
| * @global wpdb $wpdb WordPress database abstraction object. | ||
| * | ||
| * @param bool $force_db Optional. Force cleanup to run against the database even when an external object cache is used. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe global documentation should not be removed.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the note — I’ve restored the @global wpdb documentation and pushed an update. |
||
| * @param bool $force_db Optional. Force cleanup to run against the database even when an | ||
| * external object cache is used. Default false. | ||
| * @param string|bool $prefix Optional. Transient name prefix. When provided, only expired transients | ||
| * matching this prefix will be deleted. This parameter is only applied | ||
| * when not using an external object cache or when `$force_db` is true. | ||
| * Default false. | ||
| */ | ||
| function delete_expired_transients( $force_db = false ) { | ||
| function delete_expired_transients( $force_db = false, $prefix = false ) { | ||
| global $wpdb; | ||
|
|
||
| // Skip DB cleanup when using external object cache unless forced. | ||
| if ( ! $force_db && wp_using_ext_object_cache() ) { | ||
| return; | ||
| } | ||
|
|
||
| $transient_like = $wpdb->esc_like( | ||
| '_transient_' . ( $prefix ? $prefix : '' ) | ||
| ) . '%'; | ||
|
|
||
| $timeout_like = $wpdb->esc_like( '_transient_timeout_' ) . '%'; | ||
|
|
||
| $wpdb->query( | ||
| $wpdb->prepare( | ||
| "DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b | ||
| WHERE a.option_name LIKE %s | ||
| AND a.option_name NOT LIKE %s | ||
| AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) ) | ||
| AND b.option_value < %d", | ||
| $wpdb->esc_like( '_transient_' ) . '%', | ||
| $wpdb->esc_like( '_transient_timeout_' ) . '%', | ||
| "DELETE a, b | ||
| FROM {$wpdb->options} a | ||
| JOIN {$wpdb->options} b | ||
| ON b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) ) | ||
| WHERE a.option_name LIKE %s | ||
| AND a.option_name NOT LIKE %s | ||
| AND b.option_value < %d", | ||
| $transient_like, | ||
| $timeout_like, | ||
| time() | ||
| ) | ||
| ); | ||
|
|
||
| if ( ! is_multisite() ) { | ||
| // Single site stores site transients in the options table. | ||
| // Single-site: site transients stored in options table. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Revert |
||
|
|
||
| $site_transient_like = $wpdb->esc_like( | ||
| '_site_transient_' . ( $prefix ? $prefix : '' ) | ||
| ) . '%'; | ||
|
|
||
| $site_timeout_like = $wpdb->esc_like( '_site_transient_timeout_' ) . '%'; | ||
|
|
||
| $wpdb->query( | ||
| $wpdb->prepare( | ||
| "DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b | ||
| WHERE a.option_name LIKE %s | ||
| AND a.option_name NOT LIKE %s | ||
| AND b.option_name = CONCAT( '_site_transient_timeout_', SUBSTRING( a.option_name, 17 ) ) | ||
| AND b.option_value < %d", | ||
| $wpdb->esc_like( '_site_transient_' ) . '%', | ||
| $wpdb->esc_like( '_site_transient_timeout_' ) . '%', | ||
| "DELETE a, b | ||
| FROM {$wpdb->options} a | ||
| JOIN {$wpdb->options} b | ||
| ON b.option_name = CONCAT( '_site_transient_timeout_', SUBSTRING( a.option_name, 17 ) ) | ||
| WHERE a.option_name LIKE %s | ||
| AND a.option_name NOT LIKE %s | ||
| AND b.option_value < %d", | ||
| $site_transient_like, | ||
| $site_timeout_like, | ||
| time() | ||
| ) | ||
| ); | ||
|
|
||
| } elseif ( is_main_site() && is_main_network() ) { | ||
| // Multisite stores site transients in the sitemeta table. | ||
| // Multisite: site transients stored in sitemeta. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Revert |
||
|
|
||
| $site_transient_like = $wpdb->esc_like( | ||
| '_site_transient_' . ( $prefix ? $prefix : '' ) | ||
| ) . '%'; | ||
|
|
||
| $site_timeout_like = $wpdb->esc_like( '_site_transient_timeout_' ) . '%'; | ||
|
|
||
| $wpdb->query( | ||
| $wpdb->prepare( | ||
| "DELETE a, b FROM {$wpdb->sitemeta} a, {$wpdb->sitemeta} b | ||
| WHERE a.meta_key LIKE %s | ||
| AND a.meta_key NOT LIKE %s | ||
| AND b.meta_key = CONCAT( '_site_transient_timeout_', SUBSTRING( a.meta_key, 17 ) ) | ||
| AND b.meta_value < %d", | ||
| $wpdb->esc_like( '_site_transient_' ) . '%', | ||
| $wpdb->esc_like( '_site_transient_timeout_' ) . '%', | ||
| "DELETE a, b | ||
| FROM {$wpdb->sitemeta} a | ||
| JOIN {$wpdb->sitemeta} b | ||
| ON b.meta_key = CONCAT( '_site_transient_timeout_', SUBSTRING( a.meta_key, 17 ) ) | ||
| WHERE a.meta_key LIKE %s | ||
| AND a.meta_key NOT LIKE %s | ||
| AND b.meta_value < %d", | ||
| $site_transient_like, | ||
| $site_timeout_like, | ||
| time() | ||
| ) | ||
| ); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add
@sinceannotation for new param.