Opal-Estate-Pro/inc/admin/class-api-keys-table.php

330 lines
8.4 KiB
PHP
Raw Normal View History

2019-09-10 06:27:33 +02:00
<?php
/**
* API Key Table Class
*
* @package Opalestate
* @subpackage Admin/Tools/APIKeys
* @copyright Copyright (c) 2019, WordImpress
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
* @since 1.1
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Load WP_List_Table if not loaded
if ( ! class_exists( 'WP_List_Table' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
/**
* Opalestate_API_Keys_Table Class
*
* Renders the API Keys table
*
* @since 1.1
*/
class Opalestate_API_Keys_Table extends WP_List_Table {
/**
* @var int Number of items per page
* @since 1.1
*/
public $per_page = 30;
/**
* @var object Query results
* @since 1.1
*/
private $keys;
/**
* Get things started
*
* @since 1.1
* @see WP_List_Table::__construct()
*/
public function __construct() {
global $status, $page;
// Set parent defaults
2019-10-04 11:26:23 +02:00
parent::__construct( [
2019-09-10 06:27:33 +02:00
'singular' => esc_html__( 'API Key', 'opalestate-pro' ), // Singular name of the listed records
'plural' => esc_html__( 'API Keys', 'opalestate-pro' ), // Plural name of the listed records
'ajax' => false // Does this table support ajax?
2019-10-04 11:26:23 +02:00
] );
2019-09-10 06:27:33 +02:00
$this->query();
}
/**
* This function renders most of the columns in the list table.
*
* @access public
2019-10-04 11:26:23 +02:00
* @param array $item Contains all the data of the keys
2019-09-10 06:27:33 +02:00
* @param string $column_name The name of the column
*
* @return string Column Name
2019-10-04 11:26:23 +02:00
* @since 1.1
*
2019-09-10 06:27:33 +02:00
*/
public function column_default( $item, $column_name ) {
return $item[ $column_name ];
}
/**
* Displays the public key rows
*
* @access public
2019-10-04 11:26:23 +02:00
* @param array $item Contains all the data of the keys
2019-09-10 06:27:33 +02:00
* @param string $column_name The name of the column
*
* @return string Column Name
2019-10-04 11:26:23 +02:00
* @since 1.1
*
2019-09-10 06:27:33 +02:00
*/
public function column_key( $item ) {
return '<input onClick="this.setSelectionRange(0, this.value.length)" readonly="readonly" type="text" class="large-text" value="' . esc_attr( $item['key'] ) . '"/>';
}
/**
* Displays the token rows
*
* @access public
2019-10-04 11:26:23 +02:00
* @param array $item Contains all the data of the keys
2019-09-10 06:27:33 +02:00
* @param string $column_name The name of the column
*
* @return string Column Name
2019-10-04 11:26:23 +02:00
* @since 1.1
*
2019-09-10 06:27:33 +02:00
*/
public function column_token( $item ) {
return '<input onClick="this.setSelectionRange(0, this.value.length)" readonly="readonly" type="text" class="large-text" value="' . esc_attr( $item['token'] ) . '"/>';
}
/**
* Displays the secret key rows
*
* @access public
2019-10-04 11:26:23 +02:00
* @param array $item Contains all the data of the keys
2019-09-10 06:27:33 +02:00
* @param string $column_name The name of the column
*
* @return string Column Name
2019-10-04 11:26:23 +02:00
* @since 1.1
*
2019-09-10 06:27:33 +02:00
*/
public function column_secret( $item ) {
return '<input onClick="this.setSelectionRange(0, this.value.length)" readonly="readonly" type="text" class="large-text" value="' . esc_attr( $item['secret'] ) . '"/>';
}
/**
* Renders the column for the user field
*
* @access public
* @return void
2019-10-04 11:26:23 +02:00
* @since 1.1
2019-09-10 06:27:33 +02:00
*/
public function column_user( $item ) {
2019-10-04 11:26:23 +02:00
$actions = [];
2019-09-10 06:27:33 +02:00
if ( apply_filters( 'opalestate_api_log_requests', true ) ) {
$actions['view'] = sprintf(
'<a href="%s">%s</a>',
2019-10-04 11:26:23 +02:00
esc_url( add_query_arg( [
2019-09-10 06:27:33 +02:00
'view' => 'api_requests',
'post_type' => 'opalestate_forms',
'page' => 'opalestate-reports',
'tab' => 'logs',
2019-10-04 11:26:23 +02:00
's' => $item['email'],
], 'edit.php' ) ),
2019-09-10 06:27:33 +02:00
esc_html__( 'View API Log', 'opalestate-pro' )
);
}
$actions['reissue'] = sprintf(
'<a href="%s" class="opalestate-regenerate-api-key">%s</a>',
2019-10-04 11:26:23 +02:00
esc_url( wp_nonce_url( add_query_arg( [
'user_id' => $item['id'],
2019-09-10 06:27:33 +02:00
'opalestate_action' => 'process_api_key',
2019-10-04 11:26:23 +02:00
'opalestate_api_process' => 'regenerate',
] ), 'opalestate-api-nonce' ) ),
2019-09-10 06:27:33 +02:00
esc_html__( 'Reissue', 'opalestate-pro' )
);
$actions['revoke'] = sprintf(
'<a href="%s" class="opalestate-revoke-api-key opalestate-delete">%s</a>',
2019-10-04 11:26:23 +02:00
esc_url( wp_nonce_url( add_query_arg( [
'user_id' => $item['id'],
2019-09-10 06:27:33 +02:00
'opalestate_action' => 'process_api_key',
2019-10-04 11:26:23 +02:00
'opalestate_api_process' => 'revoke',
] ), 'opalestate-api-nonce' ) ),
2019-09-10 06:27:33 +02:00
esc_html__( 'Revoke', 'opalestate-pro' )
);
$actions = apply_filters( 'opalestate_api_row_actions', array_filter( $actions ) );
return sprintf( '%1$s %2$s', $item['user'], $this->row_actions( $actions ) );
}
/**
* Retrieve the table columns
*
* @access public
* @return array $columns Array of all the list table columns
2019-10-04 11:26:23 +02:00
* @since 1.1
2019-09-10 06:27:33 +02:00
*/
public function get_columns() {
2019-10-04 11:26:23 +02:00
$columns = [
'user' => esc_html__( 'Username', 'opalestate-pro' ),
'key' => esc_html__( 'Public Key', 'opalestate-pro' ),
'token' => esc_html__( 'Token', 'opalestate-pro' ),
'secret' => esc_html__( 'Secret Key', 'opalestate-pro' ),
];
2019-09-10 06:27:33 +02:00
return $columns;
}
/**
* Generate the table navigation above or below the table
*
* @param string $which
2019-10-04 11:26:23 +02:00
* @since 3.1.0
* @access protected
2019-09-10 06:27:33 +02:00
*/
protected function display_tablenav( $which ) {
if ( 'top' === $which ) {
wp_nonce_field( 'bulk-' . $this->_args['plural'] );
2019-10-04 11:26:23 +02:00
}
?>
<div class="tablenav <?php echo esc_attr( $which ); ?>">
2019-09-10 06:27:33 +02:00
2019-10-04 11:26:23 +02:00
<div class="alignleft actions bulkactions">
2019-09-10 06:27:33 +02:00
<?php $this->bulk_actions( $which ); ?>
2019-10-04 11:26:23 +02:00
</div>
<?php
2019-09-10 06:27:33 +02:00
$this->extra_tablenav( $which );
$this->pagination( $which );
2019-10-04 11:26:23 +02:00
?>
<br class="clear"/>
</div>
<?php
}
2019-09-10 06:27:33 +02:00
/**
* Display the key generation form
*
* @access public
* @return void
2019-10-04 11:26:23 +02:00
* @since 1.1
2019-09-10 06:27:33 +02:00
*/
public function bulk_actions( $which = '' ) {
// These aren't really bulk actions but this outputs the markup in the right place
static $opalestate_api_is_bottom;
if ( $opalestate_api_is_bottom ) {
return;
}
?>
2019-10-04 11:26:23 +02:00
<input type="hidden" name="opalestate_action" value="process_api_key"/>
<input type="hidden" name="opalestate_api_process" value="generate"/>
2019-09-10 06:27:33 +02:00
<?php wp_nonce_field( 'opalestate-api-nonce' ); ?>
2019-10-04 11:26:23 +02:00
<?php echo OpalEstate()->html->ajax_user_search( [
'name' => esc_html__( 'User', 'opalestate-pro' ),
] ); ?>
2019-09-10 06:27:33 +02:00
<?php submit_button( esc_html__( 'Generate New API Keys', 'opalestate-pro' ), 'secondary', 'submit', false ); ?>
<?php
$opalestate_api_is_bottom = true;
}
/**
* Retrieve the current page number
*
* @access public
* @return int Current page number
2019-10-04 11:26:23 +02:00
* @since 1.1
2019-09-10 06:27:33 +02:00
*/
public function get_paged() {
return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
}
/**
* Performs the key query
*
* @access public
* @return array
2019-10-04 11:26:23 +02:00
* @since 1.1
2019-09-10 06:27:33 +02:00
*/
public function query() {
2019-10-04 11:26:23 +02:00
$users = get_users( [
2019-09-10 06:27:33 +02:00
'meta_value' => 'opalestate_user_secret_key',
'number' => $this->per_page,
2019-10-04 11:26:23 +02:00
'offset' => $this->per_page * ( $this->get_paged() - 1 ),
] );
2019-09-10 06:27:33 +02:00
2019-10-04 11:26:23 +02:00
$keys = [];
2019-09-10 06:27:33 +02:00
2019-10-04 11:26:23 +02:00
foreach ( $users as $user ) {
$keys[ $user->ID ]['id'] = $user->ID;
$keys[ $user->ID ]['email'] = $user->user_email;
$keys[ $user->ID ]['user'] = '<a href="' . add_query_arg( 'user_id', $user->ID, 'user-edit.php' ) . '"><strong>' . $user->user_login . '</strong></a>';
$keys[ $user->ID ]['key'] = OpalEstate()->api_admin->get_user_public_key( $user->ID );
$keys[ $user->ID ]['secret'] = OpalEstate()->api_admin->get_user_secret_key( $user->ID );
$keys[ $user->ID ]['token'] = OpalEstate()->api_admin->get_token( $user->ID );
2019-09-10 06:27:33 +02:00
}
return $keys;
}
/**
* Retrieve count of total users with keys
*
* @access public
* @return int
2019-10-04 11:26:23 +02:00
* @since 1.1
2019-09-10 06:27:33 +02:00
*/
public function total_items() {
global $wpdb;
if ( ! get_transient( 'opalestate_total_api_keys' ) ) {
$total_items = $wpdb->get_var( "SELECT count(user_id) FROM $wpdb->usermeta WHERE meta_value='opalestate_user_secret_key'" );
set_transient( 'opalestate_total_api_keys', $total_items, 60 * 60 );
}
return get_transient( 'opalestate_total_api_keys' );
}
/**
* Setup the final data for the table
*
* @access public
* @return void
2019-10-04 11:26:23 +02:00
* @since 1.1
2019-09-10 06:27:33 +02:00
*/
public function prepare_items() {
$columns = $this->get_columns();
2019-10-04 11:26:23 +02:00
$hidden = []; // No hidden columns
$sortable = []; // Not sortable... for now
2019-09-10 06:27:33 +02:00
2019-10-04 11:26:23 +02:00
$this->_column_headers = [ $columns, $hidden, $sortable, 'id' ];
2019-09-10 06:27:33 +02:00
$data = $this->query();
$total_items = $this->total_items();
$this->items = $data;
2019-10-04 11:26:23 +02:00
$this->set_pagination_args( [
2019-09-10 06:27:33 +02:00
'total_items' => $total_items,
'per_page' => $this->per_page,
2019-10-04 11:26:23 +02:00
'total_pages' => ceil( $total_items / $this->per_page ),
]
2019-09-10 06:27:33 +02:00
);
}
}