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
*
* @ author WooThemes
* @ category Admin
* @ package WooCommerce / Admin
2015-05-15 19:12:11 +00:00
* @ version 2.4 . 0
2015-01-06 19:24:32 +00:00
*/
if ( ! defined ( 'ABSPATH' ) ) {
exit ; // Exit if accessed directly
}
if ( ! class_exists ( 'WP_List_Table' ) ) {
require_once ( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}
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 (
'singular' => __ ( 'webhook' , 'woocommerce' ),
'plural' => __ ( 'webhooks' , 'woocommerce' ),
'ajax' => false
) );
}
/**
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 .
*
* @ param WC_Post $webhook
* @ return string
*/
public function column_cb ( $webhook ) {
return sprintf ( '<input type="checkbox" name="%1$s[]" value="%2$s" />' , $this -> _args [ 'singular' ], $webhook -> ID );
}
/**
2015-11-03 12:28:01 +00:00
* Get Webhook object .
2015-01-30 13:57:42 +00:00
* @ param object $webhook
* @ return WC_Webhook
2015-01-06 19:24:32 +00:00
*/
2015-01-30 13:57:42 +00:00
private function get_webbook_object ( $webhook ) {
2015-01-06 19:24:32 +00:00
global $the_webhook ;
if ( empty ( $the_webhook ) || $the_webhook -> id != $webhook -> ID ) {
$the_webhook = new WC_Webhook ( $webhook -> ID );
}
2015-01-30 13:57:42 +00:00
return $the_webhook ;
}
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 .
2015-01-30 13:57:42 +00:00
* @ param object $webhook
* @ return string
*/
public function column_title ( $webhook ) {
$the_webhook = $this -> get_webbook_object ( $webhook );
2015-05-15 19:12:11 +00:00
$edit_link = admin_url ( 'admin.php?page=wc-settings&tab=api&section=webhooks&edit-webhook=' . $the_webhook -> id );
2015-01-30 13:57:42 +00:00
$title = _draft_or_post_title ( $the_webhook -> get_post_data () );
$post_type_object = get_post_type_object ( $the_webhook -> get_post_data () -> post_type );
$post_status = $the_webhook -> get_post_data () -> post_status ;
// Title
$output = '<strong>' ;
if ( 'trash' == $post_status ) {
$output .= esc_html ( $title );
} else {
2015-07-15 18:45:57 +00:00
$output .= '<a href="' . esc_url ( $edit_link ) . '" class="row-title">' . esc_html ( $title ) . '</a>' ;
2015-01-30 13:57:42 +00:00
}
$output .= '</strong>' ;
2015-01-06 19:24:32 +00:00
2015-01-30 13:57:42 +00:00
// Get actions
$actions = array (
'id' => sprintf ( __ ( 'ID: %d' , 'woocommerce' ), $the_webhook -> id )
);
2015-01-06 19:24:32 +00:00
2015-01-30 13:57:42 +00:00
if ( current_user_can ( $post_type_object -> cap -> edit_post , $the_webhook -> id ) && 'trash' !== $post_status ) {
2015-05-18 18:33:36 +00:00
$actions [ 'edit' ] = '<a href="' . esc_url ( $edit_link ) . '">' . __ ( 'Edit' , 'woocommerce' ) . '</a>' ;
2015-01-30 13:57:42 +00:00
}
2015-01-06 19:24:32 +00:00
2015-01-30 13:57:42 +00:00
if ( current_user_can ( $post_type_object -> cap -> delete_post , $the_webhook -> id ) ) {
if ( 'trash' == $post_status ) {
2015-05-18 18:33:36 +00:00
$actions [ 'untrash' ] = '<a title="' . esc_attr__ ( 'Restore this item from the Trash' , 'woocommerce' ) . '" href="' . wp_nonce_url ( admin_url ( sprintf ( $post_type_object -> _edit_link . '&action=untrash' , $the_webhook -> id ) ), 'untrash-post_' . $the_webhook -> id ) . '">' . __ ( 'Restore' , 'woocommerce' ) . '</a>' ;
2015-01-30 13:57:42 +00:00
} elseif ( EMPTY_TRASH_DAYS ) {
$actions [ 'trash' ] = '<a class="submitdelete" title="' . esc_attr ( __ ( 'Move this item to the Trash' , 'woocommerce' ) ) . '" href="' . get_delete_post_link ( $the_webhook -> id ) . '">' . __ ( 'Trash' , 'woocommerce' ) . '</a>' ;
}
if ( 'trash' == $post_status || ! EMPTY_TRASH_DAYS ) {
$actions [ 'delete' ] = '<a class="submitdelete" title="' . esc_attr ( __ ( 'Delete this item permanently' , 'woocommerce' ) ) . '" href="' . get_delete_post_link ( $the_webhook -> id , '' , true ) . '">' . __ ( 'Delete Permanently' , 'woocommerce' ) . '</a>' ;
}
}
2015-01-06 19:24:32 +00:00
2015-01-30 13:57:42 +00:00
$actions = apply_filters ( 'post_row_actions' , $actions , $the_webhook -> get_post_data () );
$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
2015-01-30 13:57:42 +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 .
2015-01-30 13:57:42 +00:00
* @ param object $webhook
* @ return string
*/
public function column_status ( $webhook ) {
return $this -> get_webbook_object ( $webhook ) -> get_i18n_status ();
}
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 .
2015-01-30 13:57:42 +00:00
* @ param object $webhook
* @ return string
*/
public function column_topic ( $webhook ) {
return $this -> get_webbook_object ( $webhook ) -> get_topic ();
}
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 .
2015-01-30 13:57:42 +00:00
* @ param object $webhook
* @ return string
*/
public function column_delivery_url ( $webhook ) {
return $this -> get_webbook_object ( $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
*
* @ param string $status_name
* @ param stdClass $status
*
* @ return array
*/
private function get_status_label ( $status_name , $status ) {
switch ( $status_name ) {
case 'publish' :
$label = array (
2015-01-15 11:35:13 +00:00
'singular' => __ ( 'Activated <span class="count">(%s)</span>' , 'woocommerce' ),
'plural' => __ ( 'Activated <span class="count">(%s)</span>' , 'woocommerce' ),
2015-01-07 00:02:33 +00:00
'context' => '' ,
'domain' => 'woocommerce' ,
);
break ;
case 'draft' :
$label = array (
'singular' => __ ( 'Paused <span class="count">(%s)</span>' , 'woocommerce' ),
'plural' => __ ( 'Paused <span class="count">(%s)</span>' , 'woocommerce' ),
'context' => '' ,
'domain' => 'woocommerce' ,
);
break ;
case 'pending' :
$label = array (
'singular' => __ ( 'Disabled <span class="count">(%s)</span>' , 'woocommerce' ),
'plural' => __ ( 'Disabled <span class="count">(%s)</span>' , 'woocommerce' ),
'context' => '' ,
'domain' => 'woocommerce' ,
);
break ;
default :
$label = $status -> label_count ;
break ;
}
return $label ;
}
/**
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 () {
$status_links = array ();
$num_posts = wp_count_posts ( 'shop_webhook' , 'readable' );
$class = '' ;
$total_posts = array_sum ( ( array ) $num_posts );
// Subtract post types that are not included in the admin all list.
foreach ( get_post_stati ( array ( 'show_in_admin_all_list' => false ) ) as $state ) {
$total_posts -= $num_posts -> $state ;
}
$class = empty ( $class ) && empty ( $_REQUEST [ 'status' ] ) ? ' class="current"' : '' ;
2015-05-15 19:12:11 +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_posts , 'posts' , 'woocommerce' ), number_format_i18n ( $total_posts ) ) . '</a>' ;
2015-01-07 00:02:33 +00:00
foreach ( get_post_stati ( array ( 'show_in_admin_status_list' => true ), 'objects' ) as $status ) {
$class = '' ;
$status_name = $status -> name ;
if ( ! in_array ( $status_name , array ( 'publish' , 'draft' , 'pending' , 'trash' , 'future' , 'private' , 'auto-draft' ) ) ) {
continue ;
}
if ( empty ( $num_posts -> $status_name ) ) {
continue ;
}
if ( isset ( $_REQUEST [ 'status' ] ) && $status_name == $_REQUEST [ 'status' ] ) {
$class = ' class="current"' ;
}
$label = $this -> get_status_label ( $status_name , $status );
2015-05-15 19:12:11 +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_posts -> $status_name ), number_format_i18n ( $num_posts -> $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 () {
if ( isset ( $_GET [ 'status' ] ) && 'trash' == $_GET [ 'status' ] ) {
return array (
'untrash' => __ ( 'Restore' , 'woocommerce' ),
'delete' => __ ( 'Delete Permanently' , 'woocommerce' )
);
}
return array (
'trash' => __ ( 'Move to Trash' , 'woocommerce' )
);
}
2015-01-10 01:03:22 +00:00
/**
2015-11-03 12:28:01 +00:00
* Extra controls to be displayed between bulk actions and pagination .
2015-01-10 01:03:22 +00:00
*
* @ param string $which
*/
protected function extra_tablenav ( $which ) {
if ( 'top' == $which && isset ( $_GET [ 'status' ] ) && 'trash' == $_GET [ 'status' ] && current_user_can ( 'delete_shop_webhooks' ) ) {
2015-05-18 19:01:51 +00:00
echo '<div class="alignleft actions"><a class="button apply" href="' . esc_url ( wp_nonce_url ( admin_url ( 'admin.php?page=wc-settings&tab=api§ion=webhooks&status=trash&empty_trash=1' ), 'empty_trash' ) ) . '">' . __ ( 'Empty Trash' , 'woocommerce' ) . '</a></div>' ;
2015-01-10 01:03:22 +00:00
}
}
2015-01-06 19:24:32 +00:00
/**
* Prepare table list items .
*/
public function prepare_items () {
2015-01-10 00:08:45 +00:00
$per_page = apply_filters ( 'woocommerce_webhooks_settings_posts_per_page' , 10 );
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
// Column headers
2015-01-06 19:24:32 +00:00
$this -> _column_headers = array ( $columns , $hidden , $sortable );
$current_page = $this -> get_pagenum ();
2015-01-07 00:02:33 +00:00
// Query args
$args = array (
2015-01-06 19:24:32 +00:00
'post_type' => 'shop_webhook' ,
'posts_per_page' => $per_page ,
'ignore_sticky_posts' => true ,
'paged' => $current_page
2015-01-07 00:02:33 +00:00
);
// Handle the status query
if ( ! empty ( $_REQUEST [ 'status' ] ) ) {
$args [ 'post_status' ] = sanitize_text_field ( $_REQUEST [ 'status' ] );
}
2015-01-06 19:24:32 +00:00
2015-01-10 00:41:33 +00:00
if ( ! empty ( $_REQUEST [ 's' ] ) ) {
$args [ 's' ] = sanitize_text_field ( $_REQUEST [ 's' ] );
}
2015-01-07 00:02:33 +00:00
// Get the webhooks
$webhooks = new WP_Query ( $args );
2015-01-06 19:24:32 +00:00
$this -> items = $webhooks -> posts ;
2015-01-07 00:02:33 +00:00
// Set the pagination
2015-01-06 19:24:32 +00:00
$this -> set_pagination_args ( array (
'total_items' => $webhooks -> found_posts ,
'per_page' => $per_page ,
'total_pages' => $webhooks -> max_num_pages
) );
}
}