Keine Beschreibung

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. $user_id = intval( $key['user_id'] );
  67. // Check if current user can edit other users or if it's the same user.
  68. $can_edit = current_user_can( 'edit_user', $user_id ) || get_current_user_id() === $user_id;
  69. $output = '<strong>';
  70. if ( $can_edit ) {
  71. $output .= '<a href="' . esc_url( $url ) . '" class="row-title">';
  72. }
  73. if ( empty( $key['description'] ) ) {
  74. $output .= esc_html__( 'API key', 'woocommerce' );
  75. } else {
  76. $output .= esc_html( $key['description'] );
  77. }
  78. if ( $can_edit ) {
  79. $output .= '</a>';
  80. }
  81. $output .= '</strong>';
  82. // Get actions.
  83. $actions = array(
  84. /* translators: %s: API key ID. */
  85. 'id' => sprintf( __( 'ID: %d', 'woocommerce' ), $key['key_id'] ),
  86. );
  87. if ( $can_edit ) {
  88. $actions['edit'] = '<a href="' . esc_url( $url ) . '">' . __( 'View/Edit', 'woocommerce' ) . '</a>';
  89. $actions['trash'] = '<a class="submitdelete" aria-label="' . esc_attr__( 'Revoke API key', 'woocommerce' ) . '" href="' . esc_url(
  90. wp_nonce_url(
  91. add_query_arg(
  92. array(
  93. 'revoke-key' => $key['key_id'],
  94. ),
  95. admin_url( 'admin.php?page=wc-settings&tab=advanced&section=keys' )
  96. ),
  97. 'revoke'
  98. )
  99. ) . '">' . esc_html__( 'Revoke', 'woocommerce' ) . '</a>';
  100. }
  101. $row_actions = array();
  102. foreach ( $actions as $action => $link ) {
  103. $row_actions[] = '<span class="' . esc_attr( $action ) . '">' . $link . '</span>';
  104. }
  105. $output .= '<div class="row-actions">' . implode( ' | ', $row_actions ) . '</div>';
  106. return $output;
  107. }
  108. /**
  109. * Return truncated consumer key column.
  110. *
  111. * @param array $key Key data.
  112. * @return string
  113. */
  114. public function column_truncated_key( $key ) {
  115. return '<code>&hellip;' . esc_html( $key['truncated_key'] ) . '</code>';
  116. }
  117. /**
  118. * Return user column.
  119. *
  120. * @param array $key Key data.
  121. * @return string
  122. */
  123. public function column_user( $key ) {
  124. $user = get_user_by( 'id', $key['user_id'] );
  125. if ( ! $user ) {
  126. return '';
  127. }
  128. if ( current_user_can( 'edit_user', $user->ID ) ) {
  129. return '<a href="' . esc_url( add_query_arg( array( 'user_id' => $user->ID ), admin_url( 'user-edit.php' ) ) ) . '">' . esc_html( $user->display_name ) . '</a>';
  130. }
  131. return esc_html( $user->display_name );
  132. }
  133. /**
  134. * Return permissions column.
  135. *
  136. * @param array $key Key data.
  137. * @return string
  138. */
  139. public function column_permissions( $key ) {
  140. $permission_key = $key['permissions'];
  141. $permissions = array(
  142. 'read' => __( 'Read', 'woocommerce' ),
  143. 'write' => __( 'Write', 'woocommerce' ),
  144. 'read_write' => __( 'Read/Write', 'woocommerce' ),
  145. );
  146. if ( isset( $permissions[ $permission_key ] ) ) {
  147. return esc_html( $permissions[ $permission_key ] );
  148. } else {
  149. return '';
  150. }
  151. }
  152. /**
  153. * Return last access column.
  154. *
  155. * @param array $key Key data.
  156. * @return string
  157. */
  158. public function column_last_access( $key ) {
  159. if ( ! empty( $key['last_access'] ) ) {
  160. /* translators: 1: last access date 2: last access time */
  161. $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'] ) ) );
  162. return apply_filters( 'woocommerce_api_key_last_access_datetime', $date, $key['last_access'] );
  163. }
  164. return __( 'Unknown', 'woocommerce' );
  165. }
  166. /**
  167. * Get bulk actions.
  168. *
  169. * @return array
  170. */
  171. protected function get_bulk_actions() {
  172. if ( ! current_user_can( 'remove_users' ) ) {
  173. return array();
  174. }
  175. return array(
  176. 'revoke' => __( 'Revoke', 'woocommerce' ),
  177. );
  178. }
  179. /**
  180. * Search box.
  181. *
  182. * @param string $text Button text.
  183. * @param string $input_id Input ID.
  184. */
  185. public function search_box( $text, $input_id ) {
  186. if ( empty( $_REQUEST['s'] ) && ! $this->has_items() ) { // WPCS: input var okay, CSRF ok.
  187. return;
  188. }
  189. $input_id = $input_id . '-search-input';
  190. $search_query = isset( $_REQUEST['s'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) : ''; // WPCS: input var okay, CSRF ok.
  191. echo '<p class="search-box">';
  192. echo '<label class="screen-reader-text" for="' . esc_attr( $input_id ) . '">' . esc_html( $text ) . ':</label>';
  193. echo '<input type="search" id="' . esc_attr( $input_id ) . '" name="s" value="' . esc_attr( $search_query ) . '" />';
  194. submit_button(
  195. $text,
  196. '',
  197. '',
  198. false,
  199. array(
  200. 'id' => 'search-submit',
  201. )
  202. );
  203. echo '</p>';
  204. }
  205. /**
  206. * Prepare table list items.
  207. */
  208. public function prepare_items() {
  209. global $wpdb;
  210. $per_page = $this->get_items_per_page( 'woocommerce_keys_per_page' );
  211. $current_page = $this->get_pagenum();
  212. if ( 1 < $current_page ) {
  213. $offset = $per_page * ( $current_page - 1 );
  214. } else {
  215. $offset = 0;
  216. }
  217. $search = '';
  218. if ( ! empty( $_REQUEST['s'] ) ) { // WPCS: input var okay, CSRF ok.
  219. $search = "AND description LIKE '%" . esc_sql( $wpdb->esc_like( wc_clean( wp_unslash( $_REQUEST['s'] ) ) ) ) . "%' "; // WPCS: input var okay, CSRF ok.
  220. }
  221. // Get the API keys.
  222. $keys = $wpdb->get_results(
  223. "SELECT key_id, user_id, description, permissions, truncated_key, last_access FROM {$wpdb->prefix}woocommerce_api_keys WHERE 1 = 1 {$search}" .
  224. $wpdb->prepare( 'ORDER BY key_id DESC LIMIT %d OFFSET %d;', $per_page, $offset ),
  225. ARRAY_A
  226. ); // WPCS: unprepared SQL ok.
  227. $count = $wpdb->get_var( "SELECT COUNT(key_id) FROM {$wpdb->prefix}woocommerce_api_keys WHERE 1 = 1 {$search};" ); // WPCS: unprepared SQL ok.
  228. $this->items = $keys;
  229. // Set the pagination.
  230. $this->set_pagination_args(
  231. array(
  232. 'total_items' => $count,
  233. 'per_page' => $per_page,
  234. 'total_pages' => ceil( $count / $per_page ),
  235. )
  236. );
  237. }
  238. }