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

wp-graphql / wp-graphql / 13316763745

13 Feb 2025 08:45PM UTC coverage: 82.712% (-0.3%) from 83.023%
13316763745

push

github

web-flow
Merge pull request #3307 from wp-graphql/release/v2.0.0

release: v2.0.0

195 of 270 new or added lines in 20 files covered. (72.22%)

180 existing lines in 42 files now uncovered.

13836 of 16728 relevant lines covered (82.71%)

299.8 hits per line

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

0.0
/src/Admin/GraphiQL/GraphiQL.php
1
<?php
2

3
namespace WPGraphQL\Admin\GraphiQL;
4

5
use WP_Admin_Bar;
6

7
/**
8
 * Class GraphiQL
9
 *
10
 * Sets up GraphiQL in the WordPress Admin
11
 *
12
 * @package WPGraphQL\Admin\GraphiQL
13
 */
14
class GraphiQL {
15

16
        /**
17
         * @var bool Whether GraphiQL is enabled
18
         */
19
        protected $is_enabled = false;
20

21
        /**
22
         * Initialize Admin functionality for WPGraphQL
23
         *
24
         * @return void
25
         */
UNCOV
26
        public function init() {
×
27
                $this->is_enabled = get_graphql_setting( 'graphiql_enabled' ) !== 'off';
×
28

29
                /**
30
                 * If GraphiQL is disabled, don't set it up in the Admin
31
                 */
32
                if ( ! $this->is_enabled ) {
×
33
                        return;
×
34
                }
35

36
                // Register the admin page
37
                add_action( 'admin_menu', [ $this, 'register_admin_page' ], 9 );
×
38
                add_action( 'admin_bar_menu', [ $this, 'register_admin_bar_menu' ], 100 );
×
39
                // Enqueue GraphiQL React App
40
                add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_graphiql' ] );
×
41

42
                /**
43
                 * Enqueue extension styles and scripts
44
                 *
45
                 * These extensions are part of WPGraphiQL core, but were built in a way
46
                 * to showcase how extension APIs can be used to extend WPGraphiQL
47
                 */
48
                add_action( 'enqueue_graphiql_extension', [ $this, 'graphiql_enqueue_query_composer' ] );
×
49
                add_action( 'enqueue_graphiql_extension', [ $this, 'graphiql_enqueue_auth_switch' ] );
×
50
                add_action( 'enqueue_graphiql_extension', [ $this, 'graphiql_enqueue_fullscreen_toggle' ] );
×
51
        }
52

53
        /**
54
         * Registers admin bar menu
55
         *
56
         * @param \WP_Admin_Bar $admin_bar The Admin Bar Instance
57
         *
58
         * @return void
59
         */
UNCOV
60
        public function register_admin_bar_menu( WP_Admin_Bar $admin_bar ) {
×
61

62
                if ( 'off' === get_graphql_setting( 'graphiql_enabled' ) ) {
×
63
                        return;
×
64
                }
65

66
                if ( ! current_user_can( 'manage_options' ) ) {
×
67
                        return;
×
68
                }
69

70
                if ( 'off' === get_graphql_setting( 'show_graphiql_link_in_admin_bar' ) ) {
×
71
                        return;
×
72
                }
73

74
                $icon_url = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA0MDAgNDAwIj48cGF0aCBmaWxsPSIjRTEwMDk4IiBkPSJNNTcuNDY4IDMwMi42NmwtMTQuMzc2LTguMyAxNjAuMTUtMjc3LjM4IDE0LjM3NiA4LjN6Ii8+PHBhdGggZmlsbD0iI0UxMDA5OCIgZD0iTTM5LjggMjcyLjJoMzIwLjN2MTYuNkgzOS44eiIvPjxwYXRoIGZpbGw9IiNFMTAwOTgiIGQ9Ik0yMDYuMzQ4IDM3NC4wMjZsLTE2MC4yMS05Mi41IDguMy0xNC4zNzYgMTYwLjIxIDkyLjV6TTM0NS41MjIgMTMyLjk0N2wtMTYwLjIxLTkyLjUgOC4zLTE0LjM3NiAxNjAuMjEgOTIuNXoiLz48cGF0aCBmaWxsPSIjRTEwMDk4IiBkPSJNNTQuNDgyIDEzMi44ODNsLTguMy0xNC4zNzUgMTYwLjIxLTkyLjUgOC4zIDE0LjM3NnoiLz48cGF0aCBmaWxsPSIjRTEwMDk4IiBkPSJNMzQyLjU2OCAzMDIuNjYzbC0xNjAuMTUtMjc3LjM4IDE0LjM3Ni04LjMgMTYwLjE1IDI3Ny4zOHpNNTIuNSAxMDcuNWgxNi42djE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jYgMzQuOSAzNC45IDAgMTkuMy0xNS42IDM0LjktMzQuOSAzNC45Ii8+PC9zdmc+';
×
75

76
                $icon = sprintf(
×
77
                        '<span class="custom-icon" style="
×
78
    background-image:url(\'%s\'); float:left; width:22px !important; height:22px !important;
79
    margin-left: 5px !important; margin-top: 5px !important; margin-right: 5px !important;
80
    "></span>',
×
81
                        $icon_url
×
82
                );
×
83

84
                $admin_bar->add_menu(
×
85
                        [
×
86
                                'id'    => 'graphiql-ide',
×
87
                                'title' => $icon . __( 'GraphiQL IDE', 'wp-graphql' ),
×
88
                                'href'  => trailingslashit( admin_url() ) . 'admin.php?page=graphiql-ide',
×
89
                        ]
×
90
                );
×
91
        }
92

93
        /**
94
         * Register the admin page as a subpage
95
         *
96
         * @return void
97
         */
UNCOV
98
        public function register_admin_page() {
×
99
                $svg_file = file_get_contents( WPGRAPHQL_PLUGIN_DIR . '/src/assets/wpgraphql-elephant.svg' );
×
100

101
                if ( false === $svg_file ) {
×
102
                        return;
×
103
                }
104

105
                $svg_base64 = base64_encode( $svg_file );
×
106

107
                // Top level menu page should be labeled GraphQL
108
                add_menu_page(
×
109
                        __( 'GraphQL', 'wp-graphql' ),
×
110
                        __( 'GraphQL', 'wp-graphql' ),
×
111
                        'manage_options',
×
112
                        'graphiql-ide',
×
113
                        [ $this, 'render_graphiql_admin_page' ],
×
114
                        'data:image/svg+xml;base64,' . $svg_base64
×
115
                );
×
116

117
                // Sub menu  should be labeled GraphiQL IDE
118
                add_submenu_page(
×
119
                        'graphiql-ide',
×
120
                        __( 'GraphiQL IDE', 'wp-graphql' ),
×
121
                        __( 'GraphiQL IDE', 'wp-graphql' ),
×
122
                        'manage_options',
×
123
                        'graphiql-ide',
×
124
                        [ $this, 'render_graphiql_admin_page' ]
×
125
                );
×
126
        }
127

128
        /**
129
         * Render the markup to load GraphiQL to.
130
         *
131
         * @return void
132
         */
UNCOV
133
        public function render_graphiql_admin_page() {
×
134
                $rendered = apply_filters( 'graphql_render_admin_page', '<div class="wrap" dir="ltr"><div id="graphiql" class="graphiql-container">Loading ...</div></div>' );
×
135

136
                echo wp_kses_post( $rendered );
×
137
        }
138

139
        /**
140
         * Enqueues the stylesheet and js for the WPGraphiQL app
141
         *
142
         * @return void
143
         */
UNCOV
144
        public function enqueue_graphiql() {
×
145
                if ( null === get_current_screen() || ! strpos( get_current_screen()->id, 'graphiql' ) ) {
×
146
                        return;
×
147
                }
148

149
                $asset_file = include WPGRAPHQL_PLUGIN_DIR . 'build/index.asset.php';
×
150

151
                // Setup some globals that can be used by GraphiQL
152
                // and extending scripts
153
                wp_enqueue_script(
×
154
                        'wp-graphiql', // Handle.
×
155
                        WPGRAPHQL_PLUGIN_URL . 'build/index.js',
×
156
                        $asset_file['dependencies'],
×
157
                        $asset_file['version'],
×
158
                        true
×
159
                );
×
160

161
                $app_asset_file = include WPGRAPHQL_PLUGIN_DIR . 'build/app.asset.php';
×
162

163
                wp_enqueue_script(
×
164
                        'wp-graphiql-app', // Handle.
×
165
                        WPGRAPHQL_PLUGIN_URL . 'build/app.js',
×
166
                        array_merge( [ 'wp-graphiql' ], $app_asset_file['dependencies'] ),
×
167
                        $app_asset_file['version'],
×
168
                        true
×
169
                );
×
170

171
                wp_enqueue_style(
×
172
                        'wp-graphiql-app',
×
173
                        WPGRAPHQL_PLUGIN_URL . 'build/app.css',
×
174
                        [ 'wp-components' ],
×
175
                        $app_asset_file['version']
×
176
                );
×
177

178
                wp_localize_script(
×
179
                        'wp-graphiql',
×
180
                        'wpGraphiQLSettings',
×
181
                        [
×
182
                                'nonce'             => wp_create_nonce( 'wp_rest' ),
×
183
                                'graphqlEndpoint'   => trailingslashit( site_url() ) . 'index.php?' . graphql_get_endpoint(),
×
184
                                'avatarUrl'         => 0 !== get_current_user_id() ? get_avatar_url( get_current_user_id() ) : null,
×
185
                                'externalFragments' => apply_filters( 'graphiql_external_fragments', [] ),
×
186
                        ]
×
187
                );
×
188

189
                // Extensions looking to extend GraphiQL can hook in here,
190
                // after the window object is established, but before the App renders
191
                do_action( 'enqueue_graphiql_extension' );
×
192
        }
193

194
        /**
195
         * Enqueue the GraphiQL Auth Switch extension, which adds a button to the GraphiQL toolbar
196
         * that allows the user to switch between the logged in user and the current user
197
         *
198
         * @return void
199
         */
UNCOV
200
        public function graphiql_enqueue_auth_switch() {
×
201
                $auth_switch_asset_file = include WPGRAPHQL_PLUGIN_DIR . 'build/graphiqlAuthSwitch.asset.php';
×
202

203
                wp_enqueue_script(
×
204
                        'wp-graphiql-auth-switch', // Handle.
×
205
                        WPGRAPHQL_PLUGIN_URL . 'build/graphiqlAuthSwitch.js',
×
206
                        array_merge( [ 'wp-graphiql', 'wp-graphiql-app' ], $auth_switch_asset_file['dependencies'] ),
×
207
                        $auth_switch_asset_file['version'],
×
208
                        true
×
209
                );
×
210
        }
211

212
        /**
213
         * Enqueue the Query Composer extension, which adds a button to the GraphiQL toolbar
214
         * that allows the user to open the Query Composer and compose a query with a form-based UI
215
         *
216
         * @return void
217
         */
UNCOV
218
        public function graphiql_enqueue_query_composer() {
×
219

220
                // Enqueue the assets for the Explorer before enqueueing the app,
221
                // so that the JS in the exporter that hooks into the app will be available
222
                // by time the app is enqueued
223
                $composer_asset_file = include WPGRAPHQL_PLUGIN_DIR . 'build/graphiqlQueryComposer.asset.php';
×
224

225
                wp_enqueue_script(
×
226
                        'wp-graphiql-query-composer', // Handle.
×
227
                        WPGRAPHQL_PLUGIN_URL . 'build/graphiqlQueryComposer.js',
×
228
                        array_merge( [ 'wp-graphiql', 'wp-graphiql-app' ], $composer_asset_file['dependencies'] ),
×
229
                        $composer_asset_file['version'],
×
230
                        true
×
231
                );
×
232

233
                wp_enqueue_style(
×
234
                        'wp-graphiql-query-composer',
×
235
                        WPGRAPHQL_PLUGIN_URL . 'build/graphiqlQueryComposer.css',
×
236
                        [ 'wp-components' ],
×
237
                        $composer_asset_file['version']
×
238
                );
×
239
        }
240

241
        /**
242
         * Enqueue the GraphiQL Fullscreen Toggle extension, which adds a button to the GraphiQL toolbar
243
         * that allows the user to toggle the fullscreen mode
244
         *
245
         * @return void
246
         */
UNCOV
247
        public function graphiql_enqueue_fullscreen_toggle() {
×
248
                $fullscreen_toggle_asset_file = include WPGRAPHQL_PLUGIN_DIR . 'build/graphiqlFullscreenToggle.asset.php';
×
249

250
                wp_enqueue_script(
×
251
                        'wp-graphiql-fullscreen-toggle', // Handle.
×
252
                        WPGRAPHQL_PLUGIN_URL . 'build/graphiqlFullscreenToggle.js',
×
253
                        array_merge( [ 'wp-graphiql', 'wp-graphiql-app' ], $fullscreen_toggle_asset_file['dependencies'] ),
×
254
                        $fullscreen_toggle_asset_file['version'],
×
255
                        true
×
256
                );
×
257

258
                wp_enqueue_style(
×
259
                        'wp-graphiql-fullscreen-toggle',
×
260
                        WPGRAPHQL_PLUGIN_URL . 'build/graphiqlFullscreenToggle.css',
×
261
                        [ 'wp-components' ],
×
262
                        $fullscreen_toggle_asset_file['version']
×
263
                );
×
264
        }
265
}
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

© 2026 Coveralls, Inc