Automattic\WooCommerce\Internal\RestApi\Routes\V4\Customers

Controller::update_item_permissions_check │ public │ WC 1.0

Check if a given request has access to update an item.

Метод класса: Controller{}

Хуков нет.

Возвращает

WP_Error|boolean.

Использование

$Controller = new Controller();
$Controller->update_item_permissions_check( $request );
$request(WP_REST_Request) (обязательный)
The request object.

Код Controller::update_item_permissions_check() WC 11.3.0

public function update_item_permissions_check( $request ) {
	$user = \Automattic\WooCommerce\Internal\Utilities\Users::get_user_in_current_site( $request['id'] );

	if ( is_wp_error( $user ) ) {
		return $this->get_route_error_by_code( self::INVALID_ID );
	}

	if ( ! wc_rest_check_user_permissions( 'edit', $user->ID ) ) {
		return $this->get_authentication_error_by_method( $request->get_method() );
	}

	// Check if user role is allowed to be updated.
	$allowed_roles = $this->get_allowed_roles();
	$customer      = new WC_Customer( $user->ID );

	if ( $customer && ! in_array( $customer->get_role(), $allowed_roles, true ) ) {
		// Check against existing props to be compatible with clients that will send the entire user object.
		$non_editable_props = array( 'email', 'password' );
		$customer_prop      = array( 'email' => $customer->get_email() );
		foreach ( $non_editable_props as $prop ) {
			if ( isset( $request[ $prop ] ) && ( 'password' === $prop || $request[ $prop ] !== $customer_prop[ $prop ] ) ) {
				return new WP_Error(
					'woocommerce_rest_cannot_edit',
					sprintf(
						/* translators: 1s: name of the property (email, role), 2: Role of the user (administrator, customer). */
						__( 'Sorry, %1$s cannot be updated via this endpoint for a user with role %2$s.', 'woocommerce' ),
						$prop,
						$customer->get_role()
					),
					array( 'status' => rest_authorization_required_code() )
				);
			}
		}
	}

	return true;
}