WC_API_Webhooks::validate_request()protectedWC 3.3.0

Validate the request by checking:

1) the ID is a valid integer.
2) the ID returns a valid post object and matches the provided post type.
3) the current user has the proper permissions to read/edit/delete the post.

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

Хуков нет.

Возвращает

int|WP_Error. Valid post ID or WP_Error if any of the checks fails.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->validate_request( $id, $type, $context );
$id(строка|int) (обязательный)
The post ID
$type(строка) (обязательный)
The post type, either shop_order, shop_coupon, or product.
$context(строка) (обязательный)
The context of the request, either read, edit or delete.

Список изменений

С версии 3.3.0 Введена.

Код WC_API_Webhooks::validate_request() WC 8.7.0

protected function validate_request( $id, $type, $context ) {
	$id = absint( $id );

	// Validate ID.
	if ( empty( $id ) ) {
		return new WP_Error( "woocommerce_api_invalid_webhook_id", sprintf( __( 'Invalid %s ID', 'woocommerce' ), $type ), array( 'status' => 404 ) );
	}

	$webhook = wc_get_webhook( $id );

	if ( null === $webhook ) {
		return new WP_Error( "woocommerce_api_no_webhook_found", sprintf( __( 'No %1$s found with the ID equal to %2$s', 'woocommerce' ), 'webhook', $id ), array( 'status' => 404 ) );
	}

	// Validate permissions.
	switch ( $context ) {

		case 'read':
			if ( ! current_user_can( 'manage_woocommerce' ) ) {
				return new WP_Error( "woocommerce_api_user_cannot_read_webhook", sprintf( __( 'You do not have permission to read this %s', 'woocommerce' ), 'webhook' ), array( 'status' => 401 ) );
			}
			break;

		case 'edit':
			if ( ! current_user_can( 'manage_woocommerce' ) ) {
				return new WP_Error( "woocommerce_api_user_cannot_edit_webhook", sprintf( __( 'You do not have permission to edit this %s', 'woocommerce' ), 'webhook' ), array( 'status' => 401 ) );
			}
			break;

		case 'delete':
			if ( ! current_user_can( 'manage_woocommerce' ) ) {
				return new WP_Error( "woocommerce_api_user_cannot_delete_webhook", sprintf( __( 'You do not have permission to delete this %s', 'woocommerce' ), 'webhook' ), array( 'status' => 401 ) );
			}
			break;
	}

	return $id;
}