• 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

96.91
/src/Type/ObjectType/MenuItem.php
1
<?php
2

3
namespace WPGraphQL\Type\ObjectType;
4

5
use GraphQL\Type\Definition\ResolveInfo;
6
use WPGraphQL\AppContext;
7
use WPGraphQL\Data\Connection\MenuConnectionResolver;
8
use WPGraphQL\Data\Connection\PostObjectConnectionResolver;
9
use WPGraphQL\Data\Connection\TermObjectConnectionResolver;
10
use WPGraphQL\Model\MenuItem as MenuItemModel;
11

12
class MenuItem {
13

14
        /**
15
         * Register the MenuItem Type
16
         *
17
         * @return void
18
         */
19
        public static function register_type() {
593✔
20
                register_graphql_object_type(
593✔
21
                        'MenuItem',
593✔
22
                        [
593✔
23
                                'description' => static function () {
593✔
24
                                        return __( 'Navigation menu items are the individual items assigned to a menu. These are rendered as the links in a navigation menu.', 'wp-graphql' );
18✔
25
                                },
593✔
26
                                'interfaces'  => [ 'Node', 'DatabaseIdentifier' ],
593✔
27
                                'model'       => MenuItemModel::class,
593✔
28
                                'connections' => [
593✔
29
                                        'connectedNode' => [
593✔
30
                                                'toType'      => 'MenuItemLinkable',
593✔
31
                                                'description' => static function () {
593✔
32
                                                        return __( 'Connection from MenuItem to it\'s connected node', 'wp-graphql' );
18✔
33
                                                },
593✔
34
                                                'oneToOne'    => true,
593✔
35
                                                'resolve'     => static function ( MenuItemModel $menu_item, $args, AppContext $context, ResolveInfo $info ) {
593✔
36
                                                        if ( ! isset( $menu_item->databaseId ) ) {
18✔
37
                                                                return null;
×
38
                                                        }
39

40
                                                        $object_id   = (int) get_post_meta( $menu_item->databaseId, '_menu_item_object_id', true );
18✔
41
                                                        $object_type = get_post_meta( $menu_item->databaseId, '_menu_item_type', true );
18✔
42

43
                                                        /**
44
                                                         * When this filter returns anything other than null it will be used as the resolved connection for the menu item's connected node, short-circuiting the default resolution.
45
                                                         *
46
                                                         * This is useful since we often add taxonomy terms to menus but would prefer to represent the menu item in other ways.
47
                                                         * E.g., a linked post object (or vice-versa).
48
                                                         *
49
                                                         * @param ?\GraphQL\Deferred                   $deferred_connection The AbstractConnectionResolver's connection, or null to continue with the default resolution.
50
                                                         * @param \WPGraphQL\Model\MenuItem            $menu_item           The MenuItem model.
51
                                                         * @param array<string,mixed>                  $args                The GraphQL args for the connection.
52
                                                         * @param \WPGraphQL\AppContext                $context             The AppContext object.
53
                                                         * @param \GraphQL\Type\Definition\ResolveInfo $info                The ResolveInfo object.
54
                                                         * @param int                                  $object_id           The ID of the connected object.
55
                                                         * @param string                               $object_type         The type of the connected object.
56
                                                         */
57
                                                        $deferred_connection = apply_filters( 'graphql_pre_resolve_menu_item_connected_node', null, $menu_item, $args, $context, $info, $object_id, $object_type );
18✔
58

59
                                                        if ( null !== $deferred_connection ) {
18✔
60
                                                                return $deferred_connection;
1✔
61
                                                        }
62

63
                                                        // Handle the default resolution.
64
                                                        switch ( $object_type ) {
65
                                                                // Post object
66
                                                                case 'post_type':
17✔
67
                                                                        $resolver = new PostObjectConnectionResolver( $menu_item, $args, $context, $info, 'any' );
15✔
68
                                                                        $resolver->set_query_arg( 'p', $object_id );
15✔
69

70
                                                                        // connected objects to menu items can be any post status
71
                                                                        $resolver->set_query_arg( 'post_status', 'any' );
15✔
72
                                                                        break;
15✔
73

74
                                                                // Taxonomy term
75
                                                                case 'taxonomy':
2✔
76
                                                                        $resolver = new TermObjectConnectionResolver( $menu_item, $args, $context, $info );
1✔
77
                                                                        $resolver->set_query_arg( 'include', $object_id );
1✔
78
                                                                        break;
1✔
79
                                                                default:
80
                                                                        $resolver = null;
1✔
81
                                                                        break;
1✔
82
                                                        }
83

84
                                                        return null !== $resolver ? $resolver->one_to_one()->get_connection() : null;
17✔
85
                                                },
593✔
86
                                        ],
593✔
87
                                        'menu'          => [
593✔
88
                                                'toType'      => 'Menu',
593✔
89
                                                'description' => static function () {
593✔
90
                                                        return __( 'The Menu a MenuItem is part of', 'wp-graphql' );
18✔
91
                                                },
593✔
92
                                                'oneToOne'    => true,
593✔
93
                                                'resolve'     => static function ( MenuItemModel $menu_item, $args, $context, $info ) {
593✔
94
                                                        $resolver = new MenuConnectionResolver( $menu_item, $args, $context, $info );
4✔
95
                                                        $resolver->set_query_arg( 'include', $menu_item->menuDatabaseId );
4✔
96

97
                                                        return $resolver->one_to_one()->get_connection();
4✔
98
                                                },
593✔
99
                                        ],
593✔
100
                                ],
593✔
101
                                'fields'      => static function () {
593✔
102
                                        return [
132✔
103
                                                'id'               => [
132✔
104
                                                        'description' => static function () {
132✔
105
                                                                return __( 'The globally unique identifier of the nav menu item object.', 'wp-graphql' );
18✔
106
                                                        },
132✔
107
                                                ],
132✔
108
                                                'parentId'         => [
132✔
109
                                                        'type'        => 'ID',
132✔
110
                                                        'description' => static function () {
132✔
111
                                                                return __( 'The globally unique identifier of the parent nav menu item object.', 'wp-graphql' );
18✔
112
                                                        },
132✔
113
                                                ],
132✔
114
                                                'parentDatabaseId' => [
132✔
115
                                                        'type'        => 'Int',
132✔
116
                                                        'description' => static function () {
132✔
117
                                                                return __( 'The database id of the parent menu item or null if it is the root', 'wp-graphql' );
18✔
118
                                                        },
132✔
119
                                                ],
132✔
120
                                                'cssClasses'       => [
132✔
121
                                                        'type'        => [
132✔
122
                                                                'list_of' => 'String',
132✔
123
                                                        ],
132✔
124
                                                        'description' => static function () {
132✔
125
                                                                return __( 'Class attribute for the menu item link', 'wp-graphql' );
18✔
126
                                                        },
132✔
127
                                                ],
132✔
128
                                                'description'      => [
132✔
129
                                                        'type'        => 'String',
132✔
130
                                                        'description' => static function () {
132✔
131
                                                                return __( 'Description of the menu item.', 'wp-graphql' );
18✔
132
                                                        },
132✔
133
                                                ],
132✔
134
                                                'label'            => [
132✔
135
                                                        'type'        => 'String',
132✔
136
                                                        'description' => static function () {
132✔
137
                                                                return __( 'Label or title of the menu item.', 'wp-graphql' );
18✔
138
                                                        },
132✔
139
                                                ],
132✔
140
                                                'linkRelationship' => [
132✔
141
                                                        'type'        => 'String',
132✔
142
                                                        'description' => static function () {
132✔
143
                                                                return __( 'Link relationship (XFN) of the menu item.', 'wp-graphql' );
18✔
144
                                                        },
132✔
145
                                                ],
132✔
146
                                                'menuItemId'       => [
132✔
147
                                                        'type'              => 'Int',
132✔
148
                                                        'description'       => static function () {
132✔
149
                                                                return __( 'WP ID of the menu item.', 'wp-graphql' );
18✔
150
                                                        },
132✔
151
                                                        'deprecationReason' => static function () {
132✔
152
                                                                return __( 'Deprecated in favor of the databaseId field', 'wp-graphql' );
18✔
153
                                                        },
132✔
154
                                                ],
132✔
155
                                                'target'           => [
132✔
156
                                                        'type'        => 'String',
132✔
157
                                                        'description' => static function () {
132✔
158
                                                                return __( 'Target attribute for the menu item link.', 'wp-graphql' );
18✔
159
                                                        },
132✔
160
                                                ],
132✔
161
                                                'title'            => [
132✔
162
                                                        'type'        => 'String',
132✔
163
                                                        'description' => static function () {
132✔
164
                                                                return __( 'Title attribute for the menu item link', 'wp-graphql' );
18✔
165
                                                        },
132✔
166
                                                ],
132✔
167
                                                'url'              => [
132✔
168
                                                        'type'        => 'String',
132✔
169
                                                        'description' => static function () {
132✔
170
                                                                return __( 'URL or destination of the menu item.', 'wp-graphql' );
18✔
171
                                                        },
132✔
172
                                                ],
132✔
173
                                                // Note: this field is added to the MenuItem type instead of applied by the "UniformResourceIdentifiable" interface
174
                                                // because a MenuItem is not identifiable by a uri, the connected resource is identifiable by the uri.
175
                                                'uri'              => [
132✔
176
                                                        'type'        => 'String',
132✔
177
                                                        'description' => static function () {
132✔
178
                                                                return __( 'The uri of the resource the menu item links to', 'wp-graphql' );
18✔
179
                                                        },
132✔
180
                                                ],
132✔
181
                                                'path'             => [
132✔
182
                                                        'type'        => 'String',
132✔
183
                                                        'description' => static function () {
132✔
184
                                                                return __( 'Path for the resource. Relative path for internal resources. Absolute path for external resources.', 'wp-graphql' );
18✔
185
                                                        },
132✔
186
                                                ],
132✔
187
                                                'isRestricted'     => [
132✔
188
                                                        'type'        => 'Boolean',
132✔
189
                                                        'description' => static function () {
132✔
190
                                                                return __( 'Whether the object is restricted from the current viewer', 'wp-graphql' );
18✔
191
                                                        },
132✔
192
                                                ],
132✔
193
                                                'order'            => [
132✔
194
                                                        'type'        => 'Int',
132✔
195
                                                        'description' => static function () {
132✔
196
                                                                return __( 'Menu item order', 'wp-graphql' );
18✔
197
                                                        },
132✔
198
                                                ],
132✔
199
                                                'locations'        => [
132✔
200
                                                        'type'        => [
132✔
201
                                                                'list_of' => 'MenuLocationEnum',
132✔
202
                                                        ],
132✔
203
                                                        'description' => static function () {
132✔
204
                                                                return __( 'The locations the menu item\'s Menu is assigned to', 'wp-graphql' );
18✔
205
                                                        },
132✔
206
                                                ],
132✔
207
                                                'connectedObject'  => [
132✔
208
                                                        'type'              => 'MenuItemObjectUnion',
132✔
209
                                                        'deprecationReason' => static function () {
132✔
210
                                                                return __( 'Deprecated in favor of the connectedNode field', 'wp-graphql' );
17✔
211
                                                        },
132✔
212
                                                        'description'       => static function () {
132✔
213
                                                                return __( 'The object connected to this menu item.', 'wp-graphql' );
17✔
214
                                                        },
132✔
215
                                                        'resolve'           => static function ( $menu_item, array $args, AppContext $context, $info ) {
132✔
216
                                                                $object_id   = intval( get_post_meta( $menu_item->menuItemId, '_menu_item_object_id', true ) );
14✔
217
                                                                $object_type = get_post_meta( $menu_item->menuItemId, '_menu_item_type', true );
14✔
218

219
                                                                switch ( $object_type ) {
220
                                                                        // Post object
221
                                                                        case 'post_type':
14✔
222
                                                                                $resolved_object = $context->get_loader( 'post' )->load_deferred( $object_id );
14✔
223
                                                                                break;
14✔
224

225
                                                                        // Taxonomy term
226
                                                                        case 'taxonomy':
×
227
                                                                                $resolved_object = $context->get_loader( 'term' )->load_deferred( $object_id );
×
228
                                                                                break;
×
229
                                                                        default:
230
                                                                                $resolved_object = null;
×
231
                                                                                break;
×
232
                                                                }
233

234
                                                                /**
235
                                                                 * Allow users to override how nav menu items are resolved.
236
                                                                 * This is useful since we often add taxonomy terms to menus
237
                                                                 * but would prefer to represent the menu item in other ways,
238
                                                                 * e.g., a linked post object (or vice-versa).
239
                                                                 *
240
                                                                 * @param \WP_Post|\WP_Term                    $resolved_object Post or term connected to MenuItem
241
                                                                 * @param array<string,mixed>                  $args            Array of arguments input in the field as part of the GraphQL query
242
                                                                 * @param \WPGraphQL\AppContext                $context         Object containing app context that gets passed down the resolve tree
243
                                                                 * @param \GraphQL\Type\Definition\ResolveInfo $info            Info about fields passed down the resolve tree
244
                                                                 * @param int                                  $object_id       Post or term ID of connected object
245
                                                                 * @param string                               $object_type     Type of connected object ("post_type" or "taxonomy")
246
                                                                 *
247
                                                                 * @since 0.0.30
248
                                                                 */
249
                                                                return apply_filters_deprecated(
14✔
250
                                                                        'graphql_resolve_menu_item',
14✔
251
                                                                        [
14✔
252
                                                                                $resolved_object,
14✔
253
                                                                                $args,
14✔
254
                                                                                $context,
14✔
255
                                                                                $info,
14✔
256
                                                                                $object_id,
14✔
257
                                                                                $object_type,
14✔
258
                                                                        ],
14✔
259
                                                                        '1.22.0',
14✔
260
                                                                        'graphql_pre_resolve_menu_item_connected_node',
14✔
261
                                                                        __( 'Use the `graphql_pre_resolve_menu_item_connected_node` filter on `connectedNode` instead.', 'wp-graphql' )
14✔
262
                                                                );
14✔
263
                                                        },
132✔
264
                                                ],
132✔
265
                                        ];
132✔
266
                                },
593✔
267
                        ]
593✔
268
                );
593✔
269
        }
270
}
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