/includes/admin/class-wc-admin-webhooks-table-list.php

  1. <?php 
  2. /** 
  3. * WooCommerce Webhooks Table List 
  4. * 
  5. * @author WooThemes 
  6. * @category Admin 
  7. * @package WooCommerce/Admin 
  8. * @version 2.4.0 
  9. */ 
  10.  
  11. if ( ! defined( 'ABSPATH' ) ) { 
  12. exit; // Exit if accessed directly 
  13.  
  14. if ( ! class_exists( 'WP_List_Table' ) ) { 
  15. require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); 
  16.  
  17. class WC_Admin_Webhooks_Table_List extends WP_List_Table { 
  18.  
  19. /** 
  20. * Initialize the webhook table list. 
  21. */ 
  22. public function __construct() { 
  23. parent::__construct( array( 
  24. 'singular' => 'webhook',  
  25. 'plural' => 'webhooks',  
  26. 'ajax' => false,  
  27. ) ); 
  28.  
  29. /** 
  30. * Get list columns. 
  31. * 
  32. * @return array 
  33. */ 
  34. public function get_columns() { 
  35. return array( 
  36. 'cb' => '<input type="checkbox" />',  
  37. 'title' => __( 'Name', 'woocommerce' ),  
  38. 'status' => __( 'Status', 'woocommerce' ),  
  39. 'topic' => __( 'Topic', 'woocommerce' ),  
  40. 'delivery_url' => __( 'Delivery URL', 'woocommerce' ),  
  41. ); 
  42.  
  43. /** 
  44. * Column cb. 
  45. * 
  46. * @param WC_Post $webhook 
  47. * @return string 
  48. */ 
  49. public function column_cb( $webhook ) { 
  50. return sprintf( '<input type="checkbox" name="%1$s[]" value="%2$s" />', $this->_args['singular'], $webhook->ID ); 
  51.  
  52. /** 
  53. * Get Webhook object. 
  54. * @param object $webhook 
  55. * @return WC_Webhook 
  56. */ 
  57. private function get_webbook_object( $webhook ) { 
  58. global $the_webhook; 
  59.  
  60. if ( empty( $the_webhook ) || $the_webhook->id != $webhook->ID ) { 
  61. $the_webhook = new WC_Webhook( $webhook->ID ); 
  62.  
  63. return $the_webhook; 
  64.  
  65. /** 
  66. * Return title column. 
  67. * @param object $webhook 
  68. * @return string 
  69. */ 
  70. public function column_title( $webhook ) { 
  71. $the_webhook = $this->get_webbook_object( $webhook ); 
  72. $edit_link = admin_url( 'admin.php?page=wc-settings&tab=api&section=webhooks&edit-webhook=' . $the_webhook->id ); 
  73. $title = _draft_or_post_title( $the_webhook->get_post_data() ); 
  74. $post_type_object = get_post_type_object( $the_webhook->get_post_data()->post_type ); 
  75. $post_status = $the_webhook->get_post_data()->post_status; 
  76.  
  77. // Title 
  78. $output = '<strong>'; 
  79. if ( 'trash' == $post_status ) { 
  80. $output .= esc_html( $title ); 
  81. } else { 
  82. $output .= '<a href="' . esc_url( $edit_link ) . '" class="row-title">' . esc_html( $title ) . '</a>'; 
  83. $output .= '</strong>'; 
  84.  
  85. // Get actions 
  86. $actions = array( 
  87. 'id' => sprintf( __( 'ID: %d', 'woocommerce' ), $the_webhook->id ),  
  88. ); 
  89.  
  90. if ( current_user_can( $post_type_object->cap->edit_post, $the_webhook->id ) && 'trash' !== $post_status ) { 
  91. $actions['edit'] = '<a href="' . esc_url( $edit_link ) . '">' . __( 'Edit', 'woocommerce' ) . '</a>'; 
  92.  
  93. if ( current_user_can( $post_type_object->cap->delete_post, $the_webhook->id ) ) { 
  94. if ( 'trash' == $post_status ) { 
  95. $actions['untrash'] = '<a aria-label="' . 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 ) . '">' . esc_html__( 'Restore', 'woocommerce' ) . '</a>'; 
  96. } elseif ( EMPTY_TRASH_DAYS ) { 
  97. $actions['trash'] = '<a class="submitdelete" aria-label="' . esc_attr__( 'Move this item to the Trash', 'woocommerce' ) . '" href="' . get_delete_post_link( $the_webhook->id ) . '">' . esc_html__( 'Trash', 'woocommerce' ) . '</a>'; 
  98. if ( 'trash' == $post_status || ! EMPTY_TRASH_DAYS ) { 
  99. $actions['delete'] = '<a class="submitdelete" aria-label="' . esc_attr__( 'Delete this item permanently', 'woocommerce' ) . '" href="' . get_delete_post_link( $the_webhook->id, '', true ) . '">' . esc_html__( 'Delete permanently', 'woocommerce' ) . '</a>'; 
  100.  
  101. $actions = apply_filters( 'post_row_actions', $actions, $the_webhook->get_post_data() ); 
  102. $row_actions = array(); 
  103.  
  104. foreach ( $actions as $action => $link ) { 
  105. $row_actions[] = '<span class="' . esc_attr( $action ) . '">' . $link . '</span>'; 
  106.  
  107. $output .= '<div class="row-actions">' . implode( ' | ', $row_actions ) . '</div>'; 
  108.  
  109. return $output; 
  110.  
  111. /** 
  112. * Return status column. 
  113. * @param object $webhook 
  114. * @return string 
  115. */ 
  116. public function column_status( $webhook ) { 
  117. return $this->get_webbook_object( $webhook )->get_i18n_status(); 
  118.  
  119. /** 
  120. * Return topic column. 
  121. * @param object $webhook 
  122. * @return string 
  123. */ 
  124. public function column_topic( $webhook ) { 
  125. return $this->get_webbook_object( $webhook )->get_topic(); 
  126.  
  127. /** 
  128. * Return delivery URL column. 
  129. * @param object $webhook 
  130. * @return string 
  131. */ 
  132. public function column_delivery_url( $webhook ) { 
  133. return $this->get_webbook_object( $webhook )->get_delivery_url(); 
  134.  
  135. /** 
  136. * Get the status label for webhooks. 
  137. * 
  138. * @param string $status_name 
  139. * @param stdClass $status 
  140. * 
  141. * @return array 
  142. */ 
  143. private function get_status_label( $status_name, $status ) { 
  144. switch ( $status_name ) { 
  145. case 'publish' : 
  146. /** translators: %s: count */ 
  147. $label = array( 
  148. 'singular' => __( 'Activated <span class="count">(%s)</span>', 'woocommerce' ),  
  149. 'plural' => __( 'Activated <span class="count">(%s)</span>', 'woocommerce' ),  
  150. 'context' => '',  
  151. 'domain' => 'woocommerce',  
  152. ); 
  153. break; 
  154. case 'draft' : 
  155. /** translators: %s: count */ 
  156. $label = array( 
  157. 'singular' => __( 'Paused <span class="count">(%s)</span>', 'woocommerce' ),  
  158. 'plural' => __( 'Paused <span class="count">(%s)</span>', 'woocommerce' ),  
  159. 'context' => '',  
  160. 'domain' => 'woocommerce',  
  161. ); 
  162. break; 
  163. case 'pending' : 
  164. /** translators: %s: count */ 
  165. $label = array( 
  166. 'singular' => __( 'Disabled <span class="count">(%s)</span>', 'woocommerce' ),  
  167. 'plural' => __( 'Disabled <span class="count">(%s)</span>', 'woocommerce' ),  
  168. 'context' => '',  
  169. 'domain' => 'woocommerce',  
  170. ); 
  171. break; 
  172.  
  173. default: 
  174. $label = $status->label_count; 
  175. break; 
  176.  
  177. return $label; 
  178.  
  179. /** 
  180. * Table list views. 
  181. * 
  182. * @return array 
  183. */ 
  184. protected function get_views() { 
  185. $status_links = array(); 
  186. $num_posts = wp_count_posts( 'shop_webhook', 'readable' ); 
  187. $class = ''; 
  188. $total_posts = array_sum( (array) $num_posts ); 
  189.  
  190. // Subtract post types that are not included in the admin all list. 
  191. foreach ( get_post_stati( array( 'show_in_admin_all_list' => false ) ) as $state ) { 
  192. $total_posts -= $num_posts->$state; 
  193.  
  194. $class = empty( $class ) && empty( $_REQUEST['status'] ) ? ' class="current"' : ''; 
  195. /** translators: %s: count */ 
  196. $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>'; 
  197.  
  198. foreach ( get_post_stati( array( 'show_in_admin_status_list' => true ), 'objects' ) as $status ) { 
  199. $class = ''; 
  200. $status_name = $status->name; 
  201.  
  202. if ( ! in_array( $status_name, array( 'publish', 'draft', 'pending', 'trash', 'future', 'private', 'auto-draft' ) ) ) { 
  203. continue; 
  204.  
  205. if ( empty( $num_posts->$status_name ) ) { 
  206. continue; 
  207.  
  208. if ( isset( $_REQUEST['status'] ) && $status_name == $_REQUEST['status'] ) { 
  209. $class = ' class="current"'; 
  210.  
  211. $label = $this->get_status_label( $status_name, $status ); 
  212.  
  213. $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>'; 
  214.  
  215. return $status_links; 
  216.  
  217. /** 
  218. * Get bulk actions. 
  219. * 
  220. * @return array 
  221. */ 
  222. protected function get_bulk_actions() { 
  223. if ( isset( $_GET['status'] ) && 'trash' == $_GET['status'] ) { 
  224. return array( 
  225. 'untrash' => __( 'Restore', 'woocommerce' ),  
  226. 'delete' => __( 'Delete permanently', 'woocommerce' ),  
  227. ); 
  228.  
  229. return array( 
  230. 'trash' => __( 'Move to trash', 'woocommerce' ),  
  231. ); 
  232.  
  233. /** 
  234. * Extra controls to be displayed between bulk actions and pagination. 
  235. * 
  236. * @param string $which 
  237. */ 
  238. protected function extra_tablenav( $which ) { 
  239. if ( 'top' == $which && isset( $_GET['status'] ) && 'trash' == $_GET['status'] && current_user_can( 'delete_shop_webhooks' ) ) { 
  240. 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>'; 
  241.  
  242. /** 
  243. * Prepare table list items. 
  244. */ 
  245. public function prepare_items() { 
  246. $per_page = apply_filters( 'woocommerce_webhooks_settings_posts_per_page', 10 ); 
  247. $columns = $this->get_columns(); 
  248. $hidden = array(); 
  249. $sortable = $this->get_sortable_columns(); 
  250.  
  251. // Column headers 
  252. $this->_column_headers = array( $columns, $hidden, $sortable ); 
  253.  
  254. $current_page = $this->get_pagenum(); 
  255.  
  256. // Query args 
  257. $args = array( 
  258. 'post_type' => 'shop_webhook',  
  259. 'posts_per_page' => $per_page,  
  260. 'ignore_sticky_posts' => true,  
  261. 'paged' => $current_page,  
  262. ); 
  263.  
  264. // Handle the status query 
  265. if ( ! empty( $_REQUEST['status'] ) ) { 
  266. $args['post_status'] = sanitize_text_field( $_REQUEST['status'] ); 
  267.  
  268. if ( ! empty( $_REQUEST['s'] ) ) { 
  269. $args['s'] = sanitize_text_field( $_REQUEST['s'] ); 
  270.  
  271. // Get the webhooks 
  272. $webhooks = new WP_Query( $args ); 
  273. $this->items = $webhooks->posts; 
  274.  
  275. // Set the pagination 
  276. $this->set_pagination_args( array( 
  277. 'total_items' => $webhooks->found_posts,  
  278. 'per_page' => $per_page,  
  279. 'total_pages' => $webhooks->max_num_pages,  
  280. ) ); 
.