helper
4 years ago
importers
4 years ago
list-tables
4 years ago
marketplace-suggestions
4 years ago
meta-boxes
4 years ago
notes
4 years ago
plugin-updates
5 years ago
reports
5 years ago
settings
4 years ago
views
4 years ago
class-wc-admin-addons.php
4 years ago
class-wc-admin-api-keys-table-list.php
6 years ago
class-wc-admin-api-keys.php
6 years ago
class-wc-admin-assets.php
4 years ago
class-wc-admin-attributes.php
5 years ago
class-wc-admin-customize.php
5 years ago
class-wc-admin-dashboard-setup.php
4 years ago
class-wc-admin-dashboard.php
5 years ago
class-wc-admin-duplicate-product.php
5 years ago
class-wc-admin-exporters.php
5 years ago
class-wc-admin-help.php
5 years ago
class-wc-admin-importers.php
5 years ago
class-wc-admin-log-table-list.php
5 years ago
class-wc-admin-menus.php
4 years ago
class-wc-admin-meta-boxes.php
4 years ago
class-wc-admin-notices.php
4 years ago
class-wc-admin-permalink-settings.php
5 years ago
class-wc-admin-pointers.php
5 years ago
class-wc-admin-post-types.php
5 years ago
class-wc-admin-profile.php
4 years ago
class-wc-admin-reports.php
5 years ago
class-wc-admin-settings.php
5 years ago
class-wc-admin-setup-wizard.php
4 years ago
class-wc-admin-status.php
5 years ago
class-wc-admin-taxonomies.php
5 years ago
class-wc-admin-webhooks-table-list.php
7 years ago
class-wc-admin-webhooks.php
5 years ago
class-wc-admin.php
4 years ago
wc-admin-functions.php
4 years ago
wc-meta-box-functions.php
5 years ago
class-wc-admin-webhooks-table-list.php
317 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Webhooks Table List |
| 4 | * |
| 5 | * @package WooCommerce\Admin |
| 6 | * @version 3.3.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | if ( ! class_exists( 'WP_List_Table' ) ) { |
| 12 | require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Webooks table list class. |
| 17 | */ |
| 18 | class WC_Admin_Webhooks_Table_List extends WP_List_Table { |
| 19 | |
| 20 | /** |
| 21 | * Initialize the webhook table list. |
| 22 | */ |
| 23 | public function __construct() { |
| 24 | parent::__construct( |
| 25 | array( |
| 26 | 'singular' => 'webhook', |
| 27 | 'plural' => 'webhooks', |
| 28 | 'ajax' => false, |
| 29 | ) |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * No items found text. |
| 35 | */ |
| 36 | public function no_items() { |
| 37 | esc_html_e( 'No webhooks found.', 'woocommerce' ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Get list columns. |
| 42 | * |
| 43 | * @return array |
| 44 | */ |
| 45 | public function get_columns() { |
| 46 | return array( |
| 47 | 'cb' => '<input type="checkbox" />', |
| 48 | 'title' => __( 'Name', 'woocommerce' ), |
| 49 | 'status' => __( 'Status', 'woocommerce' ), |
| 50 | 'topic' => __( 'Topic', 'woocommerce' ), |
| 51 | 'delivery_url' => __( 'Delivery URL', 'woocommerce' ), |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Column cb. |
| 57 | * |
| 58 | * @param WC_Webhook $webhook Webhook instance. |
| 59 | * @return string |
| 60 | */ |
| 61 | public function column_cb( $webhook ) { |
| 62 | return sprintf( '<input type="checkbox" name="%1$s[]" value="%2$s" />', $this->_args['singular'], $webhook->get_id() ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Return title column. |
| 67 | * |
| 68 | * @param WC_Webhook $webhook Webhook instance. |
| 69 | * @return string |
| 70 | */ |
| 71 | public function column_title( $webhook ) { |
| 72 | $edit_link = admin_url( 'admin.php?page=wc-settings&tab=advanced&section=webhooks&edit-webhook=' . $webhook->get_id() ); |
| 73 | $output = ''; |
| 74 | |
| 75 | // Title. |
| 76 | $output .= '<strong><a href="' . esc_url( $edit_link ) . '" class="row-title">' . esc_html( $webhook->get_name() ) . '</a></strong>'; |
| 77 | |
| 78 | // Get actions. |
| 79 | $actions = array( |
| 80 | /* translators: %s: webhook ID. */ |
| 81 | 'id' => sprintf( __( 'ID: %d', 'woocommerce' ), $webhook->get_id() ), |
| 82 | 'edit' => '<a href="' . esc_url( $edit_link ) . '">' . esc_html__( 'Edit', 'woocommerce' ) . '</a>', |
| 83 | /* translators: %s: webhook name */ |
| 84 | 'delete' => '<a class="submitdelete" aria-label="' . esc_attr( sprintf( __( 'Delete "%s" permanently', 'woocommerce' ), $webhook->get_name() ) ) . '" href="' . esc_url( |
| 85 | wp_nonce_url( |
| 86 | add_query_arg( |
| 87 | array( |
| 88 | 'delete' => $webhook->get_id(), |
| 89 | ), |
| 90 | admin_url( 'admin.php?page=wc-settings&tab=advanced§ion=webhooks' ) |
| 91 | ), |
| 92 | 'delete-webhook' |
| 93 | ) |
| 94 | ) . '">' . esc_html__( 'Delete permanently', 'woocommerce' ) . '</a>', |
| 95 | ); |
| 96 | |
| 97 | $actions = apply_filters( 'webhook_row_actions', $actions, $webhook ); |
| 98 | $row_actions = array(); |
| 99 | |
| 100 | foreach ( $actions as $action => $link ) { |
| 101 | $row_actions[] = '<span class="' . esc_attr( $action ) . '">' . $link . '</span>'; |
| 102 | } |
| 103 | |
| 104 | $output .= '<div class="row-actions">' . implode( ' | ', $row_actions ) . '</div>'; |
| 105 | |
| 106 | return $output; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Return status column. |
| 111 | * |
| 112 | * @param WC_Webhook $webhook Webhook instance. |
| 113 | * @return string |
| 114 | */ |
| 115 | public function column_status( $webhook ) { |
| 116 | return $webhook->get_i18n_status(); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Return topic column. |
| 121 | * |
| 122 | * @param WC_Webhook $webhook Webhook instance. |
| 123 | * @return string |
| 124 | */ |
| 125 | public function column_topic( $webhook ) { |
| 126 | return $webhook->get_topic(); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Return delivery URL column. |
| 131 | * |
| 132 | * @param WC_Webhook $webhook Webhook instance. |
| 133 | * @return string |
| 134 | */ |
| 135 | public function column_delivery_url( $webhook ) { |
| 136 | return $webhook->get_delivery_url(); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Get the status label for webhooks. |
| 141 | * |
| 142 | * @param string $status_name Status name. |
| 143 | * @param int $amount Amount of webhooks. |
| 144 | * @return array |
| 145 | */ |
| 146 | private function get_status_label( $status_name, $amount ) { |
| 147 | $statuses = wc_get_webhook_statuses(); |
| 148 | |
| 149 | if ( isset( $statuses[ $status_name ] ) ) { |
| 150 | return array( |
| 151 | 'singular' => sprintf( '%s <span class="count">(%s)</span>', esc_html( $statuses[ $status_name ] ), $amount ), |
| 152 | 'plural' => sprintf( '%s <span class="count">(%s)</span>', esc_html( $statuses[ $status_name ] ), $amount ), |
| 153 | 'context' => '', |
| 154 | 'domain' => 'woocommerce', |
| 155 | ); |
| 156 | } |
| 157 | |
| 158 | return array( |
| 159 | 'singular' => sprintf( '%s <span class="count">(%s)</span>', esc_html( $status_name ), $amount ), |
| 160 | 'plural' => sprintf( '%s <span class="count">(%s)</span>', esc_html( $status_name ), $amount ), |
| 161 | 'context' => '', |
| 162 | 'domain' => 'woocommerce', |
| 163 | ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Table list views. |
| 168 | * |
| 169 | * @return array |
| 170 | */ |
| 171 | protected function get_views() { |
| 172 | $status_links = array(); |
| 173 | $data_store = WC_Data_Store::load( 'webhook' ); |
| 174 | $num_webhooks = $data_store->get_count_webhooks_by_status(); |
| 175 | $total_webhooks = array_sum( (array) $num_webhooks ); |
| 176 | $statuses = array_keys( wc_get_webhook_statuses() ); |
| 177 | $class = empty( $_REQUEST['status'] ) ? ' class="current"' : ''; // WPCS: input var okay. CSRF ok. |
| 178 | |
| 179 | /* translators: %s: count */ |
| 180 | $status_links['all'] = "<a href='admin.php?page=wc-settings&tab=advanced&section=webhooks'$class>" . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $total_webhooks, 'posts', 'woocommerce' ), number_format_i18n( $total_webhooks ) ) . '</a>'; |
| 181 | |
| 182 | foreach ( $statuses as $status_name ) { |
| 183 | $class = ''; |
| 184 | |
| 185 | if ( empty( $num_webhooks[ $status_name ] ) ) { |
| 186 | continue; |
| 187 | } |
| 188 | |
| 189 | if ( isset( $_REQUEST['status'] ) && sanitize_key( wp_unslash( $_REQUEST['status'] ) ) === $status_name ) { // WPCS: input var okay, CSRF ok. |
| 190 | $class = ' class="current"'; |
| 191 | } |
| 192 | |
| 193 | $label = $this->get_status_label( $status_name, $num_webhooks[ $status_name ] ); |
| 194 | |
| 195 | $status_links[ $status_name ] = "<a href='admin.php?page=wc-settings&tab=advanced&section=webhooks&status=$status_name'$class>" . sprintf( translate_nooped_plural( $label, $num_webhooks[ $status_name ] ), number_format_i18n( $num_webhooks[ $status_name ] ) ) . '</a>'; |
| 196 | } |
| 197 | |
| 198 | return $status_links; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Get bulk actions. |
| 203 | * |
| 204 | * @return array |
| 205 | */ |
| 206 | protected function get_bulk_actions() { |
| 207 | return array( |
| 208 | 'delete' => __( 'Delete permanently', 'woocommerce' ), |
| 209 | ); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Process bulk actions. |
| 214 | */ |
| 215 | public function process_bulk_action() { |
| 216 | $action = $this->current_action(); |
| 217 | $webhooks = isset( $_REQUEST['webhook'] ) ? array_map( 'absint', (array) $_REQUEST['webhook'] ) : array(); // WPCS: input var okay, CSRF ok. |
| 218 | |
| 219 | if ( ! current_user_can( 'manage_woocommerce' ) ) { |
| 220 | wp_die( esc_html__( 'You do not have permission to edit Webhooks', 'woocommerce' ) ); |
| 221 | } |
| 222 | |
| 223 | if ( 'delete' === $action ) { |
| 224 | WC_Admin_Webhooks::bulk_delete( $webhooks ); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Generate the table navigation above or below the table. |
| 230 | * Included to remove extra nonce input. |
| 231 | * |
| 232 | * @param string $which The location of the extra table nav markup: 'top' or 'bottom'. |
| 233 | */ |
| 234 | protected function display_tablenav( $which ) { |
| 235 | echo '<div class="tablenav ' . esc_attr( $which ) . '">'; |
| 236 | |
| 237 | if ( $this->has_items() ) { |
| 238 | echo '<div class="alignleft actions bulkactions">'; |
| 239 | $this->bulk_actions( $which ); |
| 240 | echo '</div>'; |
| 241 | } |
| 242 | |
| 243 | $this->extra_tablenav( $which ); |
| 244 | $this->pagination( $which ); |
| 245 | echo '<br class="clear" />'; |
| 246 | echo '</div>'; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Search box. |
| 251 | * |
| 252 | * @param string $text Button text. |
| 253 | * @param string $input_id Input ID. |
| 254 | */ |
| 255 | public function search_box( $text, $input_id ) { |
| 256 | if ( empty( $_REQUEST['s'] ) && ! $this->has_items() ) { // WPCS: input var okay, CSRF ok. |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | $input_id = $input_id . '-search-input'; |
| 261 | $search_query = isset( $_REQUEST['s'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) : ''; // WPCS: input var okay, CSRF ok. |
| 262 | |
| 263 | echo '<p class="search-box">'; |
| 264 | echo '<label class="screen-reader-text" for="' . esc_attr( $input_id ) . '">' . esc_html( $text ) . ':</label>'; |
| 265 | echo '<input type="search" id="' . esc_attr( $input_id ) . '" name="s" value="' . esc_attr( $search_query ) . '" />'; |
| 266 | submit_button( |
| 267 | $text, |
| 268 | '', |
| 269 | '', |
| 270 | false, |
| 271 | array( |
| 272 | 'id' => 'search-submit', |
| 273 | ) |
| 274 | ); |
| 275 | echo '</p>'; |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Prepare table list items. |
| 280 | */ |
| 281 | public function prepare_items() { |
| 282 | $per_page = $this->get_items_per_page( 'woocommerce_webhooks_per_page' ); |
| 283 | $current_page = $this->get_pagenum(); |
| 284 | |
| 285 | // Query args. |
| 286 | $args = array( |
| 287 | 'limit' => $per_page, |
| 288 | 'offset' => $per_page * ( $current_page - 1 ), |
| 289 | ); |
| 290 | |
| 291 | // Handle the status query. |
| 292 | if ( ! empty( $_REQUEST['status'] ) ) { // WPCS: input var okay, CSRF ok. |
| 293 | $args['status'] = sanitize_key( wp_unslash( $_REQUEST['status'] ) ); // WPCS: input var okay, CSRF ok. |
| 294 | } |
| 295 | |
| 296 | if ( ! empty( $_REQUEST['s'] ) ) { // WPCS: input var okay, CSRF ok. |
| 297 | $args['search'] = sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ); // WPCS: input var okay, CSRF ok. |
| 298 | } |
| 299 | |
| 300 | $args['paginate'] = true; |
| 301 | |
| 302 | // Get the webhooks. |
| 303 | $data_store = WC_Data_Store::load( 'webhook' ); |
| 304 | $webhooks = $data_store->search_webhooks( $args ); |
| 305 | $this->items = array_map( 'wc_get_webhook', $webhooks->webhooks ); |
| 306 | |
| 307 | // Set the pagination. |
| 308 | $this->set_pagination_args( |
| 309 | array( |
| 310 | 'total_items' => $webhooks->total, |
| 311 | 'per_page' => $per_page, |
| 312 | 'total_pages' => $webhooks->max_num_pages, |
| 313 | ) |
| 314 | ); |
| 315 | } |
| 316 | } |
| 317 |