add-ons
4 years ago
donors
5 years ago
emails
4 years ago
forms
4 years ago
payments
4 years ago
reports
5 years ago
settings
4 years ago
shortcodes
5 years ago
tools
4 years ago
upgrades
5 years ago
views
4 years ago
abstract-admin-settings-page.php
6 years ago
admin-actions.php
4 years ago
admin-filters.php
6 years ago
admin-footer.php
5 years ago
admin-pages.php
4 years ago
class-addon-activation-banner.php
6 years ago
class-admin-settings.php
4 years ago
class-api-keys-table.php
6 years ago
class-blank-slate.php
6 years ago
class-give-admin.php
5 years ago
class-give-html-elements.php
6 years ago
class-i18n-module.php
6 years ago
dashboard-widgets.php
6 years ago
give-metabox-functions.php
4 years ago
import-functions.php
5 years ago
misc-functions.php
5 years ago
plugins.php
5 years ago
setting-page-functions.php
6 years ago
class-api-keys-table.php
387 lines
| 1 | <?php |
| 2 | /** |
| 3 | * API Key Table Class |
| 4 | * |
| 5 | * @package Give |
| 6 | * @subpackage Admin/Tools/APIKeys |
| 7 | * @copyright Copyright (c) 2016, GiveWP |
| 8 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 9 | * @since 1.1 |
| 10 | */ |
| 11 | |
| 12 | // Exit if accessed directly. |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | // Load WP_List_Table if not loaded |
| 18 | if ( ! class_exists( 'WP_List_Table' ) ) { |
| 19 | require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Give_API_Keys_Table Class |
| 24 | * |
| 25 | * Renders the API Keys table |
| 26 | * |
| 27 | * @since 1.1 |
| 28 | */ |
| 29 | class Give_API_Keys_Table extends WP_List_Table { |
| 30 | |
| 31 | /** |
| 32 | * @var int Number of items per page |
| 33 | * @since 1.1 |
| 34 | */ |
| 35 | public $per_page = 30; |
| 36 | |
| 37 | /** |
| 38 | * @var object Query results |
| 39 | * @since 1.1 |
| 40 | */ |
| 41 | private $keys; |
| 42 | |
| 43 | /** |
| 44 | * Get things started |
| 45 | * |
| 46 | * @since 1.1 |
| 47 | * @see WP_List_Table::__construct() |
| 48 | * |
| 49 | * @global $status |
| 50 | * @global $page |
| 51 | */ |
| 52 | public function __construct() { |
| 53 | global $status, $page; |
| 54 | |
| 55 | // Set parent defaults |
| 56 | parent::__construct( |
| 57 | array( |
| 58 | 'singular' => esc_html__( 'API Key', 'give' ), // Singular name of the listed records |
| 59 | 'plural' => esc_html__( 'API Keys', 'give' ), // Plural name of the listed records |
| 60 | 'ajax' => false, // Does this table support ajax? |
| 61 | ) |
| 62 | ); |
| 63 | |
| 64 | $this->query(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * This function renders most of the columns in the list table. |
| 69 | * |
| 70 | * @access public |
| 71 | * @since 1.1 |
| 72 | * |
| 73 | * @param array $item Contains all the data of the keys |
| 74 | * @param string $column_name The name of the column |
| 75 | * |
| 76 | * @return string Column Name |
| 77 | */ |
| 78 | public function column_default( $item, $column_name ) { |
| 79 | return $item[ $column_name ]; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Displays the public key rows |
| 84 | * |
| 85 | * @access public |
| 86 | * @since 1.1 |
| 87 | * |
| 88 | * @param array $item Contains all the data of the keys |
| 89 | * |
| 90 | * @return string Column Name |
| 91 | */ |
| 92 | public function column_key( $item ) { |
| 93 | return '<input onClick="this.setSelectionRange(0, this.value.length)" readonly="readonly" type="text" class="large-text" value="' . esc_attr( $item['key'] ) . '"/>'; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Displays the token rows |
| 98 | * |
| 99 | * @access public |
| 100 | * @since 1.1 |
| 101 | * |
| 102 | * @param array $item Contains all the data of the keys |
| 103 | * |
| 104 | * @return string Column Name |
| 105 | */ |
| 106 | public function column_token( $item ) { |
| 107 | return '<input onClick="this.setSelectionRange(0, this.value.length)" readonly="readonly" type="text" class="large-text" value="' . esc_attr( $item['token'] ) . '"/>'; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Displays the secret key rows |
| 112 | * |
| 113 | * @access public |
| 114 | * @since 1.1 |
| 115 | * |
| 116 | * @param array $item Contains all the data of the keys |
| 117 | * |
| 118 | * @return string Column Name |
| 119 | */ |
| 120 | public function column_secret( $item ) { |
| 121 | return '<input onClick="this.setSelectionRange(0, this.value.length)" readonly="readonly" type="text" class="large-text" value="' . esc_attr( $item['secret'] ) . '"/>'; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Renders the column for the user field |
| 126 | * |
| 127 | * @access public |
| 128 | * @since 1.1 |
| 129 | * @return string |
| 130 | */ |
| 131 | public function column_user( $item ) { |
| 132 | |
| 133 | $actions = array(); |
| 134 | |
| 135 | if ( apply_filters( 'give_api_log_requests', true ) ) { |
| 136 | $actions['view'] = sprintf( |
| 137 | '<a href="%s">%s</a>', |
| 138 | esc_url( |
| 139 | add_query_arg( |
| 140 | array( |
| 141 | 'section' => 'api_requests', |
| 142 | 'post_type' => 'give_forms', |
| 143 | 'page' => 'give-tools', |
| 144 | 'tab' => 'logs', |
| 145 | 's' => $item['email'], |
| 146 | ), |
| 147 | 'edit.php' |
| 148 | ) |
| 149 | ), |
| 150 | esc_html__( 'View API Log', 'give' ) |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | $actions['reissue'] = sprintf( |
| 155 | '<a href="%s" class="give-regenerate-api-key">%s</a>', |
| 156 | esc_url( |
| 157 | wp_nonce_url( |
| 158 | add_query_arg( |
| 159 | array( |
| 160 | 'user_id' => $item['id'], |
| 161 | 'give_action' => 'process_api_key', |
| 162 | 'give_api_process' => 'regenerate', |
| 163 | ) |
| 164 | ), |
| 165 | 'give-api-nonce' |
| 166 | ) |
| 167 | ), |
| 168 | esc_html__( 'Reissue', 'give' ) |
| 169 | ); |
| 170 | $actions['revoke'] = sprintf( |
| 171 | '<a href="%s" class="give-revoke-api-key give-delete">%s</a>', |
| 172 | esc_url( |
| 173 | wp_nonce_url( |
| 174 | add_query_arg( |
| 175 | array( |
| 176 | 'user_id' => $item['id'], |
| 177 | 'give_action' => 'process_api_key', |
| 178 | 'give_api_process' => 'revoke', |
| 179 | ) |
| 180 | ), |
| 181 | 'give-api-nonce' |
| 182 | ) |
| 183 | ), |
| 184 | esc_html__( 'Revoke', 'give' ) |
| 185 | ); |
| 186 | |
| 187 | $actions = apply_filters( 'give_api_row_actions', array_filter( $actions ) ); |
| 188 | |
| 189 | return sprintf( '%1$s %2$s', $item['user'], $this->row_actions( $actions ) ); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Gets the name of the primary column. |
| 194 | * |
| 195 | * @since 1.5 |
| 196 | * @access protected |
| 197 | * |
| 198 | * @return string Name of the primary column. |
| 199 | */ |
| 200 | protected function get_primary_column_name() { |
| 201 | return 'user'; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Retrieve the table columns |
| 206 | * |
| 207 | * @access public |
| 208 | * @since 1.1 |
| 209 | * @return array $columns Array of all the list table columns |
| 210 | */ |
| 211 | public function get_columns() { |
| 212 | $columns = array( |
| 213 | 'user' => esc_html__( 'Username', 'give' ), |
| 214 | 'key' => esc_html__( 'Public Key', 'give' ), |
| 215 | 'token' => esc_html__( 'Token', 'give' ), |
| 216 | 'secret' => esc_html__( 'Secret Key', 'give' ), |
| 217 | ); |
| 218 | |
| 219 | return $columns; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Generate the table navigation above or below the table |
| 224 | * |
| 225 | * @since 3.1.0 |
| 226 | * @access protected |
| 227 | * |
| 228 | * @param string $which |
| 229 | */ |
| 230 | protected function display_tablenav( $which ) { |
| 231 | if ( 'top' === $which ) { |
| 232 | wp_nonce_field( 'bulk-' . $this->_args['plural'] ); |
| 233 | } |
| 234 | ?> |
| 235 | <div class="tablenav <?php echo esc_attr( $which ); ?>"> |
| 236 | |
| 237 | <div class="alignleft actions bulkactions"> |
| 238 | <?php $this->bulk_actions( $which ); ?> |
| 239 | </div> |
| 240 | |
| 241 | <?php |
| 242 | $this->extra_tablenav( $which ); |
| 243 | $this->pagination( $which ); |
| 244 | ?> |
| 245 | |
| 246 | <br class="clear"/> |
| 247 | </div> |
| 248 | <?php |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Display the key generation form |
| 253 | * |
| 254 | * @access public |
| 255 | * @since 1.1 |
| 256 | * |
| 257 | * @param string $which |
| 258 | * |
| 259 | * @return void |
| 260 | */ |
| 261 | function bulk_actions( $which = '' ) { |
| 262 | // These aren't really bulk actions but this outputs the markup in the right place. |
| 263 | static $give_api_is_bottom; |
| 264 | |
| 265 | if ( $give_api_is_bottom ) { |
| 266 | return; |
| 267 | } |
| 268 | ?> |
| 269 | <input type="hidden" name="give_action" value="process_api_key"/> |
| 270 | <input type="hidden" name="give_api_process" value="generate"/> |
| 271 | <?php |
| 272 | wp_nonce_field( 'give-api-nonce' ); |
| 273 | /** |
| 274 | * API Key user search. |
| 275 | */ |
| 276 | $args = array( |
| 277 | 'id' => 'give-api-user-search', |
| 278 | 'name' => 'user_id', |
| 279 | ); |
| 280 | |
| 281 | echo Give()->html->ajax_user_search( $args ); |
| 282 | |
| 283 | submit_button( esc_html__( 'Generate New API Keys', 'give' ), 'secondary', 'submit', false ); |
| 284 | |
| 285 | $give_api_is_bottom = true; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Retrieve the current page number |
| 290 | * |
| 291 | * @access public |
| 292 | * @since 1.1 |
| 293 | * @return int Current page number |
| 294 | */ |
| 295 | public function get_paged() { |
| 296 | return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Performs the key query |
| 301 | * |
| 302 | * @access public |
| 303 | * @since 1.1 |
| 304 | * @return array |
| 305 | */ |
| 306 | public function query() { |
| 307 | $users = get_users( |
| 308 | array( |
| 309 | 'meta_value' => 'give_user_secret_key', |
| 310 | 'number' => $this->per_page, |
| 311 | 'offset' => $this->per_page * ( $this->get_paged() - 1 ), |
| 312 | ) |
| 313 | ); |
| 314 | $keys = array(); |
| 315 | |
| 316 | foreach ( $users as $user ) { |
| 317 | $keys[ $user->ID ]['id'] = $user->ID; |
| 318 | $keys[ $user->ID ]['email'] = $user->user_email; |
| 319 | $keys[ $user->ID ]['user'] = '<a href="' . add_query_arg( 'user_id', $user->ID, 'user-edit.php' ) . '"><strong>' . $user->user_login . '</strong></a>'; |
| 320 | |
| 321 | $keys[ $user->ID ]['key'] = Give()->api->get_user_public_key( $user->ID ); |
| 322 | $keys[ $user->ID ]['secret'] = Give()->api->get_user_secret_key( $user->ID ); |
| 323 | $keys[ $user->ID ]['token'] = Give()->api->get_token( $user->ID ); |
| 324 | } |
| 325 | |
| 326 | return $keys; |
| 327 | } |
| 328 | |
| 329 | |
| 330 | /** |
| 331 | * Retrieve count of total users with keys |
| 332 | * |
| 333 | * @access public |
| 334 | * @since 1.1 |
| 335 | * @return int |
| 336 | */ |
| 337 | public function total_items() { |
| 338 | global $wpdb; |
| 339 | |
| 340 | $total_items = Give_Cache::get( 'give_total_api_keys', true ); |
| 341 | |
| 342 | if ( ! $total_items ) { |
| 343 | $total_items = $wpdb->get_var( |
| 344 | $wpdb->prepare( |
| 345 | "SELECT count(user_id) |
| 346 | FROM {$wpdb->usermeta} WHERE meta_value=%s", |
| 347 | 'give_user_secret_key' |
| 348 | ) |
| 349 | ); |
| 350 | |
| 351 | Give_Cache::set( 'give_total_api_keys', $total_items, HOUR_IN_SECONDS, true ); |
| 352 | } |
| 353 | |
| 354 | return $total_items; |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Setup the final data for the table |
| 359 | * |
| 360 | * @access public |
| 361 | * @since 1.1 |
| 362 | * @return void |
| 363 | */ |
| 364 | public function prepare_items() { |
| 365 | $columns = $this->get_columns(); |
| 366 | |
| 367 | $hidden = array(); // No hidden columns |
| 368 | $sortable = array(); // Not sortable... for now |
| 369 | |
| 370 | $this->_column_headers = array( $columns, $hidden, $sortable ); |
| 371 | |
| 372 | $data = $this->query(); |
| 373 | |
| 374 | $total_items = $this->total_items(); |
| 375 | |
| 376 | $this->items = $data; |
| 377 | |
| 378 | $this->set_pagination_args( |
| 379 | array( |
| 380 | 'total_items' => $total_items, |
| 381 | 'per_page' => $this->per_page, |
| 382 | 'total_pages' => ceil( $total_items / $this->per_page ), |
| 383 | ) |
| 384 | ); |
| 385 | } |
| 386 | } |
| 387 |