Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Coupon_Connection_Resolver" deprecated. #497

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
"stripe/stripe-php": "^7.75",
"wp-coding-standards/wpcs": "^2.3",
"wp-graphql/wp-graphql-jwt-authentication": "^0.4.1",
"wp-graphql/wp-graphql-testcase": "^1.0.1",
"wp-graphql/wp-graphql-testcase": "^1.1.1",
"wpackagist-plugin/woocommerce": "^5.1.0",
"wpackagist-plugin/woocommerce-gateway-stripe": "^4.5",
"wpackagist-plugin/wp-graphql": "^1.2.6",
"wpackagist-plugin/wp-graphql": "^1.3.8",
"wpackagist-theme/twentytwentyone": "^1.0"
},
"config": {
Expand Down
90 changes: 89 additions & 1 deletion includes/connection/class-coupons.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

namespace WPGraphQL\WooCommerce\Connection;

use GraphQL\Error\UserError;
use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;
use WPGraphQL\WooCommerce\Data\Factory;
use WPGraphQL\Data\Connection\PostObjectConnectionResolver;

/**
* Class - Coupons
Expand All @@ -22,6 +24,13 @@ class Coupons {
* Registers the various connections from other Types to Coupon
*/
public static function register_connections() {
add_filter(
'graphql_map_input_fields_to_wp_query',
array( __CLASS__, 'map_input_fields_to_wp_query' ),
10,
7
);

// From RootQuery.
register_graphql_connection( self::get_connection_config() );
}
Expand All @@ -41,13 +50,34 @@ public static function get_connection_config( $args = array() ): array {
'fromFieldName' => 'coupons',
'connectionArgs' => self::get_connection_args(),
'resolve' => function ( $source, $args, $context, $info ) {
return Factory::resolve_coupon_connection( $source, $args, $context, $info );
$resolver = new PostObjectConnectionResolver( $source, $args, $context, $info, 'shop_coupon' );

if ( ! self::should_execute() ) {
throw new UserError( __( 'Not authorized to execute this query', 'wp-graphql-woocommerce' ) );
}

return $resolver->get_connection();
},
),
$args
);
}

/**
* Confirms the uses has the privileges to query Coupons
*
* @return bool
*/
public static function should_execute() {
$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;
}
}

/**
* Returns array of where args.
*
Expand All @@ -64,4 +94,62 @@ public static function get_connection_args(): array {
)
);
}

/**
* 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 $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 AppContext $context The AppContext object.
* @param ResolveInfo $info The ResolveInfo object.
* @param mixed|string|array $post_type The post type for the query.
*
* @return array Query arguments.
*/
public static function map_input_fields_to_wp_query( $query_args, $where_args, $source, $args, $context, $info, $post_type ) {
if ( ! in_array( 'shop_coupon', $post_type, true ) ) {
return $query_args;
}

$query_args = array_merge(
$query_args,
map_shared_input_fields_to_wp_query( $where_args ),
);

if ( ! empty( $where_args['code'] ) ) {
$id = \wc_get_coupon_id_by_code( $where_args['code'] );
$ids = $id ? array( $id ) : array( '0' );
$query_args['post__in'] = isset( $query_args['post__in'] )
? 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 AppContext $context The AppContext object
* @param ResolveInfo $info The ResolveInfo object
* @param mixed|string|array $post_type The post type for the query
*/
$query_args = apply_filters(
'graphql_map_input_fields_to_coupon_query',
$query_args,
$where_args,
$source,
$args,
$context,
$info
);

return $query_args;
}
}
72 changes: 72 additions & 0 deletions includes/connection/wc-cpt-connection-args.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,75 @@ function get_wc_cpt_connection_args(): array {
);
}

/**
* Sanitizes common post-type connection query input.
*
* @param array $input Input to be sanitize.
*
* @return array
*/
function map_shared_input_fields_to_wp_query( array $input, $ordering_meta = array() ) {
$args = array();
if ( ! empty( $input['include'] ) ) {
$args['post__in'] = $input['include'];
}

if ( ! empty( $input['exclude'] ) ) {
$args['post__not_in'] = $input['exclude'];
}

if ( ! empty( $input['parent'] ) ) {
$args['post_parent'] = $input['parent'];
}

if ( ! empty( $input['parentIn'] ) ) {
if ( ! isset( $args['post_parent__in'] ) ) {
$args['post_parent__in'] = array();
}
$args['post_parent__in'] = array_merge( $args['post_parent__in'], $input['parentIn'] );
}

if ( ! empty( $input['parentNotIn'] ) ) {
$args['post_parent__not_in'] = $input['parentNotIn'];
}

if ( ! empty( $input['search'] ) ) {
$args['s'] = $input['search'];
}

/**
* Map the orderby inputArgs to the WP_Query
*/
if ( ! empty( $input['orderby'] ) && is_array( $input['orderby'] ) ) {
$args['orderby'] = array();
foreach ( $input['orderby'] as $orderby_input ) {
/**
* These orderby options should not include the order parameter.
*/
if ( in_array(
$orderby_input['field'],
array( 'post__in', 'post_name__in', 'post_parent__in' ),
true
) ) {
$args['orderby'] = esc_sql( $orderby_input['field'] );

// Handle meta fields.
} elseif ( in_array( $orderby_input['field'], $ordering_meta, true ) ) {
$args['orderby']['meta_value_num'] = $orderby_input['order'];
$args['meta_key'] = esc_sql( $orderby_input['field'] ); // WPCS: slow query ok.

// Handle post object fields.
} elseif ( ! empty( $orderby_input['field'] ) ) {
$args['orderby'][ esc_sql( $orderby_input['field'] ) ] = esc_sql( $orderby_input['order'] );
}
}
}

if ( ! empty( $input['dateQuery'] ) ) {
$args['date_query'] = $input['dateQuery'];
}

return $args;
}


2 changes: 2 additions & 0 deletions includes/data/connection/class-coupon-connection-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

/**
* Class Coupon_Connection_Resolver
*
* @deprecated v0.10.0
*/
class Coupon_Connection_Resolver extends AbstractConnectionResolver {
/**
Expand Down
17 changes: 17 additions & 0 deletions tests/_support/TestCase/WooGraphQLTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,21 @@ protected function logout() {
wp_set_current_user( 0 );
}

/**
* The death of `! empty( $v ) ? apply_filters( $v ) : null;`
*
* @param array|mixed $possible Variable whose existence has to be verified, or
* an array containing the variable followed by a decorated value to be returned.
* @param mixed $default Default value to be returned if $possible doesn't exist.
*
* @return mixed
*/
protected function maybe( $possible, $default = null ) {
if ( is_array( $possible ) && 2 === count( $possible ) ) {
list( $possible, $decorated ) = $possible;
} else {
$decorated = $possible;
}
return ! empty( $possible ) ? $decorated : $default;
}
}
Loading