| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPGraphQL\Type\Union; |
| 4 |
|
| 5 |
use WPGraphQL\Registry\TypeRegistry; |
| 6 |
|
| 7 |
/** |
| 8 |
* @todo Remove in 3.0.0 |
| 9 |
* @deprecated 1.14.1 |
| 10 |
* @codeCoverageIgnore |
| 11 |
*/ |
| 12 |
class PostObjectUnion { |
| 13 |
|
| 14 |
/** |
| 15 |
* Registers the Type |
| 16 |
* |
| 17 |
* @param ?\WPGraphQL\Registry\TypeRegistry $type_registry |
| 18 |
* |
| 19 |
* @throws \Exception |
| 20 |
*/ |
| 21 |
public static function register_type( ?TypeRegistry $type_registry = null ): void { |
| 22 |
register_graphql_union_type( |
| 23 |
'PostObjectUnion', |
| 24 |
[ |
| 25 |
'name' => 'PostObjectUnion', |
| 26 |
'typeNames' => self::get_possible_types(), |
| 27 |
'description' => static function () { |
| 28 |
return __( 'Union between the post, page and media item types', 'wp-graphql' ); |
| 29 |
}, |
| 30 |
'resolveType' => static function ( $value ) { |
| 31 |
_doing_it_wrong( |
| 32 |
self::class, |
| 33 |
esc_attr__( 'The PostObjectUnion GraphQL type is deprecated. Use the ContentNode interface instead.', 'wp-graphql' ), |
| 34 |
'1.14.1' |
| 35 |
); |
| 36 |
|
| 37 |
$type = null; |
| 38 |
if ( isset( $value->post_type ) ) { |
| 39 |
$post_type_object = get_post_type_object( $value->post_type ); |
| 40 |
if ( isset( $post_type_object->graphql_single_name ) ) { |
| 41 |
$type = \WPGraphQL::get_type_registry()->get_type( $post_type_object->graphql_single_name ); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
return ! empty( $type ) ? $type : null; |
| 46 |
}, |
| 47 |
] |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Returns a list of possible types for the union |
| 53 |
* |
| 54 |
* @return string[] |
| 55 |
*/ |
| 56 |
public static function get_possible_types() { |
| 57 |
$possible_types = []; |
| 58 |
$allowed_post_types = \WPGraphQL::get_allowed_post_types( 'objects', [ 'graphql_kind' => 'object' ] ); |
| 59 |
|
| 60 |
foreach ( $allowed_post_types as $post_type_object ) { |
| 61 |
if ( empty( $possible_types[ $post_type_object->name ] ) && isset( $post_type_object->graphql_single_name ) ) { |
| 62 |
$possible_types[ $post_type_object->name ] = $post_type_object->graphql_single_name; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
return $possible_types; |
| 67 |
} |
| 68 |
} |
| 69 |
|