| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPGraphQL\Model; |
| 4 |
|
| 5 |
use GraphQLRelay\Relay; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class PostType - Models data for PostTypes |
| 9 |
* |
| 10 |
* @property bool $canExport |
| 11 |
* @property bool $deleteWithUser |
| 12 |
* @property ?string $description |
| 13 |
* @property bool $excludeFromSearch |
| 14 |
* @property ?string $graphqlPluralName |
| 15 |
* @property ?string $graphqlSingleName |
| 16 |
* @property bool $hasArchive |
| 17 |
* @property ?bool $hierarchical |
| 18 |
* @property ?string $id |
| 19 |
* @property object $labels |
| 20 |
* @property ?string $menuIcon |
| 21 |
* @property ?int $menuPosition |
| 22 |
* @property ?string $name |
| 23 |
* @property ?bool $public |
| 24 |
* @property bool $publiclyQueryable |
| 25 |
* @property ?string $restBase |
| 26 |
* @property ?string $restControllerClass |
| 27 |
* @property bool $showInAdminBar |
| 28 |
* @property bool $showInGraphql |
| 29 |
* @property bool $showInMenu |
| 30 |
* @property bool $showInNavMenus |
| 31 |
* @property bool $showInRest |
| 32 |
* @property bool $showUi |
| 33 |
* @property string[]|null $taxonomies |
| 34 |
* |
| 35 |
* Aliases: |
| 36 |
* @property ?string $graphql_plural_name |
| 37 |
* @property ?string $graphql_single_name |
| 38 |
* |
| 39 |
* @package WPGraphQL\Model |
| 40 |
* |
| 41 |
* @extends \WPGraphQL\Model\Model<\WP_Post_Type> |
| 42 |
*/ |
| 43 |
class PostType extends Model { |
| 44 |
/** |
| 45 |
* PostType constructor. |
| 46 |
* |
| 47 |
* @param \WP_Post_Type $post_type The incoming post type to model. |
| 48 |
*/ |
| 49 |
public function __construct( \WP_Post_Type $post_type ) { |
| 50 |
$this->data = $post_type; |
| 51 |
|
| 52 |
$allowed_restricted_fields = [ |
| 53 |
'id', |
| 54 |
'name', |
| 55 |
'description', |
| 56 |
'hierarchical', |
| 57 |
'slug', |
| 58 |
'taxonomies', |
| 59 |
'graphql_single_name', |
| 60 |
'graphqlSingleName', |
| 61 |
'graphql_plural_name', |
| 62 |
'graphqlPluralName', |
| 63 |
'showInGraphql', |
| 64 |
'isRestricted', |
| 65 |
'uri', |
| 66 |
'isPostsPage', |
| 67 |
'isFrontPage', |
| 68 |
'label', |
| 69 |
]; |
| 70 |
|
| 71 |
$capability = isset( $post_type->cap->edit_posts ) ? $post_type->cap->edit_posts : 'edit_posts'; |
| 72 |
|
| 73 |
parent::__construct( $capability, $allowed_restricted_fields ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* {@inheritDoc} |
| 78 |
*/ |
| 79 |
protected function is_private() { |
| 80 |
if ( false === $this->data->public && ( ! isset( $this->data->cap->edit_posts ) || ! current_user_can( $this->data->cap->edit_posts ) ) ) { |
| 81 |
return true; |
| 82 |
} |
| 83 |
|
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* {@inheritDoc} |
| 89 |
*/ |
| 90 |
protected function init() { |
| 91 |
if ( empty( $this->fields ) ) { |
| 92 |
$this->fields = [ |
| 93 |
'canExport' => function () { |
| 94 |
return true === $this->data->can_export; |
| 95 |
}, |
| 96 |
'deleteWithUser' => function () { |
| 97 |
return true === $this->data->delete_with_user; |
| 98 |
}, |
| 99 |
'description' => function () { |
| 100 |
return ! empty( $this->data->description ) ? $this->data->description : ''; |
| 101 |
}, |
| 102 |
'excludeFromSearch' => function () { |
| 103 |
return true === $this->data->exclude_from_search; |
| 104 |
}, |
| 105 |
'graphqlPluralName' => function () { |
| 106 |
return ! empty( $this->data->graphql_plural_name ) ? $this->data->graphql_plural_name : null; |
| 107 |
}, |
| 108 |
'graphqlSingleName' => function () { |
| 109 |
return ! empty( $this->data->graphql_single_name ) ? $this->data->graphql_single_name : null; |
| 110 |
}, |
| 111 |
'hasArchive' => function () { |
| 112 |
return ! empty( $this->uri ); |
| 113 |
}, |
| 114 |
'hierarchical' => function () { |
| 115 |
return true === $this->data->hierarchical || ! empty( $this->data->hierarchical ); |
| 116 |
}, |
| 117 |
'id' => function () { |
| 118 |
return ! empty( $this->name ) ? Relay::toGlobalId( 'post_type', $this->name ) : null; |
| 119 |
}, |
| 120 |
// If the homepage settings are to set to |
| 121 |
'isPostsPage' => function () { |
| 122 |
// the "post" ContentType is always represented as isPostsPage |
| 123 |
return 'post' === $this->name; |
| 124 |
}, |
| 125 |
'isFrontPage' => function () { |
| 126 |
if ( |
| 127 |
'post' === $this->name && |
| 128 |
( |
| 129 |
'posts' === get_option( 'show_on_front', 'posts' ) || |
| 130 |
empty( (int) get_option( 'page_on_front', 0 ) ) |
| 131 |
) |
| 132 |
) { |
| 133 |
return true; |
| 134 |
} |
| 135 |
|
| 136 |
return false; |
| 137 |
}, |
| 138 |
'label' => function () { |
| 139 |
return ! empty( $this->data->label ) ? $this->data->label : null; |
| 140 |
}, |
| 141 |
'labels' => function () { |
| 142 |
return get_post_type_labels( $this->data ); |
| 143 |
}, |
| 144 |
'menuIcon' => function () { |
| 145 |
return ! empty( $this->data->menu_icon ) ? $this->data->menu_icon : null; |
| 146 |
}, |
| 147 |
'menuPosition' => function () { |
| 148 |
return ! empty( $this->data->menu_position ) ? $this->data->menu_position : null; |
| 149 |
}, |
| 150 |
'name' => function () { |
| 151 |
return ! empty( $this->data->name ) ? $this->data->name : null; |
| 152 |
}, |
| 153 |
'public' => function () { |
| 154 |
return ! empty( $this->data->public ) ? (bool) $this->data->public : null; |
| 155 |
}, |
| 156 |
'publiclyQueryable' => function () { |
| 157 |
return true === $this->data->publicly_queryable; |
| 158 |
}, |
| 159 |
'restBase' => function () { |
| 160 |
return ! empty( $this->data->rest_base ) ? $this->data->rest_base : null; |
| 161 |
}, |
| 162 |
'restControllerClass' => function () { |
| 163 |
return ! empty( $this->data->rest_controller_class ) ? $this->data->rest_controller_class : null; |
| 164 |
}, |
| 165 |
'showInAdminBar' => function () { |
| 166 |
return true === $this->data->show_in_admin_bar; |
| 167 |
}, |
| 168 |
'showInGraphql' => function () { |
| 169 |
return true === $this->data->show_in_graphql; |
| 170 |
}, |
| 171 |
'showInMenu' => function () { |
| 172 |
return true === $this->data->show_in_menu; |
| 173 |
}, |
| 174 |
'showInNavMenus' => function () { |
| 175 |
return true === $this->data->show_in_nav_menus; |
| 176 |
}, |
| 177 |
'showInRest' => function () { |
| 178 |
return true === $this->data->show_in_rest; |
| 179 |
}, |
| 180 |
'showUi' => function () { |
| 181 |
return true === $this->data->show_ui; |
| 182 |
}, |
| 183 |
'taxonomies' => function () { |
| 184 |
$object_taxonomies = get_object_taxonomies( $this->data->name ); |
| 185 |
return ! empty( $object_taxonomies ) ? $object_taxonomies : null; |
| 186 |
}, |
| 187 |
'uri' => function () { |
| 188 |
$link = get_post_type_archive_link( $this->data->name ); |
| 189 |
return ! empty( $link ) ? trailingslashit( str_ireplace( home_url(), '', $link ) ) : null; |
| 190 |
}, |
| 191 |
|
| 192 |
// Aliases. |
| 193 |
'graphql_plural_name' => function () { |
| 194 |
return $this->graphqlPluralName; |
| 195 |
}, |
| 196 |
'graphql_single_name' => function () { |
| 197 |
return $this->graphqlSingleName; |
| 198 |
}, |
| 199 |
]; |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|