WC_Admin_Webhooks_Table_List

The WooCommerce WC Admin Webhooks Table List class.

Defined (1)

The class is defined in the following location(s).

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