• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql / 15710056976

17 Jun 2025 02:27PM UTC coverage: 84.17% (-0.1%) from 84.287%
15710056976

push

github

actions-user
release: merge develop into master for v2.3.3

15925 of 18920 relevant lines covered (84.17%)

258.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.62
/src/Model/PostType.php
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 ) {
21✔
50
                $this->data = $post_type;
21✔
51

52
                $allowed_restricted_fields = [
21✔
53
                        'id',
21✔
54
                        'name',
21✔
55
                        'description',
21✔
56
                        'hierarchical',
21✔
57
                        'slug',
21✔
58
                        'taxonomies',
21✔
59
                        'graphql_single_name',
21✔
60
                        'graphqlSingleName',
21✔
61
                        'graphql_plural_name',
21✔
62
                        'graphqlPluralName',
21✔
63
                        'showInGraphql',
21✔
64
                        'isRestricted',
21✔
65
                        'uri',
21✔
66
                        'isPostsPage',
21✔
67
                        'isFrontPage',
21✔
68
                        'label',
21✔
69
                ];
21✔
70

71
                $capability = isset( $post_type->cap->edit_posts ) ? $post_type->cap->edit_posts : 'edit_posts';
21✔
72

73
                parent::__construct( $capability, $allowed_restricted_fields );
21✔
74
        }
75

76
        /**
77
         * {@inheritDoc}
78
         */
79
        protected function is_private() {
21✔
80
                if ( false === $this->data->public && ( ! isset( $this->data->cap->edit_posts ) || ! current_user_can( $this->data->cap->edit_posts ) ) ) {
21✔
81
                        return true;
2✔
82
                }
83

84
                return false;
21✔
85
        }
86

87
        /**
88
         * {@inheritDoc}
89
         */
90
        protected function init() {
21✔
91
                if ( empty( $this->fields ) ) {
21✔
92
                        $this->fields = [
21✔
93
                                'canExport'           => function () {
21✔
94
                                        return true === $this->data->can_export;
3✔
95
                                },
21✔
96
                                'deleteWithUser'      => function () {
21✔
97
                                        return true === $this->data->delete_with_user;
3✔
98
                                },
21✔
99
                                'description'         => function () {
21✔
100
                                        return ! empty( $this->data->description ) ? $this->data->description : '';
3✔
101
                                },
21✔
102
                                'excludeFromSearch'   => function () {
21✔
103
                                        return true === $this->data->exclude_from_search;
3✔
104
                                },
21✔
105
                                'graphqlPluralName'   => function () {
21✔
106
                                        return ! empty( $this->data->graphql_plural_name ) ? $this->data->graphql_plural_name : null;
3✔
107
                                },
21✔
108
                                'graphqlSingleName'   => function () {
21✔
109
                                        return ! empty( $this->data->graphql_single_name ) ? $this->data->graphql_single_name : null;
3✔
110
                                },
21✔
111
                                'hasArchive'          => function () {
21✔
112
                                        return ! empty( $this->uri );
3✔
113
                                },
21✔
114
                                'hierarchical'        => function () {
21✔
115
                                        return true === $this->data->hierarchical || ! empty( $this->data->hierarchical );
3✔
116
                                },
21✔
117
                                'id'                  => function () {
21✔
118
                                        return ! empty( $this->name ) ? Relay::toGlobalId( 'post_type', $this->name ) : null;
21✔
119
                                },
21✔
120
                                // If the homepage settings are to set to
121
                                'isPostsPage'         => function () {
21✔
122
                                        // the "post" ContentType is always represented as isPostsPage
123
                                        return 'post' === $this->name;
2✔
124
                                },
21✔
125
                                'isFrontPage'         => function () {
21✔
126
                                        if (
127
                                                'post' === $this->name &&
2✔
128
                                                (
129
                                                        'posts' === get_option( 'show_on_front', 'posts' ) ||
2✔
130
                                                        empty( (int) get_option( 'page_on_front', 0 ) )
2✔
131
                                                )
132
                                        ) {
133
                                                return true;
2✔
134
                                        }
135

136
                                        return false;
×
137
                                },
21✔
138
                                'label'               => function () {
21✔
139
                                        return ! empty( $this->data->label ) ? $this->data->label : null;
3✔
140
                                },
21✔
141
                                'labels'              => function () {
21✔
142
                                        return get_post_type_labels( $this->data );
1✔
143
                                },
21✔
144
                                'menuIcon'            => function () {
21✔
145
                                        return ! empty( $this->data->menu_icon ) ? $this->data->menu_icon : null;
3✔
146
                                },
21✔
147
                                'menuPosition'        => function () {
21✔
148
                                        return ! empty( $this->data->menu_position ) ? $this->data->menu_position : null;
3✔
149
                                },
21✔
150
                                'name'                => function () {
21✔
151
                                        return ! empty( $this->data->name ) ? $this->data->name : null;
21✔
152
                                },
21✔
153
                                'public'              => function () {
21✔
154
                                        return ! empty( $this->data->public ) ? (bool) $this->data->public : null;
3✔
155
                                },
21✔
156
                                'publiclyQueryable'   => function () {
21✔
157
                                        return true === $this->data->publicly_queryable;
3✔
158
                                },
21✔
159
                                'restBase'            => function () {
21✔
160
                                        return ! empty( $this->data->rest_base ) ? $this->data->rest_base : null;
3✔
161
                                },
21✔
162
                                'restControllerClass' => function () {
21✔
163
                                        return ! empty( $this->data->rest_controller_class ) ? $this->data->rest_controller_class : null;
3✔
164
                                },
21✔
165
                                'showInAdminBar'      => function () {
21✔
166
                                        return true === $this->data->show_in_admin_bar;
3✔
167
                                },
21✔
168
                                'showInGraphql'       => function () {
21✔
169
                                        return true === $this->data->show_in_graphql;
3✔
170
                                },
21✔
171
                                'showInMenu'          => function () {
21✔
172
                                        return true === $this->data->show_in_menu;
3✔
173
                                },
21✔
174
                                'showInNavMenus'      => function () {
21✔
175
                                        return true === $this->data->show_in_nav_menus;
3✔
176
                                },
21✔
177
                                'showInRest'          => function () {
21✔
178
                                        return true === $this->data->show_in_rest;
3✔
179
                                },
21✔
180
                                'showUi'              => function () {
21✔
181
                                        return true === $this->data->show_ui;
3✔
182
                                },
21✔
183
                                'taxonomies'          => function () {
21✔
184
                                        $object_taxonomies = get_object_taxonomies( $this->data->name );
3✔
185
                                        return ! empty( $object_taxonomies ) ? $object_taxonomies : null;
3✔
186
                                },
21✔
187
                                'uri'                 => function () {
21✔
188
                                        $link = get_post_type_archive_link( $this->data->name );
8✔
189
                                        return ! empty( $link ) ? trailingslashit( str_ireplace( home_url(), '', $link ) ) : null;
8✔
190
                                },
21✔
191

192
                                // Aliases.
193
                                'graphql_plural_name' => function () {
21✔
194
                                        return $this->graphqlPluralName;
×
195
                                },
21✔
196
                                'graphql_single_name' => function () {
21✔
197
                                        return $this->graphqlSingleName;
×
198
                                },
21✔
199
                        ];
21✔
200
                }
201
        }
202
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc