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