WC_Auth::create_keys( string $app_name, string $app_user_id, string $scope )

Create keys.


Description Description


Parameters Parameters

$app_name

(Required) App name.

$app_user_id

(Required) User ID.

$scope

(Required) Scope.


Top ↑

Return Return

(array)


Top ↑

Source Source

File: includes/class-wc-auth.php

	protected function create_keys( $app_name, $app_user_id, $scope ) {
		global $wpdb;

		$description = sprintf(
			/* translators: 1: app name 2: scope 3: date 4: time */
			__( '%1$s - API %2$s (created on %3$s at %4$s).', 'woocommerce' ),
			wc_clean( $app_name ),
			$this->get_i18n_scope( $scope ),
			date_i18n( wc_date_format() ),
			date_i18n( wc_time_format() )
		);
		$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,
		);
	}

Top ↑

Changelog Changelog

Changelog
Version Description
2.4.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.