• 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/Settings/Settings.php
1
<?php
2

3
namespace WPGraphQL\Admin\Settings;
4

5
/**
6
 * Class Settings
7
 *
8
 * @package WPGraphQL\Admin\Settings
9
 */
10
class Settings {
11

12
        /**
13
         * @var \WPGraphQL\Admin\Settings\SettingsRegistry
14
         */
15
        public $settings_api;
16

17
        /**
18
         * WP_ENVIRONMENT_TYPE
19
         *
20
         * @var string The WordPress environment.
21
         */
22
        protected $wp_environment;
23

24
        /**
25
         * Initialize the WPGraphQL Settings Pages
26
         *
27
         * @return void
28
         */
UNCOV
29
        public function init() {
×
30
                $this->wp_environment = $this->get_wp_environment();
×
31
                $this->settings_api   = new SettingsRegistry();
×
32

33
                add_action( 'admin_menu', [ $this, 'add_options_page' ] );
×
34
                add_action( 'init', [ $this, 'register_settings' ] );
×
35
                add_action( 'admin_init', [ $this, 'initialize_settings_page' ] );
×
36
                add_action( 'admin_enqueue_scripts', [ $this, 'initialize_settings_page_scripts' ] );
×
37
        }
38

39
        /**
40
         * Return the environment. Default to production.
41
         *
42
         * @return string The environment set using WP_ENVIRONMENT_TYPE.
43
         */
UNCOV
44
        protected function get_wp_environment() {
×
45
                if ( function_exists( 'wp_get_environment_type' ) ) {
×
46
                        return wp_get_environment_type();
×
47
                }
48

49
                return 'production';
×
50
        }
51

52
        /**
53
         * Add the options page to the WP Admin
54
         *
55
         * @return void
56
         */
UNCOV
57
        public function add_options_page() {
×
58
                $graphiql_enabled = get_graphql_setting( 'graphiql_enabled' );
×
59

60
                if ( 'off' === $graphiql_enabled ) {
×
61
                        add_menu_page(
×
62
                                __( 'WPGraphQL Settings', 'wp-graphql' ),
×
63
                                __( 'GraphQL', 'wp-graphql' ),
×
64
                                'manage_options',
×
65
                                'graphql-settings',
×
66
                                [ $this, 'render_settings_page' ],
×
67
                                'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA0MDAgNDAwIj48cGF0aCBmaWxsPSIjRTEwMDk4IiBkPSJNNTcuNDY4IDMwMi42NmwtMTQuMzc2LTguMyAxNjAuMTUtMjc3LjM4IDE0LjM3NiA4LjN6Ii8+PHBhdGggZmlsbD0iI0UxMDA5OCIgZD0iTTM5LjggMjcyLjJoMzIwLjN2MTYuNkgzOS44eiIvPjxwYXRoIGZpbGw9IiNFMTAwOTgiIGQ9Ik0yMDYuMzQ4IDM3NC4wMjZsLTE2MC4yMS05Mi41IDguMy0xNC4zNzYgMTYwLjIxIDkyLjV6TTM0NS41MjIgMTMyLjk0N2wtMTYwLjIxLTkyLjUgOC4zLTE0LjM3NiAxNjAuMjEgOTIuNXoiLz48cGF0aCBmaWxsPSIjRTEwMDk4IiBkPSJNNTQuNDgyIDEzMi44ODNsLTguMy0xNC4zNzUgMTYwLjIxLTkyLjUgOC4zIDE0LjM3NnoiLz48cGF0aCBmaWxsPSIjRTEwMDk4IiBkPSJNMzQyLjU2OCAzMDIuNjYzbC0xNjAuMTUtMjc3LjM4IDE0LjM3Ni04LjMgMTYwLjE1IDI3Ny4zOHpNNTIuNSAxMDcuNWgxNi42djE4NUg1Mi41ek0zMzAuOSAxMDcuNWgxNi42djE4NWgtMTYuNnoiLz48cGF0aCBmaWxsPSIjRTEwMDk4IiBkPSJNMjAzLjUyMiAzNjdsLTcuMjUtMTIuNTU4IDEzOS4zNC04MC40NSA3LjI1IDEyLjU1N3oiLz48cGF0aCBmaWxsPSIjRTEwMDk4IiBkPSJNMzY5LjUgMjk3LjljLTkuNiAxNi43LTMxIDIyLjQtNDcuNyAxMi44LTE2LjctOS42LTIyLjQtMzEtMTIuOC00Ny43IDkuNi0xNi43IDMxLTIyLjQgNDcuNy0xMi44IDE2LjggOS43IDIyLjUgMzEgMTIuOCA0Ny43TTkwLjkgMTM3Yy05LjYgMTYuNy0zMSAyMi40LTQ3LjcgMTIuOC0xNi43LTkuNi0yMi40LTMxLTEyLjgtNDcuNyA5LjYtMTYuNyAzMS0yMi40IDQ3LjctMTIuOCAxNi43IDkuNyAyMi40IDMxIDEyLjggNDcuN00zMC41IDI5Ny45Yy05LjYtMTYuNy0zLjktMzggMTIuOC00Ny43IDE2LjctOS42IDM4LTMuOSA0Ny43IDEyLjggOS42IDE2LjcgMy45IDM4LTEyLjggNDcuNy0xNi44IDkuNi0zOC4xIDMuOS00Ny43LTEyLjhNMzA5LjEgMTM3Yy05LjYtMTYuNy0zLjktMzggMTIuOC00Ny43IDE2LjctOS42IDM4LTMuOSA0Ny43IDEyLjggOS42IDE2LjcgMy45IDM4LTEyLjggNDcuNy0xNi43IDkuNi0zOC4xIDMuOS00Ny43LTEyLjhNMjAwIDM5NS44Yy0xOS4zIDAtMzQuOS0xNS42LTM0LjktMzQuOSAwLTE5LjMgMTUuNi0zNC45IDM0LjktMzQuOSAxOS4zIDAgMzQuOSAxNS42IDM0LjkgMzQuOSAwIDE5LjItMTUuNiAzNC45LTM0LjkgMzQuOU0yMDAgNzRjLTE5LjMgMC0zNC45LTE1LjYtMzQuOS0zNC45IDAtMTkuMyAxNS42LTM0LjkgMzQuOS0zNC45IDE5LjMgMCAzNC45IDE1LjYgMzQuOSAzNC45IDAgMTkuMy0xNS42IDM0LjktMzQuOSAzNC45Ii8+PC9zdmc+'
×
68
                        );
×
69
                } else {
70
                        add_submenu_page(
×
71
                                'graphiql-ide',
×
72
                                __( 'WPGraphQL Settings', 'wp-graphql' ),
×
73
                                __( 'Settings', 'wp-graphql' ),
×
74
                                'manage_options',
×
75
                                'graphql-settings',
×
76
                                [ $this, 'render_settings_page' ]
×
77
                        );
×
78
                }
79
        }
80

81
        /**
82
         * Registers the initial settings for WPGraphQL
83
         *
84
         * @return void
85
         */
UNCOV
86
        public function register_settings() {
×
87
                $this->settings_api->register_section(
×
88
                        'graphql_general_settings',
×
89
                        [
×
90
                                'title' => __( 'WPGraphQL General Settings', 'wp-graphql' ),
×
91
                        ]
×
92
                );
×
93

94
                $custom_endpoint = apply_filters( 'graphql_endpoint', null );
×
95
                $this->settings_api->register_field(
×
96
                        'graphql_general_settings',
×
97
                        [
×
98
                                'name'              => 'graphql_endpoint',
×
99
                                'label'             => __( 'GraphQL Endpoint', 'wp-graphql' ),
×
100
                                'desc'              => sprintf(
×
101
                                        // translators: %1$s is the site url, %2$s is the default endpoint
102
                                        __( 'The endpoint (path) for the GraphQL API on the site. <a target="_blank" href="%1$s/%2$s">%1$s/%2$s</a>. <br/><strong>Note:</strong> Changing the endpoint to something other than "graphql" <em>could</em> have an affect on tooling in the GraphQL ecosystem', 'wp-graphql' ),
×
103
                                        site_url(),
×
104
                                        get_graphql_setting( 'graphql_endpoint', 'graphql' )
×
105
                                ),
×
106
                                'type'              => 'text',
×
107
                                'value'             => ! empty( $custom_endpoint ) ? $custom_endpoint : null,
×
108
                                'default'           => ! empty( $custom_endpoint ) ? $custom_endpoint : 'graphql',
×
109
                                'disabled'          => ! empty( $custom_endpoint ),
×
110
                                'sanitize_callback' => static function ( $value ) {
×
111
                                        if ( empty( $value ) ) {
×
112
                                                add_settings_error( 'graphql_endpoint', 'required', __( 'The "GraphQL Endpoint" field is required and cannot be blank. The default endpoint is "graphql"', 'wp-graphql' ), 'error' );
×
113

114
                                                return 'graphql';
×
115
                                        }
116

117
                                        return $value;
×
118
                                },
×
119
                        ]
×
120
                );
×
121

122
                $this->settings_api->register_fields(
×
123
                        'graphql_general_settings',
×
124
                        [
×
125
                                [
×
126
                                        'name'    => 'restrict_endpoint_to_logged_in_users',
×
127
                                        'label'   => __( 'Restrict Endpoint to Authenticated Users', 'wp-graphql' ),
×
128
                                        'desc'    => __( 'Limit the execution of GraphQL operations to authenticated requests. Non-authenticated requests to the GraphQL endpoint will not execute and will return an error.', 'wp-graphql' ),
×
129
                                        'type'    => 'checkbox',
×
130
                                        'default' => 'off',
×
131
                                ],
×
132
                                [
×
133
                                        'name'    => 'batch_queries_enabled',
×
134
                                        'label'   => __( 'Enable Batch Queries', 'wp-graphql' ),
×
135
                                        'desc'    => __( 'WPGraphQL supports batch queries, or the ability to send multiple GraphQL operations in a single HTTP request. Batch requests are enabled by default.', 'wp-graphql' ),
×
136
                                        'type'    => 'checkbox',
×
137
                                        'default' => 'on',
×
138
                                ],
×
139
                                [
×
140
                                        'name'    => 'batch_limit',
×
141
                                        'label'   => __( 'Batch Query Limit', 'wp-graphql' ),
×
142
                                        'desc'    => __( 'If Batch Queries are enabled, this value sets the max number of batch operations to allow per request. Requests containing more batch operations than allowed will be rejected before execution.', 'wp-graphql' ),
×
143
                                        'type'    => 'number',
×
144
                                        'default' => 10,
×
145
                                ],
×
146
                                [
×
147
                                        'name'    => 'query_depth_enabled',
×
148
                                        'label'   => __( 'Enable Query Depth Limiting', 'wp-graphql' ),
×
149
                                        'desc'    => __( 'Enabling this will limit the depth of queries WPGraphQL will execute using the value of the Max Depth setting.', 'wp-graphql' ),
×
150
                                        'type'    => 'checkbox',
×
151
                                        'default' => 'off',
×
152
                                ],
×
153
                                [
×
154
                                        'name'              => 'query_depth_max',
×
155
                                        'label'             => __( 'Max Depth to allow for GraphQL Queries', 'wp-graphql' ),
×
156
                                        'desc'              => __( 'If Query Depth limiting is enabled, this is the number of levels WPGraphQL will allow. Queries with deeper nesting will be rejected. Must be a positive integer value. Default 10.', 'wp-graphql' ),
×
157
                                        'type'              => 'number',
×
158
                                        'default'           => 10,
×
159
                                        'sanitize_callback' => static function ( $value ) {
×
160
                                                // if the entered value is not a positive integer, default to 10
161
                                                if ( ! absint( $value ) ) {
×
162
                                                        $value = 10;
×
163
                                                }
164
                                                return absint( $value );
×
165
                                        },
×
166
                                ],
×
167
                                [
×
168
                                        'name'     => 'query_analyzer_enabled',
×
169
                                        'label'    => __( 'Enable GraphQL Type Tracking', 'wp-graphql' ),
×
170
                                        'desc'     => sprintf(
×
171
                                                // translators: %s is either empty or a string with a note about force enabling.
172
                                                __( 'When enabled, WPGraphQL will track the Types, Models, and Nodes used in the request, and return those values in the headers for use in debugging or header-based cache invalidation. %s', 'wp-graphql' ),
×
173
                                                true === \WPGraphQL::debug() ? '<br /><strong>' . __( 'NOTE: This setting is force enabled because GraphQL Debug Mode is enabled. ', 'wp-graphql' ) . '</strong>' : ''
×
174
                                        ),
×
175
                                        'type'     => 'checkbox',
×
176
                                        'disabled' => true === \WPGraphQL::debug(),
×
177
                                        'value'    => true === \WPGraphQL\Utils\QueryAnalyzer::is_enabled() ? 'on' : get_graphql_setting( 'query_analyzer_enabled', 'off' ),
×
178
                                ],
×
179
                                [
×
180
                                        'name'    => 'graphiql_enabled',
×
181
                                        'label'   => __( 'Enable GraphiQL IDE', 'wp-graphql' ),
×
182
                                        'desc'    => __( 'GraphiQL IDE is a tool for exploring the GraphQL Schema and test GraphQL operations. Uncheck this to disable GraphiQL in the Dashboard.', 'wp-graphql' ),
×
183
                                        'type'    => 'checkbox',
×
184
                                        'default' => 'on',
×
185
                                ],
×
186
                                [
×
187
                                        'name'    => 'show_graphiql_link_in_admin_bar',
×
188
                                        'label'   => __( 'GraphiQL IDE Admin Bar Link', 'wp-graphql' ),
×
189
                                        'desc'    => __( 'Show GraphiQL IDE Link in the WordPress Admin Bar', 'wp-graphql' ),
×
190
                                        'type'    => 'checkbox',
×
191
                                        'default' => 'on',
×
192
                                ],
×
193
                                [
×
194
                                        'name'    => 'delete_data_on_deactivate',
×
195
                                        'label'   => __( 'Delete Data on Deactivation', 'wp-graphql' ),
×
196
                                        'desc'    => __( 'Delete settings and any other data stored by WPGraphQL upon de-activation of the plugin. Un-checking this will keep data after the plugin is de-activated.', 'wp-graphql' ),
×
197
                                        'type'    => 'checkbox',
×
198
                                        'default' => 'on',
×
199
                                ],
×
200
                                [
×
201
                                        'name'     => 'debug_mode_enabled',
×
202
                                        'label'    => __( 'Enable GraphQL Debug Mode', 'wp-graphql' ),
×
203
                                        'desc'     => defined( 'GRAPHQL_DEBUG' )
×
204
                                                // translators: %s is the value of the GRAPHQL_DEBUG constant
×
205
                                                ? sprintf( __( 'This setting is disabled. "GRAPHQL_DEBUG" has been set to "%s" with code', 'wp-graphql' ), GRAPHQL_DEBUG ? 'true' : 'false' )
×
206
                                                : __( 'Whether GraphQL requests should execute in "debug" mode. This setting is disabled if <strong>GRAPHQL_DEBUG</strong> is defined in wp-config.php. <br/>This will provide more information in GraphQL errors but can leak server implementation details so this setting is <strong>NOT RECOMMENDED FOR PRODUCTION ENVIRONMENTS</strong>.', 'wp-graphql' ),
×
207
                                        'type'     => 'checkbox',
×
208
                                        'value'    => true === \WPGraphQL::debug() ? 'on' : get_graphql_setting( 'debug_mode_enabled', 'off' ),
×
209
                                        'disabled' => defined( 'GRAPHQL_DEBUG' ),
×
210
                                        'default'  => 'off',
×
211
                                ],
×
212
                                [
×
213
                                        'name'    => 'tracing_enabled',
×
214
                                        'label'   => __( 'Enable GraphQL Tracing', 'wp-graphql' ),
×
215
                                        'desc'    => __( 'Adds trace data to the extensions portion of GraphQL responses. This can help identify bottlenecks for specific fields.', 'wp-graphql' ),
×
216
                                        'type'    => 'checkbox',
×
217
                                        'default' => 'off',
×
218
                                ],
×
219
                                [
×
220
                                        'name'    => 'tracing_user_role',
×
221
                                        'label'   => __( 'Tracing Role', 'wp-graphql' ),
×
222
                                        'desc'    => __( 'If Tracing is enabled, this limits it to requests from users with the specified User Role.', 'wp-graphql' ),
×
223
                                        'type'    => 'user_role_select',
×
224
                                        'default' => 'administrator',
×
225
                                ],
×
226
                                [
×
227
                                        'name'    => 'query_logs_enabled',
×
228
                                        'label'   => __( 'Enable GraphQL Query Logs', 'wp-graphql' ),
×
229
                                        'desc'    => __( 'Adds SQL Query logs to the extensions portion of GraphQL responses. <br/><strong>Note:</strong> This is a debug tool that can have an impact on performance and is not recommended to have active in production.', 'wp-graphql' ),
×
230
                                        'type'    => 'checkbox',
×
231
                                        'default' => 'off',
×
232
                                ],
×
233
                                [
×
234
                                        'name'    => 'query_log_user_role',
×
235
                                        'label'   => __( 'Query Log Role', 'wp-graphql' ),
×
236
                                        'desc'    => __( 'If Query Logs are enabled, this limits them to requests from users with the specified User Role.', 'wp-graphql' ),
×
237
                                        'type'    => 'user_role_select',
×
238
                                        'default' => 'administrator',
×
239
                                ],
×
240
                                [
×
241
                                        'name'     => 'public_introspection_enabled',
×
242
                                        'label'    => __( 'Enable Public Introspection', 'wp-graphql' ),
×
243
                                        'desc'     => sprintf(
×
244
                                                // translators: %s is either empty or a string with a note about debug mode.
245
                                                __( 'GraphQL Introspection is a feature that allows the GraphQL Schema to be queried. For Production and Staging environments, WPGraphQL will by default limit introspection queries to authenticated requests. Checking this enables Introspection for public requests, regardless of environment. %s ', 'wp-graphql' ),
×
246
                                                true === \WPGraphQL::debug() ? '<strong>' . __( 'NOTE: This setting is force enabled because GraphQL Debug Mode is enabled. ', 'wp-graphql' ) . '</strong>' : ''
×
247
                                        ),
×
248
                                        'type'     => 'checkbox',
×
249
                                        'default'  => ( 'local' === $this->get_wp_environment() || 'development' === $this->get_wp_environment() ) ? 'on' : 'off',
×
250
                                        'value'    => true === \WPGraphQL::debug() ? 'on' : get_graphql_setting( 'public_introspection_enabled', 'off' ),
×
251
                                        'disabled' => true === \WPGraphQL::debug(),
×
252
                                ],
×
253
                        ]
×
254
                );
×
255

256
                // Action to hook into to register settings
257
                do_action( 'graphql_register_settings', $this );
×
258
        }
259

260
        /**
261
         * Initialize the settings admin page
262
         *
263
         * @return void
264
         */
UNCOV
265
        public function initialize_settings_page() {
×
266
                $this->settings_api->admin_init();
×
267
        }
268

269
        /**
270
         * Initialize the styles and scripts used on the settings admin page
271
         *
272
         * @param ?string $hook_suffix The current admin page.
273
         */
UNCOV
274
        public function initialize_settings_page_scripts( ?string $hook_suffix ): void {
×
275

276
                if ( ! $hook_suffix ) {
×
277
                        return;
×
278
                }
279

280
                $this->settings_api->admin_enqueue_scripts( $hook_suffix );
×
281
        }
282

283
        /**
284
         * Render the settings page in the admin
285
         *
286
         * @return void
287
         */
UNCOV
288
        public function render_settings_page() {
×
289
                ?>
290
                <div class="wrap">
×
291
                        <?php
×
292
                        settings_errors();
×
293
                        $this->settings_api->show_navigation();
×
294
                        $this->settings_api->show_forms();
×
295
                        ?>
296
                </div>
×
297
                <?php
×
298
        }
299
}
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