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

wp-graphql / wp-graphql / 14716683875

28 Apr 2025 07:58PM UTC coverage: 84.287% (+1.6%) from 82.648%
14716683875

push

github

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

15905 of 18870 relevant lines covered (84.29%)

257.23 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
class PostType extends Model {
42

43
        /**
44
         * Stores the incoming WP_Post_Type to be modeled
45
         *
46
         * @var \WP_Post_Type $data
47
         */
48
        protected $data;
49

50
        /**
51
         * PostType constructor.
52
         *
53
         * @param \WP_Post_Type $post_type The incoming post type to model.
54
         */
55
        public function __construct( \WP_Post_Type $post_type ) {
21✔
56
                $this->data = $post_type;
21✔
57

58
                $allowed_restricted_fields = [
21✔
59
                        'id',
21✔
60
                        'name',
21✔
61
                        'description',
21✔
62
                        'hierarchical',
21✔
63
                        'slug',
21✔
64
                        'taxonomies',
21✔
65
                        'graphql_single_name',
21✔
66
                        'graphqlSingleName',
21✔
67
                        'graphql_plural_name',
21✔
68
                        'graphqlPluralName',
21✔
69
                        'showInGraphql',
21✔
70
                        'isRestricted',
21✔
71
                        'uri',
21✔
72
                        'isPostsPage',
21✔
73
                        'isFrontPage',
21✔
74
                        'label',
21✔
75
                ];
21✔
76

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

79
                parent::__construct( $capability, $allowed_restricted_fields );
21✔
80
        }
81

82
        /**
83
         * {@inheritDoc}
84
         */
85
        protected function is_private() {
21✔
86
                if ( false === $this->data->public && ( ! isset( $this->data->cap->edit_posts ) || ! current_user_can( $this->data->cap->edit_posts ) ) ) {
21✔
87
                        return true;
2✔
88
                }
89

90
                return false;
21✔
91
        }
92

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

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

198
                                // Aliases.
199
                                'graphql_plural_name' => function () {
21✔
200
                                        return $this->graphqlPluralName;
×
201
                                },
21✔
202
                                'graphql_single_name' => function () {
21✔
203
                                        return $this->graphqlSingleName;
×
204
                                },
21✔
205
                        ];
21✔
206
                }
207
        }
208
}
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