From 5814dc3a97193dc9c23d7c53b68db7731751d5bb Mon Sep 17 00:00:00 2001 From: rspublishing Date: Fri, 30 Sep 2016 10:39:37 +0200 Subject: [PATCH 1/3] Adds a system status tool for clearing orphaned variations --- ...-wc-rest-system-status-tools-controller.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) 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 e8c56c8c1cf..faa3b1f9244 100644 --- a/includes/api/class-wc-rest-system-status-tools-controller.php +++ b/includes/api/class-wc-rest-system-status-tools-controller.php @@ -121,6 +121,11 @@ class WC_REST_System_Status_Tools_Controller extends WC_REST_Controller { 'button' => __( 'Clear expired transients', 'woocommerce' ), 'desc' => __( 'This tool will clear ALL expired transients from WordPress.', 'woocommerce' ), ), + 'delete_orphaned_variations' => array( + 'name' => __( 'Orphaned Variations','woocommerce'), + 'button' => __( 'Delete orphaned variations','woocommerce' ), + 'desc' => __( 'This tool will delete all variations which have no parent.', 'woocommerce' ), + ), 'recount_terms' => array( 'name' => __( 'Term counts', 'woocommerce' ), 'button' => __( 'Recount terms', 'woocommerce' ), @@ -199,7 +204,6 @@ class WC_REST_System_Status_Tools_Controller extends WC_REST_Controller { /** * Update (execute) a tool. - * @param WP_REST_Request $request * @return WP_Error|WP_REST_Response */ @@ -374,6 +378,18 @@ class WC_REST_System_Status_Tools_Controller extends WC_REST_Controller { $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 ); + break; + /** + * Delete orphans + */ + case 'delete_orphaned_variations' : + global $wpdb; + // Delete meta and term relationships with no post + $result = absint( $wpdb->query( "DELETE products + FROM {$wpdb->posts} products + LEFT JOIN {$wpdb->posts} wp ON wp.ID = products.post_parent + WHERE wp.ID IS NULL AND products.post_type = 'product_variation';" ) ); + $message = sprintf( __( '%d Orphaned Variations Deleted', 'woocommerce' ), $result ); break; case 'reset_roles' : // Remove then re-add caps and roles From 424948f4de2dc59057a339d3157a4669bbdd0c79 Mon Sep 17 00:00:00 2001 From: rspublishing Date: Fri, 30 Sep 2016 16:15:56 +0200 Subject: [PATCH 2/3] Coding standards --- .../class-wc-rest-system-status-tools-controller.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 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 faa3b1f9244..600295b3a0c 100644 --- a/includes/api/class-wc-rest-system-status-tools-controller.php +++ b/includes/api/class-wc-rest-system-status-tools-controller.php @@ -122,10 +122,10 @@ class WC_REST_System_Status_Tools_Controller extends WC_REST_Controller { 'desc' => __( 'This tool will clear ALL expired transients from WordPress.', 'woocommerce' ), ), 'delete_orphaned_variations' => array( - 'name' => __( 'Orphaned Variations','woocommerce'), - 'button' => __( 'Delete orphaned variations','woocommerce' ), - 'desc' => __( 'This tool will delete all variations which have no parent.', 'woocommerce' ), - ), + 'name' => __( 'Orphaned Variations','woocommerce' ), + 'button' => __( 'Delete orphaned variations','woocommerce' ), + 'desc' => __( 'This tool will delete all variations which have no parent.', 'woocommerce' ), + ), 'recount_terms' => array( 'name' => __( 'Term counts', 'woocommerce' ), 'button' => __( 'Recount terms', 'woocommerce' ), @@ -379,12 +379,10 @@ class WC_REST_System_Status_Tools_Controller extends WC_REST_Controller { $message = sprintf( __( '%d Transients Rows Cleared', 'woocommerce' ), $rows + $rows2 ); break; + case 'delete_orphaned_variations' : /** * Delete orphans */ - case 'delete_orphaned_variations' : - global $wpdb; - // Delete meta and term relationships with no post $result = absint( $wpdb->query( "DELETE products FROM {$wpdb->posts} products LEFT JOIN {$wpdb->posts} wp ON wp.ID = products.post_parent From 180ac717a8b41d806feeb6df265cacfcdb5abbd6 Mon Sep 17 00:00:00 2001 From: rspublishing Date: Fri, 30 Sep 2016 16:23:17 +0200 Subject: [PATCH 3/3] Coding standards --- .../api/class-wc-rest-system-status-tools-controller.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 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 600295b3a0c..a5a5df66112 100644 --- a/includes/api/class-wc-rest-system-status-tools-controller.php +++ b/includes/api/class-wc-rest-system-status-tools-controller.php @@ -122,10 +122,10 @@ class WC_REST_System_Status_Tools_Controller extends WC_REST_Controller { 'desc' => __( 'This tool will clear ALL expired transients from WordPress.', 'woocommerce' ), ), 'delete_orphaned_variations' => array( - 'name' => __( 'Orphaned Variations','woocommerce' ), - 'button' => __( 'Delete orphaned variations','woocommerce' ), - 'desc' => __( 'This tool will delete all variations which have no parent.', 'woocommerce' ), - ), + 'name' => __( 'Orphaned Variations', 'woocommerce' ), + 'button' => __( 'Delete orphaned variations', 'woocommerce' ), + 'desc' => __( 'This tool will delete all variations which have no parent.', 'woocommerce' ), + ), 'recount_terms' => array( 'name' => __( 'Term counts', 'woocommerce' ), 'button' => __( 'Recount terms', 'woocommerce' ),