WC_Admin_API_Keys_Table_List

The WooCommerce WC Admin API Keys Table List class.

Defined (1)

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

/includes/admin/class-wc-admin-api-keys-table-list.php  
  1. class WC_Admin_API_Keys_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' => __( 'key', 'woocommerce' ),  
  9. 'plural' => __( 'keys', '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. 'description' => __( 'Description', 'woocommerce' ),  
  21. 'truncated_key' => __( 'Consumer Key Ending In', 'woocommerce' ),  
  22. 'user' => __( 'User', 'woocommerce' ),  
  23. 'permissions' => __( 'Permissions', 'woocommerce' ),  
  24. 'last_access' => __( 'Last Access', 'woocommerce' ) 
  25. ); 
  26.  
  27. /** 
  28. * Column cb. 
  29. * @param array $key 
  30. * @return string 
  31. */ 
  32. public function column_cb( $key ) { 
  33. return sprintf( '<input type="checkbox" name="key[]" value="%1$s" />', $key['key_id'] ); 
  34.  
  35. /** 
  36. * Return description column. 
  37. * @param array $key 
  38. * @return string 
  39. */ 
  40. public function column_description( $key ) { 
  41. $url = admin_url( 'admin.php?page=wc-settings&tab=api§ion=keys&edit-key=' . $key['key_id'] ); 
  42.  
  43. $output = '<strong>'; 
  44. $output .= '<a href="' . esc_url( $url ) . '" class="row-title">'; 
  45. if ( empty( $key['description'] ) ) { 
  46. $output .= esc_html__( 'API Key', 'woocommerce' ); 
  47. } else { 
  48. $output .= esc_html( $key['description'] ); 
  49. $output .= '</a>'; 
  50. $output .= '</strong>'; 
  51.  
  52. // Get actions 
  53. $actions = array( 
  54. 'id' => sprintf( __( 'ID: %d', 'woocommerce' ), $key['key_id'] ),  
  55. 'edit' => '<a href="' . esc_url( $url ) . '">' . __( 'View/Edit', 'woocommerce' ) . '</a>',  
  56. 'trash' => '<a class="submitdelete" title="' . esc_attr__( 'Revoke API Key', 'woocommerce' ) . '" href="' . esc_url( wp_nonce_url( add_query_arg( array( 'revoke-key' => $key['key_id'] ), admin_url( 'admin.php?page=wc-settings&tab=api§ion=keys' ) ), 'revoke' ) ) . '">' . __( 'Revoke', 'woocommerce' ) . '</a>' 
  57. ); 
  58.  
  59. $row_actions = array(); 
  60.  
  61. foreach ( $actions as $action => $link ) { 
  62. $row_actions[] = '<span class="' . esc_attr( $action ) . '">' . $link . '</span>'; 
  63.  
  64. $output .= '<div class="row-actions">' . implode( ' | ', $row_actions ) . '</div>'; 
  65.  
  66. return $output; 
  67.  
  68. /** 
  69. * Return truncated consumer key column. 
  70. * @param array $key 
  71. * @return string 
  72. */ 
  73. public function column_truncated_key( $key ) { 
  74. return '<code>…' . esc_html( $key['truncated_key'] ) . '</code>'; 
  75.  
  76. /** 
  77. * Return user column. 
  78. * @param array $key 
  79. * @return string 
  80. */ 
  81. public function column_user( $key ) { 
  82. $user = get_user_by( 'id', $key['user_id'] ); 
  83.  
  84. if ( ! $user ) { 
  85. return ''; 
  86.  
  87. if ( current_user_can( 'edit_user', $user->ID ) ) { 
  88. return '<a href="' . esc_url( add_query_arg( array( 'user_id' => $user->ID ), admin_url( 'user-edit.php' ) ) ) . '">' . esc_html( $user->display_name ) . '</a>'; 
  89.  
  90. return esc_html( $user->display_name ); 
  91.  
  92. /** 
  93. * Return permissions column. 
  94. * @param array $key 
  95. * @return string 
  96. */ 
  97. public function column_permissions( $key ) { 
  98. $permission_key = $key['permissions']; 
  99. $permissions = array( 
  100. 'read' => __( 'Read', 'woocommerce' ),  
  101. 'write' => __( 'Write', 'woocommerce' ),  
  102. 'read_write' => __( 'Read/Write', 'woocommerce' ) 
  103. ); 
  104.  
  105. if ( isset( $permissions[ $permission_key ] ) ) { 
  106. return esc_html( $permissions[ $permission_key ] ); 
  107. } else { 
  108. return ''; 
  109.  
  110. /** 
  111. * Return last access column. 
  112. * @param array $key 
  113. * @return string 
  114. */ 
  115. public function column_last_access( $key ) { 
  116. if ( ! empty( $key['last_access'] ) ) { 
  117. $date = sprintf( _x( '%1$s at %2$s', 'date and time', 'woocommerce' ), date_i18n( wc_date_format(), strtotime( $key['last_access'] ) ), date_i18n( wc_time_format(), strtotime( $key['last_access'] ) ) ); 
  118.  
  119. return apply_filters( 'woocommerce_api_key_last_access_datetime', $date, $key['last_access'] ); 
  120.  
  121. return __( 'Unknown', 'woocommerce' ); 
  122.  
  123. /** 
  124. * Get bulk actions. 
  125. * @return array 
  126. */ 
  127. protected function get_bulk_actions() { 
  128. return array( 
  129. 'revoke' => __( 'Revoke', 'woocommerce' ) 
  130. ); 
  131.  
  132. /** 
  133. * Prepare table list items. 
  134. */ 
  135. public function prepare_items() { 
  136. global $wpdb; 
  137.  
  138. $per_page = apply_filters( 'woocommerce_api_keys_settings_items_per_page', 10 ); 
  139. $columns = $this->get_columns(); 
  140. $hidden = array(); 
  141. $sortable = $this->get_sortable_columns(); 
  142.  
  143. // Column headers 
  144. $this->_column_headers = array( $columns, $hidden, $sortable ); 
  145.  
  146. $current_page = $this->get_pagenum(); 
  147. if ( 1 < $current_page ) { 
  148. $offset = $per_page * ( $current_page - 1 ); 
  149. } else { 
  150. $offset = 0; 
  151.  
  152. $search = ''; 
  153.  
  154. if ( ! empty( $_REQUEST['s'] ) ) { 
  155. $search = "AND description LIKE '%" . esc_sql( $wpdb->esc_like( wc_clean( $_REQUEST['s'] ) ) ) . "%' "; 
  156.  
  157. // Get the API keys 
  158. $keys = $wpdb->get_results( 
  159. "SELECT key_id, user_id, description, permissions, truncated_key, last_access FROM {$wpdb->prefix}woocommerce_api_keys WHERE 1 = 1 {$search}" . 
  160. $wpdb->prepare( "ORDER BY key_id DESC LIMIT %d OFFSET %d;", $per_page, $offset ), ARRAY_A 
  161. ); 
  162.  
  163. $count = $wpdb->get_var( "SELECT COUNT(key_id) FROM {$wpdb->prefix}woocommerce_api_keys WHERE 1 = 1 {$search};" ); 
  164.  
  165. $this->items = $keys; 
  166.  
  167. // Set the pagination 
  168. $this->set_pagination_args( array( 
  169. 'total_items' => $count,  
  170. 'per_page' => $per_page,  
  171. 'total_pages' => ceil( $count / $per_page ) 
  172. ) );