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