Skip to content

Commit

Permalink
feat: Various features and tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
seebeen committed Feb 1, 2025
1 parent 8429f9e commit 14d1c60
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
composer.lock
vendor
xwc-helper-fns.php
72 changes: 72 additions & 0 deletions Functions/API_Key.php
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,
);
}
}
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
"autoload": {
"psr-4": {
"XWC\\": ""
}
},
"files": [
"xwc-helper-fns-api.php"
]
}
}
31 changes: 31 additions & 0 deletions xwc-helper-fns-api.php
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;

0 comments on commit 14d1c60

Please sign in to comment.