Files

364 lines
11 KiB
PHP
Raw Permalink Normal View History

2019-03-31 00:21:31 -04:00
<?php
/**
* Connection - Orders
*
* Registers connections to Order
*
2019-10-25 19:13:36 -04:00
* @package WPGraphQL\WooCommerce\Connection
2019-03-31 00:21:31 -04:00
*/
2019-10-25 19:13:36 -04:00
namespace WPGraphQL\WooCommerce\Connection;
2019-03-31 00:21:31 -04:00
2020-02-27 21:30:21 -03:00
use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;
2023-06-21 13:21:29 -04:00
use WPGraphQL\WooCommerce\Data\Connection\Order_Connection_Resolver;
use WPGraphQL\WooCommerce\Model\Customer;
2019-03-31 00:21:31 -04:00
/**
* Class - Orders
*/
class Orders {
2019-03-31 00:21:31 -04:00
/**
* Registers the various connections from other Types to Customer
2023-06-13 23:17:02 +03:00
*
* @return void
2019-03-31 00:21:31 -04:00
*/
public static function register_connections() {
2021-06-04 00:05:24 -04:00
// From RootQuery To Orders.
2023-06-21 13:21:29 -04:00
register_graphql_connection(
self::get_connection_config()
);
2021-06-04 00:05:24 -04:00
// From Customer To Orders.
2019-03-31 00:21:31 -04:00
register_graphql_connection(
self::get_connection_config(
[
2019-03-31 00:21:31 -04:00
'fromType' => 'Customer',
'fromFieldName' => 'orders',
'resolve' => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
2023-06-21 13:21:29 -04:00
$resolver = new Order_Connection_Resolver( $source, $args, $context, $info );
2020-08-11 19:18:19 -04:00
2021-06-03 21:37:09 -04:00
return self::get_customer_order_connection( $resolver, $source );
2021-01-21 22:41:43 -05:00
},
]
2019-03-31 00:21:31 -04:00
)
);
2021-06-04 00:05:24 -04:00
// From RootQuery To Refunds.
register_graphql_connection(
self::get_connection_config(
[
2021-06-04 00:05:24 -04:00
'toType' => 'Refund',
'fromFieldName' => 'refunds',
'connectionArgs' => self::get_refund_connection_args(),
],
2021-06-04 00:05:24 -04:00
'shop_order_refund'
)
);
// From Order To Refunds.
register_graphql_connection(
self::get_connection_config(
[
2021-06-04 00:05:24 -04:00
'fromType' => 'Order',
'toType' => 'Refund',
'fromFieldName' => 'refunds',
'connectionArgs' => self::get_refund_connection_args(),
'resolve' => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
2023-06-21 13:21:29 -04:00
$resolver = new Order_Connection_Resolver( $source, $args, $context, $info, 'shop_order_refund' );
2021-06-04 00:05:24 -04:00
2023-06-21 13:21:29 -04:00
$resolver->set_should_execute( true );
$resolver->set_query_arg( 'parent', $source->ID );
2021-06-04 00:05:24 -04:00
return $resolver->get_connection();
},
],
2021-06-04 00:05:24 -04:00
'shop_order_refund'
)
);
// From Customer To Refunds.
register_graphql_connection(
self::get_connection_config(
[
2021-06-04 00:05:24 -04:00
'fromType' => 'Customer',
'toType' => 'Refund',
'fromFieldName' => 'refunds',
'connectionArgs' => self::get_refund_connection_args(),
'resolve' => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
2023-06-21 13:21:29 -04:00
$resolver = new Order_Connection_Resolver( $source, $args, $context, $info, 'shop_order_refund' );
2021-06-04 00:05:24 -04:00
2023-06-21 13:21:29 -04:00
return self::get_customer_refund_connection( $resolver, $source );
2021-06-04 00:05:24 -04:00
},
],
2021-06-04 00:05:24 -04:00
'shop_order_refund'
)
);
2019-03-31 00:21:31 -04:00
}
/**
* Given an array of $args, this returns the connection config, merging the provided args
2020-02-27 21:30:21 -03:00
* with the defaults.
2019-03-31 00:21:31 -04:00
*
2021-06-04 19:58:37 -04:00
* @param array $args Connection configuration.
* @param string $post_type Connection resolving post-type.
2021-06-03 21:37:09 -04:00
*
2019-03-31 00:21:31 -04:00
* @return array
*/
public static function get_connection_config( $args = [], $post_type = 'shop_order' ): array {
2021-06-04 00:05:24 -04:00
// Get Post type object for use in connection resolve function.
2023-06-13 23:17:02 +03:00
/**
* Get connection post type.
*
* @var \WP_Post_Type $post_object
*/
2021-06-04 00:05:24 -04:00
$post_object = get_post_type_object( $post_type );
2020-02-27 21:30:21 -03:00
return array_merge(
[
2020-02-27 21:30:21 -03:00
'fromType' => 'RootQuery',
'toType' => 'Order',
'fromFieldName' => 'orders',
'connectionArgs' => self::get_connection_args( 'private' ),
'resolve' => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) use ( $post_object ) {
2021-06-04 00:05:24 -04:00
// Check if user shop manager.
$not_manager = ! current_user_can( $post_object->cap->edit_posts );
// Remove any where arguments that require querying user to have "shop manager" role.
if ( $not_manager && 'shop_order' === $post_object->name && isset( $args['where'] ) ) {
$args['where'] = \array_intersect_key( $args['where'], self::get_connection_args( 'public' ) );
}
2021-06-04 00:05:24 -04:00
// Initialize connection resolver.
2023-06-21 13:21:29 -04:00
$resolver = new Order_Connection_Resolver( $source, $args, $context, $info, $post_object->name );
2021-06-04 00:05:24 -04:00
/**
* If not shop manager, restrict results to orders/refunds owned by querying user
* and return the connection.
*/
2023-06-21 13:21:29 -04:00
if ( $not_manager ) {
return 'shop_order_refund' === $post_object->name
? self::get_customer_refund_connection( $resolver, new Customer( 'session', ! is_user_logged_in() ) )
: self::get_customer_order_connection( $resolver, new Customer( 'session', ! is_user_logged_in() ) );
2021-06-04 00:05:24 -04:00
}
2023-06-21 13:21:29 -04:00
return $resolver->get_connection();
2021-06-04 00:05:24 -04:00
},
],
2020-02-27 21:30:21 -03:00
$args
2019-03-31 00:21:31 -04:00
);
}
/**
2020-02-27 21:30:21 -03:00
* Returns array of where args.
*
2020-02-27 21:30:21 -03:00
* @param string $access Connection argument access-level.
2019-03-31 00:21:31 -04:00
* @return array
*/
2020-02-27 21:30:21 -03:00
public static function get_connection_args( $access = 'public' ): array {
switch ( $access ) {
case 'private':
return array_merge(
get_wc_cpt_connection_args(),
[
2023-07-31 15:28:39 -04:00
'statuses' => [
'type' => [ 'list_of' => 'OrderStatusEnum' ],
'description' => static function () {
return __( 'Limit result set to orders assigned a specific status.', 'graphql-for-ecommerce' );
},
],
2023-07-31 15:28:39 -04:00
'customerId' => [
'type' => 'Int',
'description' => static function () {
return __( 'Limit result set to orders assigned a specific customer.', 'graphql-for-ecommerce' );
},
],
2023-07-31 15:28:39 -04:00
'customersIn' => [
'type' => [ 'list_of' => 'Int' ],
'description' => static function () {
return __( 'Limit result set to orders assigned a specific group of customers.', 'graphql-for-ecommerce' );
},
],
2023-07-31 15:28:39 -04:00
'productId' => [
'type' => 'Int',
'description' => static function () {
return __( 'Limit result set to orders assigned a specific product.', 'graphql-for-ecommerce' );
},
],
2023-07-31 15:28:39 -04:00
'orderby' => [
'type' => [ 'list_of' => 'OrdersOrderbyInput' ],
'description' => static function () {
return __( 'What paramater to use to order the objects by.', 'graphql-for-ecommerce' );
},
],
2023-07-31 15:28:39 -04:00
'billingEmail' => [
'type' => 'String',
'description' => static function () {
return __( 'Limit result set to orders assigned a specific billing email.', 'graphql-for-ecommerce' );
},
2023-07-31 15:28:39 -04:00
],
]
);
case 'public':
default:
return array_merge(
get_wc_cpt_connection_args(),
[
'statuses' => [
'type' => [ 'list_of' => 'OrderStatusEnum' ],
'description' => static function () {
return __( 'Limit result set to orders assigned a specific status.', 'graphql-for-ecommerce' );
},
],
'productId' => [
'type' => 'Int',
'description' => static function () {
return __( 'Limit result set to orders assigned a specific product.', 'graphql-for-ecommerce' );
},
],
'orderby' => [
'type' => [ 'list_of' => 'OrdersOrderbyInput' ],
'description' => static function () {
return __( 'What paramater to use to order the objects by.', 'graphql-for-ecommerce' );
},
],
'search' => [
'type' => 'String',
'description' => static function () {
return __( 'Limit results to those matching a string.', 'graphql-for-ecommerce' );
},
],
'dateQuery' => [
'type' => 'DateQueryInput',
'description' => static function () {
return __( 'Filter the connection based on dates.', 'graphql-for-ecommerce' );
},
],
]
);
}//end switch
2019-03-31 00:21:31 -04:00
}
2021-06-04 00:05:24 -04:00
/**
* Returns array of where args.
*
* @return array
*/
public static function get_refund_connection_args(): array {
return array_merge(
get_wc_cpt_connection_args(),
[
'statuses' => [
'type' => [ 'list_of' => 'String' ],
'description' => static function () {
return __( 'Limit result set to refunds assigned a specific status.', 'graphql-for-ecommerce' );
},
],
'orderIn' => [
'type' => [ 'list_of' => 'Int' ],
'description' => static function () {
return __( 'Limit result set to refunds from a specific group of order IDs.', 'graphql-for-ecommerce' );
},
],
]
2021-06-04 00:05:24 -04:00
);
}
/**
* Returns order connection filter by customer.
*
* @param \WPGraphQL\WooCommerce\Data\Connection\Order_Connection_Resolver $resolver Connection resolver.
* @param \WPGraphQL\WooCommerce\Model\Customer $customer Customer object of querying user.
*
* @return array|\GraphQL\Deferred
*/
private static function get_customer_order_connection( $resolver, Customer $customer ) {
// If not "billing email" or "ID" set bail early by returning an empty connection.
if ( empty( $customer->billing['email'] ) && ( empty( absint( $customer->ID ) ) ) ) {
return [
'nodes' => [],
'edges' => [],
];
}
$target_customer_id = absint( $customer->ID );
$current_customer_id = absint( \WC()->customer->get_id() );
if ( ! empty( $target_customer_id ) ) {
$resolver->set_query_arg( 'customer_id', $target_customer_id );
$resolver->set_should_execute( $current_customer_id === $target_customer_id );
} elseif ( ! empty( $customer->billing['email'] ) ) {
$resolver->set_query_arg( 'billing_email', $customer->billing['email'] );
$resolver->set_should_execute( \WC()->customer->get_billing_email() === $customer->billing['email'] );
}
return $resolver->get_connection();
}
/**
* Returns refund connection filter by customer.
*
* @param \WPGraphQL\WooCommerce\Data\Connection\Order_Connection_Resolver $resolver Connection resolver.
* @param \WPGraphQL\WooCommerce\Model\Customer $customer Customer object of querying user.
*
* @return array|\GraphQL\Deferred
*/
private static function get_customer_refund_connection( Order_Connection_Resolver $resolver, Customer $customer ) {
$empty_results = [
'pageInfo' => null,
'nodes' => [],
'edges' => [],
];
// If not "billing email" or "ID" set bail early by returning an empty connection.
if ( empty( $customer->billing['email'] ) && empty( $customer->ID ) ) {
return $empty_results;
}
$order_ids = [];
$customer_id = $customer->ID;
$billing_email = $customer->billing['email'];
if ( ! empty( $customer_id ) ) {
$args = [
'customer_id' => $customer_id,
'return' => 'ids',
];
/** @var array<int> $order_ids_by_customer_id */
$order_ids_by_customer_id = wc_get_orders( $args );
if ( is_array( $order_ids_by_customer_id ) ) {
$order_ids = $order_ids_by_customer_id;
}
}
if ( ! empty( $billing_email ) ) {
$args = [
'billing_email' => $billing_email,
'return' => 'ids',
];
/** @var array<int> $order_ids_by_email */
$order_ids_by_email = wc_get_orders( $args );
// Merge the arrays of order IDs.
if ( is_array( $order_ids_by_email ) ) {
$order_ids = array_merge( $order_ids, $order_ids_by_email );
}
}
// If no orders found, return empty connection.
if ( empty( $order_ids ) ) {
return $empty_results;
}
// Remove duplicates.
$order_ids = array_unique( $order_ids );
// Set connection args.
$resolver->set_should_execute(
( 0 !== $customer_id && \WC()->customer->get_id() === $customer_id )
|| \WC()->customer->get_billing_email() === $billing_email
);
$resolver->set_query_arg( 'post_parent__in', array_map( 'absint', $order_ids ) );
// Execute and return connection.
return $resolver->get_connection();
}
2019-03-31 00:21:31 -04:00
}