From 5c5a8f4e8e4dfa112e5ea8a7c86272b3980fe3fb Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 5 Sep 2017 11:54:04 +0100 Subject: [PATCH] Helper functions --- ...wc-rest-system-status-tools-controller.php | 23 +------------ includes/wc-core-functions.php | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/includes/api/class-wc-rest-system-status-tools-controller.php b/includes/api/class-wc-rest-system-status-tools-controller.php index 17b0caef5d1..c84513df048 100644 --- a/includes/api/class-wc-rest-system-status-tools-controller.php +++ b/includes/api/class-wc-rest-system-status-tools-controller.php @@ -388,28 +388,7 @@ class WC_REST_System_Status_Tools_Controller extends WC_REST_Controller { $message = __( 'Product transients cleared', 'woocommerce' ); break; case 'clear_expired_transients' : - /* - * Deletes all expired transients. The multi-table delete syntax is used. - * to delete the transient record from table a, and the corresponding. - * transient_timeout record from table b. - * - * Based on code inside core's upgrade_network() function. - */ - $sql = "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"; - $rows = $wpdb->query( $wpdb->prepare( $sql, $wpdb->esc_like( '_transient_' ) . '%', $wpdb->esc_like( '_transient_timeout_' ) . '%', time() ) ); - - $sql = "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"; - $rows2 = $wpdb->query( $wpdb->prepare( $sql, $wpdb->esc_like( '_site_transient_' ) . '%', $wpdb->esc_like( '_site_transient_timeout_' ) . '%', time() ) ); - - $message = sprintf( __( '%d transients rows cleared', 'woocommerce' ), $rows + $rows2 ); + $message = sprintf( __( '%d transients rows cleared', 'woocommerce' ), wc_delete_expired_transients() ); break; case 'delete_orphaned_variations' : /** diff --git a/includes/wc-core-functions.php b/includes/wc-core-functions.php index 11b26a06acc..899d55313d5 100644 --- a/includes/wc-core-functions.php +++ b/includes/wc-core-functions.php @@ -1822,3 +1822,36 @@ function wc_prevent_dangerous_auto_updates( $should_update, $plugin ) { return $should_update; } add_filter( 'auto_update_plugin', 'wc_prevent_dangerous_auto_updates', 99, 2 ); + +/** + * Delete expired transients. + * + * Deletes all expired transients. The multi-table delete syntax is used. + * to delete the transient record from table a, and the corresponding. + * transient_timeout record from table b. + * + * Based on code inside core's upgrade_network() function. + * + * @since 3.2.0 + * @return int Number of transients that were cleared. + */ +function wc_delete_expired_transients() { + global $wpdb; + + $sql = "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"; + $rows = $wpdb->query( $wpdb->prepare( $sql, $wpdb->esc_like( '_transient_' ) . '%', $wpdb->esc_like( '_transient_timeout_' ) . '%', time() ) ); + + $sql = "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"; + $rows2 = $wpdb->query( $wpdb->prepare( $sql, $wpdb->esc_like( '_site_transient_' ) . '%', $wpdb->esc_like( '_site_transient_timeout_' ) . '%', time() ) ); + + return absint( $rows + $rows2 ); +} +add_action( 'woocommerce_installed', 'wc_delete_expired_transients' );