class-wc-admin-api-keys-table-list.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * WooCommerce API Keys Table List
  4. *
  5. * @package WooCommerce\Admin
  6. * @version 2.4.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. * API Keys table list class.
  14. */
  15. class WC_Admin_API_Keys_Table_List extends WP_List_Table {
  16. /**
  17. * Initialize the API key table list.
  18. */
  19. public function __construct() {
  20. parent::__construct(
  21. array(
  22. 'singular' => 'key',
  23. 'plural' => 'keys',
  24. 'ajax' => false,
  25. )
  26. );
  27. }
  28. /**
  29. * No items found text.
  30. */
  31. public function no_items() {
  32. esc_html_e( 'No keys 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' => __( 'Description', 'woocommerce' ),
  43. 'truncated_key' => __( 'Consumer key ending in', 'woocommerce' ),
  44. 'user' => __( 'User', 'woocommerce' ),
  45. 'permissions' => __( 'Permissions', 'woocommerce' ),
  46. 'last_access' => __( 'Last access', 'woocommerce' ),
  47. );
  48. }
  49. /**
  50. * Column cb.
  51. *
  52. * @param array $key Key data.
  53. * @return string
  54. */
  55. public function column_cb( $key ) {
  56. return sprintf( '<input type="checkbox" name="key[]" value="%1$s" />', $key['key_id'] );
  57. }
  58. /**
  59. * Return title column.
  60. *
  61. * @param array $key Key data.
  62. * @return string
  63. */
  64. public function column_title( $key ) {
  65. $url = admin_url( 'admin.php?page=wc-settings&tab=advanced&section=keys&edit-key=' . $key['key_id'] );
  66. $output = '<strong>';
  67. $output .= '<a href="' . esc_url( $url ) . '" class="row-title">';
  68. if ( empty( $key['description'] ) ) {
  69. $output .= esc_html__( 'API key', 'woocommerce' );
  70. } else {
  71. $output .= esc_html( $key['description'] );
  72. }
  73. $output .= '</a>';
  74. $output .= '</strong>';
  75. // Get actions.
  76. $actions = array(
  77. /* translators: %s: API key ID. */
  78. 'id' => sprintf( __( 'ID: %d', 'woocommerce' ), $key['key_id'] ),
  79. 'edit' => '<a href="' . esc_url( $url ) . '">' . __( 'View/Edit', 'woocommerce' ) . '</a>',
  80. 'trash' => '<a class="submitdelete" aria-label="' . esc_attr__( 'Revoke API key', 'woocommerce' ) . '" href="' . esc_url(
  81. wp_nonce_url(
  82. add_query_arg(
  83. array(
  84. 'revoke-key' => $key['key_id'],
  85. ), admin_url( 'admin.php?page=wc-settings&tab=advanced&section=keys' )
  86. ), 'revoke'
  87. )
  88. ) . '">' . esc_html__( 'Revoke', 'woocommerce' ) . '</a>',
  89. );
  90. $row_actions = array();
  91. foreach ( $actions as $action => $link ) {
  92. $row_actions[] = '<span class="' . esc_attr( $action ) . '">' . $link . '</span>';
  93. }
  94. $output .= '<div class="row-actions">' . implode( ' | ', $row_actions ) . '</div>';
  95. return $output;
  96. }
  97. /**
  98. * Return truncated consumer key column.
  99. *
  100. * @param array $key Key data.
  101. * @return string
  102. */
  103. public function column_truncated_key( $key ) {
  104. return '<code>&hellip;' . esc_html( $key['truncated_key'] ) . '</code>';
  105. }
  106. /**
  107. * Return user column.
  108. *
  109. * @param array $key Key data.
  110. * @return string
  111. */
  112. public function column_user( $key ) {
  113. $user = get_user_by( 'id', $key['user_id'] );
  114. if ( ! $user ) {
  115. return '';
  116. }
  117. if ( current_user_can( 'edit_user', $user->ID ) ) {
  118. return '<a href="' . esc_url( add_query_arg( array( 'user_id' => $user->ID ), admin_url( 'user-edit.php' ) ) ) . '">' . esc_html( $user->display_name ) . '</a>';
  119. }
  120. return esc_html( $user->display_name );
  121. }
  122. /**
  123. * Return permissions column.
  124. *
  125. * @param array $key Key data.
  126. * @return string
  127. */
  128. public function column_permissions( $key ) {
  129. $permission_key = $key['permissions'];
  130. $permissions = array(
  131. 'read' => __( 'Read', 'woocommerce' ),
  132. 'write' => __( 'Write', 'woocommerce' ),
  133. 'read_write' => __( 'Read/Write', 'woocommerce' ),
  134. );
  135. if ( isset( $permissions[ $permission_key ] ) ) {
  136. return esc_html( $permissions[ $permission_key ] );
  137. } else {
  138. return '';
  139. }
  140. }
  141. /**
  142. * Return last access column.
  143. *
  144. * @param array $key Key data.
  145. * @return string
  146. */
  147. public function column_last_access( $key ) {
  148. if ( ! empty( $key['last_access'] ) ) {
  149. /* translators: 1: last access date 2: last access time */
  150. $date = sprintf( __( '%1$s at %2$s', 'woocommerce' ), date_i18n( wc_date_format(), strtotime( $key['last_access'] ) ), date_i18n( wc_time_format(), strtotime( $key['last_access'] ) ) );
  151. return apply_filters( 'woocommerce_api_key_last_access_datetime', $date, $key['last_access'] );
  152. }
  153. return __( 'Unknown', 'woocommerce' );
  154. }
  155. /**
  156. * Get bulk actions.
  157. *
  158. * @return array
  159. */
  160. protected function get_bulk_actions() {
  161. return array(
  162. 'revoke' => __( 'Revoke', 'woocommerce' ),
  163. );
  164. }
  165. /**
  166. * Search box.
  167. *
  168. * @param string $text Button text.
  169. * @param string $input_id Input ID.
  170. */
  171. public function search_box( $text, $input_id ) {
  172. if ( empty( $_REQUEST['s'] ) && ! $this->has_items() ) { // WPCS: input var okay, CSRF ok.
  173. return;
  174. }
  175. $input_id = $input_id . '-search-input';
  176. $search_query = isset( $_REQUEST['s'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) : ''; // WPCS: input var okay, CSRF ok.
  177. echo '<p class="search-box">';
  178. echo '<label class="screen-reader-text" for="' . esc_attr( $input_id ) . '">' . esc_html( $text ) . ':</label>';
  179. echo '<input type="search" id="' . esc_attr( $input_id ) . '" name="s" value="' . esc_attr( $search_query ) . '" />';
  180. submit_button(
  181. $text, '', '', false,
  182. array(
  183. 'id' => 'search-submit',
  184. )
  185. );
  186. echo '</p>';
  187. }
  188. /**
  189. * Prepare table list items.
  190. */
  191. public function prepare_items() {
  192. global $wpdb;
  193. $per_page = $this->get_items_per_page( 'woocommerce_keys_per_page' );
  194. $current_page = $this->get_pagenum();
  195. if ( 1 < $current_page ) {
  196. $offset = $per_page * ( $current_page - 1 );
  197. } else {
  198. $offset = 0;
  199. }
  200. $search = '';
  201. if ( ! empty( $_REQUEST['s'] ) ) { // WPCS: input var okay, CSRF ok.
  202. $search = "AND description LIKE '%" . esc_sql( $wpdb->esc_like( wc_clean( wp_unslash( $_REQUEST['s'] ) ) ) ) . "%' "; // WPCS: input var okay, CSRF ok.
  203. }
  204. // Get the API keys.
  205. $keys = $wpdb->get_results(
  206. "SELECT key_id, user_id, description, permissions, truncated_key, last_access FROM {$wpdb->prefix}woocommerce_api_keys WHERE 1 = 1 {$search}" .
  207. $wpdb->prepare( 'ORDER BY key_id DESC LIMIT %d OFFSET %d;', $per_page, $offset ), ARRAY_A
  208. ); // WPCS: unprepared SQL ok.
  209. $count = $wpdb->get_var( "SELECT COUNT(key_id) FROM {$wpdb->prefix}woocommerce_api_keys WHERE 1 = 1 {$search};" ); // WPCS: unprepared SQL ok.
  210. $this->items = $keys;
  211. // Set the pagination.
  212. $this->set_pagination_args(
  213. array(
  214. 'total_items' => $count,
  215. 'per_page' => $per_page,
  216. 'total_pages' => ceil( $count / $per_page ),
  217. )
  218. );
  219. }
  220. }