Skip to content

Commit 7cc076f

Browse files
committed
When deleting transients, also delete site transients.
1 parent 16d921e commit 7cc076f

File tree

1 file changed

+45
-10
lines changed

1 file changed

+45
-10
lines changed

src/Transient_Command.php

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ private function delete_expired() {
272272
}
273273

274274
// The above queries delete the transient and the transient timeout
275-
// thus each transient is counted as 2.
275+
// thus each transient is counted twice.
276276
$count = $count / 2;
277277

278278
if ( $count > 0 ) {
@@ -288,26 +288,61 @@ private function delete_expired() {
288288

289289
/**
290290
* Deletes all transients.
291+
*
292+
* Always deletes the transients from the database too.
291293
*/
292294
private function delete_all() {
293-
global $wpdb, $_wp_using_ext_object_cache;
295+
global $wpdb;
294296

295-
// Always delete all transients from DB too.
296297
$count = $wpdb->query(
297-
"DELETE FROM $wpdb->options
298-
WHERE option_name LIKE '\_transient\_%'
299-
OR option_name LIKE '\_site\_transient\_%'"
298+
$wpdb->prepare(
299+
"DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
300+
WHERE a.option_name LIKE %s
301+
AND a.option_name NOT LIKE %s
302+
AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) )",
303+
$wpdb->esc_like( '_transient_' ) . '%',
304+
$wpdb->esc_like( '_transient_timeout_' ) . '%'
305+
)
300306
);
301307

308+
if ( ! is_multisite() ) {
309+
// Non-Multisite stores site transients in the options table.
310+
$count += $wpdb->query(
311+
$wpdb->prepare(
312+
"DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
313+
WHERE a.option_name LIKE %s
314+
AND a.option_name NOT LIKE %s
315+
AND b.option_name = CONCAT( '_site_transient_timeout_', SUBSTRING( a.option_name, 17 ) )",
316+
$wpdb->esc_like( '_site_transient_' ) . '%',
317+
$wpdb->esc_like( '_site_transient_timeout_' ) . '%'
318+
)
319+
);
320+
} elseif ( is_multisite() && is_main_site() && is_main_network() ) {
321+
// Multisite stores site transients in the sitemeta table.
322+
$count += $wpdb->query(
323+
$wpdb->prepare(
324+
"DELETE a, b FROM {$wpdb->sitemeta} a, {$wpdb->sitemeta} b
325+
WHERE a.meta_key LIKE %s
326+
AND a.meta_key NOT LIKE %s
327+
AND b.meta_key = CONCAT( '_site_transient_timeout_', SUBSTRING( a.meta_key, 17 ) )",
328+
$wpdb->esc_like( '_site_transient_' ) . '%',
329+
$wpdb->esc_like( '_site_transient_timeout_' ) . '%'
330+
)
331+
);
332+
}
333+
334+
// The above queries delete the transient and the transient timeout
335+
// thus each transient is counted twice.
336+
$count = $count / 2;
337+
302338
if ( $count > 0 ) {
303339
WP_CLI::success( "$count transients deleted from the database." );
304340
} else {
305-
WP_CLI::success( "No transients found." );
341+
WP_CLI::success( 'No transients found.' );
306342
}
307343

308-
if ( $_wp_using_ext_object_cache ) {
309-
WP_CLI::warning( 'Transients are stored in an external object cache, and this command only deletes those stored in the database. You must flush the cache to delete all transients.');
344+
if ( wp_using_ext_object_cache() ) {
345+
WP_CLI::warning( 'Transients are stored in an external object cache, and this command only deletes those stored in the database. You must flush the cache to delete all transients.' );
310346
}
311347
}
312-
313348
}

0 commit comments

Comments
 (0)