2015-05-16 02:03:24 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-11-03 13:53:50 +00:00
|
|
|
* WooCommerce Admin API Keys Class
|
2015-05-16 02:03:24 +00:00
|
|
|
*
|
|
|
|
* @author WooThemes
|
|
|
|
* @category Admin
|
|
|
|
* @package WooCommerce/Admin
|
|
|
|
* @version 2.4.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* WC_Admin_API_Keys.
|
2015-05-16 02:03:24 +00:00
|
|
|
*/
|
|
|
|
class WC_Admin_API_Keys {
|
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Initialize the API Keys admin actions.
|
2015-05-16 02:03:24 +00:00
|
|
|
*/
|
|
|
|
public function __construct() {
|
2015-05-18 18:23:05 +00:00
|
|
|
add_action( 'admin_init', array( $this, 'actions' ) );
|
|
|
|
}
|
2015-05-16 02:03:24 +00:00
|
|
|
|
2015-05-18 18:23:05 +00:00
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Check if is API Keys settings page.
|
2015-05-18 18:23:05 +00:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function is_api_keys_settings_page() {
|
|
|
|
return isset( $_GET['page'] )
|
|
|
|
&& 'wc-settings' == $_GET['page']
|
|
|
|
&& isset( $_GET['tab'] )
|
|
|
|
&& 'api' == $_GET['tab']
|
|
|
|
&& isset( $_GET['section'] )
|
|
|
|
&& 'keys' == isset( $_GET['section'] );
|
2015-05-16 02:03:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Page output.
|
2015-05-16 02:03:24 +00:00
|
|
|
*/
|
|
|
|
public static function page_output() {
|
|
|
|
// Hide the save button
|
|
|
|
$GLOBALS['hide_save_button'] = true;
|
|
|
|
|
|
|
|
if ( isset( $_GET['create-key'] ) || isset( $_GET['edit-key'] ) ) {
|
2015-05-18 18:23:05 +00:00
|
|
|
$key_id = isset( $_GET['edit-key'] ) ? absint( $_GET['edit-key'] ) : 0;
|
|
|
|
$key_data = self::get_key_data( $key_id );
|
|
|
|
|
2015-05-16 02:03:24 +00:00
|
|
|
include( 'settings/views/html-keys-edit.php' );
|
|
|
|
} else {
|
|
|
|
self::table_list_output();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Table list output.
|
2015-05-16 02:03:24 +00:00
|
|
|
*/
|
|
|
|
private static function table_list_output() {
|
|
|
|
echo '<h3>' . __( 'Keys/Apps', 'woocommerce' ) . ' <a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=api§ion=keys&create-key=1' ) ) . '" class="add-new-h2">' . __( 'Add Key', 'woocommerce' ) . '</a></h3>';
|
|
|
|
|
|
|
|
$keys_table_list = new WC_Admin_API_Keys_Table_List();
|
|
|
|
$keys_table_list->prepare_items();
|
|
|
|
|
|
|
|
echo '<input type="hidden" name="page" value="wc-settings" />';
|
|
|
|
echo '<input type="hidden" name="tab" value="api" />';
|
|
|
|
echo '<input type="hidden" name="section" value="keys" />';
|
|
|
|
|
|
|
|
$keys_table_list->views();
|
|
|
|
$keys_table_list->search_box( __( 'Search Key', 'woocommerce' ), 'key' );
|
|
|
|
$keys_table_list->display();
|
|
|
|
}
|
2015-05-16 03:09:29 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Get key data.
|
2015-05-16 03:09:29 +00:00
|
|
|
*
|
|
|
|
* @param int $key_id
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private static function get_key_data( $key_id ) {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$empty = array(
|
2015-07-16 18:42:00 +00:00
|
|
|
'key_id' => 0,
|
|
|
|
'user_id' => '',
|
|
|
|
'description' => '',
|
|
|
|
'permissions' => '',
|
|
|
|
'truncated_key' => '',
|
|
|
|
'last_access' => ''
|
2015-05-16 03:09:29 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if ( 0 == $key_id ) {
|
|
|
|
return $empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
$key = $wpdb->get_row( $wpdb->prepare( "
|
2015-07-16 18:42:00 +00:00
|
|
|
SELECT key_id, user_id, description, permissions, truncated_key, last_access
|
2015-05-16 03:09:29 +00:00
|
|
|
FROM {$wpdb->prefix}woocommerce_api_keys
|
|
|
|
WHERE key_id = %d
|
|
|
|
", $key_id ), ARRAY_A );
|
|
|
|
|
|
|
|
if ( is_null( $key ) ) {
|
|
|
|
return $empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $key;
|
|
|
|
}
|
2015-05-18 18:23:05 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* API Keys admin actions.
|
2015-05-18 18:23:05 +00:00
|
|
|
*/
|
|
|
|
public function actions() {
|
|
|
|
if ( $this->is_api_keys_settings_page() ) {
|
2015-05-18 18:46:16 +00:00
|
|
|
// Revoke key
|
|
|
|
if ( isset( $_GET['revoke-key'] ) ) {
|
|
|
|
$this->revoke_key();
|
|
|
|
}
|
|
|
|
|
2015-05-18 18:23:05 +00:00
|
|
|
// Bulk actions
|
2015-05-18 19:12:47 +00:00
|
|
|
if ( isset( $_GET['action'] ) && isset( $_GET['key'] ) ) {
|
|
|
|
$this->bulk_actions();
|
2015-05-18 18:23:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notices.
|
|
|
|
*/
|
|
|
|
public static function notices() {
|
2015-06-08 22:41:35 +00:00
|
|
|
if ( isset( $_GET['revoked'] ) && 1 == $_GET['revoked'] ) {
|
|
|
|
WC_Admin_Settings::add_message( __( 'API Key revoked successfully.', 'woocommerce' ) );
|
2015-05-18 18:23:05 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-18 18:46:16 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Revoke key.
|
2015-05-18 18:46:16 +00:00
|
|
|
*/
|
|
|
|
private function revoke_key() {
|
|
|
|
if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'revoke' ) ) {
|
|
|
|
wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
$key_id = absint( $_GET['revoke-key'] );
|
2015-05-18 19:12:47 +00:00
|
|
|
$this->remove_key( $key_id );
|
2015-05-18 18:46:16 +00:00
|
|
|
|
2015-06-08 22:41:35 +00:00
|
|
|
wp_redirect( esc_url_raw( add_query_arg( array( 'revoked' => 1 ), admin_url( 'admin.php?page=wc-settings&tab=api§ion=keys' ) ) ) );
|
2015-05-18 18:46:16 +00:00
|
|
|
exit();
|
|
|
|
}
|
2015-05-18 19:12:47 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Bulk actions.
|
2015-05-18 19:12:47 +00:00
|
|
|
*/
|
|
|
|
private function bulk_actions() {
|
|
|
|
if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-settings' ) ) {
|
|
|
|
wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
$keys = array_map( 'absint', (array) $_GET['key'] );
|
|
|
|
|
|
|
|
if ( 'revoke' == $_GET['action'] ) {
|
|
|
|
$this->bulk_revoke_key( $keys );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Bulk revoke key.
|
2015-05-18 19:12:47 +00:00
|
|
|
*
|
2015-05-18 19:46:52 +00:00
|
|
|
* @param array $keys
|
2015-05-18 19:12:47 +00:00
|
|
|
*/
|
|
|
|
private function bulk_revoke_key( $keys ) {
|
|
|
|
foreach ( $keys as $key_id ) {
|
|
|
|
$this->remove_key( $key_id );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Remove key.
|
2015-05-18 19:12:47 +00:00
|
|
|
*
|
|
|
|
* @param int $key_id
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function remove_key( $key_id ) {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$delete = $wpdb->delete( $wpdb->prefix . 'woocommerce_api_keys', array( 'key_id' => $key_id ), array( '%d' ) );
|
|
|
|
|
|
|
|
return $delete;
|
|
|
|
}
|
2015-05-16 02:03:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
new WC_Admin_API_Keys();
|