Ping webhook delivery URL on save and show errors

Closes #9751 @claudiosmweb
This commit is contained in:
Mike Jolley 2015-12-04 12:47:30 +00:00
parent 398cde4770
commit bcdce81606
3 changed files with 36 additions and 12 deletions

View File

@ -106,7 +106,7 @@ class WC_Admin_Webhooks_Table_List extends WP_List_Table {
$actions['trash'] = '<a class="submitdelete" title="' . esc_attr( __( 'Move this item to the Trash', 'woocommerce' ) ) . '" href="' . get_delete_post_link( $the_webhook->id ) . '">' . __( 'Trash', 'woocommerce' ) . '</a>';
}
if ( 'trash' == $post_status || ! EMPTY_TRASH_DAYS ) {
$actions['delete'] = '<a class="submitdelete" title="' . esc_attr( __( 'Delete this item permanently', 'woocommerce' ) ) . '" href="' . get_delete_post_link( $the_webhook->id, '', true ) . '">' . __( 'Delete Permanently', 'woocommerce' ) . '</a>';
$actions['delete'] = '<a class="submitdelete" title="' . esc_attr( __( 'Delete this item permanently', 'woocommerce' ) ) . '" href="' . get_delete_post_link( $the_webhook->id ) . '">' . __( 'Delete Permanently', 'woocommerce' ) . '</a>';
}
}

View File

@ -151,18 +151,26 @@ class WC_Admin_Webhooks {
// Topic
$this->update_topic( $webhook );
// Ping the webhook at the first time that is activated
$peding_delivery = get_post_meta( $webhook->id, '_webhook_pending_delivery', true );
if ( isset( $_POST['webhook_status'] ) && 'active' === $_POST['webhook_status'] && $peding_delivery ) {
$webhook->deliver_ping();
}
// Run actions
do_action( 'woocommerce_webhook_options_save', $webhook->id );
delete_transient( 'woocommerce_webhook_ids' );
// Ping the webhook at the first time that is activated
$pending_delivery = get_post_meta( $webhook->id, '_webhook_pending_delivery', true );
if ( isset( $_POST['webhook_status'] ) && 'active' === $_POST['webhook_status'] && $pending_delivery ) {
$result = $webhook->deliver_ping();
if ( is_wp_error( $result ) ) {
// Redirect to webhook edit page to avoid settings save actions
wp_safe_redirect( admin_url( 'admin.php?page=wc-settings&tab=api&section=webhooks&edit-webhook=' . $webhook->id . '&error=' . urlencode( $result->get_error_message() ) ) );
exit();
}
}
// Redirect to webhook edit page to avoid settings save actions
wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=api&section=webhooks&edit-webhook=' . $webhook->id . '&updated=1' ) );
wp_safe_redirect( admin_url( 'admin.php?page=wc-settings&tab=api&section=webhooks&edit-webhook=' . $webhook->id . '&updated=1' ) );
exit();
}
@ -382,6 +390,10 @@ class WC_Admin_Webhooks {
if ( isset( $_GET['created'] ) ) {
WC_Admin_Settings::add_message( __( 'Webhook created successfully.', 'woocommerce' ) );
}
if ( isset( $_GET['error'] ) ) {
WC_Admin_Settings::add_error( wc_clean( $_GET['error'] ) );
}
}
/**

View File

@ -589,15 +589,26 @@ class WC_Webhook {
* Send a test ping to the delivery URL, sent when the webhook is first created.
*
* @since 2.2
* @return bool|WP_Error
*/
public function deliver_ping() {
$args = array(
'user-agent' => sprintf( 'WooCommerce/%s Hookshot (WordPress/%s)', WC_VERSION, $GLOBALS['wp_version'] ),
'body' => "webhook_id={$this->id}",
);
wp_safe_remote_post( $this->get_delivery_url(), $args );
$test = wp_safe_remote_post( $this->get_delivery_url(), $args );
$response_code = wp_remote_retrieve_response_code( $test );
if ( is_wp_error( $test ) ) {
return new WP_Error( 'error', __( 'Error: Delivery URL cannot be reached: ' . $test->get_error_message() ) );
}
if ( 200 !== $response_code ) {
return new WP_Error( 'error', __( 'Error: Delivery URL returned response code ' . absint( $response_code ) ) );
}
return true;
}
/**
@ -684,8 +695,9 @@ class WC_Webhook {
* @param string $url
*/
public function set_delivery_url( $url ) {
update_post_meta( $this->id, '_delivery_url', esc_url_raw( $url, array( 'http', 'https' ) ) );
if ( update_post_meta( $this->id, '_delivery_url', esc_url_raw( $url, array( 'http', 'https' ) ) ) ) {
update_post_meta( $this->id, '_webhook_pending_delivery', true );
}
}
/**