Added the wc_is_webhook_valid_topic() and wc_is_valid_url() conditional functions

This commit is contained in:
Claudio Sanches 2014-12-17 10:16:14 -02:00
parent d1bed7bef9
commit 5b318b1d2a
4 changed files with 88 additions and 90 deletions

View File

@ -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' ) ) ) {

View File

@ -39,37 +39,37 @@ class WC_Meta_Box_Webhook_Actions {
#poststuff #woocommerce-webhook-actions .inside { padding: 0; margin: 0; }
</style>
<?php if ( '0000-00-00 00:00:00' != $post->post_modified_gmt ) : ?>
<ul class="order_actions submitbox">
<?php if ( '0000-00-00 00:00:00' == $post->post_date_gmt ) : ?>
<li class="wide">
<strong><?php _e( 'Created at' ); ?>:</strong> <?php echo self::get_date_i18n( $post->post_modified_gmt ); ?>
</li>
<?php else : ?>
<li class="wide">
<strong><?php _e( 'Created at' ); ?>:</strong> <?php echo self::get_date_i18n( $post->post_date_gmt ); ?>
</li>
<li class="wide">
<strong><?php _e( 'Updated at' ); ?>:</strong> <?php echo self::get_date_i18n( $post->post_modified_gmt ); ?>
</li>
<?php endif; ?>
</ul>
<?php endif; ?>
<?php if ( '0000-00-00 00:00:00' != $post->post_modified_gmt ) : ?>
<ul class="order_actions submitbox">
<?php if ( '0000-00-00 00:00:00' == $post->post_date_gmt ) : ?>
<li class="wide">
<strong><?php _e( 'Created at' ); ?>:</strong> <?php echo self::get_date_i18n( $post->post_modified_gmt ); ?>
</li>
<?php else : ?>
<li class="wide">
<strong><?php _e( 'Created at' ); ?>:</strong> <?php echo self::get_date_i18n( $post->post_date_gmt ); ?>
</li>
<li class="wide">
<strong><?php _e( 'Updated at' ); ?>:</strong> <?php echo self::get_date_i18n( $post->post_modified_gmt ); ?>
</li>
<?php endif; ?>
</ul>
<?php endif; ?>
<div class="submitbox" id="submitpost">
<div id="major-publishing-actions">
<?php if ( current_user_can( 'delete_post', $post->ID ) ) : ?>
<div id="delete-action">
<a class="submitdelete deletion" href="<?php echo esc_url( get_delete_post_link( $post->ID ) ); ?>"><?php echo ( ! EMPTY_TRASH_DAYS ) ? __( 'Delete Permanently', 'woocommerce' ) : __( 'Move to Trash', 'woocommerce' ); ?></a></div>
<?php endif; ?>
<div class="submitbox" id="submitpost">
<div id="major-publishing-actions">
<?php if ( current_user_can( 'delete_post', $post->ID ) ) : ?>
<div id="delete-action">
<a class="submitdelete deletion" href="<?php echo esc_url( get_delete_post_link( $post->ID ) ); ?>"><?php echo ( ! EMPTY_TRASH_DAYS ) ? __( 'Delete Permanently', 'woocommerce' ) : __( 'Move to Trash', 'woocommerce' ); ?></a></div>
<?php endif; ?>
<div id="publishing-action">
<span class="spinner"></span>
<input type="submit" class="button button-primary button-large" name="save" id="publish" accesskey="p" value="<?php _e( 'Save Webhook', 'woocommerce' ); ?>" data-tip="<?php _e( 'Save/update the Webhook', 'woocommerce' ); ?>" />
</div>
<div class="clear"></div>
<div id="publishing-action">
<span class="spinner"></span>
<input type="submit" class="button button-primary button-large" name="save" id="publish" accesskey="p" value="<?php _e( 'Save Webhook', 'woocommerce' ); ?>" data-tip="<?php _e( 'Save/update the Webhook', 'woocommerce' ); ?>" />
</div>
<div class="clear"></div>
</div>
<?php
</div>
<?php
}
}

View File

@ -173,12 +173,12 @@ class WC_API_Webhooks extends WC_API_Resource {
$data = apply_filters( 'woocommerce_api_create_webhook_data', $data, $this );
// validate topic
if ( empty( $data['topic'] ) || ! $this->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
*

View File

@ -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;
}