-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
composer.lock | ||
vendor | ||
xwc-helper-fns.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php //phpcs:disable Squiz.Commenting.FunctionComment.Missing | ||
|
||
namespace XWC\Functions; | ||
|
||
use WP_User; | ||
|
||
/** | ||
* WooCommerce REST API Key helper functions. | ||
*/ | ||
class API_Key { | ||
private static function get_user_id( int|WP_User $user ): int { | ||
$user_id = \is_int( $user ) ? $user : $user->ID; | ||
|
||
if ( $user_id && ! \current_user_can( 'edit_user', $user_id ) && \get_current_user_id() !== $user_id ) { | ||
throw new \InvalidArgumentException( | ||
\esc_html__( | ||
'You do not have permission to assign API Keys to the selected user.', | ||
'woocommerce', | ||
), | ||
); | ||
} | ||
|
||
return $user_id; | ||
} | ||
|
||
private static function get_permissions( string $permissions ): string { | ||
return \in_array( $permissions, array( 'read', 'write', 'read_write' ), true ) | ||
? \sanitize_text_field( $permissions ) | ||
: 'read'; | ||
} | ||
|
||
public static function create( int|WP_User $user, string $description, string $permissions, ?string $meta_key ): array { | ||
$user_id = self::get_user_id( $user ); | ||
$permissions = self::get_permissions( $permissions ); | ||
$description = \wc_trim_string( $description, 200 ); | ||
$consumer_key = 'ck_' . \wc_rand_hash(); | ||
$consumer_secret = 'cs_' . \wc_rand_hash(); | ||
|
||
//phpcs:disable SlevomatCodingStandard.Arrays.AlphabeticallySortedByKeys.IncorrectKeyOrder | ||
$data = 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 ), | ||
); | ||
//phpcs:enable SlevomatCodingStandard.Arrays.AlphabeticallySortedByKeys.IncorrectKeyOrder | ||
|
||
global $wpdb; | ||
|
||
$wpdb->insert( | ||
$wpdb->prefix . 'woocommerce_api_keys', | ||
$data, | ||
array( '%d', '%s', '%s', '%s', '%s', '%s' ), | ||
); | ||
|
||
if ( 0 === $wpdb->insert_id ) { | ||
throw new \Exception( \esc_html__( 'There was an error generating your API Key.', 'woocommerce' ) ); | ||
} | ||
|
||
if ( null !== $meta_key ) { | ||
\update_user_meta( $user_id, $meta_key, $wpdb->insert_id ); | ||
} | ||
|
||
return array( | ||
'ck' => $consumer_key, | ||
'cs' => $consumer_secret, | ||
'id' => $wpdb->insert_id, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,9 @@ | |
"autoload": { | ||
"psr-4": { | ||
"XWC\\": "" | ||
} | ||
}, | ||
"files": [ | ||
"xwc-helper-fns-api.php" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
/** | ||
* WooCommerce API Key helper functions definition file. | ||
* | ||
* @package eXtended WooCommerce | ||
* @subpackage Helper\Functions | ||
*/ | ||
|
||
use XWC\Functions\API_Key; | ||
|
||
if ( ! function_exists( 'xwc_create_api_key' ) ) : | ||
/** | ||
* Create a new API key for a user. | ||
* | ||
* @param int|WP_User $user User ID or object. | ||
* @param string $description Description of the API key. | ||
* @param 'read'|'write'|'read_write' $scope Permissions for the API key. | ||
* @param string|null $meta_key Optional. User meta key to store the API key ID. | ||
* @return array{ | ||
* ck: string, | ||
* cs: string, | ||
* id: int, | ||
* } | ||
* | ||
* @throws InvalidArgumentException If the user does not have permission to create an API key. | ||
* @throws Exception If there was an error generating the API key. | ||
*/ | ||
function xwc_create_api_key( int|WP_User $user, string $description = '', string $scope = 'read', ?string $meta_key = null ): array { | ||
return API_Key::create( $user, $description, $scope, $meta_key ); | ||
} | ||
endif; |