Skip to content

Commit 2206b8d

Browse files
committed
Use polyfill for esc_like() to support older WP core versions.
1 parent 219d045 commit 2206b8d

File tree

1 file changed

+41
-21
lines changed

1 file changed

+41
-21
lines changed

src/Transient_Command.php

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -228,33 +228,46 @@ public function type() {
228228
private function delete_expired() {
229229
global $wpdb;
230230

231-
$time = time();
232-
233231
$count = $wpdb->query(
234-
"DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
235-
WHERE a.option_name LIKE '\\_transient\\_%'
236-
AND a.option_name NOT LIKE '\\_transient\\_timeout\\_%'
237-
AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) )
238-
AND b.option_value < {$time}"
232+
$wpdb->prepare(
233+
"DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
234+
WHERE a.option_name LIKE %s
235+
AND a.option_name NOT LIKE %s
236+
AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) )
237+
AND b.option_value < %d",
238+
\WP_CLI\Utils\esc_like( '_transient_' ) . '%',
239+
\WP_CLI\Utils\esc_like( '_transient_timeout_' ) . '%',
240+
time()
241+
)
239242
);
240243

241244
if ( ! is_multisite() ) {
242245
// Non-Multisite stores site transients in the options table.
243246
$count += $wpdb->query(
244-
"DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
245-
WHERE a.option_name LIKE '\\_site\\_transient\\_%'
246-
AND a.option_name NOT LIKE '\\_site\\_transient\\_timeout\\_%'
247-
AND b.option_name = CONCAT( '_site_transient_timeout_', SUBSTRING( a.option_name, 17 ) )
248-
AND b.option_value < {$time}"
247+
$wpdb->prepare(
248+
"DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
249+
WHERE a.option_name LIKE %s
250+
AND a.option_name NOT LIKE %s
251+
AND b.option_name = CONCAT( '_site_transient_timeout_', SUBSTRING( a.option_name, 17 ) )
252+
AND b.option_value < %d",
253+
\WP_CLI\Utils\esc_like( '_site_transient_' ) . '%',
254+
\WP_CLI\Utils\esc_like( '_site_transient_timeout_' ) . '%',
255+
time()
256+
)
249257
);
250258
} elseif ( is_multisite() && is_main_site() && is_main_network() ) {
251259
// Multisite stores site transients in the sitemeta table.
252260
$count += $wpdb->query(
253-
"DELETE a, b FROM {$wpdb->sitemeta} a, {$wpdb->sitemeta} b
254-
WHERE a.meta_key LIKE '\\_site\\_transient\\_%'
255-
AND a.meta_key NOT LIKE '\\_site\\_transient\\_timeout\\_%'
256-
AND b.meta_key = CONCAT( '_site_transient_timeout_', SUBSTRING( a.meta_key, 17 ) )
257-
AND b.meta_value < {$time}"
261+
$wpdb->prepare(
262+
"DELETE a, b FROM {$wpdb->sitemeta} a, {$wpdb->sitemeta} b
263+
WHERE a.meta_key LIKE %s
264+
AND a.meta_key NOT LIKE %s
265+
AND b.meta_key = CONCAT( '_site_transient_timeout_', SUBSTRING( a.meta_key, 17 ) )
266+
AND b.meta_value < %d",
267+
\WP_CLI\Utils\esc_like( '_site_transient_' ) . '%',
268+
\WP_CLI\Utils\esc_like( '_site_transient_timeout_' ) . '%',
269+
time()
270+
)
258271
);
259272
}
260273

@@ -283,18 +296,25 @@ private function delete_all() {
283296
global $wpdb;
284297

285298
$count = $wpdb->query(
286-
"DELETE FROM {$wpdb->options} WHERE option_name LIKE '\\_transient\\_%'"
299+
$wpdb->prepare(
300+
"DELETE FROM $wpdb->options WHERE option_name LIKE %s",
301+
\WP_CLI\Utils\esc_like( '_transient_' ) . '%'
302+
)
287303
);
288304

289305
if ( ! is_multisite() ) {
290306
// Non-Multisite stores site transients in the options table.
291307
$count += $wpdb->query(
292-
"DELETE FROM {$wpdb->options} WHERE option_name LIKE '\\_site\\_transient\\_%'"
308+
$wpdb->prepare(
309+
"DELETE FROM $wpdb->options WHERE option_name LIKE %s",
310+
\WP_CLI\Utils\esc_like( '_site_transient_' ) . '%'
311+
)
293312
);
294313
} elseif ( is_multisite() && is_main_site() && is_main_network() ) {
295314
// Multisite stores site transients in the sitemeta table.
296-
$count += $wpdb->query(
297-
"DELETE FROM {$wpdb->sitemeta} WHERE option_name LIKE '\\_site\\_transient\\_%'"
315+
$count += $wpdb->prepare(
316+
"DELETE FROM $wpdb->sitemeta WHERE option_name LIKE %s",
317+
\WP_CLI\Utils\esc_like( '_site_transient_' ) . '%'
298318
);
299319
}
300320

0 commit comments

Comments
 (0)