From 262627578252ea80a66f73974c0aca0ff90d5c25 Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Fri, 9 Jan 2015 15:42:01 -0200 Subject: [PATCH] Edit and deleted webhooks --- .../class-wc-admin-webhooks-table-list.php | 2 +- includes/admin/class-wc-admin-webhooks.php | 171 ++++++++++++++++++ includes/admin/class-wc-admin.php | 1 + .../settings/class-wc-settings-webhooks.php | 124 +++---------- .../settings/views/html-webhooks-edit.php | 14 +- 5 files changed, 209 insertions(+), 103 deletions(-) create mode 100644 includes/admin/class-wc-admin-webhooks.php diff --git a/includes/admin/class-wc-admin-webhooks-table-list.php b/includes/admin/class-wc-admin-webhooks-table-list.php index 56a5314fc2d..ce62dfb9ebe 100644 --- a/includes/admin/class-wc-admin-webhooks-table-list.php +++ b/includes/admin/class-wc-admin-webhooks-table-list.php @@ -87,7 +87,7 @@ class WC_Admin_Webhooks_Table_List extends WP_List_Table { $actions['id'] = sprintf( __( 'ID: %d', 'woocommerce' ), $the_webhook->id ); - if ( current_user_can( $post_type_object->cap->edit_post, $the_webhook->id ) ) { + if ( current_user_can( $post_type_object->cap->edit_post, $the_webhook->id ) && 'trash' !== $the_webhook->post_data->post_status ) { $actions['edit'] = '' . __( 'Edit', 'woocommerce' ) . ''; } diff --git a/includes/admin/class-wc-admin-webhooks.php b/includes/admin/class-wc-admin-webhooks.php new file mode 100644 index 00000000000..3fae092f889 --- /dev/null +++ b/includes/admin/class-wc-admin-webhooks.php @@ -0,0 +1,171 @@ +update( $wpdb->posts, array( 'post_title' => $name ), array( 'ID' => $webhook_id ) ); + } + + /** + * Updated the Webhook status + * + * @param WC_Webhook $webhook + */ + private function update_status( $webhook ) { + $status = ! empty( $_POST['webhook_status'] ) ? wc_clean( $_POST['webhook_status'] ) : ''; + + $webhook->update_status( $status ); + } + + /** + * Updated the Webhook delivery URL + * + * @param WC_Webhook $webhook + */ + private function update_delivery_url( $webhook ) { + $delivery_url = ! empty( $_POST['webhook_delivery_url'] ) ? $_POST['webhook_delivery_url'] : ''; + + if ( wc_is_valid_url( $delivery_url ) ) { + $webhook->set_delivery_url( $delivery_url ); + } + } + + /** + * Updated the Webhook secret + * + * @param WC_Webhook $webhook + */ + private function update_secret( $webhook ) { + $secret = ! empty( $_POST['webhook_secret'] ) ? $_POST['webhook_secret'] : get_user_meta( get_current_user_id(), 'woocommerce_api_consumer_secret', true ); + + $webhook->set_secret( $secret ); + } + + /** + * Updated the Webhook topic + * + * @param WC_Webhook $webhook + */ + private function update_topic( $webhook ) { + if ( ! empty( $_POST['webhook_topic'] ) ) { + list( $resource, $event ) = explode( '.', wc_clean( $_POST['webhook_topic'] ) ); + + if ( 'action' === $resource ) { + $event = ! empty( $_POST['webhook_action_event'] ) ? wc_clean( $_POST['webhook_action_event'] ) : ''; + } else if ( ! in_array( $resource, array( 'coupon', 'customer', 'order', 'product' ) ) && ! empty( $_POST['webhook_custom_topic'] ) ) { + list( $resource, $event ) = explode( '.', wc_clean( $_POST['webhook_custom_topic'] ) ); + } + + $topic = $resource . '.' . $event; + + if ( wc_is_webhook_valid_topic( $topic ) ) { + $webhook->set_topic( $topic ); + } + } + } + + /** + * Set Webhook post data. + * + * @param int $webhook_id + */ + private function set_post_data( $webhook_id ) { + global $wpdb; + + $password = uniqid( 'webhook_' ); + $password = strlen( $password ) > 20 ? substr( $password, 0, 20 ) : $password; + + $wpdb->update( + $wpdb->posts, + array( + 'post_password' => $password, + 'ping_status' => 'closed', + 'comment_status' => 'open' + ), + array( 'ID' => $webhook_id ) + ); + } + + /** + * Save method + */ + public function save() { + if ( isset( $_GET['page'] ) && 'wc-settings' == $_GET['page'] && isset( $_GET['tab'] ) && 'webhooks' == $_GET['tab'] && isset( $_POST['save'] ) && isset( $_POST['webhook_id'] ) ) { + + if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-settings' ) ) { + die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) ); + } + + $webhook_id = absint( $_POST['webhook_id'] ); + + if ( ! current_user_can( 'edit_shop_webhook', $webhook_id ) ) { + return; + } + + $webhook = new WC_Webhook( $webhook_id ); + + // Name + $this->update_name( $webhook->id ); + + // Status + $this->update_status( $webhook ); + + // Delivery URL + $this->update_delivery_url( $webhook ); + + // Secret + $this->update_secret( $webhook ); + + // Topic + $this->update_topic( $webhook ); + + // Webhook Created + if ( isset( $_POST['original_post_status'] ) && 'auto-draft' === $_POST['original_post_status'] ) { + // Set Post data like ping status and password + $this->set_post_data( $webhook->id ); + + // Ping webhook + $webhook->deliver_ping(); + } + + do_action( 'woocommerce_webhook_options_save', $webhook->id ); + + // 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(); + } + } +} + +new WC_Admin_Webhooks(); diff --git a/includes/admin/class-wc-admin.php b/includes/admin/class-wc-admin.php index fed4d57db39..13c6c46a768 100644 --- a/includes/admin/class-wc-admin.php +++ b/includes/admin/class-wc-admin.php @@ -47,6 +47,7 @@ class WC_Admin { include( 'class-wc-admin-welcome.php' ); include( 'class-wc-admin-notices.php' ); include( 'class-wc-admin-assets.php' ); + include( 'class-wc-admin-webhooks.php' ); // Help if ( apply_filters( 'woocommerce_enable_admin_help_tab', true ) ) { diff --git a/includes/admin/settings/class-wc-settings-webhooks.php b/includes/admin/settings/class-wc-settings-webhooks.php index c28cf78956b..40464e3f985 100644 --- a/includes/admin/settings/class-wc-settings-webhooks.php +++ b/includes/admin/settings/class-wc-settings-webhooks.php @@ -42,7 +42,12 @@ class WC_Settings_Webhooks extends WC_Settings_Page { */ public function form_method( $method ) { if ( isset( $_GET['edit-webhook'] ) ) { - return 'post'; + $webhook_id = absint( $_GET['edit-webhook'] ); + $webhook = new WC_Webhook( $webhook_id ); + + if ( 'trash' != $webhook->post_data->post_status ) { + return 'post'; + } } return 'get'; @@ -69,6 +74,12 @@ class WC_Settings_Webhooks extends WC_Settings_Page { WC_Admin_Settings::add_message( sprintf( _n( '1 webhook permanently deleted.', '%d webhooks permanently deleted.', $deleted, 'woocommerce' ), $deleted ) ); } + + if ( isset( $_GET['updated'] ) ) { + $updated = absint( $_GET['updated'] ); + + WC_Admin_Settings::add_message( __( 'Webhook saved successfully.', 'woocommerce' ) ); + } } /** @@ -87,11 +98,10 @@ class WC_Settings_Webhooks extends WC_Settings_Page { /** * Edit webhook output + * + * @param WC_Webhook $webhook */ - private function edit_output() { - $webhook_id = absint( $_GET['edit-webhook'] ); - $webhook = new WC_Webhook( $webhook_id ); - + private function edit_output( $webhook ) { include_once( 'views/html-webhooks-edit.php' ); } @@ -136,10 +146,16 @@ class WC_Settings_Webhooks extends WC_Settings_Page { $GLOBALS['hide_save_button'] = true; if ( isset( $_GET['edit-webhook'] ) ) { - $this->edit_output(); - } else { - $this->table_list_output(); + $webhook_id = absint( $_GET['edit-webhook'] ); + $webhook = new WC_Webhook( $webhook_id ); + + if ( 'trash' != $webhook->post_data->post_status ) { + $this->edit_output( $webhook ); + return; + } } + + $this->table_list_output(); } /** @@ -169,98 +185,6 @@ class WC_Settings_Webhooks extends WC_Settings_Page { ); } - /** - * Updated the Webhook name - * - * @param int $webhook_id - */ - private function update_name( $webhook_id ) { - global $wpdb; - - $name = ! empty( $_POST['webhook_name'] ) ? $_POST['webhook_name'] : sprintf( __( 'Webhook created on %s', 'woocommerce' ), strftime( _x( '%b %d, %Y @ %I:%M %p', 'Webhook created on date parsed by strftime', 'woocommerce' ) ) ); - $wpdb->update( $wpdb->posts, array( 'post_title' => $name ), array( 'ID' => $webhook_id ) ); - } - - /** - * Updated the Webhook status - * - * @param WC_Webhook $webhook - */ - private function update_status( $webhook ) { - $status = ! empty( $_POST['webhook_status'] ) ? wc_clean( $_POST['webhook_status'] ) : ''; - - $webhook->update_status( $status ); - } - - /** - * Updated the Webhook delivery URL - * - * @param WC_Webhook $webhook - */ - private function update_delivery_url( $webhook ) { - $delivery_url = ! empty( $_POST['webhook_delivery_url'] ) ? $_POST['webhook_delivery_url'] : ''; - - if ( wc_is_valid_url( $delivery_url ) ) { - $webhook->set_delivery_url( $delivery_url ); - } - } - - /** - * Updated the Webhook secret - * - * @param WC_Webhook $webhook - */ - private function update_secret( $webhook ) { - $secret = ! empty( $_POST['webhook_secret'] ) ? $_POST['webhook_secret'] : get_user_meta( get_current_user_id(), 'woocommerce_api_consumer_secret', true ); - - $webhook->set_secret( $secret ); - } - - /** - * Updated the Webhook topic - * - * @param WC_Webhook $webhook - */ - private function update_topic( $webhook ) { - if ( ! empty( $_POST['webhook_topic'] ) ) { - list( $resource, $event ) = explode( '.', wc_clean( $_POST['webhook_topic'] ) ); - - if ( 'action' === $resource ) { - $event = ! empty( $_POST['webhook_action_event'] ) ? wc_clean( $_POST['webhook_action_event'] ) : ''; - } else if ( ! in_array( $resource, array( 'coupon', 'customer', 'order', 'product' ) ) && ! empty( $_POST['webhook_custom_topic'] ) ) { - list( $resource, $event ) = explode( '.', wc_clean( $_POST['webhook_custom_topic'] ) ); - } - - $topic = $resource . '.' . $event; - - if ( wc_is_webhook_valid_topic( $topic ) ) { - $webhook->set_topic( $topic ); - } - } - } - - /** - * Set Webhook post data. - * - * @param int $webhook_id - */ - private function set_post_data( $webhook_id ) { - global $wpdb; - - $password = uniqid( 'webhook_' ); - $password = strlen( $password ) > 20 ? substr( $password, 0, 20 ) : $password; - - $wpdb->update( - $wpdb->posts, - array( - 'post_password' => $password, - 'ping_status' => 'closed', - 'comment_status' => 'open' - ), - array( 'ID' => $webhook_id ) - ); - } - /** * Get the logs navigation. * diff --git a/includes/admin/settings/views/html-webhooks-edit.php b/includes/admin/settings/views/html-webhooks-edit.php index e5d69789fd2..72c093a923a 100644 --- a/includes/admin/settings/views/html-webhooks-edit.php +++ b/includes/admin/settings/views/html-webhooks-edit.php @@ -112,8 +112,8 @@ if ( ! defined( 'ABSPATH' ) ) { -
-

+
+

post_data->post_modified_gmt ) : ?> @@ -145,6 +145,16 @@ if ( ! defined( 'ABSPATH' ) ) { + + +
+

+ id ) ) : ?> + + + +

+