From 2d707689d97b4252070215f3eff1d16a32c22bba Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 23 Jan 2015 13:28:30 +0000 Subject: [PATCH] Add transient cache to webhooks Closes #7184 --- includes/admin/class-wc-admin-webhooks.php | 8 +++++-- includes/api/class-wc-api-webhooks.php | 6 ++++++ woocommerce.php | 25 +++++++++++----------- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/includes/admin/class-wc-admin-webhooks.php b/includes/admin/class-wc-admin-webhooks.php index 33e287cb4a5..a4fa3d4dc0b 100644 --- a/includes/admin/class-wc-admin-webhooks.php +++ b/includes/admin/class-wc-admin-webhooks.php @@ -142,6 +142,8 @@ class WC_Admin_Webhooks { do_action( 'woocommerce_webhook_options_save', $webhook->id ); + delete_transient( 'woocommerce_webhook_ids' ); + // Redirect to webhook edit page to avoid settings save actions wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=webhooks&edit-webhook=' . $webhook->id . '&updated=1' ) ); exit(); @@ -171,6 +173,8 @@ class WC_Admin_Webhooks { update_post_meta( $webhook_id, '_webhook_pending_delivery', true ); + delete_transient( 'woocommerce_webhook_ids' ); + // Redirect to edit page wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=webhooks&edit-webhook=' . $webhook_id . '&created=1' ) ); exit(); @@ -227,6 +231,8 @@ class WC_Admin_Webhooks { $webhooks = array_map( 'absint', (array) $_GET['webhook'] ); + delete_transient( 'woocommerce_webhook_ids' ); + switch ( $_GET['action'] ) { case 'trash' : $this->bulk_trash( $webhooks ); @@ -237,7 +243,6 @@ class WC_Admin_Webhooks { case 'delete' : $this->bulk_trash( $webhooks, true ); break; - default : break; } @@ -275,7 +280,6 @@ class WC_Admin_Webhooks { */ public function actions() { if ( $this->is_webhook_settings_page() ) { - // Save if ( isset( $_POST['save'] ) && isset( $_POST['webhook_id'] ) ) { $this->save(); diff --git a/includes/api/class-wc-api-webhooks.php b/includes/api/class-wc-api-webhooks.php index 5a4bc26379e..74b5c7b516b 100644 --- a/includes/api/class-wc-api-webhooks.php +++ b/includes/api/class-wc-api-webhooks.php @@ -217,6 +217,8 @@ class WC_API_Webhooks extends WC_API_Resource { do_action( 'woocommerce_api_create_webhook', $webhook->id, $this ); + delete_transient( 'woocommerce_webhook_ids' ); + return $this->get_webhook( $webhook->id ); } catch ( WC_API_Exception $e ) { @@ -298,6 +300,8 @@ class WC_API_Webhooks extends WC_API_Resource { do_action( 'woocommerce_api_edit_webhook', $webhook->id, $this ); + delete_transient( 'woocommerce_webhook_ids' ); + return $this->get_webhook( $id ); } catch ( WC_API_Exception $e ) { @@ -323,6 +327,8 @@ class WC_API_Webhooks extends WC_API_Resource { do_action( 'woocommerce_api_delete_webhook', $id, $this ); + delete_transient( 'woocommerce_webhook_ids' ); + // no way to manage trashed webhooks at the moment, so force delete return $this->delete( $id, 'webhook', true ); } diff --git a/woocommerce.php b/woocommerce.php index 5981ee42003..d9cd07cce17 100644 --- a/woocommerce.php +++ b/woocommerce.php @@ -457,19 +457,18 @@ final class WooCommerce { * @since 2.2 */ private function load_webhooks() { - $args = array( - 'fields' => 'ids', - 'post_type' => 'shop_webhook', - 'post_status' => 'publish', - ); - - $query = new WP_Query( $args ); - - if ( ! empty( $query->posts ) ) { - foreach ( $query->posts as $id ) { - $webhook = new WC_Webhook( $id ); - $webhook->enqueue(); - } + if ( false === ( $webhooks = get_transient( 'woocommerce_webhook_ids' ) ) ) { + $webhooks = get_posts( array( + 'fields' => 'ids', + 'post_type' => 'shop_webhook', + 'post_status' => 'publish', + 'posts_per_page' => -1 + ) ); + set_transient( 'woocommerce_webhook_ids', $webhooks ); + } + foreach ( $webhooks as $webhook_id ) { + $webhook = new WC_Webhook( $webhook_id ); + $webhook->enqueue(); } }