PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/woocommerce/includes/admin/class-wc-admin-webhooks-table-list.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 316 lines | 177 code | 45 blank | 94 comment | 13 complexity | 10daddac1711a37f3606e8151f57e9d2 MD5 | raw file
  1. <?php
  2. /**
  3. * WooCommerce Webhooks Table List
  4. *
  5. * @package WooCommerce\Admin
  6. * @version 3.3.0
  7. */
  8. defined( 'ABSPATH' ) || exit;
  9. if ( ! class_exists( 'WP_List_Table' ) ) {
  10. require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
  11. }
  12. /**
  13. * Webooks table list class.
  14. */
  15. class WC_Admin_Webhooks_Table_List extends WP_List_Table {
  16. /**
  17. * Initialize the webhook table list.
  18. */
  19. public function __construct() {
  20. parent::__construct(
  21. array(
  22. 'singular' => 'webhook',
  23. 'plural' => 'webhooks',
  24. 'ajax' => false,
  25. )
  26. );
  27. }
  28. /**
  29. * No items found text.
  30. */
  31. public function no_items() {
  32. esc_html_e( 'No webhooks found.', 'woocommerce' );
  33. }
  34. /**
  35. * Get list columns.
  36. *
  37. * @return array
  38. */
  39. public function get_columns() {
  40. return array(
  41. 'cb' => '<input type="checkbox" />',
  42. 'title' => __( 'Name', 'woocommerce' ),
  43. 'status' => __( 'Status', 'woocommerce' ),
  44. 'topic' => __( 'Topic', 'woocommerce' ),
  45. 'delivery_url' => __( 'Delivery URL', 'woocommerce' ),
  46. );
  47. }
  48. /**
  49. * Column cb.
  50. *
  51. * @param WC_Webhook $webhook Webhook instance.
  52. * @return string
  53. */
  54. public function column_cb( $webhook ) {
  55. return sprintf( '<input type="checkbox" name="%1$s[]" value="%2$s" />', $this->_args['singular'], $webhook->get_id() );
  56. }
  57. /**
  58. * Return title column.
  59. *
  60. * @param WC_Webhook $webhook Webhook instance.
  61. * @return string
  62. */
  63. public function column_title( $webhook ) {
  64. $edit_link = admin_url( 'admin.php?page=wc-settings&amp;tab=advanced&amp;section=webhooks&amp;edit-webhook=' . $webhook->get_id() );
  65. $output = '';
  66. // Title.
  67. $output .= '<strong><a href="' . esc_url( $edit_link ) . '" class="row-title">' . esc_html( $webhook->get_name() ) . '</a></strong>';
  68. // Get actions.
  69. $actions = array(
  70. /* translators: %s: webhook ID. */
  71. 'id' => sprintf( __( 'ID: %d', 'woocommerce' ), $webhook->get_id() ),
  72. 'edit' => '<a href="' . esc_url( $edit_link ) . '">' . esc_html__( 'Edit', 'woocommerce' ) . '</a>',
  73. /* translators: %s: webhook name */
  74. 'delete' => '<a class="submitdelete" aria-label="' . esc_attr( sprintf( __( 'Delete "%s" permanently', 'woocommerce' ), $webhook->get_name() ) ) . '" href="' . esc_url(
  75. wp_nonce_url(
  76. add_query_arg(
  77. array(
  78. 'delete' => $webhook->get_id(),
  79. ),
  80. admin_url( 'admin.php?page=wc-settings&tab=advanced&section=webhooks' )
  81. ),
  82. 'delete-webhook'
  83. )
  84. ) . '">' . esc_html__( 'Delete permanently', 'woocommerce' ) . '</a>',
  85. );
  86. $actions = apply_filters( 'webhook_row_actions', $actions, $webhook );
  87. $row_actions = array();
  88. foreach ( $actions as $action => $link ) {
  89. $row_actions[] = '<span class="' . esc_attr( $action ) . '">' . $link . '</span>';
  90. }
  91. $output .= '<div class="row-actions">' . implode( ' | ', $row_actions ) . '</div>';
  92. return $output;
  93. }
  94. /**
  95. * Return status column.
  96. *
  97. * @param WC_Webhook $webhook Webhook instance.
  98. * @return string
  99. */
  100. public function column_status( $webhook ) {
  101. return $webhook->get_i18n_status();
  102. }
  103. /**
  104. * Return topic column.
  105. *
  106. * @param WC_Webhook $webhook Webhook instance.
  107. * @return string
  108. */
  109. public function column_topic( $webhook ) {
  110. return $webhook->get_topic();
  111. }
  112. /**
  113. * Return delivery URL column.
  114. *
  115. * @param WC_Webhook $webhook Webhook instance.
  116. * @return string
  117. */
  118. public function column_delivery_url( $webhook ) {
  119. return $webhook->get_delivery_url();
  120. }
  121. /**
  122. * Get the status label for webhooks.
  123. *
  124. * @param string $status_name Status name.
  125. * @param int $amount Amount of webhooks.
  126. * @return array
  127. */
  128. private function get_status_label( $status_name, $amount ) {
  129. $statuses = wc_get_webhook_statuses();
  130. if ( isset( $statuses[ $status_name ] ) ) {
  131. return array(
  132. 'singular' => sprintf( '%s <span class="count">(%s)</span>', esc_html( $statuses[ $status_name ] ), $amount ),
  133. 'plural' => sprintf( '%s <span class="count">(%s)</span>', esc_html( $statuses[ $status_name ] ), $amount ),
  134. 'context' => '',
  135. 'domain' => 'woocommerce',
  136. );
  137. }
  138. return array(
  139. 'singular' => sprintf( '%s <span class="count">(%s)</span>', esc_html( $status_name ), $amount ),
  140. 'plural' => sprintf( '%s <span class="count">(%s)</span>', esc_html( $status_name ), $amount ),
  141. 'context' => '',
  142. 'domain' => 'woocommerce',
  143. );
  144. }
  145. /**
  146. * Table list views.
  147. *
  148. * @return array
  149. */
  150. protected function get_views() {
  151. $status_links = array();
  152. $data_store = WC_Data_Store::load( 'webhook' );
  153. $num_webhooks = $data_store->get_count_webhooks_by_status();
  154. $total_webhooks = array_sum( (array) $num_webhooks );
  155. $statuses = array_keys( wc_get_webhook_statuses() );
  156. $class = empty( $_REQUEST['status'] ) ? ' class="current"' : ''; // WPCS: input var okay. CSRF ok.
  157. /* translators: %s: count */
  158. $status_links['all'] = "<a href='admin.php?page=wc-settings&amp;tab=advanced&amp;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>';
  159. foreach ( $statuses as $status_name ) {
  160. $class = '';
  161. if ( empty( $num_webhooks[ $status_name ] ) ) {
  162. continue;
  163. }
  164. if ( isset( $_REQUEST['status'] ) && sanitize_key( wp_unslash( $_REQUEST['status'] ) ) === $status_name ) { // WPCS: input var okay, CSRF ok.
  165. $class = ' class="current"';
  166. }
  167. $label = $this->get_status_label( $status_name, $num_webhooks[ $status_name ] );
  168. $status_links[ $status_name ] = "<a href='admin.php?page=wc-settings&amp;tab=advanced&amp;section=webhooks&amp;status=$status_name'$class>" . sprintf( translate_nooped_plural( $label, $num_webhooks[ $status_name ] ), number_format_i18n( $num_webhooks[ $status_name ] ) ) . '</a>';
  169. }
  170. return $status_links;
  171. }
  172. /**
  173. * Get bulk actions.
  174. *
  175. * @return array
  176. */
  177. protected function get_bulk_actions() {
  178. return array(
  179. 'delete' => __( 'Delete permanently', 'woocommerce' ),
  180. );
  181. }
  182. /**
  183. * Process bulk actions.
  184. */
  185. public function process_bulk_action() {
  186. $action = $this->current_action();
  187. $webhooks = isset( $_REQUEST['webhook'] ) ? array_map( 'absint', (array) $_REQUEST['webhook'] ) : array(); // WPCS: input var okay, CSRF ok.
  188. if ( ! current_user_can( 'manage_woocommerce' ) ) {
  189. wp_die( esc_html__( 'You do not have permission to edit Webhooks', 'woocommerce' ) );
  190. }
  191. if ( 'delete' === $action ) {
  192. WC_Admin_Webhooks::bulk_delete( $webhooks );
  193. }
  194. }
  195. /**
  196. * Generate the table navigation above or below the table.
  197. * Included to remove extra nonce input.
  198. *
  199. * @param string $which The location of the extra table nav markup: 'top' or 'bottom'.
  200. */
  201. protected function display_tablenav( $which ) {
  202. echo '<div class="tablenav ' . esc_attr( $which ) . '">';
  203. if ( $this->has_items() ) {
  204. echo '<div class="alignleft actions bulkactions">';
  205. $this->bulk_actions( $which );
  206. echo '</div>';
  207. }
  208. $this->extra_tablenav( $which );
  209. $this->pagination( $which );
  210. echo '<br class="clear" />';
  211. echo '</div>';
  212. }
  213. /**
  214. * Search box.
  215. *
  216. * @param string $text Button text.
  217. * @param string $input_id Input ID.
  218. */
  219. public function search_box( $text, $input_id ) {
  220. if ( empty( $_REQUEST['s'] ) && ! $this->has_items() ) { // WPCS: input var okay, CSRF ok.
  221. return;
  222. }
  223. $input_id = $input_id . '-search-input';
  224. $search_query = isset( $_REQUEST['s'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) : ''; // WPCS: input var okay, CSRF ok.
  225. echo '<p class="search-box">';
  226. echo '<label class="screen-reader-text" for="' . esc_attr( $input_id ) . '">' . esc_html( $text ) . ':</label>';
  227. echo '<input type="search" id="' . esc_attr( $input_id ) . '" name="s" value="' . esc_attr( $search_query ) . '" />';
  228. submit_button(
  229. $text,
  230. '',
  231. '',
  232. false,
  233. array(
  234. 'id' => 'search-submit',
  235. )
  236. );
  237. echo '</p>';
  238. }
  239. /**
  240. * Prepare table list items.
  241. */
  242. public function prepare_items() {
  243. $per_page = $this->get_items_per_page( 'woocommerce_webhooks_per_page' );
  244. $current_page = $this->get_pagenum();
  245. // Query args.
  246. $args = array(
  247. 'limit' => $per_page,
  248. 'offset' => $per_page * ( $current_page - 1 ),
  249. );
  250. // Handle the status query.
  251. if ( ! empty( $_REQUEST['status'] ) ) { // WPCS: input var okay, CSRF ok.
  252. $args['status'] = sanitize_key( wp_unslash( $_REQUEST['status'] ) ); // WPCS: input var okay, CSRF ok.
  253. }
  254. if ( ! empty( $_REQUEST['s'] ) ) { // WPCS: input var okay, CSRF ok.
  255. $args['search'] = sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ); // WPCS: input var okay, CSRF ok.
  256. }
  257. $args['paginate'] = true;
  258. // Get the webhooks.
  259. $data_store = WC_Data_Store::load( 'webhook' );
  260. $webhooks = $data_store->search_webhooks( $args );
  261. $this->items = array_map( 'wc_get_webhook', $webhooks->webhooks );
  262. // Set the pagination.
  263. $this->set_pagination_args(
  264. array(
  265. 'total_items' => $webhooks->total,
  266. 'per_page' => $per_page,
  267. 'total_pages' => $webhooks->max_num_pages,
  268. )
  269. );
  270. }
  271. }