1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
<?php
namespace WPGraphQL\Type\PostType;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQLRelay\Relay;
use WPGraphQL\AppContext;
use WPGraphQL\Type\WPObjectType;
use WPGraphQL\Types;
class PostTypeType extends WPObjectType {
private static $type_name;
private static $fields;
private static $labels_details;
public function __construct() {
self::$type_name = 'PostType';
$config = [
'name' => self::$type_name,
'description' => __( 'An Post Type object', 'wp-graphql' ),
'fields' => self::fields(),
'interfaces' => [ self::node_interface() ],
];
parent::__construct( $config );
}
private static function fields() {
if ( null === self::$fields ) :
$allowed_taxonomies = \WPGraphQL::$allowed_taxonomies;
self::$fields = function() use ( $allowed_taxonomies ) {
$fields = [
'id' => [
'type' => Types::non_null( Types::id() ),
'resolve' => function( \WP_Post_Type $post_type, $args, AppContext $context, ResolveInfo $info ) {
return ( ! empty( $post_type->name ) && ! empty( $post_type->name ) ) ? Relay::toGlobalId( 'postType', $post_type->name ) : null;
},
],
'name' => [
'type' => Types::string(),
'description' => __( 'The internal name of the post type. This should not be used for display purposes.', 'wp-graphql' ),
],
'label' => [
'type' => Types::string(),
'description' => __( 'Display name of the content type.', 'wp-graphql' ),
],
'labels' => [
'type' => self::labels_details(),
'description' => __( 'Details about the post type labels.', 'wp-graphql' ),
'resolve' => function( \WP_Post_Type $post_type, $args, $context, ResolveInfo $info ) {
return get_post_type_labels( $post_type );
},
],
'description' => [
'type' => Types::string(),
'description' => __( 'Description of the content type.', 'wp-graphql' ),
],
'public' => [
'type' => Types::boolean(),
'description' => __( 'Whether a post type is intended for use publicly either via the admin interface or by front-end users. While the default settings of exclude_from_search, publicly_queryable, show_ui, and show_in_nav_menus are inherited from public, each does not rely on this relationship and controls a very specific intention.', 'wp-graphql' ),
],
'hierarchical' => [
'type' => Types::boolean(),
'description' => __( 'Whether the post type is hierarchical, for example pages.', 'wp-graphql' ),
],
'excludeFromSearch' => [
'type' => Types::boolean(),
'description' => __( 'Whether to exclude posts with this post type from front end search results.', 'wp-graphql' ),
'resolve' => function( \WP_Post_Type $post_type, $args, AppContext $context, ResolveInfo $info ) {
return ( true === $post_type->exclude_from_search ) ? true : false;
},
],
'publiclyQueryable' => [
'type' => Types::boolean(),
'description' => __( 'Whether queries can be performed on the front end for the post type as part of parse_request().', 'wp-graphql' ),
'resolve' => function( \WP_Post_Type $post_type, array $args, AppContext $context, ResolveInfo $info ) {
return ( true === $post_type->publicly_queryable ) ? true : false;
},
],
'showUi' => [
'type' => Types::boolean(),
'description' => __( 'Whether to generate and allow a UI for managing this post type in the admin.', 'wp-graphql' ),
'resolve' => function( \WP_Post_Type $post_type, array $args, AppContext $context, ResolveInfo $info ) {
return ( true === $post_type->show_ui ) ? true : false;
},
],
'showInMenu' => [
'type' => Types::boolean(),
'description' => __( 'Where to show the post type in the admin menu. To work, $show_ui must be true. If true, the post type is shown in its own top level menu. If false, no menu is shown. If a string of an existing top level menu (eg. "tools.php" or "edit.php?post_type=page"), the post type will be placed as a sub-menu of that.', 'wp-graphql' ),
'resolve' => function( \WP_Post_Type $post_type, array $args, AppContext $context, ResolveInfo $info ) {
return ( true === $post_type->show_in_menu ) ? true : false;
},
],
'showInNavMenus' => [
'type' => Types::boolean(),
'description' => __( 'Makes this post type available for selection in navigation menus.', 'wp-graphql' ),
'resolve' => function( \WP_Post_Type $post_type, array $args, AppContext $context, ResolveInfo $info ) {
return ( true === $post_type->show_in_nav_menus ) ? true : false;
},
],
'showInAdminBar' => [
'type' => Types::boolean(),
'description' => __( 'Makes this post type available via the admin bar.', 'wp-graphql' ),
'resolve' => function( \WP_Post_Type $post_type, array $args, AppContext $context, ResolveInfo $info ) {
return empty( true === $post_type->show_in_admin_bar ) ? true : false;
},
],
'menuPosition' => [
'type' => Types::int(),
'description' => __( 'The position of this post type in the menu. Only applies if show_in_menu is true.', 'wp-graphql' ),
'resolve' => function( \WP_Post_Type $post_type, array $args, AppContext $context, ResolveInfo $info ) {
return ! empty( $post_type->menu_position ) ? $post_type->menu_position : null;
},
],
'menuIcon' => [
'type' => Types::string(),
'description' => __( 'The name of the icon file to display as a menu icon.', 'wp-graphql' ),
'resolve' => function( \WP_Post_Type $post_type, array $args, AppContext $context, ResolveInfo $info ) {
return ! empty( $post_type->menu_icon ) ? $post_type->menu_icon : null;
},
],
'hasArchive' => [
'type' => Types::boolean(),
'description' => __( 'Whether this content type should have archives. Content archives are generated by type and by date.', 'wp-graphql' ),
'resolve' => function( \WP_Post_Type $post_type, array $args, AppContext $context, ResolveInfo $info ) {
return ( true === $post_type->has_archive ) ? true : false;
},
],
'canExport' => [
'type' => Types::boolean(),
'description' => __( 'Whether this content type should can be exported.', 'wp-graphql' ),
'resolve' => function( \WP_Post_Type $post_type, array $args, AppContext $context, ResolveInfo $info ) {
return ( true === $post_type->can_export ) ? true : false;
},
],
'deleteWithUser' => [
'type' => Types::boolean(),
'description' => __( 'Whether delete this type of content when the author of it is deleted from the system.', 'wp-graphql' ),
'resolve' => function( \WP_Post_Type $post_type, array $args, AppContext $context, ResolveInfo $info ) {
return ( true === $post_type->delete_with_user ) ? true : false;
},
],
'showInRest' => [
'type' => Types::boolean(),
'description' => __( 'Whether to add the post type route in the REST API `wp/v2` namespace.', 'wp-graphql' ),
'resolve' => function( \WP_Post_Type $post_type, array $args, AppContext $context, ResolveInfo $info ) {
return ( true === $post_type->show_in_rest ) ? true : false;
},
],
'restBase' => [
'type' => Types::string(),
'description' => __( 'Name of content type to diplay in REST API `wp/v2` namespace.', 'wp-graphql' ),
'resolve' => function( \WP_Post_Type $post_type, array $args, AppContext $context, ResolveInfo $info ) {
return ! empty( $post_type->rest_base ) ? $post_type->rest_base : null;
},
],
'restControllerClass' => [
'type' => Types::string(),
'description' => __( 'The REST Controller class assigned to handling this content type.', 'wp-graphql' ),
'resolve' => function( \WP_Post_Type $post_type, array $args, AppContext $context, ResolveInfo $info ) {
return ! empty( $post_type->rest_controller_class ) ? $post_type->rest_controller_class : null;
},
],
'showInGraphql' => [
'type' => Types::boolean(),
'description' => __( 'Whether to add the post type to the GraphQL Schema.', 'wp-graphql' ),
'resolve' => function( \WP_Post_Type $post_type, array $args, AppContext $context, ResolveInfo $info ) {
return ( true === $post_type->show_in_graphql ) ? true : false;
},
],
'graphqlSingleName' => [
'type' => Types::string(),
'description' => __( 'The singular name of the post type within the GraphQL Schema.', 'wp-graphql' ),
'resolve' => function( \WP_Post_Type $post_type, array $args, AppContext $context, ResolveInfo $info ) {
return ! empty( $post_type->graphql_single_name ) ? $post_type->graphql_single_name : null;
},
],
'graphqlPluralName' => [
'type' => Types::string(),
'description' => __( 'The plural name of the post type within the GraphQL Schema.', 'wp-graphql' ),
'resolve' => function( \WP_Post_Type $post_type, array $args, AppContext $context, ResolveInfo $info ) {
return ! empty( $post_type->graphql_plural_name ) ? $post_type->graphql_plural_name : null;
},
],
'connectedTaxonomyNames' => [
'type' => Types::list_of( Types::string() ),
'args' => [
'taxonomies' => [
'type' => Types::list_of( Types::taxonomy_enum() ),
'description' => __( 'Select which taxonomies to limit the results to', 'wp-graphql' ),
],
],
'description' => __( 'A list of Taxonomies associated with the post type', 'wp-graphql' ),
'resolve' => function( \WP_Post_Type $post_type_object, array $args, $context, ResolveInfo $info ) use ( $allowed_taxonomies ) {
$object_taxonomies = get_object_taxonomies( $post_type_object->name );
$taxonomy_names = [];
$allowed_taxonomies = ! empty( $args['taxonomies'] ) && is_array( $args['taxonomies'] ) ? $args['taxonomies'] : $allowed_taxonomies;
if ( ! empty( $object_taxonomies ) && is_array( $object_taxonomies ) ) {
foreach ( $object_taxonomies as $taxonomy ) {
if ( in_array( $taxonomy, $allowed_taxonomies, true ) ) {
$taxonomy_names[] = $taxonomy;
}
}
}
return ! empty( $taxonomy_names ) ? $taxonomy_names : null;
},
],
'connectedTaxonomies' => [
'type' => Types::list_of( Types::taxonomy() ),
'args' => [
'taxonomies' => [
'type' => Types::list_of( Types::taxonomy_enum() ),
'description' => __( 'Select which taxonomies to limit the results to', 'wp-graphql' ),
],
],
'description' => __( 'List of Taxonomies connected to the Post Type', 'wp-graphql' ),
'resolve' => function( \WP_Post_Type $post_type_object, array $args, AppContext $context, ResolveInfo $info ) use ( $allowed_taxonomies ) {
$tax_objects = [];
$allowed_taxonomies = ! empty( $args['taxonomies'] ) && is_array( $args['taxonomies'] ) ? $args['taxonomies'] : $allowed_taxonomies;
if ( ! empty( $allowed_taxonomies ) && is_array( $allowed_taxonomies ) ) {
foreach ( $allowed_taxonomies as $taxonomy ) {
if ( in_array( $taxonomy, get_object_taxonomies( $post_type_object->name ), true ) ) {
$tax_object = get_taxonomy( $taxonomy );
$tax_objects[ $tax_object->graphql_single_name ] = $tax_object;
}
}
}
return ! empty( $tax_objects ) ? $tax_objects : null;
},
],
];
return self::prepare_fields( $fields, self::$type_name );
};
endif;
return self::$fields;
}
private static function labels_details() {
if ( null === self::$labels_details ) {
self::$labels_details = new WPObjectType( [
'name' => 'LabelsDetails',
'fields' => function() {
$fields = [
'name' => [
'type' => Types::string(),
'description' => __( 'General name for the post type, usually plural.', 'wp-graphql' ),
],
'singularName' => [
'type' => Types::string(),
'description' => __( 'Name for one object of this post type.', 'wp-graphql' ),
'resolve' => function( $labels ) {
return ! empty( $labels->singular_name ) ? $labels->singular_name : null;
},
],
'addNew' => [
'type' => Types::string(),
'description' => __( 'Default is ‘Add New’ for both hierarchical and non-hierarchical types.', 'wp-graphql' ),
'resolve' => function( $labels ) {
return ! empty( $labels->add_new ) ? $labels->add_new : null;
},
],
'addNewItem' => [
'type' => Types::string(),
'description' => __( 'Label for adding a new singular item.', 'wp-graphql' ),
'resolve' => function( $labels ) {
return ! empty( $labels->add_new_item ) ? $labels->add_new_item : null;
},
],
'editItem' => [
'type' => Types::string(),
'description' => __( 'Label for editing a singular item.', 'wp-graphql' ),
'resolve' => function( $labels ) {
return ! empty( $labels->edit_item ) ? $labels->edit_item : null;
},
],
'newItem' => [
'type' => Types::string(),
'description' => __( 'Label for the new item page title.', 'wp-graphql' ),
'resolve' => function( $labels ) {
return ! empty( $labels->new_item ) ? $labels->new_item : null;
},
],
'viewItem' => [
'type' => Types::string(),
'description' => __( 'Label for viewing a singular item.', 'wp-graphql' ),
'resolve' => function( $labels ) {
return ! empty( $labels->view_item ) ? $labels->view_item : null;
},
],
'viewItems' => [
'type' => Types::string(),
'description' => __( 'Label for viewing post type archives.', 'wp-graphql' ),
'resolve' => function( $labels ) {
return ! empty( $labels->view_items ) ? $labels->view_items : null;
},
],
'searchItems' => [
'type' => Types::string(),
'description' => __( 'Label for searching plural items.', 'wp-graphql' ),
'resolve' => function( $labels ) {
return ! empty( $labels->search_items ) ? $labels->search_items : null;
},
],
'notFound' => [
'type' => Types::string(),
'description' => __( 'Label used when no items are found.', 'wp-graphql' ),
'resolve' => function( $labels ) {
return ! empty( $labels->not_found ) ? $labels->not_found : null;
},
],
'notFoundInTrash' => [
'type' => Types::string(),
'description' => __( 'Label used when no items are in the trash.', 'wp-graphql' ),
'resolve' => function( $labels ) {
return ! empty( $labels->not_found_in_trash ) ? $labels->not_found_in_trash : null;
},
],
'parentItemColon' => [
'type' => Types::string(),
'description' => __( 'Label used to prefix parents of hierarchical items.', 'wp-graphql' ),
'resolve' => function( $labels ) {
return ! empty( $labels->parent_item_colon ) ? $labels->parent_item_colon : null;
},
],
'allItems' => [
'type' => Types::string(),
'description' => __( 'Label to signify all items in a submenu link.', 'wp-graphql' ),
'resolve' => function( $labels ) {
return ! empty( $labels->all_items ) ? $labels->all_items : null;
},
],
'archives' => [
'type' => Types::string(),
'description' => __( 'Label for archives in nav menus', 'wp-graphql' ),
],
'attributes' => [
'type' => Types::string(),
'description' => __( 'Label for the attributes meta box.', 'wp-graphql' ),
],
'insertIntoItem' => [
'type' => Types::string(),
'description' => __( 'Label for the media frame button.', 'wp-graphql' ),
'resolve' => function( $labels ) {
return ! empty( $labels->insert_into_item ) ? $labels->insert_into_item : null;
},
],
'uploadedToThisItem' => [
'type' => Types::string(),
'description' => __( 'Label for the media frame filter.', 'wp-graphql' ),
'resolve' => function( $labels ) {
return ! empty( $labels->uploaded_to_this_item ) ? $labels->uploaded_to_this_item : null;
},
],
'featuredImage' => [
'type' => Types::string(),
'description' => __( 'Label for the Featured Image meta box title.', 'wp-graphql' ),
'resolve' => function( $labels ) {
return ! empty( $labels->featured_image ) ? $labels->featured_image : null;
},
],
'setFeaturedImage' => [
'type' => Types::string(),
'description' => __( 'Label for setting the featured image.', 'wp-graphql' ),
'resolve' => function( $labels ) {
return ! empty( $labels->set_featured_image ) ? $labels->set_featured_image : null;
},
],
'removeFeaturedImage' => [
'type' => Types::string(),
'description' => __( 'Label for removing the featured image.', 'wp-graphql' ),
'resolve' => function( $labels ) {
return ! empty( $labels->remove_featured_image ) ? $labels->remove_featured_image : null;
},
],
'useFeaturedImage' => [
'type' => Types::string(),
'description' => __( 'Label in the media frame for using a featured image.', 'wp-graphql' ),
'resolve' => function( $labels ) {
return ! empty( $labels->use_featured_item ) ? $labels->use_featured_item : null;
},
],
'menuName' => [
'type' => Types::string(),
'description' => __( 'Label for the menu name.', 'wp-graphql' ),
'resolve' => function( $labels ) {
return ! empty( $labels->menu_name ) ? $labels->menu_name : null;
},
],
'filterItemsList' => [
'type' => Types::string(),
'description' => __( 'Label for the table views hidden heading.', 'wp-graphql' ),
'resolve' => function( $labels ) {
return ! empty( $labels->filter_items_list ) ? $labels->filter_items_list : null;
},
],
'itemsListNavigation' => [
'type' => Types::string(),
'description' => __( 'Label for the table pagination hidden heading.', 'wp-graphql' ),
'resolve' => function( $labels ) {
return ! empty( $labels->items_list_navigation ) ? $labels->items_list_navigation : null;
},
],
'itemsList' => [
'type' => Types::string(),
'description' => __( 'Label for the table hidden heading.', 'wp-graphql' ),
'resolve' => function( $labels ) {
return ! empty( $labels->items_list ) ? $labels->items_list : null;
},
],
];
return self::prepare_fields( $fields, 'LabelsDetails' );
},
] );
}
return ! empty( self::$labels_details ) ? self::$labels_details : null;
}
}