@@ -222,28 +222,67 @@ public function type() {
222222
223223 /**
224224 * Deletes all expired transients.
225+ *
226+ * Always deletes the transients from the database too.
225227 */
226228 private function delete_expired () {
227- global $ wpdb, $ _wp_using_ext_object_cache ;
229+ global $ wpdb ;
228230
229- // Always delete all transients from DB too.
230- $ time = current_time ('timestamp ' );
231231 $ count = $ wpdb ->query (
232- "DELETE a, b FROM $ wpdb ->options a, $ wpdb ->options b WHERE
233- a.option_name LIKE '\_transient\_%' AND
234- a.option_name NOT LIKE '\_transient\_timeout\_%' AND
235- b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) )
236- 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+ $ wpdb ->esc_like ( '_transient_ ' ) . '% ' ,
239+ $ wpdb ->esc_like ( '_transient_timeout_ ' ) . '% ' ,
240+ time ()
241+ )
237242 );
238243
244+ if ( ! is_multisite () ) {
245+ // Non-Multisite stores site transients in the options table.
246+ $ count += $ wpdb ->query (
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+ $ wpdb ->esc_like ( '_site_transient_ ' ) . '% ' ,
254+ $ wpdb ->esc_like ( '_site_transient_timeout_ ' ) . '% ' ,
255+ time ()
256+ )
257+ );
258+ } elseif ( is_multisite () && is_main_site () && is_main_network () ) {
259+ // Multisite stores site transients in the sitemeta table.
260+ $ count += $ wpdb ->query (
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+ $ wpdb ->esc_like ( '_site_transient_ ' ) . '% ' ,
268+ $ wpdb ->esc_like ( '_site_transient_timeout_ ' ) . '% ' ,
269+ time ()
270+ )
271+ );
272+ }
273+
274+ // The above queries delete the transient and the transient timeout
275+ // thus each transient is counted as 2.
276+ $ count = $ count / 2 ;
277+
239278 if ( $ count > 0 ) {
240279 WP_CLI ::success ( "$ count expired transients deleted from the database. " );
241280 } else {
242- WP_CLI ::success ( " No expired transients found. " );
281+ WP_CLI ::success ( ' No expired transients found. ' );
243282 }
244283
245- if ( $ _wp_using_ext_object_cache ) {
246- 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. ' );
284+ if ( wp_using_ext_object_cache () ) {
285+ 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. ' );
247286 }
248287 }
249288
0 commit comments