• 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.34
/src/Mutation/UpdateSettings.php
1
<?php
2

3
namespace WPGraphQL\Mutation;
4

5
use GraphQL\Error\UserError;
6
use WPGraphQL\Data\DataSource;
7
use WPGraphQL\Registry\TypeRegistry;
8
use WPGraphQL\Utils\Utils;
9

10
/**
11
 * Class UpdateSettings
12
 *
13
 * @package WPGraphQL\Mutation
14
 */
15
class UpdateSettings {
16

17
        /**
18
         * Registers the CommentCreate mutation.
19
         *
20
         * @param \WPGraphQL\Registry\TypeRegistry $type_registry The WPGraphQL TypeRegistry
21
         *
22
         * @return void
23
         */
24
        public static function register_mutation( TypeRegistry $type_registry ) {
593✔
25
                $output_fields = self::get_output_fields( $type_registry );
593✔
26
                $input_fields  = self::get_input_fields( $type_registry );
593✔
27

28
                if ( empty( $output_fields ) || empty( $input_fields ) ) {
593✔
29
                        return;
×
30
                }
31

32
                register_graphql_mutation(
593✔
33
                        'updateSettings',
593✔
34
                        [
593✔
35
                                'inputFields'         => $input_fields,
593✔
36
                                'outputFields'        => $output_fields,
593✔
37
                                'mutateAndGetPayload' => static function ( $input ) use ( $type_registry ) {
593✔
38
                                        return self::mutate_and_get_payload( $input, $type_registry );
4✔
39
                                },
593✔
40
                        ]
593✔
41
                );
593✔
42
        }
43

44
        /**
45
         * Defines the mutation input field configuration.
46
         *
47
         * @param \WPGraphQL\Registry\TypeRegistry $type_registry The WPGraphQL TypeRegistry
48
         *
49
         * @return array<string,array<string,mixed>>
50
         */
51
        public static function get_input_fields( TypeRegistry $type_registry ) {
593✔
52
                $allowed_settings = DataSource::get_allowed_settings( $type_registry );
593✔
53

54
                $input_fields = [];
593✔
55

56
                if ( ! empty( $allowed_settings ) ) {
593✔
57

58
                        /**
59
                         * Loop through the $allowed_settings and build fields
60
                         * for the individual settings
61
                         */
62
                        foreach ( $allowed_settings as $key => $setting ) {
593✔
63
                                if ( ! isset( $setting['type'] ) || ! $type_registry->get_type( $setting['type'] ) ) {
593✔
64
                                        continue;
×
65
                                }
66

67
                                /**
68
                                 * Determine if the individual setting already has a
69
                                 * REST API name, if not use the option name.
70
                                 * Sanitize the field name to be camelcase
71
                                 */
72
                                if ( ! empty( $setting['show_in_rest']['name'] ) ) {
593✔
73
                                        $individual_setting_key = lcfirst( $setting['group'] . 'Settings' . str_replace( '_', '', ucwords( $setting['show_in_rest']['name'], '_' ) ) );
593✔
74
                                } else {
75
                                        $individual_setting_key = lcfirst( $setting['group'] . 'Settings' . str_replace( '_', '', ucwords( $key, '_' ) ) );
593✔
76
                                }
77

78
                                $replaced_setting_key = graphql_format_name( $individual_setting_key, ' ', '/[^a-zA-Z0-9 -]/' );
593✔
79

80
                                if ( ! empty( $replaced_setting_key ) ) {
593✔
81
                                        $individual_setting_key = $replaced_setting_key;
593✔
82
                                }
83

84
                                $individual_setting_key = lcfirst( $individual_setting_key );
593✔
85
                                $individual_setting_key = lcfirst( str_replace( '_', ' ', ucwords( $individual_setting_key, '_' ) ) );
593✔
86
                                $individual_setting_key = lcfirst( str_replace( '-', ' ', ucwords( $individual_setting_key, '_' ) ) );
593✔
87
                                $individual_setting_key = lcfirst( str_replace( ' ', '', ucwords( $individual_setting_key, ' ' ) ) );
593✔
88

89
                                /**
90
                                 * Dynamically build the individual setting,
91
                                 * then add it to the $input_fields
92
                                 */
93
                                $input_fields[ $individual_setting_key ] = [
593✔
94
                                        'type'        => $setting['type'],
593✔
95
                                        'description' => $setting['description'],
593✔
96
                                ];
593✔
97
                        }
98
                }
99

100
                return $input_fields;
593✔
101
        }
102

103
        /**
104
         * Defines the mutation output field configuration.
105
         *
106
         * @param \WPGraphQL\Registry\TypeRegistry $type_registry The WPGraphQL TypeRegistry
107
         *
108
         * @return array<string,array<string,mixed>>
109
         */
110
        public static function get_output_fields( TypeRegistry $type_registry ) {
593✔
111

112
                /**
113
                 * Get the allowed setting groups and their fields
114
                 */
115
                $allowed_setting_groups = DataSource::get_allowed_settings_by_group( $type_registry );
593✔
116

117
                $output_fields = [];
593✔
118

119
                /**
120
                 * Get all of the settings, regardless of group
121
                 */
122
                $output_fields['allSettings'] = [
593✔
123
                        'type'        => 'Settings',
593✔
124
                        'description' => static function () {
593✔
125
                                return __( 'Update all settings.', 'wp-graphql' );
15✔
126
                        },
593✔
127
                        'resolve'     => static function () {
593✔
128
                                return true;
3✔
129
                        },
593✔
130
                ];
593✔
131

132
                if ( ! empty( $allowed_setting_groups ) && is_array( $allowed_setting_groups ) ) {
593✔
133
                        foreach ( $allowed_setting_groups as $group => $setting_type ) {
593✔
134
                                $setting_type      = DataSource::format_group_name( $group );
593✔
135
                                $setting_type_name = Utils::format_type_name( $setting_type . 'Settings' );
593✔
136

137
                                $output_fields[ Utils::format_field_name( $setting_type_name ) ] = [
593✔
138
                                        'type'        => $setting_type_name,
593✔
139
                                        // translators: %s is the setting type name
140
                                        'description' => static function () use ( $setting_type_name ) {
593✔
141
                                                // translators: %s is the setting type name
142
                                                return sprintf( __( 'Update the %s setting.', 'wp-graphql' ), $setting_type_name );
15✔
143
                                        },
593✔
144
                                        'resolve'     => static function () use ( $setting_type_name ) {
593✔
145
                                                return $setting_type_name;
3✔
146
                                        },
593✔
147
                                ];
593✔
148
                        }
149
                }
150
                return $output_fields;
593✔
151
        }
152

153
        /**
154
         * Defines the mutation data modification closure.
155
         *
156
         * @param array<string,mixed>              $input The mutation input
157
         * @param \WPGraphQL\Registry\TypeRegistry $type_registry The WPGraphQL TypeRegistry
158
         *
159
         * @return array<string,array<string,string>>
160
         *
161
         * @throws \GraphQL\Error\UserError
162
         */
163
        public static function mutate_and_get_payload( array $input, TypeRegistry $type_registry ): array {
4✔
164
                /**
165
                 * Check that the user can manage setting options
166
                 */
167
                if ( ! current_user_can( 'manage_options' ) ) {
4✔
168
                        throw new UserError( esc_html__( 'Sorry, you are not allowed to edit settings as this user.', 'wp-graphql' ) );
1✔
169
                }
170

171
                /**
172
                 * The $updatable_settings_options will store all of the allowed
173
                 * settings in a WP ready format
174
                 */
175
                $updatable_settings_options = [];
3✔
176

177
                $allowed_settings = DataSource::get_allowed_settings( $type_registry );
3✔
178

179
                /**
180
                 * Loop through the $allowed_settings and build the insert options array
181
                 */
182
                foreach ( $allowed_settings as $key => $setting ) {
3✔
183

184
                        /**
185
                         * Determine if the individual setting already has a
186
                         * REST API name, if not use the option name.
187
                         * Sanitize the field name to be camelcase
188
                         */
189
                        if ( isset( $setting['show_in_rest']['name'] ) && ! empty( $setting['show_in_rest']['name'] ) ) {
3✔
190
                                $individual_setting_key = lcfirst( $setting['group'] . 'Settings' . str_replace( '_', '', ucwords( $setting['show_in_rest']['name'], '_' ) ) );
3✔
191
                        } else {
192
                                $individual_setting_key = lcfirst( $setting['group'] . 'Settings' . str_replace( '_', '', ucwords( $key, '_' ) ) );
3✔
193
                        }
194

195
                        /**
196
                         * Dynamically build the individual setting,
197
                         * then add it to $updatable_settings_options
198
                         */
199
                        $updatable_settings_options[ Utils::format_field_name( $individual_setting_key ) ] = [
3✔
200
                                'option' => $key,
3✔
201
                                'group'  => $setting['group'],
3✔
202
                        ];
3✔
203
                }
204

205
                foreach ( $input as $key => $value ) {
3✔
206
                        /**
207
                         * Throw an error if the input field is the site url,
208
                         * as we do not want users changing it and breaking all
209
                         * the things
210
                         */
211
                        if ( 'generalSettingsUrl' === $key ) {
3✔
212
                                throw new UserError( esc_html__( 'Sorry, that is not allowed, speak with your site administrator to change the site URL.', 'wp-graphql' ) );
×
213
                        }
214

215
                        /**
216
                         * Check to see that the input field exists in settings, if so grab the option
217
                         * name and update the option
218
                         */
219
                        if ( array_key_exists( $key, $updatable_settings_options ) ) {
3✔
220
                                update_option( $updatable_settings_options[ $key ]['option'], $value );
3✔
221
                        }
222
                }
223

224
                return $updatable_settings_options;
3✔
225
        }
226
}
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