diff --git a/includes/data/mutation/class-order-mutation.php b/includes/data/mutation/class-order-mutation.php index 78b48251..614de7a8 100644 --- a/includes/data/mutation/class-order-mutation.php +++ b/includes/data/mutation/class-order-mutation.php @@ -14,6 +14,25 @@ use GraphQL\Error\UserError; * Class - Order_Mutation */ class Order_Mutation { + /** + * Filterable authentication function. + * + * @param string $mutation Mutation being executed. + * + * @return boolean + */ + public static function authorized( $mutation = 'create', $input, $context, $info ) { + $post_type_object = get_post_type_object( 'shop_order' ); + + return apply_filters( + "authorized_to_{$mutation}_orders", + ! current_user_can( $post_type_object->cap->create_posts ), + $input, + $context, + $info + ); + } + /** * Create an order. * diff --git a/includes/mutation/class-order-create.php b/includes/mutation/class-order-create.php index d7138114..91c17ecd 100644 --- a/includes/mutation/class-order-create.php +++ b/includes/mutation/class-order-create.php @@ -133,9 +133,7 @@ class Order_Create { */ public static function mutate_and_get_payload() { return function( $input, AppContext $context, ResolveInfo $info ) { - $post_type_object = get_post_type_object( 'shop_order' ); - - if ( ! current_user_can( $post_type_object->cap->create_posts ) ) { + if ( Order_Mutation::authorized( 'create', $input, $context, $info ) ) { throw new UserError( __( 'User does not have the capabilities necessary to create an order.', 'wp-graphql-woocommerce' ) ); } diff --git a/includes/mutation/class-order-delete.php b/includes/mutation/class-order-delete.php index 787c3a4c..2d174186 100644 --- a/includes/mutation/class-order-delete.php +++ b/includes/mutation/class-order-delete.php @@ -84,9 +84,7 @@ class Order_Delete { */ public static function mutate_and_get_payload() { return function( $input, AppContext $context, ResolveInfo $info ) { - $post_type_object = get_post_type_object( 'shop_order' ); - - if ( ! current_user_can( $post_type_object->cap->create_posts ) ) { + if ( Order_Mutation::authorized( 'delete', $input, $context, $info ) ) { throw new UserError( __( 'User does not have the capabilities necessary to delete an order.', 'wp-graphql-woocommerce' ) ); } diff --git a/includes/mutation/class-order-update.php b/includes/mutation/class-order-update.php index a21d8d03..5ee0d6cb 100644 --- a/includes/mutation/class-order-update.php +++ b/includes/mutation/class-order-update.php @@ -85,9 +85,7 @@ class Order_Update { */ public static function mutate_and_get_payload() { return function( $input, AppContext $context, ResolveInfo $info ) { - $post_type_object = get_post_type_object( 'shop_order' ); - - if ( ! current_user_can( $post_type_object->cap->create_posts ) ) { + if ( Order_Mutation::authorized( 'update', $input, $context, $info ) ) { throw new UserError( __( 'User does not have the capabilities necessary to update an order.', 'wp-graphql-woocommerce' ) ); }