From 5b318b1d2ad9868454593c5c4b41c7c2f0f1fe7a Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Wed, 17 Dec 2014 10:16:14 -0200 Subject: [PATCH] Added the wc_is_webhook_valid_topic() and wc_is_valid_url() conditional functions --- includes/admin/class-wc-admin-post-types.php | 2 +- .../class-wc-meta-box-webhook-actions.php | 56 ++++++++-------- includes/api/class-wc-api-webhooks.php | 65 ++----------------- includes/wc-conditional-functions.php | 55 ++++++++++++++++ 4 files changed, 88 insertions(+), 90 deletions(-) diff --git a/includes/admin/class-wc-admin-post-types.php b/includes/admin/class-wc-admin-post-types.php index 95ee85b0bac..72e8cdd1590 100644 --- a/includes/admin/class-wc-admin-post-types.php +++ b/includes/admin/class-wc-admin-post-types.php @@ -1892,7 +1892,7 @@ class WC_Admin_Post_Types { /** * Disable the auto-save functionality for Orders. */ - public function disable_autosave(){ + public function disable_autosave() { global $post; if ( $post && in_array( get_post_type( $post->ID ), wc_get_order_types( 'order-meta-boxes' ) ) ) { diff --git a/includes/admin/meta-boxes/class-wc-meta-box-webhook-actions.php b/includes/admin/meta-boxes/class-wc-meta-box-webhook-actions.php index 0654b54425f..79035a31b66 100644 --- a/includes/admin/meta-boxes/class-wc-meta-box-webhook-actions.php +++ b/includes/admin/meta-boxes/class-wc-meta-box-webhook-actions.php @@ -39,37 +39,37 @@ class WC_Meta_Box_Webhook_Actions { #poststuff #woocommerce-webhook-actions .inside { padding: 0; margin: 0; } - post_modified_gmt ) : ?> - - + post_modified_gmt ) : ?> + + -
-
- ID ) ) : ?> -
-
- +
+
+ ID ) ) : ?> +
+
+ -
- - -
-
+
+ +
+
- + is_valid_topic( strtolower( $data['topic'] ) ) ) { + if ( empty( $data['topic'] ) || ! wc_is_webhook_valid_topic( strtolower( $data['topic'] ) ) ) { throw new WC_API_Exception( 'woocommerce_api_invalid_webhook_topic', __( 'Webhook topic is required and must be valid', 'woocommerce' ), 400 ); } // validate delivery URL - if ( empty( $data['delivery_url'] ) || ! $this->is_valid_url( $data['delivery_url'] ) ) { + if ( empty( $data['delivery_url'] ) || ! wc_is_valid_url( $data['delivery_url'] ) ) { throw new WC_API_Exception( 'woocommerce_api_invalid_webhook_delivery_url', __( 'Webhook delivery URL must be a valid URL starting with http:// or https://', 'woocommerce' ), 400 ); } @@ -249,7 +249,7 @@ class WC_API_Webhooks extends WC_API_Resource { // update topic if ( ! empty( $data['topic'] ) ) { - if ( $this->is_valid_topic( strtolower( $data['topic'] ) ) ) { + if ( wc_is_webhook_valid_topic( strtolower( $data['topic'] ) ) ) { $webhook->set_topic( $data['topic'] ); @@ -260,7 +260,7 @@ class WC_API_Webhooks extends WC_API_Resource { // update delivery URL if ( ! empty( $data['delivery_url'] ) ) { - if ( $this->is_valid_url( $data['delivery_url'] ) ) { + if ( wc_is_valid_url( $data['delivery_url'] ) ) { $webhook->set_delivery_url( $data['delivery_url'] ); @@ -303,63 +303,6 @@ class WC_API_Webhooks extends WC_API_Resource { } } - - /** - * Check if the given topic is a valid webhook topic, a topic is valid if: - * - * + starts with `action.woocommerce_` or `action.wc_` - * + it has a valid resource & event - * - * @since 2.2 - * @param string $topic webhook topic - * @return bool true if valid, false otherwise - */ - private function is_valid_topic( $topic ) { - - // custom topics are prefixed with woocommerce_ or wc_ are valid - if ( 0 === strpos( $topic, 'action.woocommerce_' ) || 0 === strpos( $topic, 'action.wc_' ) ) { - return true; - } - - @list( $resource, $event ) = explode( '.', $topic ); - - if ( ! isset( $resource ) || ! isset( $event ) ) { - return false; - } - - $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' ) ); - - if ( in_array( $resource, $valid_resources ) && in_array( $event, $valid_events ) ) { - return true; - } - - return false; - } - - /** - * Simple check for validating a URL, it must start with http:// or https:// - * and pass FILTER_VALIDATE_URL validation - * - * @since 2.2 - * @param string $url delivery URL for the webhook - * @return bool true if valid, false otherwise - */ - private function is_valid_url( $url ) { - - // must start with http:// or https:// - if ( 0 !== strpos( $url, 'http://' ) && 0 !== strpos( $url, 'https://' ) ) { - return false; - } - - // must pass validation - if ( ! filter_var( $url, FILTER_VALIDATE_URL ) ) { - return false; - } - - return true; - } - /** * Delete a webhook * diff --git a/includes/wc-conditional-functions.php b/includes/wc-conditional-functions.php index 5334013aaa1..a540c71783e 100644 --- a/includes/wc-conditional-functions.php +++ b/includes/wc-conditional-functions.php @@ -326,3 +326,58 @@ if ( ! function_exists( 'wc_prices_include_tax' ) ) { return get_option( 'woocommerce_prices_include_tax' ) === 'yes'; } } + +/** + * Check if the given topic is a valid webhook topic, a topic is valid if: + * + * + starts with `action.woocommerce_` or `action.wc_` + * + it has a valid resource & event + * + * @param string $topic webhook topic + * @return bool true if valid, false otherwise + */ +function wc_is_webhook_valid_topic( $topic ) { + + // custom topics are prefixed with woocommerce_ or wc_ are valid + if ( 0 === strpos( $topic, 'action.woocommerce_' ) || 0 === strpos( $topic, 'action.wc_' ) ) { + return true; + } + + @list( $resource, $event ) = explode( '.', $topic ); + + if ( ! isset( $resource ) || ! isset( $event ) ) { + return false; + } + + $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' ) ); + + if ( in_array( $resource, $valid_resources ) && in_array( $event, $valid_events ) ) { + return true; + } + + return false; +} + + +/** + * Simple check for validating a URL, it must start with http:// or https:// + * and pass FILTER_VALIDATE_URL validation + * + * @param string $url + * @return bool + */ +function wc_is_valid_url( $url ) { + + // must start with http:// or https:// + if ( 0 !== strpos( $url, 'http://' ) && 0 !== strpos( $url, 'https://' ) ) { + return false; + } + + // must pass validation + if ( ! filter_var( $url, FILTER_VALIDATE_URL ) ) { + return false; + } + + return true; +}