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