Update our schedule actions to “trash” status and let Action Scheduler handle cleanup.

This commit is contained in:
Jeff Stieler 2019-03-13 11:48:38 -06:00
parent 04db0e2b8d
commit d6fa5b58f6
1 changed files with 23 additions and 20 deletions

View File

@ -37,31 +37,34 @@ class WC_Admin_ActionScheduler_WPPostStore extends ActionScheduler_wpPostStore {
/** /**
* Forcefully delete all pending WC Admin scheduled actions. * Forcefully delete all pending WC Admin scheduled actions.
* *
* Directly deletes items from the database for performance. * Directly trashes items from in database for performance.
*/ */
public function clear_pending_wcadmin_actions() { public function clear_pending_wcadmin_actions() {
global $wpdb; global $wpdb;
// Remove all scheduled action posts and their metadata. // Cancel all pending actions by trashing the posts.
$delete_pending_sql = // Action Scheduler will handle the cleanup.
"DELETE p.*, pm.* FROM {$wpdb->posts} p $action_types = array(
JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id WC_Admin_Reports_Sync::QUEUE_BATCH_ACTION,
WHERE post_type = 'scheduled-action' WC_Admin_Reports_Sync::QUEUE_DEPEDENT_ACTION,
AND post_status = 'pending' WC_Admin_Reports_Sync::CUSTOMERS_BATCH_ACTION,
AND post_title LIKE 'wc-admin_%'"; WC_Admin_Reports_Sync::ORDERS_BATCH_ACTION,
WC_Admin_Reports_Sync::ORDERS_LOOKUP_BATCH_INIT,
WC_Admin_Reports_Sync::SINGLE_ORDER_ACTION,
);
// phpcs:ignore WordPress.DB.PreparedSQL foreach ( $action_types as $action_type ) {
$wpdb->query( $delete_pending_sql ); $wpdb->update(
$wpdb->posts,
// Delete all taxonomy data related to the WC Admin scheduled action group. array(
$group_term = get_term_by( 'slug', WC_Admin_Reports_Sync::QUEUE_GROUP, parent::GROUP_TAXONOMY ); 'post_status' => 'trash',
),
if ( $group_term ) { array(
$wpdb->delete( $wpdb->term_relationships, array( 'term_taxonomy_id' => $group_term->term_taxonomy_id ), array( '%d' ) ); 'post_type' => 'scheduled-action',
$wpdb->delete( $wpdb->term_taxonomy, array( 'term_id' => $group_term->term_id ), array( '%d' ) ); 'post_status' => 'pending',
$wpdb->delete( $wpdb->terms, array( 'term_id' => $group_term->term_id ), array( '%d' ) ); 'post_title' => $action_type,
)
clean_taxonomy_cache( parent::GROUP_TAXONOMY ); );
} }
} }
} }