WC_REST_WCCOM_Site_Controller::check_permission()publicWC 7.8.0

Check permissions.

Please note that access to this endpoint is also governed by the WC_WCCOM_Site::authenticate_wccom() method.

Method of the class: WC_REST_WCCOM_Site_Controller{}

Hooks from the method

Return

true|false|WP_Error.

Usage

$WC_REST_WCCOM_Site_Controller = new WC_REST_WCCOM_Site_Controller();
$WC_REST_WCCOM_Site_Controller->check_permission();

Changelog

Since 7.8.0 Introduced.

WC_REST_WCCOM_Site_Controller::check_permission() code WC 9.4.2

public function check_permission() {
	$current_user = wp_get_current_user();

	if ( empty( $current_user ) || ( $current_user instanceof WP_User && ! $current_user->exists() ) ) {
		/**
		 * This filter allows to provide a custom error message when the user is not authenticated.
		 *
		 * @since 3.7.0
		 */
		$error = apply_filters(
			WC_WCCOM_Site::AUTH_ERROR_FILTER_NAME,
			new Installer_Error( Installer_Error_Codes::NOT_AUTHENTICATED )
		);
		return new WP_Error(
			$error->get_error_code(),
			$error->get_error_message(),
			array( 'status' => $error->get_http_code() )
		);
	}

	if ( ! $this->user_has_permission( $current_user ) ) {
		$error = new Installer_Error( Installer_Error_Codes::NO_PERMISSION );
		return new WP_Error(
			$error->get_error_code(),
			$error->get_error_message(),
			array( 'status' => $error->get_http_code() )
		);
	}

	return true;
}