• 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

98.16
/src/Type/InterfaceType/ContentNode.php
1
<?php
2
namespace WPGraphQL\Type\InterfaceType;
3

4
use WPGraphQL\Data\Connection\ContentTypeConnectionResolver;
5
use WPGraphQL\Data\Connection\EnqueuedScriptsConnectionResolver;
6
use WPGraphQL\Data\Connection\EnqueuedStylesheetConnectionResolver;
7
use WPGraphQL\Model\Post;
8
use WPGraphQL\Registry\TypeRegistry;
9

10
class ContentNode {
11

12
        /**
13
         * Adds the ContentNode Type to the WPGraphQL Registry
14
         *
15
         * @param \WPGraphQL\Registry\TypeRegistry $type_registry
16
         *
17
         * @return void
18
         * @throws \Exception
19
         */
20
        public static function register_type( TypeRegistry $type_registry ) {
600✔
21

22
                /**
23
                 * The Content interface represents Post Types and the common shared fields
24
                 * across Post Type Objects
25
                 */
26
                register_graphql_interface_type(
600✔
27
                        'ContentNode',
600✔
28
                        [
600✔
29
                                'interfaces'  => [ 'Node', 'UniformResourceIdentifiable' ],
600✔
30
                                'description' => static function () {
600✔
31
                                        return __( 'Base interface for content objects like posts, pages, and media items. Provides common fields available across these content types.', 'wp-graphql' );
19✔
32
                                },
600✔
33
                                'connections' => [
600✔
34
                                        'contentType'         => [
600✔
35
                                                'toType'   => 'ContentType',
600✔
36
                                                'resolve'  => static function ( Post $source, $args, $context, $info ) {
600✔
37
                                                        if ( $source->isRevision ) {
3✔
38
                                                                $parent    = get_post( $source->parentDatabaseId );
×
39
                                                                $post_type = $parent->post_type ?? null;
×
40
                                                        } else {
41
                                                                $post_type = $source->post_type ?? null;
3✔
42
                                                        }
43

44
                                                        if ( empty( $post_type ) ) {
3✔
45
                                                                return null;
×
46
                                                        }
47

48
                                                        $resolver = new ContentTypeConnectionResolver( $source, $args, $context, $info );
3✔
49

50
                                                        return $resolver->one_to_one()->set_query_arg( 'name', $post_type )->get_connection();
3✔
51
                                                },
600✔
52
                                                'oneToOne' => true,
600✔
53
                                        ],
600✔
54
                                        'enqueuedScripts'     => [
600✔
55
                                                'toType'  => 'EnqueuedScript',
600✔
56
                                                'resolve' => static function ( $source, $args, $context, $info ) {
600✔
57
                                                        $resolver = new EnqueuedScriptsConnectionResolver( $source, $args, $context, $info );
17✔
58

59
                                                        return $resolver->get_connection();
17✔
60
                                                },
600✔
61
                                        ],
600✔
62
                                        'enqueuedStylesheets' => [
600✔
63
                                                'toType'  => 'EnqueuedStylesheet',
600✔
64
                                                'resolve' => static function ( $source, $args, $context, $info ) {
600✔
65
                                                        $resolver = new EnqueuedStylesheetConnectionResolver( $source, $args, $context, $info );
17✔
66
                                                        return $resolver->get_connection();
17✔
67
                                                },
600✔
68
                                        ],
600✔
69
                                ],
600✔
70
                                'resolveType' => static function ( Post $post ) use ( $type_registry ) {
600✔
71

72
                                        /**
73
                                         * The resolveType callback is used at runtime to determine what Type an object
74
                                         * implementing the ContentNode Interface should be resolved as.
75
                                         *
76
                                         * You can filter this centrally using the "graphql_wp_interface_type_config" filter
77
                                         * to override if you need something other than a Post object to be resolved via the
78
                                         * $post->post_type attribute.
79
                                         */
80
                                        $type      = null;
36✔
81
                                        $post_type = isset( $post->post_type ) ? $post->post_type : null;
36✔
82

83
                                        if ( isset( $post->post_type ) && 'revision' === $post->post_type ) {
36✔
84
                                                $parent = get_post( $post->parentDatabaseId );
5✔
85
                                                if ( $parent instanceof \WP_Post ) {
5✔
86
                                                        $post_type = $parent->post_type;
5✔
87
                                                }
88
                                        }
89

90
                                        $post_type_object = ! empty( $post_type ) ? get_post_type_object( $post_type ) : null;
36✔
91

92
                                        if ( isset( $post_type_object->graphql_single_name ) ) {
36✔
93
                                                $type = $type_registry->get_type( $post_type_object->graphql_single_name );
36✔
94
                                        }
95

96
                                        return ! empty( $type ) ? $type : null;
36✔
97
                                },
600✔
98
                                'fields'      => static function () {
600✔
99
                                        return [
365✔
100
                                                'contentTypeName'           => [
365✔
101
                                                        'type'        => [ 'non_null' => 'String' ],
365✔
102
                                                        'description' => static function () {
365✔
103
                                                                return __( 'The name of the Content Type the node belongs to', 'wp-graphql' );
19✔
104
                                                        },
365✔
105
                                                        'resolve'     => static function ( $node ) {
365✔
106
                                                                return $node->post_type;
1✔
107
                                                        },
365✔
108
                                                ],
365✔
109
                                                'template'                  => [
365✔
110
                                                        'type'        => 'ContentTemplate',
365✔
111
                                                        'description' => static function () {
365✔
112
                                                                return __( 'The template assigned to a node of content', 'wp-graphql' );
19✔
113
                                                        },
365✔
114
                                                ],
365✔
115
                                                'databaseId'                => [
365✔
116
                                                        'type'        => [
365✔
117
                                                                'non_null' => 'Int',
365✔
118
                                                        ],
365✔
119
                                                        'description' => static function () {
365✔
120
                                                                return __( 'The ID of the node in the database.', 'wp-graphql' );
19✔
121
                                                        },
365✔
122
                                                ],
365✔
123
                                                'date'                      => [
365✔
124
                                                        'type'        => 'String',
365✔
125
                                                        'description' => static function () {
365✔
126
                                                                return __( 'Post publishing date.', 'wp-graphql' );
19✔
127
                                                        },
365✔
128
                                                ],
365✔
129
                                                'dateGmt'                   => [
365✔
130
                                                        'type'        => 'String',
365✔
131
                                                        'description' => static function () {
365✔
132
                                                                return __( 'The publishing date set in GMT.', 'wp-graphql' );
19✔
133
                                                        },
365✔
134
                                                ],
365✔
135
                                                'enclosure'                 => [
365✔
136
                                                        'type'        => 'String',
365✔
137
                                                        'description' => static function () {
365✔
138
                                                                return __( 'The RSS enclosure for the object', 'wp-graphql' );
19✔
139
                                                        },
365✔
140
                                                ],
365✔
141
                                                'status'                    => [
365✔
142
                                                        'type'        => 'String',
365✔
143
                                                        'description' => static function () {
365✔
144
                                                                return __( 'The current status of the object', 'wp-graphql' );
19✔
145
                                                        },
365✔
146
                                                ],
365✔
147
                                                'slug'                      => [
365✔
148
                                                        'type'        => 'String',
365✔
149
                                                        'description' => static function () {
365✔
150
                                                                return __( 'The uri slug for the post. This is equivalent to the WP_Post->post_name field and the post_name column in the database for the "post_objects" table.', 'wp-graphql' );
19✔
151
                                                        },
365✔
152
                                                ],
365✔
153
                                                'modified'                  => [
365✔
154
                                                        'type'        => 'String',
365✔
155
                                                        'description' => static function () {
365✔
156
                                                                return __( 'The local modified time for a post. If a post was recently updated the modified field will change to match the corresponding time.', 'wp-graphql' );
19✔
157
                                                        },
365✔
158
                                                ],
365✔
159
                                                'modifiedGmt'               => [
365✔
160
                                                        'type'        => 'String',
365✔
161
                                                        'description' => static function () {
365✔
162
                                                                return __( 'The GMT modified time for a post. If a post was recently updated the modified field will change to match the corresponding time in GMT.', 'wp-graphql' );
19✔
163
                                                        },
365✔
164
                                                ],
365✔
165
                                                'guid'                      => [
365✔
166
                                                        'type'        => 'String',
365✔
167
                                                        'description' => static function () {
365✔
168
                                                                return __( 'The global unique identifier for this post. This currently matches the value stored in WP_Post->guid and the guid column in the "post_objects" database table.', 'wp-graphql' );
19✔
169
                                                        },
365✔
170
                                                ],
365✔
171
                                                'desiredSlug'               => [
365✔
172
                                                        'type'        => 'String',
365✔
173
                                                        'description' => static function () {
365✔
174
                                                                return __( 'The desired slug of the post', 'wp-graphql' );
19✔
175
                                                        },
365✔
176
                                                ],
365✔
177
                                                'link'                      => [
365✔
178
                                                        'type'        => 'String',
365✔
179
                                                        'description' => static function () {
365✔
180
                                                                return __( 'The permalink of the post', 'wp-graphql' );
19✔
181
                                                        },
365✔
182
                                                ],
365✔
183
                                                'isRestricted'              => [
365✔
184
                                                        'type'        => 'Boolean',
365✔
185
                                                        'description' => static function () {
365✔
186
                                                                return __( 'Whether the object is restricted from the current viewer', 'wp-graphql' );
19✔
187
                                                        },
365✔
188
                                                ],
365✔
189
                                                'isPreview'                 => [
365✔
190
                                                        'type'        => 'Boolean',
365✔
191
                                                        'description' => static function () {
365✔
192
                                                                return __( 'Whether the object is a node in the preview state', 'wp-graphql' );
19✔
193
                                                        },
365✔
194
                                                ],
365✔
195
                                                'previewRevisionDatabaseId' => [
365✔
196
                                                        'type'        => 'Int',
365✔
197
                                                        'description' => static function () {
365✔
198
                                                                return __( 'The database id of the preview node', 'wp-graphql' );
19✔
199
                                                        },
365✔
200
                                                ],
365✔
201
                                                'previewRevisionId'         => [
365✔
202
                                                        'type'        => 'ID',
365✔
203
                                                        'description' => static function () {
365✔
204
                                                                return __( 'Whether the object is a node in the preview state', 'wp-graphql' );
19✔
205
                                                        },
365✔
206
                                                ],
365✔
207
                                        ];
365✔
208
                                },
600✔
209
                        ]
600✔
210
                );
600✔
211
        }
212
}
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