WC_Auth::create_keys
Create keys.
Method of the class: WC_Auth{}
No Hooks.
Returns
Array.
Usage
// protected - for code of main (parent) or child class $result = $this->create_keys( $app_name, $app_user_id, $scope );
- $app_name(string) (required)
- App name.
- $app_user_id(string) (required)
- User ID.
- $scope(string) (required)
- Scope.
Changelog
| Since 2.4.0 | Introduced. |
WC_Auth::create_keys() WC Auth::create keys code WC 10.8.1
protected function create_keys( $app_name, $app_user_id, $scope ) {
global $wpdb;
$description = sprintf(
'%s - API (%s)',
wc_trim_string( wc_clean( $app_name ), 170 ),
gmdate( 'Y-m-d H:i:s' )
);
$user = wp_get_current_user();
// Created API keys.
$permissions = in_array( $scope, array( 'read', 'write', 'read_write' ), true ) ? sanitize_text_field( $scope ) : 'read';
$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' => $consumer_secret,
'truncated_key' => substr( $consumer_key, -7 ),
),
array(
'%d',
'%s',
'%s',
'%s',
'%s',
'%s',
)
);
return array(
'key_id' => $wpdb->insert_id,
'user_id' => $app_user_id,
'consumer_key' => $consumer_key,
'consumer_secret' => $consumer_secret,
'key_permissions' => $permissions,
);
}