Added new api key generation methods
This commit is contained in:
parent
45c4afd390
commit
89ddda9ff6
|
@ -0,0 +1,102 @@
|
||||||
|
/*global jQuery, Backbone, _, woocommerce_admin_api_keys */
|
||||||
|
(function( $ ) {
|
||||||
|
|
||||||
|
var APIView = Backbone.View.extend({
|
||||||
|
el: $( '#key-fields' ),
|
||||||
|
events: {
|
||||||
|
'click input#update_api_key': 'saveKey'
|
||||||
|
},
|
||||||
|
initialize: function(){
|
||||||
|
_.bindAll( this, 'saveKey' );
|
||||||
|
},
|
||||||
|
block: function() {
|
||||||
|
$( this.el ).block({
|
||||||
|
message: null,
|
||||||
|
overlayCSS: {
|
||||||
|
background: '#fff',
|
||||||
|
opacity: 0.6
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
unblock: function() {
|
||||||
|
$( this.el ).unblock();
|
||||||
|
},
|
||||||
|
initTipTip: function() {
|
||||||
|
$( '.copy-key', this.el ).tipTip({
|
||||||
|
'attribute': 'data-tip',
|
||||||
|
'activation': 'click',
|
||||||
|
'fadeIn': 50,
|
||||||
|
'fadeOut': 50,
|
||||||
|
'delay': 0
|
||||||
|
});
|
||||||
|
|
||||||
|
$( document.body ).on( 'copy', '.copy-key', function( e ) {
|
||||||
|
e.clipboardData.clearData();
|
||||||
|
e.clipboardData.setData( 'text/plain', $.trim( $( this ).prev( 'code' ).html() ) );
|
||||||
|
e.preventDefault();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
createQRCode: function( consumer_key, consumer_secret ) {
|
||||||
|
$( '#keys-qrcode' ).qrcode({
|
||||||
|
text: consumer_key + '|' + consumer_secret,
|
||||||
|
width: 120,
|
||||||
|
height: 120
|
||||||
|
});
|
||||||
|
},
|
||||||
|
saveKey: function( e ) {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
self.block();
|
||||||
|
|
||||||
|
Backbone.ajax({
|
||||||
|
method: 'POST',
|
||||||
|
dataType: 'json',
|
||||||
|
url: woocommerce_admin_api_keys.ajax_url,
|
||||||
|
data: {
|
||||||
|
action: 'woocommerce_update_api_key',
|
||||||
|
security: woocommerce_admin_api_keys.update_api_nonce,
|
||||||
|
key_id: $( '#key_id', self.el ).val(),
|
||||||
|
description: $( '#key_description', self.el ).val(),
|
||||||
|
user: $( '#key_user', self.el ).val(),
|
||||||
|
permissions: $( '#key_permissions', self.el ).val()
|
||||||
|
},
|
||||||
|
success: function( response ) {
|
||||||
|
$( '.wc-api-message', self.el ).remove();
|
||||||
|
|
||||||
|
if ( response.success ) {
|
||||||
|
var data = response.data;
|
||||||
|
|
||||||
|
$( 'h3', self.el ).first().append( '<div class="wc-api-message updated"><p>' + data.message + '</p></div>' );
|
||||||
|
|
||||||
|
$( '#key_id', self.el ).val( data.key_id );
|
||||||
|
$( '#key_description', self.el ).val( data.description );
|
||||||
|
$( '#key_user', self.el ).val( data.user_id );
|
||||||
|
$( '#key_permissions', self.el ).val( data.permissions );
|
||||||
|
|
||||||
|
if ( 0 < data.consumer_key.length && 0 < data.consumer_secret.length ) {
|
||||||
|
$( '#update_api_key', self.el ).val( woocommerce_admin_api_keys.i18n_save_changes ).after( data.revoke_url );
|
||||||
|
|
||||||
|
var keysTemplate = _.template( $( '#api-keys-template' ).html(), {
|
||||||
|
consumer_key: data.consumer_key,
|
||||||
|
consumer_secret: data.consumer_secret
|
||||||
|
});
|
||||||
|
|
||||||
|
$( 'p.submit', self.el ).before( keysTemplate );
|
||||||
|
self.createQRCode( data.consumer_key, data.consumer_secret );
|
||||||
|
self.initTipTip();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$( 'h3', self.el ).first().append( '<div class="wc-api-message error"><p>' + response.data.message + '</p></div>' );
|
||||||
|
}
|
||||||
|
|
||||||
|
self.unblock();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
new APIView();
|
||||||
|
|
||||||
|
})( jQuery );
|
|
@ -0,0 +1 @@
|
||||||
|
!function(a){var b=Backbone.View.extend({el:a("#key-fields"),events:{"click input#update_api_key":"saveKey"},initialize:function(){_.bindAll(this,"saveKey")},block:function(){a(this.el).block({message:null,overlayCSS:{background:"#fff",opacity:.6}})},unblock:function(){a(this.el).unblock()},initTipTip:function(){a(".copy-key",this.el).tipTip({attribute:"data-tip",activation:"click",fadeIn:50,fadeOut:50,delay:0}),a(document.body).on("copy",".copy-key",function(b){b.clipboardData.clearData(),b.clipboardData.setData("text/plain",a.trim(a(this).prev("code").html())),b.preventDefault()})},createQRCode:function(b,c){a("#keys-qrcode").qrcode({text:b+"|"+c,width:120,height:120})},saveKey:function(b){b.preventDefault();var c=this;c.block(),Backbone.ajax({method:"POST",dataType:"json",url:woocommerce_admin_api_keys.ajax_url,data:{action:"woocommerce_update_api_key",security:woocommerce_admin_api_keys.update_api_nonce,key_id:a("#key_id",c.el).val(),description:a("#key_description",c.el).val(),user:a("#key_user",c.el).val(),permissions:a("#key_permissions",c.el).val()},success:function(b){if(a(".wc-api-message",c.el).remove(),b.success){var d=b.data;if(a("h3",c.el).first().append('<div class="wc-api-message updated"><p>'+d.message+"</p></div>"),a("#key_id",c.el).val(d.key_id),a("#key_description",c.el).val(d.description),a("#key_user",c.el).val(d.user_id),a("#key_permissions",c.el).val(d.permissions),0<d.consumer_key.length&&0<d.consumer_secret.length){a("#update_api_key",c.el).val(woocommerce_admin_api_keys.i18n_save_changes).after(d.revoke_url);var e=_.template(a("#api-keys-template").html(),{consumer_key:d.consumer_key,consumer_secret:d.consumer_secret});a("p.submit",c.el).before(e),c.createQRCode(d.consumer_key,d.consumer_secret),c.initTipTip()}}else a("h3",c.el).first().append('<div class="wc-api-message error"><p>'+b.data.message+"</p></div>");c.unblock()}})}});new b}(jQuery);
|
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -176,6 +176,7 @@ class WC_Admin_API_Keys_Table_List extends WP_List_Table {
|
||||||
FROM {$wpdb->prefix}woocommerce_api_keys
|
FROM {$wpdb->prefix}woocommerce_api_keys
|
||||||
WHERE 1 = 1
|
WHERE 1 = 1
|
||||||
$search
|
$search
|
||||||
|
ORDER BY key_id DESC
|
||||||
LIMIT %d
|
LIMIT %d
|
||||||
OFFSET %d
|
OFFSET %d
|
||||||
", $per_page, $offset ), ARRAY_A );
|
", $per_page, $offset ), ARRAY_A );
|
||||||
|
|
|
@ -113,11 +113,6 @@ class WC_Admin_API_Keys {
|
||||||
*/
|
*/
|
||||||
public function actions() {
|
public function actions() {
|
||||||
if ( $this->is_api_keys_settings_page() ) {
|
if ( $this->is_api_keys_settings_page() ) {
|
||||||
// Generate Key / Edit Key
|
|
||||||
if ( isset( $_POST['update_api_key'] ) && isset( $_POST['key_id'] ) ) {
|
|
||||||
$this->update_key();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Revoke key
|
// Revoke key
|
||||||
if ( isset( $_GET['revoke-key'] ) ) {
|
if ( isset( $_GET['revoke-key'] ) ) {
|
||||||
$this->revoke_key();
|
$this->revoke_key();
|
||||||
|
@ -134,112 +129,8 @@ class WC_Admin_API_Keys {
|
||||||
* Notices.
|
* Notices.
|
||||||
*/
|
*/
|
||||||
public static function notices() {
|
public static function notices() {
|
||||||
if ( isset( $_GET['status'] ) ) {
|
if ( isset( $_GET['revoked'] ) && 1 == $_GET['revoked'] ) {
|
||||||
|
|
||||||
switch ( intval( $_GET['status'] ) ) {
|
|
||||||
case 2 :
|
|
||||||
WC_Admin_Settings::add_message( __( 'API Key generated successfully.', 'woocommerce' ) );
|
|
||||||
break;
|
|
||||||
case 3 :
|
|
||||||
WC_Admin_Settings::add_message( __( 'API Key revoked successfully.', 'woocommerce' ) );
|
WC_Admin_Settings::add_message( __( 'API Key revoked successfully.', 'woocommerce' ) );
|
||||||
break;
|
|
||||||
case -1 :
|
|
||||||
WC_Admin_Settings::add_error( __( 'Description is missing.', 'woocommerce' ) );
|
|
||||||
break;
|
|
||||||
case -2 :
|
|
||||||
WC_Admin_Settings::add_error( __( 'User is missing.', 'woocommerce' ) );
|
|
||||||
break;
|
|
||||||
case -3 :
|
|
||||||
WC_Admin_Settings::add_error( __( 'Description is missing.', 'woocommerce' ) );
|
|
||||||
break;
|
|
||||||
|
|
||||||
default :
|
|
||||||
WC_Admin_Settings::add_message( __( 'API Key updated successfully.', 'woocommerce' ) );
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Update Key
|
|
||||||
*/
|
|
||||||
private function update_key() {
|
|
||||||
global $wpdb;
|
|
||||||
|
|
||||||
if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-settings' ) ) {
|
|
||||||
wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( ! current_user_can( 'manage_woocommerce' ) ) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$url = admin_url( 'admin.php?page=wc-settings&tab=api§ion=keys' );
|
|
||||||
$key_id = absint( $_POST['key_id'] );
|
|
||||||
$status = 1;
|
|
||||||
|
|
||||||
try {
|
|
||||||
if ( empty( $_POST['key_description'] ) ) {
|
|
||||||
throw new Exception( 'Description is missing', -1 );
|
|
||||||
}
|
|
||||||
if ( empty( $_POST['key_user'] ) ) {
|
|
||||||
throw new Exception( 'User is missing', -2 );
|
|
||||||
}
|
|
||||||
if ( empty( $_POST['key_permissions'] ) ) {
|
|
||||||
throw new Exception( 'permissions is missing', -3 );
|
|
||||||
}
|
|
||||||
|
|
||||||
$description = sanitize_text_field( $_POST['key_description'] );
|
|
||||||
$permissions = ( in_array( $_POST['key_permissions'], array( 'read', 'write', 'read_write' ) ) ) ? sanitize_text_field( $_POST['key_permissions'] ) : 'read';
|
|
||||||
$user_id = absint( $_POST['key_user'] );
|
|
||||||
|
|
||||||
if ( 0 < $key_id ) {
|
|
||||||
$wpdb->update(
|
|
||||||
$wpdb->prefix . 'woocommerce_api_keys',
|
|
||||||
array(
|
|
||||||
'user_id' => $user_id,
|
|
||||||
'description' => $description,
|
|
||||||
'permissions' => $permissions
|
|
||||||
),
|
|
||||||
array( 'key_id' => $key_id ),
|
|
||||||
array(
|
|
||||||
'%d',
|
|
||||||
'%s',
|
|
||||||
'%s'
|
|
||||||
),
|
|
||||||
array( '%d' )
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
$status = 2;
|
|
||||||
$consumer_key = 'ck_' . wc_rand_hash();
|
|
||||||
$consumer_secret = 'cs_' . wc_rand_hash();
|
|
||||||
|
|
||||||
$wpdb->insert(
|
|
||||||
$wpdb->prefix . 'woocommerce_api_keys',
|
|
||||||
array(
|
|
||||||
'user_id' => $user_id,
|
|
||||||
'description' => $description,
|
|
||||||
'permissions' => $permissions,
|
|
||||||
'consumer_key' => wc_api_hash( $consumer_key ),
|
|
||||||
'consumer_secret' => wc_api_hash( $consumer_secret )
|
|
||||||
),
|
|
||||||
array(
|
|
||||||
'%d',
|
|
||||||
'%s',
|
|
||||||
'%s',
|
|
||||||
'%s',
|
|
||||||
'%s'
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
$key_id = $wpdb->insert_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
wp_redirect( esc_url_raw( add_query_arg( array( 'edit-key' => $key_id, 'status' => $status ), $url ) ) );
|
|
||||||
exit();
|
|
||||||
} catch ( Exception $e ) {
|
|
||||||
wp_redirect( esc_url_raw( add_query_arg( array( 'edit-key' => $key_id, 'status' => $e->getCode() ), $url ) ) );
|
|
||||||
exit();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -254,7 +145,7 @@ class WC_Admin_API_Keys {
|
||||||
$key_id = absint( $_GET['revoke-key'] );
|
$key_id = absint( $_GET['revoke-key'] );
|
||||||
$this->remove_key( $key_id );
|
$this->remove_key( $key_id );
|
||||||
|
|
||||||
wp_redirect( esc_url_raw( add_query_arg( array( 'status' => 3 ), admin_url( 'admin.php?page=wc-settings&tab=api§ion=keys' ) ) ) );
|
wp_redirect( esc_url_raw( add_query_arg( array( 'revoked' => 1 ), admin_url( 'admin.php?page=wc-settings&tab=api§ion=keys' ) ) ) );
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -88,7 +88,7 @@ class WC_Admin_Assets {
|
||||||
wp_register_script( 'round', WC()->plugin_url() . '/assets/js/admin/round' . $suffix . '.js', array( 'jquery' ), WC_VERSION );
|
wp_register_script( 'round', WC()->plugin_url() . '/assets/js/admin/round' . $suffix . '.js', array( 'jquery' ), WC_VERSION );
|
||||||
wp_register_script( 'wc-admin-meta-boxes', WC()->plugin_url() . '/assets/js/admin/meta-boxes' . $suffix . '.js', array( 'jquery', 'jquery-ui-datepicker', 'jquery-ui-sortable', 'accounting', 'round', 'wc-enhanced-select', 'plupload-all', 'stupidtable' ), WC_VERSION );
|
wp_register_script( 'wc-admin-meta-boxes', WC()->plugin_url() . '/assets/js/admin/meta-boxes' . $suffix . '.js', array( 'jquery', 'jquery-ui-datepicker', 'jquery-ui-sortable', 'accounting', 'round', 'wc-enhanced-select', 'plupload-all', 'stupidtable' ), WC_VERSION );
|
||||||
wp_register_script( 'zeroclipboard', WC()->plugin_url() . '/assets/js/zeroclipboard/jquery.zeroclipboard' . $suffix . '.js', array( 'jquery' ), WC_VERSION );
|
wp_register_script( 'zeroclipboard', WC()->plugin_url() . '/assets/js/zeroclipboard/jquery.zeroclipboard' . $suffix . '.js', array( 'jquery' ), WC_VERSION );
|
||||||
wp_register_script( 'qrcode', WC()->plugin_url() . '/assets/js/admin/jquery.qrcode.min.js', array( 'jquery' ), WC_VERSION );
|
wp_register_script( 'qrcode', WC()->plugin_url() . '/assets/js/jquery-qrcode/jquery.qrcode' . $suffix . '.js', array( 'jquery' ), WC_VERSION );
|
||||||
wp_register_script( 'stupidtable', WC()->plugin_url() . '/assets/js/stupidtable/stupidtable' . $suffix . '.js', array( 'jquery' ), WC_VERSION );
|
wp_register_script( 'stupidtable', WC()->plugin_url() . '/assets/js/stupidtable/stupidtable' . $suffix . '.js', array( 'jquery' ), WC_VERSION );
|
||||||
wp_register_script( 'wc-admin-notices', WC()->plugin_url() . '/assets/js/admin/woocommerce_notices' . $suffix . '.js', array( 'jquery' ), WC_VERSION, true );
|
wp_register_script( 'wc-admin-notices', WC()->plugin_url() . '/assets/js/admin/woocommerce_notices' . $suffix . '.js', array( 'jquery' ), WC_VERSION, true );
|
||||||
|
|
||||||
|
@ -292,13 +292,22 @@ class WC_Admin_Assets {
|
||||||
}
|
}
|
||||||
|
|
||||||
// API settings
|
// API settings
|
||||||
if ( 'woocommerce_page_wc-settings' === $screen->id && isset( $_GET['section'] ) && 'keys' == $_GET['section'] ) {
|
if ( $wc_screen_id . '_page_wc-settings' === $screen->id && isset( $_GET['section'] ) && 'keys' == $_GET['section'] ) {
|
||||||
wp_enqueue_script( 'qrcode' );
|
// wp_enqueue_script( 'wc-api-keys', WC()->plugin_url() . '/assets/js/admin/api-keys' . $suffix . '.js', array( 'jquery', 'woocommerce_admin', 'underscore', 'backbone', 'qrcode', 'zeroclipboard' ), WC_VERSION, true );
|
||||||
wp_enqueue_script( 'zeroclipboard' );
|
wp_enqueue_script( 'wc-api-keys', WC()->plugin_url() . '/assets/js/admin/api-keys.js', array( 'jquery', 'woocommerce_admin', 'underscore', 'backbone', 'qrcode', 'zeroclipboard' ), WC_VERSION, true );
|
||||||
|
wp_localize_script(
|
||||||
|
'wc-api-keys',
|
||||||
|
'woocommerce_admin_api_keys',
|
||||||
|
array(
|
||||||
|
'ajax_url' => admin_url( 'admin-ajax.php' ),
|
||||||
|
'update_api_nonce' => wp_create_nonce( 'update-api-key' ),
|
||||||
|
'i18n_save_changes' => __( 'Save Changes', 'woocommerce' )
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// System status
|
// System status
|
||||||
if ( 'woocommerce_page_wc-status' === $screen->id ) {
|
if ( $wc_screen_id . '_page_wc-status' === $screen->id ) {
|
||||||
wp_enqueue_script( 'zeroclipboard' );
|
wp_enqueue_script( 'zeroclipboard' );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,10 +4,11 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|
||||||
<input type="hidden" name="key_id" value="<?php echo esc_attr( $key_id ); ?>" />
|
|
||||||
|
|
||||||
<div id="key-fields" class="settings-panel">
|
<div id="key-fields" class="settings-panel">
|
||||||
<h3><?php _e( 'Key Details', 'woocommerce' ); ?></h3>
|
<h3><?php _e( 'Key Details', 'woocommerce' ); ?></h3>
|
||||||
|
|
||||||
|
<input type="hidden" id="key_id" value="<?php echo esc_attr( $key_id ); ?>" />
|
||||||
|
|
||||||
<table class="form-table">
|
<table class="form-table">
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr valign="top">
|
<tr valign="top">
|
||||||
|
@ -16,7 +17,7 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||||
<img class="help_tip" data-tip="<?php esc_attr_e( 'Friendly name for identifying this key.', 'woocommerce' ); ?>" src="<?php echo WC()->plugin_url(); ?>/assets/images/help.png" height="16" width="16" />
|
<img class="help_tip" data-tip="<?php esc_attr_e( 'Friendly name for identifying this key.', 'woocommerce' ); ?>" src="<?php echo WC()->plugin_url(); ?>/assets/images/help.png" height="16" width="16" />
|
||||||
</th>
|
</th>
|
||||||
<td class="forminp">
|
<td class="forminp">
|
||||||
<input name="key_description" id="key_description" type="text" class="input-text regular-input" value="<?php echo esc_attr( $key_data['description'] ); ?>" />
|
<input id="key_description" type="text" class="input-text regular-input" value="<?php echo esc_attr( $key_data['description'] ); ?>" />
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr valign="top">
|
<tr valign="top">
|
||||||
|
@ -31,7 +32,7 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||||
$user = get_user_by( 'id', $user_id );
|
$user = get_user_by( 'id', $user_id );
|
||||||
$user_string = esc_html( $user->display_name ) . ' (#' . absint( $user->ID ) . ' – ' . esc_html( $user->user_email );
|
$user_string = esc_html( $user->display_name ) . ' (#' . absint( $user->ID ) . ' – ' . esc_html( $user->user_email );
|
||||||
?>
|
?>
|
||||||
<input type="hidden" class="wc-customer-search" name="key_user" data-placeholder="<?php esc_html_e( 'Search for a customer…', 'woocommerce' ); ?>" data-selected="<?php echo esc_attr( $user_string ); ?>" value="<?php echo esc_attr( $user_id ); ?>" data-allow_clear="true" />
|
<input type="hidden" class="wc-customer-search" id="key_user" data-placeholder="<?php esc_html_e( 'Search for a customer…', 'woocommerce' ); ?>" data-selected="<?php echo esc_attr( $user_string ); ?>" value="<?php echo esc_attr( $user_id ); ?>" data-allow_clear="true" />
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr valign="top">
|
<tr valign="top">
|
||||||
|
@ -40,7 +41,7 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||||
<img class="help_tip" data-tip="<?php _e( 'Select the access type of these keys.', 'woocommerce' ); ?>" src="<?php echo WC()->plugin_url(); ?>/assets/images/help.png" height="16" width="16" />
|
<img class="help_tip" data-tip="<?php _e( 'Select the access type of these keys.', 'woocommerce' ); ?>" src="<?php echo WC()->plugin_url(); ?>/assets/images/help.png" height="16" width="16" />
|
||||||
</th>
|
</th>
|
||||||
<td class="forminp">
|
<td class="forminp">
|
||||||
<select name="key_permissions" id="key_permissions" class="wc-enhanced-select">
|
<select id="key_permissions" class="wc-enhanced-select">
|
||||||
<?php
|
<?php
|
||||||
$permissions = array(
|
$permissions = array(
|
||||||
'read' => __( 'Read', 'woocommerce' ),
|
'read' => __( 'Read', 'woocommerce' ),
|
||||||
|
@ -54,59 +55,6 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||||
</select>
|
</select>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<?php if ( ! empty( $key_data['consumer_key'] ) && ! empty( $key_data['consumer_secret'] ) ) : ?>
|
|
||||||
<tr valign="top" id="webhook-action-event-wrap">
|
|
||||||
<th scope="row" class="titledesc">
|
|
||||||
<?php _e( 'Consumer Key', 'woocommerce' ); ?>
|
|
||||||
</th>
|
|
||||||
<td class="forminp">
|
|
||||||
<code id="key_consumer_key"><?php echo esc_html( $key_data['consumer_key'] ); ?></code> <button type="button" class="button-secondary copy-key" data-tip="<?php _e( 'Copied!', 'woocommerce' ); ?>"><?php _e( 'Copy', 'woocommerce' ); ?></button>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
<tr valign="top" id="webhook-action-event-wrap">
|
|
||||||
<th scope="row" class="titledesc">
|
|
||||||
<label for="key_consumer_secret"><?php _e( 'Consumer Secret', 'woocommerce' ); ?></label>
|
|
||||||
</th>
|
|
||||||
<td class="forminp">
|
|
||||||
<code id="key_consumer_secret"><?php echo esc_html( $key_data['consumer_secret'] ); ?></code> <button type="button" class="button-secondary copy-key" data-tip="<?php _e( 'Copied!', 'woocommerce' ); ?>"><?php _e( 'Copy', 'woocommerce' ); ?></button>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
<tr valign="top" id="webhook-action-event-wrap">
|
|
||||||
<th scope="row" class="titledesc">
|
|
||||||
<?php _e( 'QRCode', 'woocommerce' ); ?>
|
|
||||||
</th>
|
|
||||||
<td class="forminp">
|
|
||||||
<div id="qrcode_wrap" data-consumer_key="<?php echo esc_attr( $key_data['consumer_key'] ); ?>" data-consumer_secret="<?php echo esc_attr( $key_data['consumer_secret'] ); ?>"></div>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
jQuery( function( $ ) {
|
|
||||||
// Copy to clipboard
|
|
||||||
$( '.copy-key' ).tipTip({
|
|
||||||
'attribute': 'data-tip',
|
|
||||||
'activation': 'click',
|
|
||||||
'fadeIn': 50,
|
|
||||||
'fadeOut': 50,
|
|
||||||
'delay': 0
|
|
||||||
});
|
|
||||||
|
|
||||||
$( document.body ).on( 'copy', '.copy-key', function( e ) {
|
|
||||||
e.clipboardData.clearData();
|
|
||||||
e.clipboardData.setData( 'text/plain', $.trim( $( this ).prev( 'code' ).html() ) );
|
|
||||||
e.preventDefault();
|
|
||||||
});
|
|
||||||
|
|
||||||
// Generate QR Code
|
|
||||||
var qrcodeWrap = $( '#qrcode_wrap' );
|
|
||||||
qrcodeWrap.qrcode({
|
|
||||||
text: qrcodeWrap.data( 'consumer_key' ) + '|' + qrcodeWrap.data( 'consumer_secret' ),
|
|
||||||
width: 120,
|
|
||||||
height: 120
|
|
||||||
});
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
<?php endif; ?>
|
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
|
@ -117,6 +65,7 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||||
submit_button( __( 'Generate API Key', 'woocommerce' ), 'primary', 'update_api_key' );
|
submit_button( __( 'Generate API Key', 'woocommerce' ), 'primary', 'update_api_key' );
|
||||||
} else {
|
} else {
|
||||||
?>
|
?>
|
||||||
|
<p></p>
|
||||||
<p class="submit">
|
<p class="submit">
|
||||||
<?php submit_button( __( 'Save Changes', 'woocommerce' ), 'primary', 'update_api_key', false ); ?>
|
<?php submit_button( __( 'Save Changes', 'woocommerce' ), 'primary', 'update_api_key', false ); ?>
|
||||||
<a style="color: #a00; text-decoration: none; margin-left: 10px;" href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'revoke-key' => $key_id ), admin_url( 'admin.php?page=wc-settings&tab=api§ion=keys' ) ), 'revoke' ) ); ?>"><?php _e( 'Revoke Key', 'woocommerce' ); ?></a>
|
<a style="color: #a00; text-decoration: none; margin-left: 10px;" href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'revoke-key' => $key_id ), admin_url( 'admin.php?page=wc-settings&tab=api§ion=keys' ) ), 'revoke' ) ); ?>"><?php _e( 'Revoke Key', 'woocommerce' ); ?></a>
|
||||||
|
@ -125,3 +74,34 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<script type="text/template" id="api-keys-template">
|
||||||
|
<table class="form-table">
|
||||||
|
<tbody>
|
||||||
|
<tr valign="top">
|
||||||
|
<th scope="row" class="titledesc">
|
||||||
|
<?php _e( 'Consumer Key', 'woocommerce' ); ?>
|
||||||
|
</th>
|
||||||
|
<td class="forminp">
|
||||||
|
<code id="key_consumer_key"><%- consumer_key %></code> <button type="button" class="button-secondary copy-key" data-tip="<?php _e( 'Copied!', 'woocommerce' ); ?>"><?php _e( 'Copy', 'woocommerce' ); ?></button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr valign="top">
|
||||||
|
<th scope="row" class="titledesc">
|
||||||
|
<label for="key_consumer_secret"><?php _e( 'Consumer Secret', 'woocommerce' ); ?></label>
|
||||||
|
</th>
|
||||||
|
<td class="forminp">
|
||||||
|
<code id="key_consumer_secret"><%- consumer_secret %></code> <button type="button" class="button-secondary copy-key" data-tip="<?php _e( 'Copied!', 'woocommerce' ); ?>"><?php _e( 'Copy', 'woocommerce' ); ?></button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr valign="top">
|
||||||
|
<th scope="row" class="titledesc">
|
||||||
|
<?php _e( 'QRCode', 'woocommerce' ); ?>
|
||||||
|
</th>
|
||||||
|
<td class="forminp">
|
||||||
|
<div id="keys-qrcode"></div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</script>
|
||||||
|
|
|
@ -128,7 +128,8 @@ class WC_AJAX {
|
||||||
'product_ordering' => false,
|
'product_ordering' => false,
|
||||||
'refund_line_items' => false,
|
'refund_line_items' => false,
|
||||||
'delete_refund' => false,
|
'delete_refund' => false,
|
||||||
'rated' => false
|
'rated' => false,
|
||||||
|
'update_api_key' => false
|
||||||
);
|
);
|
||||||
|
|
||||||
foreach ( $ajax_events as $ajax_event => $nopriv ) {
|
foreach ( $ajax_events as $ajax_event => $nopriv ) {
|
||||||
|
@ -2185,6 +2186,97 @@ class WC_AJAX {
|
||||||
update_option( 'woocommerce_admin_footer_text_rated', 1 );
|
update_option( 'woocommerce_admin_footer_text_rated', 1 );
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create/Update API key
|
||||||
|
*/
|
||||||
|
public static function update_api_key() {
|
||||||
|
ob_start();
|
||||||
|
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
check_ajax_referer( 'update-api-key', 'security' );
|
||||||
|
|
||||||
|
if ( ! current_user_can( 'manage_woocommerce' ) ) {
|
||||||
|
die(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
if ( empty( $_POST['description'] ) ) {
|
||||||
|
throw new Exception( __( 'Description is missing.', 'woocommerce' ) );
|
||||||
|
}
|
||||||
|
if ( empty( $_POST['user'] ) ) {
|
||||||
|
throw new Exception( __( 'User is missing.', 'woocommerce' ) );
|
||||||
|
}
|
||||||
|
if ( empty( $_POST['permissions'] ) ) {
|
||||||
|
throw new Exception( __( 'Permissions is missing.', 'woocommerce' ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
$key_id = absint( $_POST['key_id'] );
|
||||||
|
$description = sanitize_text_field( $_POST['description'] );
|
||||||
|
$permissions = ( in_array( $_POST['permissions'], array( 'read', 'write', 'read_write' ) ) ) ? sanitize_text_field( $_POST['permissions'] ) : 'read';
|
||||||
|
$user_id = absint( $_POST['user'] );
|
||||||
|
|
||||||
|
if ( 0 < $key_id ) {
|
||||||
|
$data = array(
|
||||||
|
'user_id' => $user_id,
|
||||||
|
'description' => $description,
|
||||||
|
'permissions' => $permissions
|
||||||
|
);
|
||||||
|
|
||||||
|
$wpdb->update(
|
||||||
|
$wpdb->prefix . 'woocommerce_api_keys',
|
||||||
|
$data,
|
||||||
|
array( 'key_id' => $key_id ),
|
||||||
|
array(
|
||||||
|
'%d',
|
||||||
|
'%s',
|
||||||
|
'%s'
|
||||||
|
),
|
||||||
|
array( '%d' )
|
||||||
|
);
|
||||||
|
|
||||||
|
$data['key_id'] = $key_id;
|
||||||
|
$data['consumer_key'] = '';
|
||||||
|
$data['consumer_secret'] = '';
|
||||||
|
$data['message'] = __( 'API Key updated successfully.', 'woocommerce' );
|
||||||
|
} else {
|
||||||
|
$status = 2;
|
||||||
|
$consumer_key = 'ck_' . wc_rand_hash();
|
||||||
|
$consumer_secret = 'cs_' . wc_rand_hash();
|
||||||
|
|
||||||
|
$data = array(
|
||||||
|
'user_id' => $user_id,
|
||||||
|
'description' => $description,
|
||||||
|
'permissions' => $permissions,
|
||||||
|
'consumer_key' => wc_api_hash( $consumer_key ),
|
||||||
|
'consumer_secret' => wc_api_hash( $consumer_secret )
|
||||||
|
);
|
||||||
|
|
||||||
|
$wpdb->insert(
|
||||||
|
$wpdb->prefix . 'woocommerce_api_keys',
|
||||||
|
$data,
|
||||||
|
array(
|
||||||
|
'%d',
|
||||||
|
'%s',
|
||||||
|
'%s',
|
||||||
|
'%s',
|
||||||
|
'%s'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$data['key_id'] = $wpdb->insert_id;
|
||||||
|
$data['consumer_key'] = $consumer_key;
|
||||||
|
$data['consumer_secret'] = $consumer_secret;
|
||||||
|
$data['message'] = __( 'API Key generated successfully. Make sure to copy your new API keys now. You won\'t be able to see it again!', 'woocommerce' );
|
||||||
|
$data['revoke_url'] = '<a style="color: #a00; text-decoration: none; margin-left: 10px;" href="' . esc_url( wp_nonce_url( add_query_arg( array( 'revoke-key' => $data['key_id'] ), admin_url( 'admin.php?page=wc-settings&tab=api§ion=keys' ) ), 'revoke' ) ). '">' . __( 'Revoke Key', 'woocommerce' ) . '</a>';
|
||||||
|
}
|
||||||
|
|
||||||
|
wp_send_json_success( $data );
|
||||||
|
} catch ( Exception $e ) {
|
||||||
|
wp_send_json_error( array( 'message' => $e->getMessage() ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
WC_AJAX::init();
|
WC_AJAX::init();
|
||||||
|
|
Loading…
Reference in New Issue