2019-03-31 00:21:31 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Connection - Coupons
|
|
|
|
|
*
|
|
|
|
|
* Registers connections to Coupon
|
|
|
|
|
*
|
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
|
|
|
|
2021-05-19 19:45:02 -04:00
|
|
|
use WPGraphQL\Data\Connection\PostObjectConnectionResolver;
|
2019-03-31 00:21:31 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class - Coupons
|
|
|
|
|
*/
|
2019-09-29 15:11:02 -04:00
|
|
|
class Coupons {
|
2019-03-31 00:21:31 -04:00
|
|
|
/**
|
|
|
|
|
* Registers the various connections from other Types to Coupon
|
2023-06-13 23:17:02 +03:00
|
|
|
*
|
|
|
|
|
* @return void
|
2019-03-31 00:21:31 -04:00
|
|
|
*/
|
|
|
|
|
public static function register_connections() {
|
|
|
|
|
// From RootQuery.
|
|
|
|
|
register_graphql_connection( self::get_connection_config() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
*
|
|
|
|
|
* @param array $args - Connection configuration.
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2022-08-26 14:53:36 -04:00
|
|
|
public static function get_connection_config( $args = [] ): array {
|
2020-02-27 21:30:21 -03:00
|
|
|
return array_merge(
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
2020-02-27 21:30:21 -03:00
|
|
|
'fromType' => 'RootQuery',
|
|
|
|
|
'toType' => 'Coupon',
|
|
|
|
|
'fromFieldName' => 'coupons',
|
|
|
|
|
'connectionArgs' => self::get_connection_args(),
|
2023-07-19 22:56:42 +03:00
|
|
|
'resolve' => static function ( $source, $args, $context, $info ) {
|
2021-05-19 19:45:02 -04:00
|
|
|
$resolver = new PostObjectConnectionResolver( $source, $args, $context, $info, 'shop_coupon' );
|
|
|
|
|
|
|
|
|
|
if ( ! self::should_execute() ) {
|
2022-12-07 15:04:39 -07:00
|
|
|
return [
|
|
|
|
|
'edges' => [],
|
|
|
|
|
'nodes' => [],
|
|
|
|
|
];
|
2021-05-19 19:45:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $resolver->get_connection();
|
2020-02-27 21:30:21 -03:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
2020-02-27 21:30:21 -03:00
|
|
|
$args
|
2019-03-31 00:21:31 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-19 19:45:02 -04:00
|
|
|
/**
|
|
|
|
|
* Confirms the uses has the privileges to query Coupons
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public static function should_execute() {
|
2023-06-13 23:17:02 +03:00
|
|
|
/**
|
|
|
|
|
* Get coupon post type.
|
|
|
|
|
*
|
|
|
|
|
* @var \WP_Post_Type $post_type_obj
|
|
|
|
|
*/
|
2021-05-19 19:45:02 -04:00
|
|
|
$post_type_obj = get_post_type_object( 'shop_coupon' );
|
|
|
|
|
switch ( true ) {
|
|
|
|
|
case current_user_can( $post_type_obj->cap->edit_posts ):
|
|
|
|
|
return true;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-31 00:21:31 -04:00
|
|
|
/**
|
2020-02-27 21:30:21 -03:00
|
|
|
* Returns array of where args.
|
2019-03-31 00:21:31 -04:00
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2020-02-27 21:30:21 -03:00
|
|
|
public static function get_connection_args(): array {
|
2019-04-10 12:26:37 -04:00
|
|
|
return array_merge(
|
2020-03-27 12:18:06 -04:00
|
|
|
get_wc_cpt_connection_args(),
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
|
|
|
|
'code' => [
|
2019-04-10 12:26:37 -04:00
|
|
|
'type' => 'String',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Limit result set to resources with a specific code.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
]
|
2019-04-09 20:42:51 -04:00
|
|
|
);
|
2019-03-31 00:21:31 -04:00
|
|
|
}
|
2021-05-19 19:45:02 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This allows plugins/themes to hook in and alter what $args should be allowed to be passed
|
|
|
|
|
* from a GraphQL Query to the WP_Query
|
|
|
|
|
*
|
2023-07-19 23:01:50 +03:00
|
|
|
* @param array $query_args The mapped query arguments.
|
|
|
|
|
* @param array $where_args Query "where" args.
|
|
|
|
|
* @param mixed $source The query results for a query calling this.
|
|
|
|
|
* @param array $args All of the arguments for the query (not just the "where" args).
|
|
|
|
|
* @param \WPGraphQL\AppContext $context The AppContext object.
|
|
|
|
|
* @param \GraphQL\Type\Definition\ResolveInfo $info The ResolveInfo object.
|
|
|
|
|
* @param mixed|string|array $post_type The post type for the query.
|
2021-05-19 19:45:02 -04:00
|
|
|
*
|
|
|
|
|
* @return array Query arguments.
|
|
|
|
|
*/
|
|
|
|
|
public static function map_input_fields_to_wp_query( $query_args, $where_args, $source, $args, $context, $info, $post_type ) {
|
2021-06-03 21:37:09 -04:00
|
|
|
$not_coupon_query = is_string( $post_type )
|
|
|
|
|
? 'shop_coupon' !== $post_type
|
|
|
|
|
: ! in_array( 'shop_coupon', $post_type, true );
|
|
|
|
|
if ( $not_coupon_query ) {
|
2021-05-19 19:45:02 -04:00
|
|
|
return $query_args;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$query_args = array_merge(
|
|
|
|
|
$query_args,
|
2021-06-04 00:05:24 -04:00
|
|
|
map_shared_input_fields_to_wp_query( $where_args )
|
2021-05-19 19:45:02 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ( ! empty( $where_args['code'] ) ) {
|
2021-06-03 21:37:09 -04:00
|
|
|
$id = \wc_get_coupon_id_by_code( $where_args['code'] );
|
2022-08-26 14:53:36 -04:00
|
|
|
$ids = $id ? [ $id ] : [ '0' ];
|
2021-06-03 21:37:09 -04:00
|
|
|
$query_args['post__in'] = ! empty( $query_args['post__in'] )
|
2021-05-19 19:45:02 -04:00
|
|
|
? array_intersect( $ids, $query_args['post__in'] )
|
|
|
|
|
: $ids;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Filter the input fields
|
|
|
|
|
* This allows plugins/themes to hook in and alter what $args should be allowed to be passed
|
|
|
|
|
* from a GraphQL Query to the WP_Query
|
|
|
|
|
*
|
|
|
|
|
* @param array $args The mapped query arguments
|
|
|
|
|
* @param array $where_args Query "where" args
|
|
|
|
|
* @param mixed $source The query results for a query calling this
|
|
|
|
|
* @param array $all_args All of the arguments for the query (not just the "where" args)
|
2023-07-19 23:01:50 +03:00
|
|
|
* @param \WPGraphQL\AppContext $context The AppContext object
|
|
|
|
|
* @param \GraphQL\Type\Definition\ResolveInfo $info The ResolveInfo object
|
2021-05-19 19:45:02 -04:00
|
|
|
*/
|
|
|
|
|
$query_args = apply_filters(
|
|
|
|
|
'graphql_map_input_fields_to_coupon_query',
|
|
|
|
|
$query_args,
|
|
|
|
|
$where_args,
|
|
|
|
|
$source,
|
|
|
|
|
$args,
|
|
|
|
|
$context,
|
|
|
|
|
$info
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return $query_args;
|
|
|
|
|
}
|
2019-03-31 00:21:31 -04:00
|
|
|
}
|