HEX
Server: Apache/2.4.65 (Debian)
System: Linux d8108ab83977 5.15.0-124-generic #134-Ubuntu SMP Fri Sep 27 20:20:17 UTC 2024 x86_64
User: www-data (33)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: /var/www/html/wp-content/plugins/wp-graphql/deprecated/MenuItemObjectUnion.php
<?php

namespace WPGraphQL\Type\Union;

use WPGraphQL\Model\Post;
use WPGraphQL\Model\Term;
use WPGraphQL\Registry\TypeRegistry;

/**
 * Class MenuItemObjectUnion
 *
 * @todo Remove in 3.0.0
 * @deprecated 0.10.3
 * @codeCoverageIgnore
 */
class MenuItemObjectUnion {

	/**
	 * Registers the Type
	 *
	 * @param ?\WPGraphQL\Registry\TypeRegistry $type_registry
	 *
	 * @return void
	 * @throws \Exception
	 */
	public static function register_type( ?TypeRegistry $type_registry = null ) {
		register_graphql_union_type(
			'MenuItemObjectUnion',
			[
				'typeNames'   => self::get_possible_types(),
				'description' => static function () {
					return __( 'Deprecated in favor of MenuItemLinkable Interface', 'wp-graphql' );
				},
				'resolveType' => static function ( $obj ) {
					_doing_it_wrong(
						self::class,
						esc_attr__( 'The MenuItemObjectUnion GraphQL type is deprecated in favor of MenuItemLinkable Interface and will be removed in a future version of WPGraphQL', 'wp-graphql' ),
						'0.10.3'
					);
					$type_registry = \WPGraphQL::get_type_registry();
					// Post object
					if ( $obj instanceof Post && isset( $obj->post_type ) && ! empty( $obj->post_type ) ) {
						/** @var \WP_Post_Type $post_type_object */
						$post_type_object = get_post_type_object( $obj->post_type );

						return $type_registry->get_type( $post_type_object->graphql_single_name );
					}

					// Taxonomy term
					if ( $obj instanceof Term && ! empty( $obj->taxonomyName ) ) {
						/** @var \WP_Taxonomy $tax_object */
						$tax_object = get_taxonomy( $obj->taxonomyName );

						return $type_registry->get_type( $tax_object->graphql_single_name );
					}

					return $obj;
				},
			]
		);
	}

	/**
	 * Returns a list of possible types for the union
	 *
	 * @return string[]
	 */
	public static function get_possible_types() {

		/**
		 * The possible types for MenuItems should be just the TermObjects and PostTypeObjects that are
		 * registered to "show_in_graphql" and "show_in_nav_menus"
		 */
		$args = [
			'show_in_nav_menus' => true,
			'graphql_kind'      => 'object',
		];

		$possible_types = [];

		/**
		 * Add post types that are allowed in WPGraphQL.
		 */
		foreach ( \WPGraphQL::get_allowed_post_types( 'objects', $args ) as $post_type_object ) {
			if ( isset( $post_type_object->graphql_single_name ) ) {
				$possible_types[] = $post_type_object->graphql_single_name;
			}
		}

		// Add taxonomies that are allowed in WPGraphQL.
		foreach ( \WPGraphQL::get_allowed_taxonomies( 'objects', $args ) as $tax_object ) {
			if ( isset( $tax_object->graphql_single_name ) ) {
				$possible_types[] = $tax_object->graphql_single_name;
			}
		}

		return $possible_types;
	}
}