From 027e7b1f9487ca1b77038719aa0362552311ed54 Mon Sep 17 00:00:00 2001 From: claudiulodro Date: Thu, 27 Apr 2017 11:44:57 -0700 Subject: [PATCH] Add restored webhook --- .../settings/views/html-webhooks-edit.php | 33 ++++++++++--------- includes/class-wc-webhook.php | 13 ++++++-- includes/wc-conditional-functions.php | 2 +- .../unit-tests/util/conditional-functions.php | 3 ++ 4 files changed, 33 insertions(+), 18 deletions(-) diff --git a/includes/admin/settings/views/html-webhooks-edit.php b/includes/admin/settings/views/html-webhooks-edit.php index ce5a70357d1..09d2701690d 100644 --- a/includes/admin/settings/views/html-webhooks-edit.php +++ b/includes/admin/settings/views/html-webhooks-edit.php @@ -51,21 +51,24 @@ if ( ! defined( 'ABSPATH' ) ) { $topic_data = WC_Admin_Webhooks::get_topic_data( $webhook ); $topics = apply_filters( 'woocommerce_webhook_topics', array( - '' => __( 'Select an option…', 'woocommerce' ), - 'coupon.created' => __( 'Coupon created', 'woocommerce' ), - 'coupon.updated' => __( 'Coupon updated', 'woocommerce' ), - 'coupon.deleted' => __( 'Coupon deleted', 'woocommerce' ), - 'customer.created' => __( 'Customer created', 'woocommerce' ), - 'customer.updated' => __( 'Customer updated', 'woocommerce' ), - 'customer.deleted' => __( 'Customer deleted', 'woocommerce' ), - 'order.created' => __( 'Order created', 'woocommerce' ), - 'order.updated' => __( 'Order updated', 'woocommerce' ), - 'order.deleted' => __( 'Order deleted', 'woocommerce' ), - 'product.created' => __( 'Product created', 'woocommerce' ), - 'product.updated' => __( 'Product updated', 'woocommerce' ), - 'product.deleted' => __( 'Product deleted', 'woocommerce' ), - 'action' => __( 'Action', 'woocommerce' ), - 'custom' => __( 'Custom', 'woocommerce' ), + '' => __( 'Select an option…', 'woocommerce' ), + 'coupon.created' => __( 'Coupon created', 'woocommerce' ), + 'coupon.updated' => __( 'Coupon updated', 'woocommerce' ), + 'coupon.deleted' => __( 'Coupon deleted', 'woocommerce' ), + 'coupon.restored' => __( 'Coupon restored', 'woocommerce' ), + 'customer.created' => __( 'Customer created', 'woocommerce' ), + 'customer.updated' => __( 'Customer updated', 'woocommerce' ), + 'customer.deleted' => __( 'Customer deleted', 'woocommerce' ), + 'order.created' => __( 'Order created', 'woocommerce' ), + 'order.updated' => __( 'Order updated', 'woocommerce' ), + 'order.deleted' => __( 'Order deleted', 'woocommerce' ), + 'order.restored' => __( 'Order restored', 'woocommerce' ), + 'product.created' => __( 'Product created', 'woocommerce' ), + 'product.updated' => __( 'Product updated', 'woocommerce' ), + 'product.deleted' => __( 'Product deleted', 'woocommerce' ), + 'product.restored' => __( 'Product restored', 'woocommerce' ), + 'action' => __( 'Action', 'woocommerce' ), + 'custom' => __( 'Custom', 'woocommerce' ), ) ); foreach ( $topics as $topic_slug => $topic_name ) : ?> diff --git a/includes/class-wc-webhook.php b/includes/class-wc-webhook.php index bb24408c850..0e8245f120f 100644 --- a/includes/class-wc-webhook.php +++ b/includes/class-wc-webhook.php @@ -137,8 +137,8 @@ class WC_Webhook { // only active webhooks can be delivered if ( 'active' != $this->get_status() ) { $should_deliver = false; - } elseif ( in_array( $current_action, array( 'delete_post', 'wp_trash_post' ), true ) ) { - // Only deliver deleted event for coupons, orders, and products. + } elseif ( in_array( $current_action, array( 'delete_post', 'wp_trash_post', 'untrashed_post' ), true ) ) { + // Only deliver deleted/restored event for coupons, orders, and products. if ( isset( $GLOBALS['post_type'] ) && ! in_array( $GLOBALS['post_type'], array( 'shop_coupon', 'shop_order', 'product' ) ) ) { $should_deliver = false; } @@ -619,6 +619,9 @@ class WC_Webhook { 'coupon.deleted' => array( 'wp_trash_post', ), + 'coupon.restored' => array( + 'untrashed_post', + ), 'customer.created' => array( 'user_register', 'woocommerce_created_customer', @@ -646,6 +649,9 @@ class WC_Webhook { 'order.deleted' => array( 'wp_trash_post', ), + 'order.restored' => array( + 'untrashed_post', + ), 'product.created' => array( 'woocommerce_process_product_meta', 'woocommerce_api_create_product', @@ -659,6 +665,9 @@ class WC_Webhook { 'product.deleted' => array( 'wp_trash_post', ), + 'product.restored' => array( + 'untrashed_post', + ), ); $topic_hooks = apply_filters( 'woocommerce_webhook_topic_hooks', $topic_hooks, $this ); diff --git a/includes/wc-conditional-functions.php b/includes/wc-conditional-functions.php index ccaddd75c99..682c0aa0252 100644 --- a/includes/wc-conditional-functions.php +++ b/includes/wc-conditional-functions.php @@ -351,7 +351,7 @@ function wc_is_webhook_valid_topic( $topic ) { } $valid_resources = apply_filters( 'woocommerce_valid_webhook_resources', array( 'coupon', 'customer', 'order', 'product' ) ); - $valid_events = apply_filters( 'woocommerce_valid_webhook_events', array( 'created', 'updated', 'deleted' ) ); + $valid_events = apply_filters( 'woocommerce_valid_webhook_events', array( 'created', 'updated', 'deleted', 'restored' ) ); if ( in_array( $resource, $valid_resources ) && in_array( $event, $valid_events ) ) { return true; diff --git a/tests/unit-tests/util/conditional-functions.php b/tests/unit-tests/util/conditional-functions.php index be466e5ef0d..e79c5cd59e9 100644 --- a/tests/unit-tests/util/conditional-functions.php +++ b/tests/unit-tests/util/conditional-functions.php @@ -49,15 +49,18 @@ class WC_Tests_Conditional_Functions extends WC_Unit_Test_Case { array( true, wc_is_webhook_valid_topic( 'product.created' ) ), array( true, wc_is_webhook_valid_topic( 'product.updated' ) ), array( true, wc_is_webhook_valid_topic( 'product.deleted' ) ), + array( true, wc_is_webhook_valid_topic( 'product.restored' ) ), array( true, wc_is_webhook_valid_topic( 'order.created' ) ), array( true, wc_is_webhook_valid_topic( 'order.updated' ) ), array( true, wc_is_webhook_valid_topic( 'order.deleted' ) ), + array( true, wc_is_webhook_valid_topic( 'order.restored' ) ), array( true, wc_is_webhook_valid_topic( 'customer.created' ) ), array( true, wc_is_webhook_valid_topic( 'customer.updated' ) ), array( true, wc_is_webhook_valid_topic( 'customer.deleted' ) ), array( true, wc_is_webhook_valid_topic( 'coupon.created' ) ), array( true, wc_is_webhook_valid_topic( 'coupon.updated' ) ), array( true, wc_is_webhook_valid_topic( 'coupon.deleted' ) ), + array( true, wc_is_webhook_valid_topic( 'coupon.restored' ) ), array( false, wc_is_webhook_valid_topic( 'coupon.upgraded' ) ), array( false, wc_is_webhook_valid_topic( 'wc.product.updated' ) ), array( false, wc_is_webhook_valid_topic( 'missingdot' ) ),