2015-05-16 02:03:24 +00:00
< ? php
/**
2015-11-03 13:53:50 +00:00
* WooCommerce API Keys Table List
2015-05-16 02:03:24 +00:00
*
2018-02-05 19:45:12 +00:00
* @ package WooCommerce\Admin
* @ version 2.4 . 0
2015-05-16 02:03:24 +00:00
*/
2018-02-10 04:56:06 +00:00
defined ( 'ABSPATH' ) || exit ;
2015-05-16 02:03:24 +00:00
if ( ! class_exists ( 'WP_List_Table' ) ) {
2018-02-10 04:56:06 +00:00
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ;
2015-05-16 02:03:24 +00:00
}
2018-02-10 04:56:06 +00:00
/**
* API Keys table list class .
*/
2015-05-16 02:03:24 +00:00
class WC_Admin_API_Keys_Table_List extends WP_List_Table {
/**
2018-02-10 04:29:06 +00:00
* Initialize the API key table list .
2015-05-16 02:03:24 +00:00
*/
public function __construct () {
2018-03-05 18:59:17 +00:00
parent :: __construct (
array (
'singular' => 'key' ,
'plural' => 'keys' ,
'ajax' => false ,
)
);
2015-05-16 02:03:24 +00:00
}
2018-02-10 04:29:06 +00:00
/**
* No items found text .
*/
public function no_items () {
esc_html_e ( 'No keys found.' , 'woocommerce' );
}
2015-05-16 02:03:24 +00:00
/**
2015-11-03 12:28:01 +00:00
* Get list columns .
2015-05-16 02:03:24 +00:00
*
* @ return array
*/
public function get_columns () {
return array (
2015-07-16 18:42:00 +00:00
'cb' => '<input type="checkbox" />' ,
2018-02-10 04:32:02 +00:00
'title' => __ ( 'Description' , 'woocommerce' ),
2016-10-12 10:16:30 +00:00
'truncated_key' => __ ( 'Consumer key ending in' , 'woocommerce' ),
2015-07-16 18:42:00 +00:00
'user' => __ ( 'User' , 'woocommerce' ),
'permissions' => __ ( 'Permissions' , 'woocommerce' ),
2016-10-12 10:16:30 +00:00
'last_access' => __ ( 'Last access' , 'woocommerce' ),
2015-05-16 02:03:24 +00:00
);
}
/**
2015-11-03 12:28:01 +00:00
* Column cb .
2015-05-16 02:03:24 +00:00
*
2018-02-10 04:56:06 +00:00
* @ param array $key Key data .
2015-05-16 02:03:24 +00:00
* @ return string
*/
public function column_cb ( $key ) {
2015-09-03 20:21:23 +00:00
return sprintf ( '<input type="checkbox" name="key[]" value="%1$s" />' , $key [ 'key_id' ] );
2015-05-16 02:03:24 +00:00
}
/**
2018-02-10 04:56:06 +00:00
* Return title column .
2015-05-16 02:03:24 +00:00
*
2018-02-10 04:56:06 +00:00
* @ param array $key Key data .
2015-05-16 02:03:24 +00:00
* @ return string
*/
2018-02-10 04:32:02 +00:00
public function column_title ( $key ) {
2018-04-12 15:59:42 +00:00
$url = admin_url ( 'admin.php?page=wc-settings&tab=advanced§ion=keys&edit-key=' . $key [ 'key_id' ] );
2015-05-18 18:33:36 +00:00
2018-02-10 04:56:06 +00:00
$output = '<strong>' ;
2015-07-15 18:45:57 +00:00
$output .= '<a href="' . esc_url ( $url ) . '" class="row-title">' ;
2015-05-16 02:03:24 +00:00
if ( empty ( $key [ 'description' ] ) ) {
2016-10-12 10:16:30 +00:00
$output .= esc_html__ ( 'API key' , 'woocommerce' );
2015-05-16 02:03:24 +00:00
} else {
$output .= esc_html ( $key [ 'description' ] );
}
2015-05-18 18:33:36 +00:00
$output .= '</a>' ;
2015-05-16 02:03:24 +00:00
$output .= '</strong>' ;
2018-02-10 04:56:06 +00:00
// Get actions.
2015-05-18 18:33:36 +00:00
$actions = array (
2018-02-10 04:56:06 +00:00
/* translators: %s: API key ID. */
2015-05-18 18:33:36 +00:00
'id' => sprintf ( __ ( 'ID: %d' , 'woocommerce' ), $key [ 'key_id' ] ),
'edit' => '<a href="' . esc_url ( $url ) . '">' . __ ( 'View/Edit' , 'woocommerce' ) . '</a>' ,
2018-03-05 18:59:17 +00:00
'trash' => '<a class="submitdelete" aria-label="' . esc_attr__ ( 'Revoke API key' , 'woocommerce' ) . '" href="' . esc_url (
wp_nonce_url (
add_query_arg (
array (
'revoke-key' => $key [ 'key_id' ],
2018-04-12 15:59:42 +00:00
), admin_url ( 'admin.php?page=wc-settings&tab=advanced§ion=keys' )
2018-03-05 18:59:17 +00:00
), 'revoke'
)
) . '">' . esc_html__ ( 'Revoke' , 'woocommerce' ) . '</a>' ,
2015-05-18 18:33:36 +00:00
);
$row_actions = array ();
foreach ( $actions as $action => $link ) {
$row_actions [] = '<span class="' . esc_attr ( $action ) . '">' . $link . '</span>' ;
}
2016-09-02 01:51:31 +00:00
$output .= '<div class="row-actions">' . implode ( ' | ' , $row_actions ) . '</div>' ;
2015-05-18 18:33:36 +00:00
2015-05-16 02:03:24 +00:00
return $output ;
}
2015-07-16 18:42:00 +00:00
/**
2015-11-03 12:28:01 +00:00
* Return truncated consumer key column .
2015-07-16 18:42:00 +00:00
*
2018-02-10 04:56:06 +00:00
* @ param array $key Key data .
2015-07-16 18:42:00 +00:00
* @ return string
*/
public function column_truncated_key ( $key ) {
return '<code>…' . esc_html ( $key [ 'truncated_key' ] ) . '</code>' ;
}
2015-05-16 02:03:24 +00:00
/**
2015-11-03 12:28:01 +00:00
* Return user column .
2015-05-16 02:03:24 +00:00
*
2018-02-10 04:56:06 +00:00
* @ param array $key Key data .
2015-05-16 02:03:24 +00:00
* @ return string
*/
public function column_user ( $key ) {
$user = get_user_by ( 'id' , $key [ 'user_id' ] );
if ( ! $user ) {
return '' ;
}
2016-02-28 15:22:51 +00:00
if ( current_user_can ( 'edit_user' , $user -> ID ) ) {
2016-02-28 15:19:57 +00:00
return '<a href="' . esc_url ( add_query_arg ( array ( 'user_id' => $user -> ID ), admin_url ( 'user-edit.php' ) ) ) . '">' . esc_html ( $user -> display_name ) . '</a>' ;
2015-05-16 02:03:24 +00:00
}
2016-02-28 15:19:57 +00:00
return esc_html ( $user -> display_name );
2015-05-16 02:03:24 +00:00
}
/**
2015-11-03 12:28:01 +00:00
* Return permissions column .
2015-05-16 02:03:24 +00:00
*
2018-02-10 04:56:06 +00:00
* @ param array $key Key data .
2015-05-16 02:03:24 +00:00
* @ return string
*/
public function column_permissions ( $key ) {
$permission_key = $key [ 'permissions' ];
$permissions = array (
'read' => __ ( 'Read' , 'woocommerce' ),
'write' => __ ( 'Write' , 'woocommerce' ),
2016-08-27 01:46:45 +00:00
'read_write' => __ ( 'Read/Write' , 'woocommerce' ),
2015-05-16 02:03:24 +00:00
);
if ( isset ( $permissions [ $permission_key ] ) ) {
return esc_html ( $permissions [ $permission_key ] );
} else {
return '' ;
}
}
2015-07-16 18:42:00 +00:00
/**
2015-11-03 12:28:01 +00:00
* Return last access column .
2015-07-16 18:42:00 +00:00
*
2018-02-10 04:56:06 +00:00
* @ param array $key Key data .
2015-07-16 18:42:00 +00:00
* @ return string
*/
public function column_last_access ( $key ) {
if ( ! empty ( $key [ 'last_access' ] ) ) {
2016-10-24 07:31:07 +00:00
/* translators: 1: last access date 2: last access time */
$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' ] ) ) );
2015-07-16 18:42:00 +00:00
return apply_filters ( 'woocommerce_api_key_last_access_datetime' , $date , $key [ 'last_access' ] );
}
2015-07-28 15:44:20 +00:00
return __ ( 'Unknown' , 'woocommerce' );
2015-07-16 18:42:00 +00:00
}
2015-05-16 02:03:24 +00:00
/**
2015-11-03 12:28:01 +00:00
* Get bulk actions .
2015-05-16 02:03:24 +00:00
*
* @ return array
*/
protected function get_bulk_actions () {
return array (
2016-08-27 01:46:45 +00:00
'revoke' => __ ( 'Revoke' , 'woocommerce' ),
2015-05-16 02:03:24 +00:00
);
}
2018-07-04 18:43:31 +00:00
/**
* Search box .
*
* @ param string $text Button text .
* @ param string $input_id Input ID .
*/
public function search_box ( $text , $input_id ) {
if ( empty ( $_REQUEST [ 's' ] ) && ! $this -> has_items () ) { // WPCS: input var okay, CSRF ok.
return ;
}
$input_id = $input_id . '-search-input' ;
$search_query = isset ( $_REQUEST [ 's' ] ) ? sanitize_text_field ( wp_unslash ( $_REQUEST [ 's' ] ) ) : '' ; // WPCS: input var okay, CSRF ok.
echo '<p class="search-box">' ;
echo '<label class="screen-reader-text" for="' . esc_attr ( $input_id ) . '">' . esc_html ( $text ) . ':</label>' ;
echo '<input type="search" id="' . esc_attr ( $input_id ) . '" name="s" value="' . esc_attr ( $search_query ) . '" />' ;
submit_button (
$text , '' , '' , false ,
array (
'id' => 'search-submit' ,
)
);
echo '</p>' ;
}
2015-05-16 02:03:24 +00:00
/**
* Prepare table list items .
*/
public function prepare_items () {
global $wpdb ;
2018-02-23 04:18:56 +00:00
$per_page = $this -> get_items_per_page ( 'woocommerce_keys_per_page' );
2015-05-16 02:03:24 +00:00
$current_page = $this -> get_pagenum ();
2018-02-13 02:46:11 +00:00
2015-05-16 02:03:24 +00:00
if ( 1 < $current_page ) {
$offset = $per_page * ( $current_page - 1 );
} else {
$offset = 0 ;
}
$search = '' ;
2015-08-07 09:39:52 +00:00
2018-02-10 04:56:06 +00:00
if ( ! empty ( $_REQUEST [ 's' ] ) ) { // WPCS: input var okay, CSRF ok.
$search = " AND description LIKE '% " . esc_sql ( $wpdb -> esc_like ( wc_clean ( wp_unslash ( $_REQUEST [ 's' ] ) ) ) ) . " %' " ; // WPCS: input var okay, CSRF ok.
2015-05-16 02:03:24 +00:00
}
2018-02-10 04:56:06 +00:00
// Get the API keys.
2015-08-07 10:37:15 +00:00
$keys = $wpdb -> get_results (
" SELECT key_id, user_id, description, permissions, truncated_key, last_access FROM { $wpdb -> prefix } woocommerce_api_keys WHERE 1 = 1 { $search } " .
2018-02-10 04:56:06 +00:00
$wpdb -> prepare ( 'ORDER BY key_id DESC LIMIT %d OFFSET %d;' , $per_page , $offset ), ARRAY_A
); // WPCS: unprepared SQL ok.
2015-08-07 10:37:15 +00:00
2018-02-10 04:56:06 +00:00
$count = $wpdb -> get_var ( " SELECT COUNT(key_id) FROM { $wpdb -> prefix } woocommerce_api_keys WHERE 1 = 1 { $search } ; " ); // WPCS: unprepared SQL ok.
2015-05-16 02:03:24 +00:00
$this -> items = $keys ;
2018-02-10 04:56:06 +00:00
// Set the pagination.
2018-03-05 18:59:17 +00:00
$this -> set_pagination_args (
array (
'total_items' => $count ,
'per_page' => $per_page ,
'total_pages' => ceil ( $count / $per_page ),
)
);
2015-05-16 02:03:24 +00:00
}
}