Files

49 lines
1.4 KiB
PHP
Raw Permalink Normal View History

2019-03-31 23:16:11 -04:00
<?php
/**
* Connection type - Posts
*
* Registers connections to Posts
*
2019-10-25 19:13:36 -04:00
* @package WPGraphQL\WooCommerce\Connection
2019-03-31 23:16:11 -04:00
*/
2019-10-25 19:13:36 -04:00
namespace WPGraphQL\WooCommerce\Connection;
2019-03-31 23:16:11 -04:00
2020-06-26 15:51:55 -04:00
use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;
use WPGraphQL\Data\Connection\PostObjectConnectionResolver;
use WPGraphQL\Type\Connection\PostObjects;
2019-03-31 23:16:11 -04:00
/**
* Class - Posts
*/
class Posts extends PostObjects {
/**
2020-02-27 21:30:21 -03:00
* Registers the various connections from other WooCommerce Types to other WordPress post-types.
2019-03-31 23:16:11 -04:00
*/
public static function register_connections() {
2019-10-24 22:11:04 -04:00
register_graphql_connection(
self::get_connection_config(
get_post_type_object( 'attachment' ),
[
2019-10-24 22:11:04 -04:00
'fromType' => 'Product',
'toType' => 'MediaItem',
'fromFieldName' => 'galleryImages',
'resolve' => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
2020-06-26 15:51:55 -04:00
$resolver = new PostObjectConnectionResolver( $source, $args, $context, $info, 'attachment' );
$resolver->set_query_arg( 'post_type', 'attachment' );
$resolver->set_query_arg( 'post__in', $source->gallery_image_ids );
2021-01-21 22:41:43 -05:00
// Change default ordering.
if ( ! in_array( 'orderby', array_keys( $resolver->get_query_args() ), true ) ) {
2020-06-26 15:51:55 -04:00
$resolver->set_query_arg( 'orderby', 'post__in' );
}
return $resolver->get_connection();
},
]
2019-10-24 22:11:04 -04:00
)
);
2019-03-31 23:16:11 -04:00
}
}