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', 'woocommerce' ),  
  9. 'plural' => __( 'webhooks', 'woocommerce' ),  
  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 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>'; 
  78. } elseif ( EMPTY_TRASH_DAYS ) { 
  79. $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>'; 
  80. if ( 'trash' == $post_status || ! EMPTY_TRASH_DAYS ) { 
  81. $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>'; 
  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. $label = array( 
  127. 'singular' => __( 'Activated <span class="count">(%s)</span>', 'woocommerce' ),  
  128. 'plural' => __( 'Activated <span class="count">(%s)</span>', 'woocommerce' ),  
  129. 'context' => '',  
  130. 'domain' => 'woocommerce',  
  131. ); 
  132. break; 
  133. case 'draft' : 
  134. $label = array( 
  135. 'singular' => __( 'Paused <span class="count">(%s)</span>', 'woocommerce' ),  
  136. 'plural' => __( 'Paused <span class="count">(%s)</span>', 'woocommerce' ),  
  137. 'context' => '',  
  138. 'domain' => 'woocommerce',  
  139. ); 
  140. break; 
  141. case 'pending' : 
  142. $label = array( 
  143. 'singular' => __( 'Disabled <span class="count">(%s)</span>', 'woocommerce' ),  
  144. 'plural' => __( 'Disabled <span class="count">(%s)</span>', 'woocommerce' ),  
  145. 'context' => '',  
  146. 'domain' => 'woocommerce',  
  147. ); 
  148. break; 
  149.  
  150. default: 
  151. $label = $status->label_count; 
  152. break; 
  153.  
  154. return $label; 
  155.  
  156. /** 
  157. * Table list views. 
  158. * @return array 
  159. */ 
  160. protected function get_views() { 
  161. $status_links = array(); 
  162. $num_posts = wp_count_posts( 'shop_webhook', 'readable' ); 
  163. $class = ''; 
  164. $total_posts = array_sum( (array) $num_posts ); 
  165.  
  166. // Subtract post types that are not included in the admin all list. 
  167. foreach ( get_post_stati( array( 'show_in_admin_all_list' => false ) ) as $state ) { 
  168. $total_posts -= $num_posts->$state; 
  169.  
  170. $class = empty( $class ) && empty( $_REQUEST['status'] ) ? ' class="current"' : ''; 
  171. $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>'; 
  172.  
  173. foreach ( get_post_stati( array( 'show_in_admin_status_list' => true ), 'objects' ) as $status ) { 
  174. $class = ''; 
  175. $status_name = $status->name; 
  176.  
  177. if ( ! in_array( $status_name, array( 'publish', 'draft', 'pending', 'trash', 'future', 'private', 'auto-draft' ) ) ) { 
  178. continue; 
  179.  
  180. if ( empty( $num_posts->$status_name ) ) { 
  181. continue; 
  182.  
  183. if ( isset( $_REQUEST['status'] ) && $status_name == $_REQUEST['status'] ) { 
  184. $class = ' class="current"'; 
  185.  
  186. $label = $this->get_status_label( $status_name, $status ); 
  187.  
  188. $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>'; 
  189.  
  190. return $status_links; 
  191.  
  192. /** 
  193. * Get bulk actions. 
  194. * @return array 
  195. */ 
  196. protected function get_bulk_actions() { 
  197. if ( isset( $_GET['status'] ) && 'trash' == $_GET['status'] ) { 
  198. return array( 
  199. 'untrash' => __( 'Restore', 'woocommerce' ),  
  200. 'delete' => __( 'Delete Permanently', 'woocommerce' ) 
  201. ); 
  202.  
  203. return array( 
  204. 'trash' => __( 'Move to Trash', 'woocommerce' ) 
  205. ); 
  206.  
  207. /** 
  208. * Extra controls to be displayed between bulk actions and pagination. 
  209. * @param string $which 
  210. */ 
  211. protected function extra_tablenav( $which ) { 
  212. if ( 'top' == $which && isset( $_GET['status'] ) && 'trash' == $_GET['status'] && current_user_can( 'delete_shop_webhooks' ) ) { 
  213. 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>'; 
  214.  
  215. /** 
  216. * Prepare table list items. 
  217. */ 
  218. public function prepare_items() { 
  219. $per_page = apply_filters( 'woocommerce_webhooks_settings_posts_per_page', 10 ); 
  220. $columns = $this->get_columns(); 
  221. $hidden = array(); 
  222. $sortable = $this->get_sortable_columns(); 
  223.  
  224. // Column headers 
  225. $this->_column_headers = array( $columns, $hidden, $sortable ); 
  226.  
  227. $current_page = $this->get_pagenum(); 
  228.  
  229. // Query args 
  230. $args = array( 
  231. 'post_type' => 'shop_webhook',  
  232. 'posts_per_page' => $per_page,  
  233. 'ignore_sticky_posts' => true,  
  234. 'paged' => $current_page 
  235. ); 
  236.  
  237. // Handle the status query 
  238. if ( ! empty( $_REQUEST['status'] ) ) { 
  239. $args['post_status'] = sanitize_text_field( $_REQUEST['status'] ); 
  240.  
  241. if ( ! empty( $_REQUEST['s'] ) ) { 
  242. $args['s'] = sanitize_text_field( $_REQUEST['s'] ); 
  243.  
  244. // Get the webhooks 
  245. $webhooks = new WP_Query( $args ); 
  246. $this->items = $webhooks->posts; 
  247.  
  248. // Set the pagination 
  249. $this->set_pagination_args( array( 
  250. 'total_items' => $webhooks->found_posts,  
  251. 'per_page' => $per_page,  
  252. 'total_pages' => $webhooks->max_num_pages 
  253. ) );