Skip to content

Commit d46dada

Browse files
committed
Honor the --network flag when deleting all/expired transients
1 parent 1c61b98 commit d46dada

File tree

2 files changed

+131
-102
lines changed

2 files changed

+131
-102
lines changed

features/transient.feature

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@ Feature: Manage WordPress transient cache
5555
Success: Transient deleted.
5656
"""
5757

58-
When I run `wp transient delete --all`
59-
And I run `wp transient set foo bar --network`
58+
When I run `wp transient set foo bar --network`
6059
And I run `wp transient set foo2 bar2 --network`
61-
And I run `wp transient delete --all`
60+
And I run `wp transient delete --all --network`
6261
Then STDOUT should be:
6362
"""
6463
Success: 2 transients deleted from the database.
@@ -73,14 +72,19 @@ Feature: Manage WordPress transient cache
7372
Error: Please specify transient key, or use --all or --expired.
7473
"""
7574

76-
When I run `wp transient delete --all`
77-
And I run `wp transient set foo bar`
75+
When I run `wp transient set foo bar`
7876
And I run `wp transient set foo2 bar2`
7977
And I run `wp transient set foo3 bar3 --network`
8078
And I run `wp transient delete --all`
8179
Then STDOUT should be:
8280
"""
83-
Success: 3 transients deleted from the database.
81+
Success: 2 transients deleted from the database.
82+
"""
83+
84+
When I run `wp transient delete --all --network`
85+
Then STDOUT should be:
86+
"""
87+
Success: 1 transient deleted from the database.
8488
"""
8589

8690
When I try `wp transient get foo`
@@ -129,7 +133,7 @@ Feature: Manage WordPress transient cache
129133

130134
# Change timeout to be in the past.
131135
When I run `wp option update _site_transient_timeout_foo 1321009871`
132-
And I run `wp transient delete --expired`
136+
And I run `wp transient delete --expired --network`
133137
Then STDOUT should be:
134138
"""
135139
Success: 1 expired transient deleted from the database.
@@ -141,28 +145,38 @@ Feature: Manage WordPress transient cache
141145
Warning: Transient with key "foo" is not set.
142146
"""
143147

144-
When I run `wp transient delete --all`
145-
And I run `wp transient set foo bar`
148+
When I run `wp transient set foo bar`
146149
And I run `wp transient set foo2 bar2 600`
147150
And I run `wp transient set foo3 bar3 --network`
148151
And I run `wp transient set foo4 bar4 600 --network`
149152
And I run `wp transient delete --all`
150153
Then STDOUT should be:
151154
"""
152-
Success: 4 transients deleted from the database.
155+
Success: 2 transients deleted from the database.
156+
"""
157+
158+
When I run `wp transient delete --all --network`
159+
Then STDOUT should be:
160+
"""
161+
Success: 2 transients deleted from the database.
153162
"""
154163

155164
Scenario: Network transient delete and other flags
156165
Given a WP multisite install
157166

158-
When I run `wp transient delete --all`
159-
And I run `wp transient set foo bar`
167+
When I run `wp transient set foo bar`
160168
And I run `wp transient set foo2 bar2`
161169
And I run `wp transient set foo3 bar3 --network`
162170
And I run `wp transient delete --all`
163171
Then STDOUT should be:
164172
"""
165-
Success: 3 transients deleted from the database.
173+
Success: 2 transients deleted from the database.
174+
"""
175+
176+
When I run `wp transient delete --all --network`
177+
Then STDOUT should be:
178+
"""
179+
Success: 1 transient deleted from the database.
166180
"""
167181

168182
When I try `wp transient get foo`
@@ -211,7 +225,7 @@ Feature: Manage WordPress transient cache
211225

212226
# Change timeout to be in the past.
213227
When I run `wp site option update _site_transient_timeout_foo 1321009871`
214-
And I run `wp transient delete --expired`
228+
And I run `wp transient delete --expired --network`
215229
Then STDOUT should be:
216230
"""
217231
Success: 1 expired transient deleted from the database.
@@ -223,13 +237,18 @@ Feature: Manage WordPress transient cache
223237
Warning: Transient with key "foo" is not set.
224238
"""
225239

226-
When I run `wp transient delete --all`
227-
And I run `wp transient set foo bar`
240+
When I run `wp transient set foo bar`
228241
And I run `wp transient set foo2 bar2 600`
229242
And I run `wp transient set foo3 bar3 --network`
230243
And I run `wp transient set foo4 bar4 600 --network`
231244
And I run `wp transient delete --all`
232245
Then STDOUT should be:
233246
"""
234-
Success: 4 transients deleted from the database.
247+
Success: 2 transients deleted from the database.
248+
"""
249+
250+
When I run `wp transient delete --all --network`
251+
Then STDOUT should be:
252+
"""
253+
Success: 2 transients deleted from the database.
235254
"""

src/Transient_Command.php

Lines changed: 95 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -168,23 +168,23 @@ public function set( $args, $assoc_args ) {
168168
public function delete( $args, $assoc_args ) {
169169
$key = ( ! empty( $args ) ) ? $args[0] : NULL;
170170

171-
$all = Utils\get_flag_value( $assoc_args, 'all' );
171+
$all = Utils\get_flag_value( $assoc_args, 'all' );
172172
$expired = Utils\get_flag_value( $assoc_args, 'expired' );
173+
$network = Utils\get_flag_value( $assoc_args, 'network' );
173174

174175
if ( true === $all ) {
175-
$this->delete_all();
176+
$this->delete_all( $network );
176177
return;
177-
}
178-
else if ( true === $expired ) {
179-
$this->delete_expired();
178+
} elseif ( true === $expired ) {
179+
$this->delete_expired( $network );
180180
return;
181181
}
182182

183183
if ( ! $key ) {
184184
WP_CLI::error( 'Please specify transient key, or use --all or --expired.' );
185185
}
186186

187-
$func = Utils\get_flag_value( $assoc_args, 'network' ) ? 'delete_site_transient' : 'delete_transient';
187+
$func = $network ? 'delete_site_transient' : 'delete_transient';
188188

189189
if ( $func( $key ) ) {
190190
WP_CLI::success( 'Transient deleted.' );
@@ -225,51 +225,57 @@ public function type() {
225225
* Deletes all expired transients.
226226
*
227227
* Only deletes the expired transients from the database.
228+
*
229+
* @param bool $network Whether to delete transients or network|site transients.
228230
*/
229-
private function delete_expired() {
231+
private function delete_expired( $network ) {
230232
global $wpdb;
231233

232-
$count = $wpdb->query(
233-
$wpdb->prepare(
234-
"DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
235-
WHERE a.option_name LIKE %s
236-
AND a.option_name NOT LIKE %s
237-
AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) )
238-
AND b.option_value < %d",
239-
Utils\esc_like( '_transient_' ) . '%',
240-
Utils\esc_like( '_transient_timeout_' ) . '%',
241-
time()
242-
)
243-
);
244-
245-
if ( ! is_multisite() ) {
246-
// Non-Multisite stores site transients in the options table.
234+
$count = 0;
235+
236+
if ( ! $network ) {
247237
$count += $wpdb->query(
248238
$wpdb->prepare(
249239
"DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
250240
WHERE a.option_name LIKE %s
251241
AND a.option_name NOT LIKE %s
252-
AND b.option_name = CONCAT( '_site_transient_timeout_', SUBSTRING( a.option_name, 17 ) )
242+
AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) )
253243
AND b.option_value < %d",
254-
Utils\esc_like( '_site_transient_' ) . '%',
255-
Utils\esc_like( '_site_transient_timeout_' ) . '%',
244+
Utils\esc_like( '_transient_' ) . '%',
245+
Utils\esc_like( '_transient_timeout_' ) . '%',
256246
time()
257247
)
258248
);
259249
} else {
260-
// Multisite stores site transients in the sitemeta table.
261-
$count += $wpdb->query(
262-
$wpdb->prepare(
263-
"DELETE a, b FROM {$wpdb->sitemeta} a, {$wpdb->sitemeta} b
264-
WHERE a.meta_key LIKE %s
265-
AND a.meta_key NOT LIKE %s
266-
AND b.meta_key = CONCAT( '_site_transient_timeout_', SUBSTRING( a.meta_key, 17 ) )
267-
AND b.meta_value < %d",
268-
Utils\esc_like( '_site_transient_' ) . '%',
269-
Utils\esc_like( '_site_transient_timeout_' ) . '%',
270-
time()
271-
)
272-
);
250+
if ( ! is_multisite() ) {
251+
// Non-Multisite stores site transients in the options table.
252+
$count += $wpdb->query(
253+
$wpdb->prepare(
254+
"DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
255+
WHERE a.option_name LIKE %s
256+
AND a.option_name NOT LIKE %s
257+
AND b.option_name = CONCAT( '_site_transient_timeout_', SUBSTRING( a.option_name, 17 ) )
258+
AND b.option_value < %d",
259+
Utils\esc_like( '_site_transient_' ) . '%',
260+
Utils\esc_like( '_site_transient_timeout_' ) . '%',
261+
time()
262+
)
263+
);
264+
} else {
265+
// Multisite stores site transients in the sitemeta table.
266+
$count += $wpdb->query(
267+
$wpdb->prepare(
268+
"DELETE a, b FROM {$wpdb->sitemeta} a, {$wpdb->sitemeta} b
269+
WHERE a.meta_key LIKE %s
270+
AND a.meta_key NOT LIKE %s
271+
AND b.meta_key = CONCAT( '_site_transient_timeout_', SUBSTRING( a.meta_key, 17 ) )
272+
AND b.meta_value < %d",
273+
Utils\esc_like( '_site_transient_' ) . '%',
274+
Utils\esc_like( '_site_transient_timeout_' ) . '%',
275+
time()
276+
)
277+
);
278+
}
273279
}
274280

275281
// The above queries delete the transient and the transient timeout
@@ -297,44 +303,25 @@ private function delete_expired() {
297303
* Deletes all transients.
298304
*
299305
* Only deletes the transients from the database.
306+
*
307+
* @param bool $network Whether to delete transients or network|site transients.
300308
*/
301-
private function delete_all() {
309+
private function delete_all( $network ) {
302310
global $wpdb;
303311

304312
// To ensure proper count values we first delete all transients with a timeout
305313
// and then the remaining transients without a timeout.
306314
$count = 0;
307315

308-
$deleted = $wpdb->query(
309-
$wpdb->prepare(
310-
"DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
311-
WHERE a.option_name LIKE %s
312-
AND a.option_name NOT LIKE %s
313-
AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) )",
314-
Utils\esc_like( '_transient_' ) . '%',
315-
Utils\esc_like( '_transient_timeout_' ) . '%'
316-
)
317-
);
318-
319-
$count += $deleted / 2; // Ignore affected rows for timeouts.
320-
321-
$count += $wpdb->query(
322-
$wpdb->prepare(
323-
"DELETE FROM $wpdb->options WHERE option_name LIKE %s",
324-
Utils\esc_like( '_transient_' ) . '%'
325-
)
326-
);
327-
328-
if ( ! is_multisite() ) {
329-
// Non-Multisite stores site transients in the options table.
316+
if ( ! $network ) {
330317
$deleted = $wpdb->query(
331318
$wpdb->prepare(
332319
"DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
333320
WHERE a.option_name LIKE %s
334321
AND a.option_name NOT LIKE %s
335-
AND b.option_name = CONCAT( '_site_transient_timeout_', SUBSTRING( a.option_name, 17 ) )",
336-
Utils\esc_like( '_site_transient_' ) . '%',
337-
Utils\esc_like( '_site_transient_timeout_' ) . '%'
322+
AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) )",
323+
Utils\esc_like( '_transient_' ) . '%',
324+
Utils\esc_like( '_transient_timeout_' ) . '%'
338325
)
339326
);
340327

@@ -343,30 +330,53 @@ private function delete_all() {
343330
$count += $wpdb->query(
344331
$wpdb->prepare(
345332
"DELETE FROM $wpdb->options WHERE option_name LIKE %s",
346-
Utils\esc_like( '_site_transient_' ) . '%'
333+
Utils\esc_like( '_transient_' ) . '%'
347334
)
348335
);
349336
} else {
350-
// Multisite stores site transients in the sitemeta table.
351-
$deleted = $wpdb->query(
352-
$wpdb->prepare(
353-
"DELETE a, b FROM {$wpdb->sitemeta} a, {$wpdb->sitemeta} b
354-
WHERE a.meta_key LIKE %s
355-
AND a.meta_key NOT LIKE %s
356-
AND b.meta_key = CONCAT( '_site_transient_timeout_', SUBSTRING( a.meta_key, 17 ) )",
357-
Utils\esc_like( '_site_transient_' ) . '%',
358-
Utils\esc_like( '_site_transient_timeout_' ) . '%'
359-
)
360-
);
361-
362-
$count += $deleted / 2; // Ignore affected rows for timeouts.
363-
364-
$count += $wpdb->query(
365-
$wpdb->prepare(
366-
"DELETE FROM $wpdb->sitemeta WHERE meta_key LIKE %s",
367-
Utils\esc_like( '_site_transient_' ) . '%'
368-
)
369-
);
337+
if ( ! is_multisite() ) {
338+
// Non-Multisite stores site transients in the options table.
339+
$deleted = $wpdb->query(
340+
$wpdb->prepare(
341+
"DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
342+
WHERE a.option_name LIKE %s
343+
AND a.option_name NOT LIKE %s
344+
AND b.option_name = CONCAT( '_site_transient_timeout_', SUBSTRING( a.option_name, 17 ) )",
345+
Utils\esc_like( '_site_transient_' ) . '%',
346+
Utils\esc_like( '_site_transient_timeout_' ) . '%'
347+
)
348+
);
349+
350+
$count += $deleted / 2; // Ignore affected rows for timeouts.
351+
352+
$count += $wpdb->query(
353+
$wpdb->prepare(
354+
"DELETE FROM $wpdb->options WHERE option_name LIKE %s",
355+
Utils\esc_like( '_site_transient_' ) . '%'
356+
)
357+
);
358+
} else {
359+
// Multisite stores site transients in the sitemeta table.
360+
$deleted = $wpdb->query(
361+
$wpdb->prepare(
362+
"DELETE a, b FROM {$wpdb->sitemeta} a, {$wpdb->sitemeta} b
363+
WHERE a.meta_key LIKE %s
364+
AND a.meta_key NOT LIKE %s
365+
AND b.meta_key = CONCAT( '_site_transient_timeout_', SUBSTRING( a.meta_key, 17 ) )",
366+
Utils\esc_like( '_site_transient_' ) . '%',
367+
Utils\esc_like( '_site_transient_timeout_' ) . '%'
368+
)
369+
);
370+
371+
$count += $deleted / 2; // Ignore affected rows for timeouts.
372+
373+
$count += $wpdb->query(
374+
$wpdb->prepare(
375+
"DELETE FROM $wpdb->sitemeta WHERE meta_key LIKE %s",
376+
Utils\esc_like( '_site_transient_' ) . '%'
377+
)
378+
);
379+
}
370380
}
371381

372382
if ( $count > 0 ) {

0 commit comments

Comments
 (0)