From 3c038e613f8b1ee278b3dd7df783433f5180d360 Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Tue, 6 Jan 2015 17:24:32 -0200 Subject: [PATCH 01/14] Created initial webhooks table list in settings page --- includes/admin/class-wc-admin-settings.php | 1 + .../class-wc-admin-webhooks-table-list.php | 171 ++++++++++++++++++ .../settings/class-wc-settings-webhooks.php | 87 +++++++++ includes/admin/views/html-admin-settings.php | 5 +- 4 files changed, 262 insertions(+), 2 deletions(-) create mode 100644 includes/admin/class-wc-admin-webhooks-table-list.php create mode 100644 includes/admin/settings/class-wc-settings-webhooks.php diff --git a/includes/admin/class-wc-admin-settings.php b/includes/admin/class-wc-admin-settings.php index 38663cdd355..236cfef8ffe 100644 --- a/includes/admin/class-wc-admin-settings.php +++ b/includes/admin/class-wc-admin-settings.php @@ -40,6 +40,7 @@ class WC_Admin_Settings { $settings[] = include( 'settings/class-wc-settings-accounts.php' ); $settings[] = include( 'settings/class-wc-settings-emails.php' ); $settings[] = include( 'settings/class-wc-settings-integrations.php' ); + $settings[] = include( 'settings/class-wc-settings-webhooks.php' ); self::$settings = apply_filters( 'woocommerce_get_settings_pages', $settings ); } diff --git a/includes/admin/class-wc-admin-webhooks-table-list.php b/includes/admin/class-wc-admin-webhooks-table-list.php new file mode 100644 index 00000000000..f52e780f990 --- /dev/null +++ b/includes/admin/class-wc-admin-webhooks-table-list.php @@ -0,0 +1,171 @@ + __( 'webhook', 'woocommerce' ), + 'plural' => __( 'webhooks', 'woocommerce' ), + 'ajax' => false + ) ); + } + + /** + * Get list columns + * + * @return array + */ + public function get_columns() { + $columns = array( + 'cb' => '', + 'title' => __( 'Name', 'woocommerce' ), + 'status' => __( 'Status', 'woocommerce' ), + 'topic' => __( 'Topic', 'woocommerce' ), + 'delivery_url' => __( 'Delivery URL', 'woocommerce' ), + ); + + return $columns; + } + + /** + * Column cb. + * + * @param WC_Post $webhook + * + * @return string + */ + public function column_cb( $webhook ) { + return sprintf( '', $this->_args['singular'], $webhook->ID ); + } + + /** + * Webhook columns. + * + * @param WC_Post $webhook + * @param string $column_name + * + * @return string + */ + public function column_default( $webhook, $column_name ) { + global $the_webhook; + + if ( empty( $the_webhook ) || $the_webhook->id != $webhook->ID ) { + $the_webhook = new WC_Webhook( $webhook->ID ); + } + + $output = ''; + + switch ( $column_name ) { + case 'title' : + $edit_link = get_edit_post_link( $the_webhook->id ); + $title = _draft_or_post_title( $the_webhook->post_data ); + $post_type_object = get_post_type_object( $the_webhook->post_data->post_type ); + + $output = '' . esc_html( $title ) . ''; + + // Get actions + $actions = array(); + + $actions['id'] = sprintf( __( 'ID: %d', 'woocommerce' ), $the_webhook->id ); + + if ( current_user_can( $post_type_object->cap->edit_post, $the_webhook->id ) ) { + $actions['edit'] = '' . __( 'Edit', 'woocommerce' ) . ''; + } + + if ( current_user_can( $post_type_object->cap->delete_post, $the_webhook->id ) ) { + + if ( 'trash' == $the_webhook->post_data->post_status ) { + $actions['untrash'] = '' . __( 'Restore', 'woocommerce' ) . ''; + } elseif ( EMPTY_TRASH_DAYS ) { + $actions['trash'] = '' . __( 'Trash', 'woocommerce' ) . ''; + } + + if ( 'trash' == $the_webhook->post_data->post_status || ! EMPTY_TRASH_DAYS ) { + $actions['delete'] = '' . __( 'Delete Permanently', 'woocommerce' ) . ''; + } + } + + $actions = apply_filters( 'post_row_actions', $actions, $the_webhook->post_data ); + + $output .= '
'; + + $i = 0; + $action_count = sizeof( $actions ); + + foreach ( $actions as $action => $link ) { + ++$i; + $sep = ( $i == $action_count ) ? '' : ' | '; + + $output .= '' . $link . $sep . ''; + } + + $output .= '
'; + + break; + case 'status' : + $output = $the_webhook->get_i18n_status(); + break; + case 'topic' : + $output = $the_webhook->get_topic(); + break; + case 'delivery_url' : + $output = $the_webhook->get_delivery_url(); + break; + + default : + break; + } + + return $output; + } + + /** + * Prepare table list items. + */ + public function prepare_items() { + $per_page = 5; + $columns = $this->get_columns(); + $hidden = array(); + $sortable = $this->get_sortable_columns(); + $this->_column_headers = array( $columns, $hidden, $sortable ); + + $current_page = $this->get_pagenum(); + + $webhooks = new WP_Query( array( + 'post_type' => 'shop_webhook', + 'posts_per_page' => $per_page, + 'ignore_sticky_posts' => true, + 'paged' => $current_page + ) ); + + $this->items = $webhooks->posts; + + $this->set_pagination_args( array( + 'total_items' => $webhooks->found_posts, + 'per_page' => $per_page, + 'total_pages' => $webhooks->max_num_pages + ) ); + } +} diff --git a/includes/admin/settings/class-wc-settings-webhooks.php b/includes/admin/settings/class-wc-settings-webhooks.php new file mode 100644 index 00000000000..da7dea9d9ce --- /dev/null +++ b/includes/admin/settings/class-wc-settings-webhooks.php @@ -0,0 +1,87 @@ +id = 'webhooks'; + $this->label = __( 'Webhooks', 'woocommerce' ); + + add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_page' ), 20 ); + add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) ); + add_action( 'woocommerce_settings_form_method_tab_' . $this->id, array( $this, 'form_method' ) ); + + $this->notices(); + } + + /** + * Form method + * + * @param string $method + * + * @return string + */ + public function form_method( $method ) { + return 'get'; + } + + /** + * Notices. + */ + protected function notices() { + if ( isset( $_GET['trashed'] ) ) { + $trashed = absint( $_GET['trashed'] ); + + WC_Admin_Settings::add_message( sprintf( _n( '1 webhook moved to the Trash.', '%d webhooks moved to the Trash.', $trashed, 'woocommerce' ), $trashed ) ); + } + } + + /** + * Table list output + */ + protected function table_list_output() { + $webhooks_table_list = new WC_Admin_Webhooks_Table_List(); + $webhooks_table_list->prepare_items(); + + echo ''; + echo ''; + + $webhooks_table_list->display(); + } + + /** + * Output the settings + */ + public function output() { + global $current_section; + + // Hide the save button + $GLOBALS['hide_save_button'] = true; + + // Display the table list. + $this->table_list_output(); + } +} + +endif; + +return new WC_Settings_Webhooks(); diff --git a/includes/admin/views/html-admin-settings.php b/includes/admin/views/html-admin-settings.php index a07cef3257a..4ba2e5b8c8b 100644 --- a/includes/admin/views/html-admin-settings.php +++ b/includes/admin/views/html-admin-settings.php @@ -10,11 +10,12 @@ if ( ! defined( 'ABSPATH' ) ) { ?>
-
+

-
-

+
+

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

+ id ) ) : ?> + + + +

+
From c71869cd6ee03ebf06a3fd651d7d371a2298dc52 Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Fri, 9 Jan 2015 16:21:19 -0200 Subject: [PATCH 09/14] Added method to create webhooks on settings page --- includes/admin/class-wc-admin-webhooks.php | 63 ++++++++++--------- .../settings/class-wc-settings-webhooks.php | 8 ++- 2 files changed, 41 insertions(+), 30 deletions(-) diff --git a/includes/admin/class-wc-admin-webhooks.php b/includes/admin/class-wc-admin-webhooks.php index 3fae092f889..57d93f9ee73 100644 --- a/includes/admin/class-wc-admin-webhooks.php +++ b/includes/admin/class-wc-admin-webhooks.php @@ -23,6 +23,7 @@ class WC_Admin_Webhooks { public function __construct() { // Save webhooks add_action( 'admin_init', array( $this, 'save' ) ); + add_action( 'admin_init', array( $this, 'create' ) ); } /** @@ -95,28 +96,6 @@ class WC_Admin_Webhooks { } } - /** - * 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 */ @@ -150,12 +129,9 @@ class WC_Admin_Webhooks { // 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 + // 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(); } @@ -166,6 +142,37 @@ class WC_Admin_Webhooks { exit(); } } + + /** + * Create Webhook + */ + public function create() { + if ( isset( $_GET['page'] ) && 'wc-settings' == $_GET['page'] && isset( $_GET['tab'] ) && 'webhooks' == $_GET['tab'] && isset( $_GET['create-webhook'] ) ) { + if ( ! current_user_can( 'publish_shop_webhooks' ) ) { + wp_die( __( 'You don\'t have permissions to create Webhooks!', 'woocommerce' ) ); + } + + $webhook_id = wp_insert_post( array( + 'post_type' => 'shop_webhook', + 'post_status' => 'pending', + 'ping_status' => 'closed', + 'post_author' => get_current_user_id(), + 'post_password' => strlen( ( $password = uniqid( 'webhook_' ) ) ) > 20 ? substr( $password, 0, 20 ) : $password, + 'post_title' => sprintf( __( 'Webhook created on %s', 'woocommerce' ), strftime( _x( '%b %d, %Y @ %I:%M %p', 'Webhook created on date parsed by strftime', 'woocommerce' ) ) ), + 'comment_status' => 'open' + ) ); + + if ( is_wp_error( $webhook_id ) ) { + wp_die( $webhook_id->get_error_messages() ); + } + + update_post_meta( $webhook_id, '_webhook_pending_delivery', true ); + + // Redirect to edit page + wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=webhooks&edit-webhook=' . $webhook_id . '&created=1' ) ); + exit(); + } + } } new WC_Admin_Webhooks(); diff --git a/includes/admin/settings/class-wc-settings-webhooks.php b/includes/admin/settings/class-wc-settings-webhooks.php index 40464e3f985..8a2442adf2e 100644 --- a/includes/admin/settings/class-wc-settings-webhooks.php +++ b/includes/admin/settings/class-wc-settings-webhooks.php @@ -76,9 +76,11 @@ class WC_Settings_Webhooks extends WC_Settings_Page { } if ( isset( $_GET['updated'] ) ) { - $updated = absint( $_GET['updated'] ); + WC_Admin_Settings::add_message( __( 'Webhook updated successfully.', 'woocommerce' ) ); + } - WC_Admin_Settings::add_message( __( 'Webhook saved successfully.', 'woocommerce' ) ); + if ( isset( $_GET['created'] ) ) { + WC_Admin_Settings::add_message( __( 'Webhook created successfully.', 'woocommerce' ) ); } } @@ -86,6 +88,8 @@ class WC_Settings_Webhooks extends WC_Settings_Page { * Table list output */ private function table_list_output() { + echo '

' . __( 'Webhooks', 'woocommerce' ) . ' ' . __( 'Add Webhook', 'woocommerce' ) . '

'; + $webhooks_table_list = new WC_Admin_Webhooks_Table_List(); $webhooks_table_list->prepare_items(); From 572d451aa61cf6acb4759ebbe6954d1110c011a7 Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Fri, 9 Jan 2015 22:08:45 -0200 Subject: [PATCH 10/14] Created webhooks bulk actions --- .../class-wc-admin-webhooks-table-list.php | 36 ++- includes/admin/class-wc-admin-webhooks.php | 222 +++++++++++++----- 2 files changed, 188 insertions(+), 70 deletions(-) diff --git a/includes/admin/class-wc-admin-webhooks-table-list.php b/includes/admin/class-wc-admin-webhooks-table-list.php index ce62dfb9ebe..07630c43a4e 100644 --- a/includes/admin/class-wc-admin-webhooks-table-list.php +++ b/includes/admin/class-wc-admin-webhooks-table-list.php @@ -79,27 +79,35 @@ class WC_Admin_Webhooks_Table_List extends WP_List_Table { $edit_link = admin_url( 'admin.php?page=wc-settings&tab=webhooks&edit-webhook=' . $the_webhook->id ); $title = _draft_or_post_title( $the_webhook->post_data ); $post_type_object = get_post_type_object( $the_webhook->post_data->post_type ); + $post_status = $the_webhook->post_data->post_status; - $output = '' . esc_html( $title ) . ''; + // Title + $output = ''; + if ( 'trash' == $post_status ) { + $output .= esc_html( $title ); + } else { + $output .= '' . esc_html( $title ) . ''; + } + $output .= ''; // Get actions $actions = array(); $actions['id'] = sprintf( __( 'ID: %d', 'woocommerce' ), $the_webhook->id ); - if ( current_user_can( $post_type_object->cap->edit_post, $the_webhook->id ) && 'trash' !== $the_webhook->post_data->post_status ) { + if ( current_user_can( $post_type_object->cap->edit_post, $the_webhook->id ) && 'trash' !== $post_status ) { $actions['edit'] = '' . __( 'Edit', 'woocommerce' ) . ''; } if ( current_user_can( $post_type_object->cap->delete_post, $the_webhook->id ) ) { - if ( 'trash' == $the_webhook->post_data->post_status ) { + if ( 'trash' == $post_status ) { $actions['untrash'] = '' . __( 'Restore', 'woocommerce' ) . ''; } elseif ( EMPTY_TRASH_DAYS ) { $actions['trash'] = '' . __( 'Trash', 'woocommerce' ) . ''; } - if ( 'trash' == $the_webhook->post_data->post_status || ! EMPTY_TRASH_DAYS ) { + if ( 'trash' == $post_status || ! EMPTY_TRASH_DAYS ) { $actions['delete'] = '' . __( 'Delete Permanently', 'woocommerce' ) . ''; } } @@ -225,11 +233,29 @@ class WC_Admin_Webhooks_Table_List extends WP_List_Table { return $status_links; } + /** + * Get bulk actions + * + * @return array + */ + protected function get_bulk_actions() { + if ( isset( $_GET['status'] ) && 'trash' == $_GET['status'] ) { + return array( + 'untrash' => __( 'Restore', 'woocommerce' ), + 'delete' => __( 'Delete Permanently', 'woocommerce' ) + ); + } + + return array( + 'trash' => __( 'Move to Trash', 'woocommerce' ) + ); + } + /** * Prepare table list items. */ public function prepare_items() { - $per_page = 10; + $per_page = apply_filters( 'woocommerce_webhooks_settings_posts_per_page', 10 ); $columns = $this->get_columns(); $hidden = array(); $sortable = $this->get_sortable_columns(); diff --git a/includes/admin/class-wc-admin-webhooks.php b/includes/admin/class-wc-admin-webhooks.php index 57d93f9ee73..292c6b261ba 100644 --- a/includes/admin/class-wc-admin-webhooks.php +++ b/includes/admin/class-wc-admin-webhooks.php @@ -21,9 +21,16 @@ class WC_Admin_Webhooks { * Initialize the webhooks admin actions */ public function __construct() { - // Save webhooks - add_action( 'admin_init', array( $this, 'save' ) ); - add_action( 'admin_init', array( $this, 'create' ) ); + add_action( 'admin_init', array( $this, 'actions' ) ); + } + + /** + * Check if is webhook settings page + * + * @return bool + */ + private function is_webhook_settigs_page() { + return isset( $_GET['page'] ) && 'wc-settings' == $_GET['page'] && isset( $_GET['tab'] ) && 'webhooks' == $_GET['tab']; } /** @@ -99,78 +106,163 @@ class WC_Admin_Webhooks { /** * 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 ); - - // 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(); - } - - 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(); + private function save() { + 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 ); + + // 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(); + } + + 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(); } /** * Create Webhook */ - public function create() { - if ( isset( $_GET['page'] ) && 'wc-settings' == $_GET['page'] && isset( $_GET['tab'] ) && 'webhooks' == $_GET['tab'] && isset( $_GET['create-webhook'] ) ) { - if ( ! current_user_can( 'publish_shop_webhooks' ) ) { - wp_die( __( 'You don\'t have permissions to create Webhooks!', 'woocommerce' ) ); + private function create() { + if ( ! current_user_can( 'publish_shop_webhooks' ) ) { + wp_die( __( 'You don\'t have permissions to create Webhooks!', 'woocommerce' ) ); + } + + $webhook_id = wp_insert_post( array( + 'post_type' => 'shop_webhook', + 'post_status' => 'pending', + 'ping_status' => 'closed', + 'post_author' => get_current_user_id(), + 'post_password' => strlen( ( $password = uniqid( 'webhook_' ) ) ) > 20 ? substr( $password, 0, 20 ) : $password, + 'post_title' => sprintf( __( 'Webhook created on %s', 'woocommerce' ), strftime( _x( '%b %d, %Y @ %I:%M %p', 'Webhook created on date parsed by strftime', 'woocommerce' ) ) ), + 'comment_status' => 'open' + ) ); + + if ( is_wp_error( $webhook_id ) ) { + wp_die( $webhook_id->get_error_messages() ); + } + + update_post_meta( $webhook_id, '_webhook_pending_delivery', true ); + + // Redirect to edit page + wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=webhooks&edit-webhook=' . $webhook_id . '&created=1' ) ); + exit(); + } + + /** + * Bulk trash/delete + * + * @param array $webhooks + * @param bool $delete + */ + private function bulk_trash( $webhooks, $delete = false ) { + foreach ( $webhooks as $webhook_id ) { + if ( $delete ) { + wp_delete_post( $webhook_id, true ); + } else { + wp_trash_post( $webhook_id ); + } + } + + $type = ! EMPTY_TRASH_DAYS || $delete ? 'deleted' : 'trashed'; + $qty = count( $webhooks ); + $status = isset( $_GET['status'] ) ? '&status=' . sanitize_text_field( $_GET['status'] ) : ''; + + // Redirect to webhooks page + wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=webhooks' . $status . '&' . $type . '=' . $qty ) ); + exit(); + } + + /** + * Bulk untrash + * + * @param array $webhooks + */ + private function bulk_untrash( $webhooks ) { + foreach ( $webhooks as $webhook_id ) { + wp_untrash_post( $webhook_id ); + } + + $qty = count( $webhooks ); + + // Redirect to webhooks page + wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=webhooks&status=trash&untrashed=' . $qty ) ); + exit(); + } + + /** + * Webhook bulk actions + */ + private function bulk_actions() { + if ( ! current_user_can( 'edit_shop_webhooks' ) ) { + wp_die( __( 'You don\'t have permissions to edit Webhooks!', 'woocommerce' ) ); + } + + $webhooks = array_map( 'absint', (array) $_GET['webhook'] ); + + switch ( $_GET['action'] ) { + case 'trash' : + $this->bulk_trash( $webhooks ); + break; + case 'untrash' : + $this->bulk_untrash( $webhooks ); + break; + case 'delete' : + $this->bulk_trash( $webhooks, true ); + break; + + default : + break; + } + } + + /** + * Webhooks admin actions + */ + public function actions() { + if ( $this->is_webhook_settigs_page() ) { + + // Save + if ( isset( $_POST['save'] ) && isset( $_POST['webhook_id'] ) ) { + $this->save(); } - $webhook_id = wp_insert_post( array( - 'post_type' => 'shop_webhook', - 'post_status' => 'pending', - 'ping_status' => 'closed', - 'post_author' => get_current_user_id(), - 'post_password' => strlen( ( $password = uniqid( 'webhook_' ) ) ) > 20 ? substr( $password, 0, 20 ) : $password, - 'post_title' => sprintf( __( 'Webhook created on %s', 'woocommerce' ), strftime( _x( '%b %d, %Y @ %I:%M %p', 'Webhook created on date parsed by strftime', 'woocommerce' ) ) ), - 'comment_status' => 'open' - ) ); - - if ( is_wp_error( $webhook_id ) ) { - wp_die( $webhook_id->get_error_messages() ); + // Create + if ( isset( $_GET['create-webhook'] ) ) { + $this->create(); } - update_post_meta( $webhook_id, '_webhook_pending_delivery', true ); - - // Redirect to edit page - wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=webhooks&edit-webhook=' . $webhook_id . '&created=1' ) ); - exit(); + // Create + if ( isset( $_GET['action'] ) && isset( $_GET['webhook'] ) ) { + $this->bulk_actions(); + } } } } From 9fa5ad756e2428af82a3fe4bae9281a07a395925 Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Fri, 9 Jan 2015 22:25:04 -0200 Subject: [PATCH 11/14] Fixed a typo --- includes/admin/class-wc-admin-webhooks.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/admin/class-wc-admin-webhooks.php b/includes/admin/class-wc-admin-webhooks.php index 292c6b261ba..ba62891aa8e 100644 --- a/includes/admin/class-wc-admin-webhooks.php +++ b/includes/admin/class-wc-admin-webhooks.php @@ -29,7 +29,7 @@ class WC_Admin_Webhooks { * * @return bool */ - private function is_webhook_settigs_page() { + private function is_webhook_settings_page() { return isset( $_GET['page'] ) && 'wc-settings' == $_GET['page'] && isset( $_GET['tab'] ) && 'webhooks' == $_GET['tab']; } @@ -247,7 +247,7 @@ class WC_Admin_Webhooks { * Webhooks admin actions */ public function actions() { - if ( $this->is_webhook_settigs_page() ) { + if ( $this->is_webhook_settings_page() ) { // Save if ( isset( $_POST['save'] ) && isset( $_POST['webhook_id'] ) ) { From 052380e1912cc5257f3bd69e6348c2990a321dd8 Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Fri, 9 Jan 2015 22:41:33 -0200 Subject: [PATCH 12/14] Added webhooks search field --- includes/admin/class-wc-admin-webhooks-table-list.php | 4 ++++ includes/admin/settings/class-wc-settings-webhooks.php | 1 + 2 files changed, 5 insertions(+) diff --git a/includes/admin/class-wc-admin-webhooks-table-list.php b/includes/admin/class-wc-admin-webhooks-table-list.php index 07630c43a4e..d55a9cd98dd 100644 --- a/includes/admin/class-wc-admin-webhooks-table-list.php +++ b/includes/admin/class-wc-admin-webhooks-table-list.php @@ -278,6 +278,10 @@ class WC_Admin_Webhooks_Table_List extends WP_List_Table { $args['post_status'] = sanitize_text_field( $_REQUEST['status'] ); } + if ( ! empty( $_REQUEST['s'] ) ) { + $args['s'] = sanitize_text_field( $_REQUEST['s'] ); + } + // Get the webhooks $webhooks = new WP_Query( $args ); $this->items = $webhooks->posts; diff --git a/includes/admin/settings/class-wc-settings-webhooks.php b/includes/admin/settings/class-wc-settings-webhooks.php index 8a2442adf2e..56cdccde9fa 100644 --- a/includes/admin/settings/class-wc-settings-webhooks.php +++ b/includes/admin/settings/class-wc-settings-webhooks.php @@ -97,6 +97,7 @@ class WC_Settings_Webhooks extends WC_Settings_Page { echo ''; $webhooks_table_list->views(); + $webhooks_table_list->search_box( __( 'Search Webhooks', 'woocommerce' ), 'webhook' ); $webhooks_table_list->display(); } From 56c49daea83c6ed9aa303231fca84b8d7f7c0d43 Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Fri, 9 Jan 2015 23:03:22 -0200 Subject: [PATCH 13/14] Added empty trash action --- .../class-wc-admin-webhooks-table-list.php | 11 ++++++ includes/admin/class-wc-admin-webhooks.php | 34 ++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/includes/admin/class-wc-admin-webhooks-table-list.php b/includes/admin/class-wc-admin-webhooks-table-list.php index d55a9cd98dd..b62fe3fab0f 100644 --- a/includes/admin/class-wc-admin-webhooks-table-list.php +++ b/includes/admin/class-wc-admin-webhooks-table-list.php @@ -251,6 +251,17 @@ class WC_Admin_Webhooks_Table_List extends WP_List_Table { ); } + /** + * Extra controls to be displayed between bulk actions and pagination + * + * @param string $which + */ + protected function extra_tablenav( $which ) { + if ( 'top' == $which && isset( $_GET['status'] ) && 'trash' == $_GET['status'] && current_user_can( 'delete_shop_webhooks' ) ) { + echo ''; + } + } + /** * Prepare table list items. */ diff --git a/includes/admin/class-wc-admin-webhooks.php b/includes/admin/class-wc-admin-webhooks.php index ba62891aa8e..33e287cb4a5 100644 --- a/includes/admin/class-wc-admin-webhooks.php +++ b/includes/admin/class-wc-admin-webhooks.php @@ -243,6 +243,33 @@ class WC_Admin_Webhooks { } } + /** + * Empty Trash + */ + public function empty_trash() { + if ( ! current_user_can( 'delete_shop_webhooks' ) ) { + wp_die( __( 'You don\'t have permissions to delete Webhooks!', 'woocommerce' ) ); + } + + $webhooks = get_posts( array( + 'post_type' => 'shop_webhook', + 'ignore_sticky_posts' => true, + 'nopaging' => true, + 'post_status' => 'trash', + 'fields' => 'ids' + ) ); + + foreach ( $webhooks as $webhook_id ) { + wp_delete_post( $webhook_id, true ); + } + + $qty = count( $webhooks ); + + // Redirect to webhooks page + wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=webhooks&deleted=' . $qty ) ); + exit(); + } + /** * Webhooks admin actions */ @@ -259,10 +286,15 @@ class WC_Admin_Webhooks { $this->create(); } - // Create + // Bulk actions if ( isset( $_GET['action'] ) && isset( $_GET['webhook'] ) ) { $this->bulk_actions(); } + + // Bulk actions + if ( isset( $_GET['empty_trash'] ) ) { + $this->empty_trash(); + } } } } From f3e5c03b38ca5d54343160c419301bd7f752b4fe Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Mon, 12 Jan 2015 09:27:09 -0200 Subject: [PATCH 14/14] Repositioned the "move to trash" and "save" buttons --- includes/admin/settings/views/html-webhooks-edit.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/admin/settings/views/html-webhooks-edit.php b/includes/admin/settings/views/html-webhooks-edit.php index 72c093a923a..374e16cca16 100644 --- a/includes/admin/settings/views/html-webhooks-edit.php +++ b/includes/admin/settings/views/html-webhooks-edit.php @@ -148,10 +148,10 @@ if ( ! defined( 'ABSPATH' ) ) {

- id ) ) : ?> - - + id ) ) : ?> + +