WC_API_Webhooks::validate_request()
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.
Method of the class: WC_API_Webhooks{}
No Hooks.
Return
Int|WP_Error
. Valid post ID or WP_Error if any of the checks fails.
Usage
// protected - for code of main (parent) or child class $result = $this->validate_request( $id, $type, $context );
- $id(string|int) (required)
- The post ID
- $type(string) (required)
- The post type, either shop_order, shop_coupon, or product.
- $context(string) (required)
- The context of the request, either read, edit or delete.
Changelog
Since 3.3.0 | Introduced. |
WC_API_Webhooks::validate_request() WC API Webhooks::validate request code WC 7.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; }