Skip to content

Commit

Permalink
Adds a system status tool for clearing orphaned variations
Browse files Browse the repository at this point in the history
  • Loading branch information
rspublishing committed Sep 30, 2016
1 parent 1fbc48a commit 5814dc3
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion includes/api/class-wc-rest-system-status-tools-controller.php
Expand Up @@ -121,6 +121,11 @@ public function get_tools() {
'button' => __( 'Clear expired transients', 'woocommerce' ), 'button' => __( 'Clear expired transients', 'woocommerce' ),
'desc' => __( 'This tool will clear ALL expired transients from WordPress.', '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( 'recount_terms' => array(
'name' => __( 'Term counts', 'woocommerce' ), 'name' => __( 'Term counts', 'woocommerce' ),
'button' => __( 'Recount terms', 'woocommerce' ), 'button' => __( 'Recount terms', 'woocommerce' ),
Expand Down Expand Up @@ -199,7 +204,6 @@ public function get_item( $request ) {


/** /**
* Update (execute) a tool. * Update (execute) a tool.
* @param WP_REST_Request $request * @param WP_REST_Request $request
* @return WP_Error|WP_REST_Response * @return WP_Error|WP_REST_Response
*/ */
Expand Down Expand Up @@ -374,6 +378,18 @@ public function execute_tool( $tool ) {
$rows2 = $wpdb->query( $wpdb->prepare( $sql, $wpdb->esc_like( '_site_transient_' ) . '%', $wpdb->esc_like( '_site_transient_timeout_' ) . '%', time() ) ); $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' ), $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; break;
case 'reset_roles' : case 'reset_roles' :
// Remove then re-add caps and roles // Remove then re-add caps and roles
Expand Down

0 comments on commit 5814dc3

Please sign in to comment.