2015-01-06 19:24:32 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-11-03 13:53:50 +00:00
|
|
|
* WooCommerce Webhooks Table List
|
2015-01-06 19:24:32 +00:00
|
|
|
*
|
2017-11-07 17:32:31 +00:00
|
|
|
* @author Automattic
|
2015-01-06 19:24:32 +00:00
|
|
|
* @category Admin
|
|
|
|
* @package WooCommerce/Admin
|
2017-11-07 17:32:31 +00:00
|
|
|
* @version 3.3.0
|
2015-01-06 19:24:32 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
2017-11-07 17:32:31 +00:00
|
|
|
exit; // Exit if accessed directly.
|
2015-01-06 19:24:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! class_exists( 'WP_List_Table' ) ) {
|
|
|
|
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
|
|
|
|
}
|
|
|
|
|
2017-11-07 17:32:31 +00:00
|
|
|
/**
|
|
|
|
* Webooks table list class.
|
|
|
|
*/
|
2015-01-06 19:24:32 +00:00
|
|
|
class WC_Admin_Webhooks_Table_List extends WP_List_Table {
|
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Initialize the webhook table list.
|
2015-01-06 19:24:32 +00:00
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct( array(
|
2017-04-05 21:13:10 +00:00
|
|
|
'singular' => 'webhook',
|
|
|
|
'plural' => 'webhooks',
|
2016-08-27 01:46:45 +00:00
|
|
|
'ajax' => false,
|
2015-01-06 19:24:32 +00:00
|
|
|
) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Get list columns.
|
2015-01-06 19:24:32 +00:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_columns() {
|
2015-01-30 13:57:42 +00:00
|
|
|
return array(
|
2015-01-06 19:24:32 +00:00
|
|
|
'cb' => '<input type="checkbox" />',
|
|
|
|
'title' => __( 'Name', 'woocommerce' ),
|
|
|
|
'status' => __( 'Status', 'woocommerce' ),
|
|
|
|
'topic' => __( 'Topic', 'woocommerce' ),
|
|
|
|
'delivery_url' => __( 'Delivery URL', 'woocommerce' ),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Column cb.
|
|
|
|
*
|
2017-11-07 17:32:31 +00:00
|
|
|
* @param WC_Webhook $webhook Webhook instance.
|
2015-01-06 19:24:32 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function column_cb( $webhook ) {
|
2017-11-09 13:04:31 +00:00
|
|
|
return sprintf( '<input type="checkbox" name="%1$s[]" value="%2$s" />', $this->_args['singular'], $webhook->get_id() );
|
2015-01-30 13:57:42 +00:00
|
|
|
}
|
2015-01-06 19:24:32 +00:00
|
|
|
|
2015-01-30 13:57:42 +00:00
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Return title column.
|
2017-11-07 17:32:31 +00:00
|
|
|
*
|
|
|
|
* @param WC_Webhook $webhook Webhook instance.
|
2015-01-30 13:57:42 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function column_title( $webhook ) {
|
2017-11-09 13:04:31 +00:00
|
|
|
$edit_link = admin_url( 'admin.php?page=wc-settings&tab=api&section=webhooks&edit-webhook=' . $webhook->get_id() );
|
|
|
|
$output = '';
|
2015-01-30 13:57:42 +00:00
|
|
|
|
2017-11-07 17:32:31 +00:00
|
|
|
// Title.
|
2017-11-09 13:04:31 +00:00
|
|
|
$output .= '<strong><a href="' . esc_url( $edit_link ) . '" class="row-title">' . esc_html( $webhook->get_name() ) . '</a></strong>';
|
2015-01-06 19:24:32 +00:00
|
|
|
|
2017-11-07 17:32:31 +00:00
|
|
|
// Get actions.
|
2015-01-30 13:57:42 +00:00
|
|
|
$actions = array(
|
2017-11-07 17:32:31 +00:00
|
|
|
/* translators: %s: webhook ID. */
|
2017-11-09 14:07:44 +00:00
|
|
|
'id' => sprintf( __( 'ID: %d', 'woocommerce' ), $webhook->get_id() ),
|
|
|
|
'edit' => '<a href="' . esc_url( $edit_link ) . '">' . esc_html__( 'Edit', 'woocommerce' ) . '</a>',
|
|
|
|
/* translators: %s: webhook name */
|
|
|
|
'delete' => '<a class="submitdelete" aria-label="' . esc_attr( sprintf( __( 'Delete "%s" permanently', 'woocommerce' ), $webhook->get_name() ) ) . '" href="' . esc_url( wp_nonce_url( add_query_arg( array(
|
|
|
|
'delete' => $webhook->get_id(),
|
|
|
|
), admin_url( 'admin.php?page=wc-settings&tab=api§ion=webhooks' ) ), 'delete-webhook' ) ) . '">' . esc_html__( 'Delete permanently', 'woocommerce' ) . '</a>',
|
2015-01-30 13:57:42 +00:00
|
|
|
);
|
2015-01-06 19:24:32 +00:00
|
|
|
|
2017-11-09 13:04:31 +00:00
|
|
|
$actions = apply_filters( 'webhook_row_actions', $actions, $webhook );
|
2015-01-30 13:57:42 +00:00
|
|
|
$row_actions = array();
|
2015-01-06 19:24:32 +00:00
|
|
|
|
2015-01-30 13:57:42 +00:00
|
|
|
foreach ( $actions as $action => $link ) {
|
|
|
|
$row_actions[] = '<span class="' . esc_attr( $action ) . '">' . $link . '</span>';
|
|
|
|
}
|
2015-01-06 19:24:32 +00:00
|
|
|
|
2016-09-02 01:51:31 +00:00
|
|
|
$output .= '<div class="row-actions">' . implode( ' | ', $row_actions ) . '</div>';
|
2015-01-06 19:24:32 +00:00
|
|
|
|
2015-01-30 13:57:42 +00:00
|
|
|
return $output;
|
|
|
|
}
|
2015-01-06 19:24:32 +00:00
|
|
|
|
2015-01-30 13:57:42 +00:00
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Return status column.
|
2017-11-07 17:32:31 +00:00
|
|
|
*
|
|
|
|
* @param WC_Webhook $webhook Webhook instance.
|
2015-01-30 13:57:42 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function column_status( $webhook ) {
|
2017-11-09 13:04:31 +00:00
|
|
|
return $webhook->get_i18n_status();
|
2015-01-30 13:57:42 +00:00
|
|
|
}
|
2015-01-06 19:24:32 +00:00
|
|
|
|
2015-01-30 13:57:42 +00:00
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Return topic column.
|
2017-11-07 17:32:31 +00:00
|
|
|
*
|
|
|
|
* @param WC_Webhook $webhook Webhook instance.
|
2015-01-30 13:57:42 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function column_topic( $webhook ) {
|
2017-11-09 13:04:31 +00:00
|
|
|
return $webhook->get_topic();
|
2015-01-30 13:57:42 +00:00
|
|
|
}
|
2015-01-06 19:24:32 +00:00
|
|
|
|
2015-01-30 13:57:42 +00:00
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Return delivery URL column.
|
2017-11-07 17:32:31 +00:00
|
|
|
*
|
|
|
|
* @param WC_Webhook $webhook Webhook instance.
|
2015-01-30 13:57:42 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function column_delivery_url( $webhook ) {
|
2017-11-09 13:04:31 +00:00
|
|
|
return $webhook->get_delivery_url();
|
2015-01-06 19:24:32 +00:00
|
|
|
}
|
|
|
|
|
2015-01-07 00:02:33 +00:00
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Get the status label for webhooks.
|
2015-01-07 00:02:33 +00:00
|
|
|
*
|
2017-11-09 13:04:31 +00:00
|
|
|
* @param string $status_name Status name.
|
|
|
|
* @param int $amount Amount of webhooks.
|
2015-01-07 00:02:33 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2017-11-09 13:04:31 +00:00
|
|
|
private function get_status_label( $status_name, $amount ) {
|
|
|
|
$statuses = wc_get_webhook_statuses();
|
|
|
|
|
|
|
|
if ( isset( $statuses[ $status_name ] ) ) {
|
|
|
|
return array(
|
|
|
|
'singular' => sprintf( '%s <span class="count">(%s)</span>', esc_html( $statuses[ $status_name ] ), $amount ),
|
|
|
|
'plural' => sprintf( '%s <span class="count">(%s)</span>', esc_html( $statuses[ $status_name ] ), $amount ),
|
|
|
|
'context' => '',
|
|
|
|
'domain' => 'woocommerce',
|
|
|
|
);
|
2015-01-07 00:02:33 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 13:04:31 +00:00
|
|
|
return array(
|
|
|
|
'singular' => sprintf( '%s <span class="count">(%s)</span>', esc_html( $status_name ), $amount ),
|
|
|
|
'plural' => sprintf( '%s <span class="count">(%s)</span>', esc_html( $status_name ), $amount ),
|
|
|
|
'context' => '',
|
|
|
|
'domain' => 'woocommerce',
|
|
|
|
);
|
2015-01-07 00:02:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Table list views.
|
2015-01-07 00:02:33 +00:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function get_views() {
|
2017-11-09 13:04:31 +00:00
|
|
|
$status_links = array();
|
|
|
|
$data_store = WC_Data_Store::load( 'webhook' );
|
|
|
|
$num_webhooks = $data_store->get_count_webhooks_by_status();
|
|
|
|
$total_webhooks = array_sum( (array) $num_webhooks );
|
|
|
|
$statuses = array_keys( wc_get_webhook_statuses() );
|
|
|
|
$class = empty( $_REQUEST['status'] ) ? ' class="current"' : ''; // WPCS: input var okay. CSRF ok.
|
2015-01-07 00:02:33 +00:00
|
|
|
|
2016-10-29 10:16:03 +00:00
|
|
|
/* translators: %s: count */
|
2017-11-09 13:04:31 +00:00
|
|
|
$status_links['all'] = "<a href='admin.php?page=wc-settings&tab=api&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>';
|
2015-01-07 00:02:33 +00:00
|
|
|
|
2017-11-09 13:04:31 +00:00
|
|
|
foreach ( $statuses as $status_name ) {
|
2015-01-07 00:02:33 +00:00
|
|
|
$class = '';
|
|
|
|
|
2017-11-09 13:04:31 +00:00
|
|
|
if ( empty( $num_webhooks[ $status_name ] ) ) {
|
2015-01-07 00:02:33 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-11-07 17:32:31 +00:00
|
|
|
if ( isset( $_REQUEST['status'] ) && sanitize_key( wp_unslash( $_REQUEST['status'] ) ) === $status_name ) { // WPCS: input var okay, CSRF ok.
|
2015-01-07 00:02:33 +00:00
|
|
|
$class = ' class="current"';
|
|
|
|
}
|
|
|
|
|
2017-11-09 13:04:31 +00:00
|
|
|
$label = $this->get_status_label( $status_name, $num_webhooks[ $status_name ] );
|
2015-01-07 00:02:33 +00:00
|
|
|
|
2017-11-09 13:04:31 +00:00
|
|
|
$status_links[ $status_name ] = "<a href='admin.php?page=wc-settings&tab=api&section=webhooks&status=$status_name'$class>" . sprintf( translate_nooped_plural( $label, $num_webhooks[ $status_name ] ), number_format_i18n( $num_webhooks[ $status_name ] ) ) . '</a>';
|
2015-01-07 00:02:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $status_links;
|
|
|
|
}
|
|
|
|
|
2015-01-10 00:08:45 +00:00
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Get bulk actions.
|
2015-01-10 00:08:45 +00:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function get_bulk_actions() {
|
|
|
|
return array(
|
2017-11-09 14:07:44 +00:00
|
|
|
'delete' => __( 'Delete permanently', 'woocommerce' ),
|
2015-01-10 00:08:45 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-11-09 13:04:31 +00:00
|
|
|
/**
|
|
|
|
* Generate the table navigation above or below the table.
|
2017-11-09 14:07:44 +00:00
|
|
|
* Included to remove extra nonce input.
|
2017-11-09 13:04:31 +00:00
|
|
|
*
|
2017-11-09 14:07:44 +00:00
|
|
|
* @param string $which The location of the extra table nav markup: 'top' or 'bottom'.
|
2017-11-09 13:04:31 +00:00
|
|
|
*/
|
|
|
|
protected function display_tablenav( $which ) {
|
|
|
|
echo '<div class="tablenav ' . esc_attr( $which ) . '">';
|
|
|
|
|
|
|
|
if ( $this->has_items() ) {
|
|
|
|
echo '<div class="alignleft actions bulkactions">';
|
|
|
|
$this->bulk_actions( $which );
|
|
|
|
echo '</div>';
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->extra_tablenav( $which );
|
|
|
|
$this->pagination( $which );
|
|
|
|
echo '<br class="clear" />';
|
|
|
|
echo '</div>';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search box.
|
|
|
|
*
|
|
|
|
* @param string $text Button text.
|
|
|
|
* @param string $input_id Input ID.
|
|
|
|
*/
|
|
|
|
public function search_box( $text, $input_id ) {
|
|
|
|
if ( empty( $_REQUEST['s'] ) && ! $this->has_items() ) { // WPCS: input var okay, CSRF ok.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$input_id = $input_id . '-search-input';
|
|
|
|
$search_query = isset( $_REQUEST['s'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) : ''; // WPCS: input var okay, CSRF ok.
|
|
|
|
|
|
|
|
echo '<p class="search-box">';
|
|
|
|
echo '<label class="screen-reader-text" for="' . esc_attr( $input_id ) . '">' . esc_html( $text ) . ':</label>';
|
|
|
|
echo '<input type="search" id="' . esc_attr( $input_id ) . '" name="s" value="' . esc_attr( $search_query ) . '" />';
|
|
|
|
submit_button( $text, '', '', false, array(
|
|
|
|
'id' => 'search-submit',
|
|
|
|
) );
|
|
|
|
echo '</p>';
|
|
|
|
}
|
|
|
|
|
2015-01-06 19:24:32 +00:00
|
|
|
/**
|
|
|
|
* Prepare table list items.
|
|
|
|
*/
|
|
|
|
public function prepare_items() {
|
2017-11-09 13:04:31 +00:00
|
|
|
$per_page = absint( apply_filters( 'woocommerce_webhooks_settings_posts_per_page', 10 ) );
|
|
|
|
$per_page = 0 === $per_page ? 10 : $per_page;
|
2015-01-06 19:24:32 +00:00
|
|
|
$columns = $this->get_columns();
|
|
|
|
$hidden = array();
|
|
|
|
$sortable = $this->get_sortable_columns();
|
2015-01-07 00:02:33 +00:00
|
|
|
|
2017-11-07 17:32:31 +00:00
|
|
|
// Column headers.
|
2015-01-06 19:24:32 +00:00
|
|
|
$this->_column_headers = array( $columns, $hidden, $sortable );
|
|
|
|
|
|
|
|
$current_page = $this->get_pagenum();
|
|
|
|
|
2017-11-07 17:32:31 +00:00
|
|
|
// Query args.
|
2015-01-07 00:02:33 +00:00
|
|
|
$args = array(
|
2017-11-09 13:04:31 +00:00
|
|
|
'limit' => $per_page,
|
|
|
|
'offset' => $per_page * ( $current_page - 1 ),
|
2015-01-07 00:02:33 +00:00
|
|
|
);
|
|
|
|
|
2017-11-07 17:32:31 +00:00
|
|
|
// Handle the status query.
|
|
|
|
if ( ! empty( $_REQUEST['status'] ) ) { // WPCS: input var okay, CSRF ok.
|
2017-11-09 13:04:31 +00:00
|
|
|
$args['status'] = sanitize_key( wp_unslash( $_REQUEST['status'] ) ); // WPCS: input var okay, CSRF ok.
|
2015-01-07 00:02:33 +00:00
|
|
|
}
|
2015-01-06 19:24:32 +00:00
|
|
|
|
2017-11-09 13:04:31 +00:00
|
|
|
if ( ! empty( $_REQUEST['s'] ) ) { // WPCS: input var okay, CSRF ok.
|
|
|
|
$args['search'] = sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ); // WPCS: input var okay, CSRF ok.
|
2015-01-10 00:41:33 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:32:31 +00:00
|
|
|
// Get the webhooks.
|
2017-11-09 13:04:31 +00:00
|
|
|
$data_store = WC_Data_Store::load( 'webhook' );
|
|
|
|
$webhooks = $data_store->search_webhooks( $args );
|
|
|
|
$this->items = array_map( 'wc_get_webhook', $webhooks );
|
|
|
|
$total_items = count( $data_store->get_webhooks_ids() );
|
2015-01-06 19:24:32 +00:00
|
|
|
|
2017-11-07 17:32:31 +00:00
|
|
|
// Set the pagination.
|
2015-01-06 19:24:32 +00:00
|
|
|
$this->set_pagination_args( array(
|
2017-11-09 13:04:31 +00:00
|
|
|
'total_items' => $total_items,
|
2015-01-06 19:24:32 +00:00
|
|
|
'per_page' => $per_page,
|
2017-11-09 13:04:31 +00:00
|
|
|
'total_pages' => ceil( $total_items / $per_page ),
|
2015-01-06 19:24:32 +00:00
|
|
|
) );
|
|
|
|
}
|
|
|
|
}
|