class-wc-admin-api-keys-table-list.php
7.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
/**
* WooCommerce API Keys Table List
*
* @package WooCommerce\Admin
* @version 2.4.0
*/
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'WP_List_Table' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
/**
* API Keys table list class.
*/
class WC_Admin_API_Keys_Table_List extends WP_List_Table {
/**
* Initialize the API key table list.
*/
public function __construct() {
parent::__construct(
array(
'singular' => 'key',
'plural' => 'keys',
'ajax' => false,
)
);
}
/**
* No items found text.
*/
public function no_items() {
esc_html_e( 'No keys found.', 'woocommerce' );
}
/**
* Get list columns.
*
* @return array
*/
public function get_columns() {
return array(
'cb' => '<input type="checkbox" />',
'title' => __( 'Description', 'woocommerce' ),
'truncated_key' => __( 'Consumer key ending in', 'woocommerce' ),
'user' => __( 'User', 'woocommerce' ),
'permissions' => __( 'Permissions', 'woocommerce' ),
'last_access' => __( 'Last access', 'woocommerce' ),
);
}
/**
* Column cb.
*
* @param array $key Key data.
* @return string
*/
public function column_cb( $key ) {
return sprintf( '<input type="checkbox" name="key[]" value="%1$s" />', $key['key_id'] );
}
/**
* Return title column.
*
* @param array $key Key data.
* @return string
*/
public function column_title( $key ) {
$url = admin_url( 'admin.php?page=wc-settings&tab=advanced§ion=keys&edit-key=' . $key['key_id'] );
$user_id = intval( $key['user_id'] );
// Check if current user can edit other users or if it's the same user.
$can_edit = current_user_can( 'edit_user', $user_id ) || get_current_user_id() === $user_id;
$output = '<strong>';
if ( $can_edit ) {
$output .= '<a href="' . esc_url( $url ) . '" class="row-title">';
}
if ( empty( $key['description'] ) ) {
$output .= esc_html__( 'API key', 'woocommerce' );
} else {
$output .= esc_html( $key['description'] );
}
if ( $can_edit ) {
$output .= '</a>';
}
$output .= '</strong>';
// Get actions.
$actions = array(
/* translators: %s: API key ID. */
'id' => sprintf( __( 'ID: %d', 'woocommerce' ), $key['key_id'] ),
);
if ( $can_edit ) {
$actions['edit'] = '<a href="' . esc_url( $url ) . '">' . __( 'View/Edit', 'woocommerce' ) . '</a>';
$actions['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'],
),
admin_url( 'admin.php?page=wc-settings&tab=advanced§ion=keys' )
),
'revoke'
)
) . '">' . esc_html__( 'Revoke', 'woocommerce' ) . '</a>';
}
$row_actions = array();
foreach ( $actions as $action => $link ) {
$row_actions[] = '<span class="' . esc_attr( $action ) . '">' . $link . '</span>';
}
$output .= '<div class="row-actions">' . implode( ' | ', $row_actions ) . '</div>';
return $output;
}
/**
* Return truncated consumer key column.
*
* @param array $key Key data.
* @return string
*/
public function column_truncated_key( $key ) {
return '<code>…' . esc_html( $key['truncated_key'] ) . '</code>';
}
/**
* Return user column.
*
* @param array $key Key data.
* @return string
*/
public function column_user( $key ) {
$user = get_user_by( 'id', $key['user_id'] );
if ( ! $user ) {
return '';
}
if ( current_user_can( 'edit_user', $user->ID ) ) {
return '<a href="' . esc_url( add_query_arg( array( 'user_id' => $user->ID ), admin_url( 'user-edit.php' ) ) ) . '">' . esc_html( $user->display_name ) . '</a>';
}
return esc_html( $user->display_name );
}
/**
* Return permissions column.
*
* @param array $key Key data.
* @return string
*/
public function column_permissions( $key ) {
$permission_key = $key['permissions'];
$permissions = array(
'read' => __( 'Read', 'woocommerce' ),
'write' => __( 'Write', 'woocommerce' ),
'read_write' => __( 'Read/Write', 'woocommerce' ),
);
if ( isset( $permissions[ $permission_key ] ) ) {
return esc_html( $permissions[ $permission_key ] );
} else {
return '';
}
}
/**
* Return last access column.
*
* @param array $key Key data.
* @return string
*/
public function column_last_access( $key ) {
if ( ! empty( $key['last_access'] ) ) {
/* 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'] ) ) );
return apply_filters( 'woocommerce_api_key_last_access_datetime', $date, $key['last_access'] );
}
return __( 'Unknown', 'woocommerce' );
}
/**
* Get bulk actions.
*
* @return array
*/
protected function get_bulk_actions() {
if ( ! current_user_can( 'remove_users' ) ) {
return array();
}
return array(
'revoke' => __( 'Revoke', 'woocommerce' ),
);
}
/**
* 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>';
}
/**
* Prepare table list items.
*/
public function prepare_items() {
global $wpdb;
$per_page = $this->get_items_per_page( 'woocommerce_keys_per_page' );
$current_page = $this->get_pagenum();
if ( 1 < $current_page ) {
$offset = $per_page * ( $current_page - 1 );
} else {
$offset = 0;
}
$search = '';
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.
}
// Get the API keys.
$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}" .
$wpdb->prepare( 'ORDER BY key_id DESC LIMIT %d OFFSET %d;', $per_page, $offset ),
ARRAY_A
); // WPCS: unprepared SQL ok.
$count = $wpdb->get_var( "SELECT COUNT(key_id) FROM {$wpdb->prefix}woocommerce_api_keys WHERE 1 = 1 {$search};" ); // WPCS: unprepared SQL ok.
$this->items = $keys;
// Set the pagination.
$this->set_pagination_args(
array(
'total_items' => $count,
'per_page' => $per_page,
'total_pages' => ceil( $count / $per_page ),
)
);
}
}