Files

158 lines
4.4 KiB
PHP
Raw Permalink Normal View History

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
*/
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
*/
public static function get_connection_config( $args = [] ): array {
2020-02-27 21:30:21 -03:00
return array_merge(
[
2020-02-27 21:30:21 -03:00
'fromType' => 'RootQuery',
'toType' => 'Coupon',
'fromFieldName' => 'coupons',
'connectionArgs' => self::get_connection_args(),
'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() ) {
return [
'edges' => [],
'nodes' => [],
];
2021-05-19 19:45:02 -04:00
}
return $resolver->get_connection();
2020-02-27 21:30:21 -03: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(
get_wc_cpt_connection_args(),
[
'code' => [
2019-04-10 12:26:37 -04:00
'type' => 'String',
'description' => static function () {
return __( 'Limit result set to resources with a specific code.', 'graphql-for-ecommerce' );
},
],
]
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
*
* @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'] );
$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)
* @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
}