mirror of
https://github.com/wp-graphql/wp-graphql-woocommerce.git
synced 2026-08-14 12:53:44 +02:00
"Root_Query::register_fields()" refactored.
This commit is contained in:
@@ -22,265 +22,476 @@ class Root_Query {
|
||||
* Return WooCommerce GraphQL queries.
|
||||
*/
|
||||
public static function register_fields() {
|
||||
// Cart queries.
|
||||
register_graphql_field(
|
||||
register_graphql_fields(
|
||||
'RootQuery',
|
||||
'cart',
|
||||
array(
|
||||
'type' => 'Cart',
|
||||
'description' => __( 'The cart object', 'wp-graphql-woocommerce' ),
|
||||
'resolve' => function() {
|
||||
$token_invalid = apply_filters( 'graphql_woocommerce_session_token_errors', null );
|
||||
if ( $token_invalid ) {
|
||||
throw new UserError( $token_invalid );
|
||||
}
|
||||
'cart' => array(
|
||||
'type' => 'Cart',
|
||||
'description' => __( 'The cart object', 'wp-graphql-woocommerce' ),
|
||||
'resolve' => function() {
|
||||
$token_invalid = apply_filters( 'graphql_woocommerce_session_token_errors', null );
|
||||
if ( $token_invalid ) {
|
||||
throw new UserError( $token_invalid );
|
||||
}
|
||||
|
||||
return Factory::resolve_cart();
|
||||
},
|
||||
)
|
||||
);
|
||||
|
||||
register_graphql_field(
|
||||
'RootQuery',
|
||||
'cartItem',
|
||||
array(
|
||||
'type' => 'CartItem',
|
||||
'args' => array(
|
||||
'key' => array(
|
||||
'type' => array( 'non_null' => 'ID' ),
|
||||
),
|
||||
return Factory::resolve_cart();
|
||||
},
|
||||
),
|
||||
'description' => __( 'The cart object', 'wp-graphql-woocommerce' ),
|
||||
'resolve' => function( $source, array $args, AppContext $context ) {
|
||||
$item = Factory::resolve_cart_item( $args['key'], $context );
|
||||
if ( empty( $item ) ) {
|
||||
throw new UserError( __( 'The key input is invalid', 'wp-graphql-woocommerce' ) );
|
||||
}
|
||||
|
||||
return $item;
|
||||
},
|
||||
)
|
||||
);
|
||||
|
||||
register_graphql_field(
|
||||
'RootQuery',
|
||||
'cartFee',
|
||||
array(
|
||||
'type' => 'CartFee',
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'type' => array( 'non_null' => 'ID' ),
|
||||
'cartItem' => array(
|
||||
'type' => 'CartItem',
|
||||
'args' => array(
|
||||
'key' => array(
|
||||
'type' => array( 'non_null' => 'ID' ),
|
||||
),
|
||||
),
|
||||
'description' => __( 'The cart object', 'wp-graphql-woocommerce' ),
|
||||
'resolve' => function( $source, array $args, AppContext $context ) {
|
||||
$item = Factory::resolve_cart_item( $args['key'], $context );
|
||||
if ( empty( $item ) ) {
|
||||
throw new UserError( __( 'The key input is invalid', 'wp-graphql-woocommerce' ) );
|
||||
}
|
||||
|
||||
return $item;
|
||||
},
|
||||
),
|
||||
'description' => __( 'The cart object', 'wp-graphql-woocommerce' ),
|
||||
'resolve' => function( $source, array $args ) {
|
||||
$fee = Factory::resolve_cart_fee( $args['id'] );
|
||||
if ( empty( $fee ) ) {
|
||||
throw new UserError( __( 'The ID input is invalid', 'wp-graphql-woocommerce' ) );
|
||||
}
|
||||
|
||||
return $fee;
|
||||
},
|
||||
)
|
||||
);
|
||||
|
||||
// Coupon queries.
|
||||
register_graphql_field(
|
||||
'RootQuery',
|
||||
'coupon',
|
||||
array(
|
||||
'type' => 'Coupon',
|
||||
'description' => __( 'A coupon object', 'wp-graphql-woocommerce' ),
|
||||
'args' => array(
|
||||
'id' => array( 'type' => array( 'non_null' => 'ID' ) ),
|
||||
'idType' => array(
|
||||
'type' => 'CouponIdTypeEnum',
|
||||
'description' => __( 'Type of ID being used identify coupon', 'wp-graphql-woocommerce' ),
|
||||
'cartFee' => array(
|
||||
'type' => 'CartFee',
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'type' => array( 'non_null' => 'ID' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
'resolve' => function ( $source, array $args, AppContext $context ) {
|
||||
$id = isset( $args['id'] ) ? $args['id'] : null;
|
||||
$id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
|
||||
|
||||
$coupon_id = null;
|
||||
switch ( $id_type ) {
|
||||
case 'code':
|
||||
$coupon_id = \wc_get_coupon_id_by_code( $id );
|
||||
break;
|
||||
case 'database_id':
|
||||
$coupon_id = absint( $id );
|
||||
break;
|
||||
case 'global_id':
|
||||
default:
|
||||
$id_components = Relay::fromGlobalId( $args['id'] );
|
||||
if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
|
||||
throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
|
||||
}
|
||||
$coupon_id = absint( $id_components['id'] );
|
||||
break;
|
||||
}
|
||||
|
||||
if ( empty( $coupon_id ) ) {
|
||||
/* translators: %1$s: ID type, %2$s: ID value */
|
||||
throw new UserError( sprintf( __( 'No coupon ID was found corresponding to the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
|
||||
} elseif ( get_post( $coupon_id )->post_type !== 'shop_coupon' ) {
|
||||
/* translators: %1$s: ID type, %2$s: ID value */
|
||||
throw new UserError( sprintf( __( 'No coupon exists with the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
|
||||
}
|
||||
|
||||
return Factory::resolve_crud_object( $coupon_id, $context );
|
||||
},
|
||||
)
|
||||
);
|
||||
|
||||
// Customer queries.
|
||||
register_graphql_field(
|
||||
'RootQuery',
|
||||
'customer',
|
||||
array(
|
||||
'type' => 'Customer',
|
||||
'description' => __( 'A customer object', 'wp-graphql-woocommerce' ),
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'type' => 'ID',
|
||||
'description' => __( 'Get the customer by their global ID', 'wp-graphql-woocommerce' ),
|
||||
),
|
||||
'customerId' => array(
|
||||
'type' => 'Int',
|
||||
'description' => __( 'Get the customer by their database ID', 'wp-graphql-woocommerce' ),
|
||||
),
|
||||
),
|
||||
'resolve' => function ( $source, array $args, AppContext $context ) {
|
||||
$customer_id = 0;
|
||||
if ( ! empty( $args['id'] ) ) {
|
||||
$id_components = Relay::fromGlobalId( $args['id'] );
|
||||
if ( ! isset( $id_components['id'] ) || ! absint( $id_components['id'] ) ) {
|
||||
'description' => __( 'The cart object', 'wp-graphql-woocommerce' ),
|
||||
'resolve' => function( $source, array $args ) {
|
||||
$fee = Factory::resolve_cart_fee( $args['id'] );
|
||||
if ( empty( $fee ) ) {
|
||||
throw new UserError( __( 'The ID input is invalid', 'wp-graphql-woocommerce' ) );
|
||||
}
|
||||
|
||||
$customer_id = absint( $id_components['id'] );
|
||||
} elseif ( ! empty( $args['customerId'] ) ) {
|
||||
$customer_id = absint( $args['customerId'] );
|
||||
}
|
||||
|
||||
$authorized = ! empty( $customer_id )
|
||||
&& ! current_user_can( 'list_users' )
|
||||
&& get_current_user_id() !== $customer_id;
|
||||
if ( $authorized ) {
|
||||
throw new UserError( __( 'Not authorized to access this customer', 'wp-graphql-woocommerce' ) );
|
||||
}
|
||||
|
||||
if ( $customer_id ) {
|
||||
return Factory::resolve_customer( $customer_id, $context );
|
||||
}
|
||||
|
||||
return Factory::resolve_session_customer();
|
||||
},
|
||||
)
|
||||
);
|
||||
|
||||
// Order queries.
|
||||
register_graphql_field(
|
||||
'RootQuery',
|
||||
'order',
|
||||
array(
|
||||
'type' => 'Order',
|
||||
'description' => __( 'A order object', 'wp-graphql-woocommerce' ),
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'type' => 'ID',
|
||||
'description' => __( 'The ID for identifying the order', 'wp-graphql-woocommerce' ),
|
||||
),
|
||||
'idType' => array(
|
||||
'type' => 'OrderIdTypeEnum',
|
||||
'description' => __( 'Type of ID being used identify order', 'wp-graphql-woocommerce' ),
|
||||
),
|
||||
'orderId' => array(
|
||||
'type' => 'Int',
|
||||
'description' => __( 'Get the order by its database ID', 'wp-graphql-woocommerce' ),
|
||||
'isDeprecated' => true,
|
||||
'deprecationReason' => __(
|
||||
'This argument has been deprecation, and will be removed in v0.5.x. Please use "order(id: value, idType: value)" instead',
|
||||
'wp-graphql-woocommerce'
|
||||
),
|
||||
),
|
||||
'orderKey' => array(
|
||||
'type' => 'String',
|
||||
'description' => __( 'Get the order by its order number', 'wp-graphql-woocommerce' ),
|
||||
'isDeprecated' => true,
|
||||
'deprecationReason' => __(
|
||||
'This argument has been deprecation, and will be removed in v0.5.x. Please use "order(id: value, idType: value)" instead',
|
||||
'wp-graphql-woocommerce'
|
||||
),
|
||||
),
|
||||
return $fee;
|
||||
},
|
||||
),
|
||||
'resolve' => function ( $source, array $args, AppContext $context ) {
|
||||
$id = isset( $args['id'] ) ? $args['id'] : null;
|
||||
$id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
|
||||
'coupon' => array(
|
||||
'type' => 'Coupon',
|
||||
'description' => __( 'A coupon object', 'wp-graphql-woocommerce' ),
|
||||
'args' => array(
|
||||
'id' => array( 'type' => array( 'non_null' => 'ID' ) ),
|
||||
'idType' => array(
|
||||
'type' => 'CouponIdTypeEnum',
|
||||
'description' => __( 'Type of ID being used identify coupon', 'wp-graphql-woocommerce' ),
|
||||
),
|
||||
),
|
||||
'resolve' => function ( $source, array $args, AppContext $context ) {
|
||||
$id = isset( $args['id'] ) ? $args['id'] : null;
|
||||
$id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
|
||||
|
||||
// Process deprecated arguments. Will be removed in v0.5.x.
|
||||
if ( ! empty( $args['orderId'] ) ) {
|
||||
$id = $args['orderId'];
|
||||
$id_type = 'database_id';
|
||||
} elseif ( ! empty( $args['orderKey'] ) ) {
|
||||
$id = $args['orderKey'];
|
||||
$id_type = 'order_number';
|
||||
}
|
||||
|
||||
$order_id = null;
|
||||
switch ( $id_type ) {
|
||||
case 'order_number':
|
||||
$order_id = \wc_get_order_id_by_order_key( $id );
|
||||
break;
|
||||
case 'database_id':
|
||||
$order_id = absint( $id );
|
||||
break;
|
||||
case 'global_id':
|
||||
default:
|
||||
$id_components = Relay::fromGlobalId( $id );
|
||||
if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
|
||||
throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
|
||||
}
|
||||
$order_id = absint( $id_components['id'] );
|
||||
break;
|
||||
}
|
||||
|
||||
if ( empty( $order_id ) ) {
|
||||
/* translators: %1$s: ID type, %2$s: ID value */
|
||||
throw new UserError( sprintf( __( 'No order ID was found corresponding to the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
|
||||
} elseif ( get_post( $order_id )->post_type !== 'shop_order' ) {
|
||||
/* translators: %1$s: ID type, %2$s: ID value */
|
||||
throw new UserError( sprintf( __( 'No order exists with the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
|
||||
}
|
||||
|
||||
// Check if user authorized to view order.
|
||||
$post_type = get_post_type_object( 'shop_order' );
|
||||
$is_authorized = current_user_can( $post_type->cap->edit_others_posts );
|
||||
if ( get_current_user_id() ) {
|
||||
$orders = wc_get_orders(
|
||||
array(
|
||||
'type' => 'shop_order',
|
||||
'post__in' => array( $order_id ),
|
||||
'customer_id' => get_current_user_id(),
|
||||
'no_rows_found' => true,
|
||||
'return' => 'ids',
|
||||
)
|
||||
);
|
||||
|
||||
if ( in_array( $order_id, $orders, true ) ) {
|
||||
$is_authorized = true;
|
||||
$coupon_id = null;
|
||||
switch ( $id_type ) {
|
||||
case 'code':
|
||||
$coupon_id = \wc_get_coupon_id_by_code( $id );
|
||||
break;
|
||||
case 'database_id':
|
||||
$coupon_id = absint( $id );
|
||||
break;
|
||||
case 'global_id':
|
||||
default:
|
||||
$id_components = Relay::fromGlobalId( $args['id'] );
|
||||
if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
|
||||
throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
|
||||
}
|
||||
$coupon_id = absint( $id_components['id'] );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$order = $is_authorized ? Factory::resolve_crud_object( $order_id, $context ) : null;
|
||||
if ( empty( $coupon_id ) ) {
|
||||
/* translators: %1$s: ID type, %2$s: ID value */
|
||||
throw new UserError( sprintf( __( 'No coupon ID was found corresponding to the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
|
||||
} elseif ( get_post( $coupon_id )->post_type !== 'shop_coupon' ) {
|
||||
/* translators: %1$s: ID type, %2$s: ID value */
|
||||
throw new UserError( sprintf( __( 'No coupon exists with the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
|
||||
}
|
||||
|
||||
return $order;
|
||||
},
|
||||
return Factory::resolve_crud_object( $coupon_id, $context );
|
||||
},
|
||||
),
|
||||
'customer' => array(
|
||||
'type' => 'Customer',
|
||||
'description' => __( 'A customer object', 'wp-graphql-woocommerce' ),
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'type' => 'ID',
|
||||
'description' => __( 'Get the customer by their global ID', 'wp-graphql-woocommerce' ),
|
||||
),
|
||||
'customerId' => array(
|
||||
'type' => 'Int',
|
||||
'description' => __( 'Get the customer by their database ID', 'wp-graphql-woocommerce' ),
|
||||
),
|
||||
),
|
||||
'resolve' => function ( $source, array $args, AppContext $context ) {
|
||||
$customer_id = 0;
|
||||
if ( ! empty( $args['id'] ) ) {
|
||||
$id_components = Relay::fromGlobalId( $args['id'] );
|
||||
if ( ! isset( $id_components['id'] ) || ! absint( $id_components['id'] ) ) {
|
||||
throw new UserError( __( 'The ID input is invalid', 'wp-graphql-woocommerce' ) );
|
||||
}
|
||||
|
||||
$customer_id = absint( $id_components['id'] );
|
||||
} elseif ( ! empty( $args['customerId'] ) ) {
|
||||
$customer_id = absint( $args['customerId'] );
|
||||
}
|
||||
|
||||
$authorized = ! empty( $customer_id )
|
||||
&& ! current_user_can( 'list_users' )
|
||||
&& get_current_user_id() !== $customer_id;
|
||||
if ( $authorized ) {
|
||||
throw new UserError( __( 'Not authorized to access this customer', 'wp-graphql-woocommerce' ) );
|
||||
}
|
||||
|
||||
if ( $customer_id ) {
|
||||
return Factory::resolve_customer( $customer_id, $context );
|
||||
}
|
||||
|
||||
return Factory::resolve_session_customer();
|
||||
},
|
||||
),
|
||||
'order' => array(
|
||||
'type' => 'Order',
|
||||
'description' => __( 'A order object', 'wp-graphql-woocommerce' ),
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'type' => 'ID',
|
||||
'description' => __( 'The ID for identifying the order', 'wp-graphql-woocommerce' ),
|
||||
),
|
||||
'idType' => array(
|
||||
'type' => 'OrderIdTypeEnum',
|
||||
'description' => __( 'Type of ID being used identify order', 'wp-graphql-woocommerce' ),
|
||||
),
|
||||
'orderId' => array(
|
||||
'type' => 'Int',
|
||||
'description' => __( 'Get the order by its database ID', 'wp-graphql-woocommerce' ),
|
||||
'isDeprecated' => true,
|
||||
'deprecationReason' => __(
|
||||
'This argument has been deprecation, and will be removed in v0.5.x. Please use "order(id: value, idType: value)" instead',
|
||||
'wp-graphql-woocommerce'
|
||||
),
|
||||
),
|
||||
'orderKey' => array(
|
||||
'type' => 'String',
|
||||
'description' => __( 'Get the order by its order number', 'wp-graphql-woocommerce' ),
|
||||
'isDeprecated' => true,
|
||||
'deprecationReason' => __(
|
||||
'This argument has been deprecation, and will be removed in v0.5.x. Please use "order(id: value, idType: value)" instead',
|
||||
'wp-graphql-woocommerce'
|
||||
),
|
||||
),
|
||||
),
|
||||
'resolve' => function ( $source, array $args, AppContext $context ) {
|
||||
$id = isset( $args['id'] ) ? $args['id'] : null;
|
||||
$id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
|
||||
|
||||
// Process deprecated arguments. Will be removed in v0.5.x.
|
||||
if ( ! empty( $args['orderId'] ) ) {
|
||||
$id = $args['orderId'];
|
||||
$id_type = 'database_id';
|
||||
} elseif ( ! empty( $args['orderKey'] ) ) {
|
||||
$id = $args['orderKey'];
|
||||
$id_type = 'order_number';
|
||||
}
|
||||
|
||||
$order_id = null;
|
||||
switch ( $id_type ) {
|
||||
case 'order_number':
|
||||
$order_id = \wc_get_order_id_by_order_key( $id );
|
||||
break;
|
||||
case 'database_id':
|
||||
$order_id = absint( $id );
|
||||
break;
|
||||
case 'global_id':
|
||||
default:
|
||||
$id_components = Relay::fromGlobalId( $id );
|
||||
if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
|
||||
throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
|
||||
}
|
||||
$order_id = absint( $id_components['id'] );
|
||||
break;
|
||||
}
|
||||
|
||||
if ( empty( $order_id ) ) {
|
||||
/* translators: %1$s: ID type, %2$s: ID value */
|
||||
throw new UserError( sprintf( __( 'No order ID was found corresponding to the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
|
||||
} elseif ( get_post( $order_id )->post_type !== 'shop_order' ) {
|
||||
/* translators: %1$s: ID type, %2$s: ID value */
|
||||
throw new UserError( sprintf( __( 'No order exists with the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
|
||||
}
|
||||
|
||||
// Check if user authorized to view order.
|
||||
$post_type = get_post_type_object( 'shop_order' );
|
||||
$is_authorized = current_user_can( $post_type->cap->edit_others_posts );
|
||||
if ( get_current_user_id() ) {
|
||||
$orders = wc_get_orders(
|
||||
array(
|
||||
'type' => 'shop_order',
|
||||
'post__in' => array( $order_id ),
|
||||
'customer_id' => get_current_user_id(),
|
||||
'no_rows_found' => true,
|
||||
'return' => 'ids',
|
||||
)
|
||||
);
|
||||
|
||||
if ( in_array( $order_id, $orders, true ) ) {
|
||||
$is_authorized = true;
|
||||
}
|
||||
}
|
||||
|
||||
$order = $is_authorized ? Factory::resolve_crud_object( $order_id, $context ) : null;
|
||||
|
||||
return $order;
|
||||
},
|
||||
),
|
||||
'productVariation' => array(
|
||||
'type' => 'ProductVariation',
|
||||
'description' => __( 'A product variation object', 'wp-graphql-woocommerce' ),
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'type' => 'ID',
|
||||
'description' => __( 'The ID for identifying the product variation', 'wp-graphql-woocommerce' ),
|
||||
),
|
||||
'idType' => array(
|
||||
'type' => 'ProductVariationIdTypeEnum',
|
||||
'description' => __( 'Type of ID being used identify product variation', 'wp-graphql-woocommerce' ),
|
||||
),
|
||||
'variationId' => array(
|
||||
'type' => 'Int',
|
||||
'description' => __( 'Get the product variation by its database ID', 'wp-graphql-woocommerce' ),
|
||||
'isDeprecated' => true,
|
||||
'deprecationReason' => __(
|
||||
'This argument has been deprecation, and will be removed in v0.5.x. Please use "productVariation(id: value, idType: DATABASE_ID)" instead',
|
||||
'wp-graphql-woocommerce'
|
||||
),
|
||||
),
|
||||
),
|
||||
'resolve' => function ( $source, array $args, AppContext $context ) {
|
||||
$id = isset( $args['id'] ) ? $args['id'] : null;
|
||||
$id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
|
||||
|
||||
/**
|
||||
* Process deprecated arguments
|
||||
*
|
||||
* Will be removed in v0.5.x.
|
||||
*/
|
||||
if ( ! empty( $args['variationId'] ) ) {
|
||||
$id = $args['variationId'];
|
||||
$id_type = 'database_id';
|
||||
}
|
||||
|
||||
$variation_id = null;
|
||||
switch ( $id_type ) {
|
||||
case 'database_id':
|
||||
$variation_id = absint( $id );
|
||||
break;
|
||||
case 'global_id':
|
||||
default:
|
||||
$id_components = Relay::fromGlobalId( $id );
|
||||
if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
|
||||
throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
|
||||
}
|
||||
$variation_id = absint( $id_components['id'] );
|
||||
break;
|
||||
}
|
||||
|
||||
if ( empty( $variation_id ) ) {
|
||||
/* translators: %1$s: ID type, %2$s: ID value */
|
||||
throw new UserError( sprintf( __( 'No product variation ID was found corresponding to the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
|
||||
} elseif ( get_post( $variation_id )->post_type !== 'product_variation' ) {
|
||||
/* translators: %1$s: ID type, %2$s: ID value */
|
||||
throw new UserError( sprintf( __( 'No product variation exists with the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
|
||||
}
|
||||
|
||||
return Factory::resolve_crud_object( $variation_id, $context );
|
||||
},
|
||||
),
|
||||
'refund' => array(
|
||||
'type' => 'Refund',
|
||||
'description' => __( 'A refund object', 'wp-graphql-woocommerce' ),
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'type' => array( 'non_null' => 'ID' ),
|
||||
'description' => __( 'The ID for identifying the refund', 'wp-graphql-woocommerce' ),
|
||||
),
|
||||
'idType' => array(
|
||||
'type' => 'RefundIdTypeEnum',
|
||||
'description' => __( 'Type of ID being used identify refund', 'wp-graphql-woocommerce' ),
|
||||
),
|
||||
),
|
||||
'resolve' => function ( $source, array $args, AppContext $context ) {
|
||||
$id = isset( $args['id'] ) ? $args['id'] : null;
|
||||
$id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
|
||||
|
||||
$refund_id = null;
|
||||
switch ( $id_type ) {
|
||||
case 'database_id':
|
||||
$refund_id = absint( $id );
|
||||
break;
|
||||
case 'global_id':
|
||||
default:
|
||||
$id_components = Relay::fromGlobalId( $id );
|
||||
if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
|
||||
throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
|
||||
}
|
||||
$refund_id = absint( $id_components['id'] );
|
||||
break;
|
||||
}
|
||||
|
||||
if ( empty( $refund_id ) ) {
|
||||
/* translators: %1$s: ID type, %2$s: ID value */
|
||||
throw new UserError( sprintf( __( 'No refund ID was found corresponding to the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
|
||||
} elseif ( get_post( $refund_id )->post_type !== 'shop_order_refund' ) {
|
||||
/* translators: %1$s: ID type, %2$s: ID value */
|
||||
throw new UserError( sprintf( __( 'No refund exists with the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
|
||||
}
|
||||
|
||||
// Check if user authorized to view order.
|
||||
$post_type = get_post_type_object( 'shop_order_refund' );
|
||||
$is_authorized = current_user_can( $post_type->cap->edit_others_posts );
|
||||
if ( get_current_user_id() ) {
|
||||
$refund = \wc_get_order( $refund_id );
|
||||
$order_id = $refund->get_parent_id();
|
||||
$orders = wc_get_orders(
|
||||
array(
|
||||
'type' => 'shop_order',
|
||||
'post__in' => array( $order_id ),
|
||||
'customer_id' => get_current_user_id(),
|
||||
'no_rows_found' => true,
|
||||
'return' => 'ids',
|
||||
)
|
||||
);
|
||||
|
||||
if ( in_array( $order_id, $orders, true ) ) {
|
||||
$is_authorized = true;
|
||||
}
|
||||
}
|
||||
|
||||
$refund = $is_authorized ? Factory::resolve_crud_object( $refund_id, $context ) : null;
|
||||
|
||||
return $refund;
|
||||
},
|
||||
),
|
||||
'shippingMethod' => array(
|
||||
'type' => 'ShippingMethod',
|
||||
'description' => __( 'A shipping method object', 'wp-graphql-woocommerce' ),
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'type' => 'ID',
|
||||
'description' => __( 'The ID for identifying the shipping method', 'wp-graphql-woocommerce' ),
|
||||
),
|
||||
'idType' => array(
|
||||
'type' => 'ShippingMethodIdTypeEnum',
|
||||
'description' => __( 'Type of ID being used identify product variation', 'wp-graphql-woocommerce' ),
|
||||
),
|
||||
'methodId' => array(
|
||||
'type' => 'ID',
|
||||
'description' => __( 'Get the shipping method by its database ID', 'wp-graphql-woocommerce' ),
|
||||
'isDeprecated' => true,
|
||||
'deprecationReason' => __(
|
||||
'This argument has been deprecation, and will be removed in v0.5.x. Please use "shippingMethod(id: value, idType: DATABASE_ID)" instead.',
|
||||
'wp-graphql-woocommerce'
|
||||
),
|
||||
),
|
||||
),
|
||||
'resolve' => function ( $source, array $args ) {
|
||||
$id = isset( $args['id'] ) ? $args['id'] : null;
|
||||
$id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
|
||||
|
||||
/**
|
||||
* Process deprecated arguments
|
||||
*
|
||||
* Will be removed in v0.5.x.
|
||||
*/
|
||||
if ( ! empty( $args['methodId'] ) ) {
|
||||
$id = $args['methodId'];
|
||||
$id_type = 'database_id';
|
||||
}
|
||||
|
||||
$method_id = null;
|
||||
switch ( $id_type ) {
|
||||
case 'database_id':
|
||||
$method_id = $id;
|
||||
break;
|
||||
case 'global_id':
|
||||
default:
|
||||
$id_components = Relay::fromGlobalId( $id );
|
||||
if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
|
||||
throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
|
||||
}
|
||||
$method_id = $id_components['id'];
|
||||
break;
|
||||
}
|
||||
|
||||
return Factory::resolve_shipping_method( $method_id );
|
||||
},
|
||||
),
|
||||
'taxRate' => array(
|
||||
'type' => 'TaxRate',
|
||||
'description' => __( 'A tax rate object', 'wp-graphql-woocommerce' ),
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'type' => 'ID',
|
||||
'description' => __( 'The ID for identifying the tax rate', 'wp-graphql-woocommerce' ),
|
||||
),
|
||||
'idType' => array(
|
||||
'type' => 'TaxRateIdTypeEnum',
|
||||
'description' => __( 'Type of ID being used identify tax rate', 'wp-graphql-woocommerce' ),
|
||||
),
|
||||
'rateId' => array(
|
||||
'type' => 'Int',
|
||||
'description' => __( 'Get the tax rate by its database ID', 'wp-graphql-woocommerce' ),
|
||||
'isDeprecated' => true,
|
||||
'deprecationReason' => __(
|
||||
'This argument has been deprecation, and will be removed in v0.5.x. Please use "taxRate(id: value, idType: DATABASE_ID)" instead.',
|
||||
'wp-graphql-woocommerce'
|
||||
),
|
||||
),
|
||||
),
|
||||
'resolve' => function ( $source, array $args, AppContext $context ) {
|
||||
$id = isset( $args['id'] ) ? $args['id'] : null;
|
||||
$id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
|
||||
|
||||
/**
|
||||
* Process deprecated arguments
|
||||
*
|
||||
* Will be removed in v0.5.x.
|
||||
*/
|
||||
if ( ! empty( $args['rateId'] ) ) {
|
||||
$id = $args['rateId'];
|
||||
$id_type = 'database_id';
|
||||
}
|
||||
|
||||
$rate_id = null;
|
||||
switch ( $id_type ) {
|
||||
case 'database_id':
|
||||
$rate_id = absint( $id );
|
||||
break;
|
||||
case 'global_id':
|
||||
default:
|
||||
$id_components = Relay::fromGlobalId( $id );
|
||||
if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
|
||||
throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
|
||||
}
|
||||
$rate_id = absint( $id_components['id'] );
|
||||
break;
|
||||
}
|
||||
|
||||
return Factory::resolve_tax_rate( $rate_id, $context );
|
||||
},
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
// Product queries
|
||||
$product_types = array( 'simple', 'variable', 'external', 'group' );
|
||||
$product_types = apply_filters(
|
||||
'woographql_register_product_queries',
|
||||
array( 'simple', 'variable', 'external', 'group' )
|
||||
);
|
||||
foreach( $product_types as $type ) {
|
||||
$field_name = "{$type}Product";
|
||||
$type_name = ucfirst( $type ) . 'Product';
|
||||
@@ -413,265 +624,5 @@ class Root_Query {
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// Product variation queries.
|
||||
register_graphql_field(
|
||||
'RootQuery',
|
||||
'productVariation',
|
||||
array(
|
||||
'type' => 'ProductVariation',
|
||||
'description' => __( 'A product variation object', 'wp-graphql-woocommerce' ),
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'type' => 'ID',
|
||||
'description' => __( 'The ID for identifying the product variation', 'wp-graphql-woocommerce' ),
|
||||
),
|
||||
'idType' => array(
|
||||
'type' => 'ProductVariationIdTypeEnum',
|
||||
'description' => __( 'Type of ID being used identify product variation', 'wp-graphql-woocommerce' ),
|
||||
),
|
||||
'variationId' => array(
|
||||
'type' => 'Int',
|
||||
'description' => __( 'Get the product variation by its database ID', 'wp-graphql-woocommerce' ),
|
||||
'isDeprecated' => true,
|
||||
'deprecationReason' => __(
|
||||
'This argument has been deprecation, and will be removed in v0.5.x. Please use "productVariation(id: value, idType: DATABASE_ID)" instead',
|
||||
'wp-graphql-woocommerce'
|
||||
),
|
||||
),
|
||||
),
|
||||
'resolve' => function ( $source, array $args, AppContext $context ) {
|
||||
$id = isset( $args['id'] ) ? $args['id'] : null;
|
||||
$id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
|
||||
|
||||
/**
|
||||
* Process deprecated arguments
|
||||
*
|
||||
* Will be removed in v0.5.x.
|
||||
*/
|
||||
if ( ! empty( $args['variationId'] ) ) {
|
||||
$id = $args['variationId'];
|
||||
$id_type = 'database_id';
|
||||
}
|
||||
|
||||
$variation_id = null;
|
||||
switch ( $id_type ) {
|
||||
case 'database_id':
|
||||
$variation_id = absint( $id );
|
||||
break;
|
||||
case 'global_id':
|
||||
default:
|
||||
$id_components = Relay::fromGlobalId( $id );
|
||||
if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
|
||||
throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
|
||||
}
|
||||
$variation_id = absint( $id_components['id'] );
|
||||
break;
|
||||
}
|
||||
|
||||
if ( empty( $variation_id ) ) {
|
||||
/* translators: %1$s: ID type, %2$s: ID value */
|
||||
throw new UserError( sprintf( __( 'No product variation ID was found corresponding to the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
|
||||
} elseif ( get_post( $variation_id )->post_type !== 'product_variation' ) {
|
||||
/* translators: %1$s: ID type, %2$s: ID value */
|
||||
throw new UserError( sprintf( __( 'No product variation exists with the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
|
||||
}
|
||||
|
||||
return Factory::resolve_crud_object( $variation_id, $context );
|
||||
},
|
||||
)
|
||||
);
|
||||
|
||||
// Refund queries.
|
||||
register_graphql_field(
|
||||
'RootQuery',
|
||||
'refund',
|
||||
array(
|
||||
'type' => 'Refund',
|
||||
'description' => __( 'A refund object', 'wp-graphql-woocommerce' ),
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'type' => array( 'non_null' => 'ID' ),
|
||||
'description' => __( 'The ID for identifying the refund', 'wp-graphql-woocommerce' ),
|
||||
),
|
||||
'idType' => array(
|
||||
'type' => 'RefundIdTypeEnum',
|
||||
'description' => __( 'Type of ID being used identify refund', 'wp-graphql-woocommerce' ),
|
||||
),
|
||||
),
|
||||
'resolve' => function ( $source, array $args, AppContext $context ) {
|
||||
$id = isset( $args['id'] ) ? $args['id'] : null;
|
||||
$id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
|
||||
|
||||
$refund_id = null;
|
||||
switch ( $id_type ) {
|
||||
case 'database_id':
|
||||
$refund_id = absint( $id );
|
||||
break;
|
||||
case 'global_id':
|
||||
default:
|
||||
$id_components = Relay::fromGlobalId( $id );
|
||||
if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
|
||||
throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
|
||||
}
|
||||
$refund_id = absint( $id_components['id'] );
|
||||
break;
|
||||
}
|
||||
|
||||
if ( empty( $refund_id ) ) {
|
||||
/* translators: %1$s: ID type, %2$s: ID value */
|
||||
throw new UserError( sprintf( __( 'No refund ID was found corresponding to the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
|
||||
} elseif ( get_post( $refund_id )->post_type !== 'shop_order_refund' ) {
|
||||
/* translators: %1$s: ID type, %2$s: ID value */
|
||||
throw new UserError( sprintf( __( 'No refund exists with the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
|
||||
}
|
||||
|
||||
// Check if user authorized to view order.
|
||||
$post_type = get_post_type_object( 'shop_order_refund' );
|
||||
$is_authorized = current_user_can( $post_type->cap->edit_others_posts );
|
||||
if ( get_current_user_id() ) {
|
||||
$refund = \wc_get_order( $refund_id );
|
||||
$order_id = $refund->get_parent_id();
|
||||
$orders = wc_get_orders(
|
||||
array(
|
||||
'type' => 'shop_order',
|
||||
'post__in' => array( $order_id ),
|
||||
'customer_id' => get_current_user_id(),
|
||||
'no_rows_found' => true,
|
||||
'return' => 'ids',
|
||||
)
|
||||
);
|
||||
|
||||
if ( in_array( $order_id, $orders, true ) ) {
|
||||
$is_authorized = true;
|
||||
}
|
||||
}
|
||||
|
||||
$refund = $is_authorized ? Factory::resolve_crud_object( $refund_id, $context ) : null;
|
||||
|
||||
return $refund;
|
||||
},
|
||||
)
|
||||
);
|
||||
|
||||
// Shipping Method queries.
|
||||
register_graphql_field(
|
||||
'RootQuery',
|
||||
'shippingMethod',
|
||||
array(
|
||||
'type' => 'ShippingMethod',
|
||||
'description' => __( 'A shipping method object', 'wp-graphql-woocommerce' ),
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'type' => 'ID',
|
||||
'description' => __( 'The ID for identifying the shipping method', 'wp-graphql-woocommerce' ),
|
||||
),
|
||||
'idType' => array(
|
||||
'type' => 'ShippingMethodIdTypeEnum',
|
||||
'description' => __( 'Type of ID being used identify product variation', 'wp-graphql-woocommerce' ),
|
||||
),
|
||||
'methodId' => array(
|
||||
'type' => 'ID',
|
||||
'description' => __( 'Get the shipping method by its database ID', 'wp-graphql-woocommerce' ),
|
||||
'isDeprecated' => true,
|
||||
'deprecationReason' => __(
|
||||
'This argument has been deprecation, and will be removed in v0.5.x. Please use "shippingMethod(id: value, idType: DATABASE_ID)" instead.',
|
||||
'wp-graphql-woocommerce'
|
||||
),
|
||||
),
|
||||
),
|
||||
'resolve' => function ( $source, array $args ) {
|
||||
$id = isset( $args['id'] ) ? $args['id'] : null;
|
||||
$id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
|
||||
|
||||
/**
|
||||
* Process deprecated arguments
|
||||
*
|
||||
* Will be removed in v0.5.x.
|
||||
*/
|
||||
if ( ! empty( $args['methodId'] ) ) {
|
||||
$id = $args['methodId'];
|
||||
$id_type = 'database_id';
|
||||
}
|
||||
|
||||
$method_id = null;
|
||||
switch ( $id_type ) {
|
||||
case 'database_id':
|
||||
$method_id = $id;
|
||||
break;
|
||||
case 'global_id':
|
||||
default:
|
||||
$id_components = Relay::fromGlobalId( $id );
|
||||
if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
|
||||
throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
|
||||
}
|
||||
$method_id = $id_components['id'];
|
||||
break;
|
||||
}
|
||||
|
||||
return Factory::resolve_shipping_method( $method_id );
|
||||
},
|
||||
)
|
||||
);
|
||||
|
||||
// TaxRate queries.
|
||||
register_graphql_field(
|
||||
'RootQuery',
|
||||
'taxRate',
|
||||
array(
|
||||
'type' => 'TaxRate',
|
||||
'description' => __( 'A tax rate object', 'wp-graphql-woocommerce' ),
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'type' => 'ID',
|
||||
'description' => __( 'The ID for identifying the tax rate', 'wp-graphql-woocommerce' ),
|
||||
),
|
||||
'idType' => array(
|
||||
'type' => 'TaxRateIdTypeEnum',
|
||||
'description' => __( 'Type of ID being used identify tax rate', 'wp-graphql-woocommerce' ),
|
||||
),
|
||||
'rateId' => array(
|
||||
'type' => 'Int',
|
||||
'description' => __( 'Get the tax rate by its database ID', 'wp-graphql-woocommerce' ),
|
||||
'isDeprecated' => true,
|
||||
'deprecationReason' => __(
|
||||
'This argument has been deprecation, and will be removed in v0.5.x. Please use "taxRate(id: value, idType: DATABASE_ID)" instead.',
|
||||
'wp-graphql-woocommerce'
|
||||
),
|
||||
),
|
||||
),
|
||||
'resolve' => function ( $source, array $args, AppContext $context ) {
|
||||
$id = isset( $args['id'] ) ? $args['id'] : null;
|
||||
$id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
|
||||
|
||||
/**
|
||||
* Process deprecated arguments
|
||||
*
|
||||
* Will be removed in v0.5.x.
|
||||
*/
|
||||
if ( ! empty( $args['rateId'] ) ) {
|
||||
$id = $args['rateId'];
|
||||
$id_type = 'database_id';
|
||||
}
|
||||
|
||||
$rate_id = null;
|
||||
switch ( $id_type ) {
|
||||
case 'database_id':
|
||||
$rate_id = absint( $id );
|
||||
break;
|
||||
case 'global_id':
|
||||
default:
|
||||
$id_components = Relay::fromGlobalId( $id );
|
||||
if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
|
||||
throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
|
||||
}
|
||||
$rate_id = absint( $id_components['id'] );
|
||||
break;
|
||||
}
|
||||
|
||||
return Factory::resolve_tax_rate( $rate_id, $context );
|
||||
},
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user