WC_API_Customers::validate_request()
Validate the request by checking:
1) the ID is a valid integer
2) the ID returns a valid WP_User
3) the current user has the proper permissions
Method of the class: WC_API_Customers{}
No Hooks.
Return
Int|WP_Error
. valid user 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 customer ID
- $type(string) (required)
- the request type, unused because this method overrides the parent class
- $context(string) (required)
- the context of the request, either read, edit or delete
Notes
Changelog
Since 2.1 | Introduced. |
WC_API_Customers::validate_request() WC API Customers::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_customer_id', __( 'Invalid customer ID', 'woocommerce' ), array( 'status' => 404 ) ); } // non-existent IDs return a valid WP_User object with the user ID = 0 $customer = new WP_User( $id ); if ( 0 === $customer->ID ) { return new WP_Error( 'woocommerce_api_invalid_customer', __( 'Invalid customer', 'woocommerce' ), array( 'status' => 404 ) ); } // validate permissions switch ( $context ) { case 'read': if ( ! current_user_can( 'list_users' ) ) { return new WP_Error( 'woocommerce_api_user_cannot_read_customer', __( 'You do not have permission to read this customer', 'woocommerce' ), array( 'status' => 401 ) ); } break; case 'edit': if ( ! current_user_can( 'edit_users' ) ) { return new WP_Error( 'woocommerce_api_user_cannot_edit_customer', __( 'You do not have permission to edit this customer', 'woocommerce' ), array( 'status' => 401 ) ); } break; case 'delete': if ( ! current_user_can( 'delete_users' ) ) { return new WP_Error( 'woocommerce_api_user_cannot_delete_customer', __( 'You do not have permission to delete this customer', 'woocommerce' ), array( 'status' => 401 ) ); } break; } return $id; }