Skip to content

Commit

Permalink
chore: linter compliances met
Browse files Browse the repository at this point in the history
  • Loading branch information
kidunot89 committed Oct 30, 2024
1 parent 7c10a3d commit 47c5552
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 22 deletions.
27 changes: 21 additions & 6 deletions includes/data/mutation/class-order-mutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ class Order_Mutation {
* @param \WPGraphQL\AppContext $context AppContext instance.
* @param \GraphQL\Type\Definition\ResolveInfo $info ResolveInfo instance.
* @param string $mutation Mutation being executed.
* @param integer|null $order_id Order ID.
*
* @param integer|null|false $order_id Order ID.
* @throws \GraphQL\Error\UserError Error locating order.
*
* @return boolean
*/
public static function authorized( $input, $context, $info, $mutation = 'create', $order_id = null ) {
Expand All @@ -35,21 +36,35 @@ public static function authorized( $input, $context, $info, $mutation = 'create'
*/
$post_type_object = get_post_type_object( 'shop_order' );

if ( $order_id === null ) {
if ( ! $order_id ) {
return apply_filters(
"graphql_woocommerce_authorized_to_{$mutation}_orders",
current_user_can($post_type_object->cap->edit_posts),
current_user_can( $post_type_object->cap->edit_posts ),
$order_id,
$input,
$context,
$info
);
}

$order = \wc_get_order( $order_id );
/** @var false|\WC_Order $order */
$order = \wc_get_order( $order_id );
if ( false === $order ) {
throw new UserError(
sprintf(
/* translators: %d: Order ID */
__( 'Failed to find order with ID of %d.', 'wp-graphql-woocommerce' ),
$order_id
)
);
}

$post_type = get_post_type( $order_id );
if ( false === $post_type ) {
throw new UserError( __( 'Failed to identify the post type of the order.', 'wp-graphql-woocommerce' ) );
}

// Return true if user is owner or admin
// Return true if user is owner or admin.
$is_owner = 0 !== get_current_user_id() && $order->get_customer_id() === get_current_user_id();
$is_admin = \wc_rest_check_post_permissions( $post_type, 'edit', $order_id );
return $is_owner || $is_admin;
Expand Down
4 changes: 2 additions & 2 deletions includes/mutation/class-coupon-create.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
use GraphQL\Error\UserError;
use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;
use WPGraphQL\Utils\Utils;
use WPGraphQL\WooCommerce\Data\Mutation\Coupon_Mutation;
use WPGraphQL\WooCommerce\Model\Coupon;
use WPGraphQL\Utils\Utils;

/**
* Class Coupon_Create
Expand Down Expand Up @@ -163,7 +163,7 @@ public static function get_output_fields() {
*/
public static function mutate_and_get_payload( $input, AppContext $context, ResolveInfo $info ) {
// Retrieve order ID.
if ( ! empty ( $input['id'] ) ) {
if ( ! empty( $input['id'] ) ) {
$coupon_id = Utils::get_database_id_from_id( $input['id'] );
} else {
$coupon_id = 0;
Expand Down
2 changes: 1 addition & 1 deletion includes/mutation/class-coupon-delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
use GraphQL\Error\UserError;
use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;
use WPGraphQL\WooCommerce\Model\Coupon;
use WPGraphQL\Utils\Utils;
use WPGraphQL\WooCommerce\Model\Coupon;

/**
* Class Coupon_Delete
Expand Down
10 changes: 7 additions & 3 deletions includes/mutation/class-order-delete-items.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
use GraphQL\Error\UserError;
use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;
use WPGraphQL\Utils\Utils;
use WPGraphQL\WooCommerce\Data\Mutation\Order_Mutation;
use WPGraphQL\WooCommerce\Model\Order;
use WPGraphQL\Utils\Utils;

/**
* Class Order_Delete_Items
Expand Down Expand Up @@ -45,11 +45,11 @@ public static function register_mutation() {
public static function get_input_fields() {
return array_merge(
[
'id' => [
'id' => [
'type' => 'ID',
'description' => __( 'Database ID or global ID of the order', 'wp-graphql-woocommerce' ),
],
'orderId' => [
'orderId' => [
'type' => 'Int',
'description' => __( 'Order WP ID', 'wp-graphql-woocommerce' ),
'deprecationReason' => __( 'Use "id" field instead.', 'wp-graphql-woocommerce' ),
Expand Down Expand Up @@ -95,6 +95,10 @@ public static function mutate_and_get_payload() {
throw new UserError( __( 'Order ID provided is missing or invalid. Please check input and try again.', 'wp-graphql-woocommerce' ) );
}

if ( ! $order_id ) {
throw new UserError( __( 'Order ID provided is invalid. Please check input and try again.', 'wp-graphql-woocommerce' ) );
}

// Check if authorized to delete items on this order.
if ( ! Order_Mutation::authorized( $input, $context, $info, 'delete-items', $order_id ) ) {
throw new UserError( __( 'User does not have the capabilities necessary to delete order items.', 'wp-graphql-woocommerce' ) );
Expand Down
8 changes: 6 additions & 2 deletions includes/mutation/class-order-delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
use GraphQL\Type\Definition\ResolveInfo;
use WC_Order_Factory;
use WPGraphQL\AppContext;
use WPGraphQL\Utils\Utils;
use WPGraphQL\WooCommerce\Data\Mutation\Order_Mutation;
use WPGraphQL\WooCommerce\Model\Order;
use WPGraphQL\Utils\Utils;

/**
* Class Order_Delete
Expand Down Expand Up @@ -87,7 +87,7 @@ public static function get_output_fields() {
public static function mutate_and_get_payload() {
return static function ( $input, AppContext $context, ResolveInfo $info ) {
// Retrieve order ID.
$order_id = null;
$order_id = false;
if ( ! empty( $input['id'] ) ) {
$order_id = Utils::get_database_id_from_id( $input['id'] );
} elseif ( ! empty( $input['orderId'] ) ) {
Expand All @@ -96,6 +96,10 @@ public static function mutate_and_get_payload() {
throw new UserError( __( 'Order ID provided is missing or invalid. Please check input and try again.', 'wp-graphql-woocommerce' ) );
}

if ( ! $order_id ) {
throw new UserError( __( 'Order ID provided is invalid. Please check input and try again.', 'wp-graphql-woocommerce' ) );
}

// Check if authorized to delete this order.
if ( ! Order_Mutation::authorized( $input, $context, $info, 'delete', $order_id ) ) {
throw new UserError( __( 'User does not have the capabilities necessary to delete an order.', 'wp-graphql-woocommerce' ) );
Expand Down
10 changes: 7 additions & 3 deletions includes/mutation/class-order-update.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
use GraphQL\Type\Definition\ResolveInfo;
use WC_Order_Factory;
use WPGraphQL\AppContext;
use WPGraphQL\Utils\Utils;
use WPGraphQL\WooCommerce\Data\Mutation\Order_Mutation;
use WPGraphQL\WooCommerce\Model\Order;
use WPGraphQL\Utils\Utils;

/**
* Class Order_Update
Expand Down Expand Up @@ -51,7 +51,7 @@ public static function get_input_fields() {
'type' => 'ID',
'description' => __( 'Database ID or global ID of the order', 'wp-graphql-woocommerce' ),
],
'orderId' => [
'orderId' => [
'type' => 'Int',
'description' => __( 'Order WP ID', 'wp-graphql-woocommerce' ),
'deprecationReason' => __( 'Use "id" field instead.', 'wp-graphql-woocommerce' ),
Expand Down Expand Up @@ -96,7 +96,11 @@ public static function mutate_and_get_payload() {
} else {
throw new UserError( __( 'Order ID provided is missing or invalid. Please check input and try again.', 'wp-graphql-woocommerce' ) );
}


if ( ! $order_id ) {
throw new UserError( __( 'Order ID provided is invalid. Please check input and try again.', 'wp-graphql-woocommerce' ) );
}

// Check if authorized to update this order.
if ( ! Order_Mutation::authorized( $input, $context, $info, 'update', $order_id ) ) {
throw new UserError( __( 'User does not have the capabilities necessary to update an order.', 'wp-graphql-woocommerce' ) );
Expand Down
5 changes: 2 additions & 3 deletions includes/mutation/class-review-update.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

use GraphQL\Error\UserError;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQLRelay\Relay;
use WPGraphQL\AppContext;
use WPGraphQL\Mutation\CommentUpdate;
use WPGraphQL\Utils\Utils;
Expand Down Expand Up @@ -80,8 +79,8 @@ public static function mutate_and_get_payload() {
'clientMutationId' => 1,
];

$payload = [];
$id = Utils::get_database_id_from_id( $input['id'] );
$payload = [];
$id = Utils::get_database_id_from_id( $input['id'] );
if ( ! $id ) {
throw new UserError( __( 'Provided review ID missing or invalid ', 'wp-graphql-woocommerce' ) );
}
Expand Down
4 changes: 2 additions & 2 deletions includes/mutation/class-tax-rate-create.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public static function get_output_fields() {
* @return array
*/
public static function mutate_and_get_payload( $input, AppContext $context, ResolveInfo $info ) {
$id = ! empty( $input['id'] ) ? Utils::get_database_id_from_id( $input['id'] ) : null;
$id = ! empty( $input['id'] ) ? Utils::get_database_id_from_id( $input['id'] ) : null;
if ( false === $id ) {
throw new UserError( __( 'Invalid ID provided.', 'wp-graphql-woocommerce' ) );
}
Expand Down Expand Up @@ -221,7 +221,7 @@ public static function mutate_and_get_payload( $input, AppContext $context, Reso
/**
* Filter tax rate object before responding.
*
* @param object $tax_rate_id The shipping method object.
* @param int $tax_rate_id The shipping method object.
* @param array $input Request input.
*/
do_action( "graphql_woocommerce_tax_rate_{$action}", $id, $input );
Expand Down

0 comments on commit 47c5552

Please sign in to comment.