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

wp-graphql / wp-graphql-woocommerce / 23675172456

28 Mar 2026 02:10AM UTC coverage: 70.983% (-18.4%) from 89.424%
23675172456

Pull #1003

github

web-flow
Merge 05339093d into 6fb7b226f
Pull Request #1003: devops: WC email template tests, COT cursor HPOS fix, checkout account auth

71 of 81 new or added lines in 5 files covered. (87.65%)

3346 existing lines in 124 files now uncovered.

12576 of 17717 relevant lines covered (70.98%)

55.38 hits per line

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

50.0
/includes/mutation/class-setting-update.php
1
<?php
2
/**
3
 * Mutation - updateWCSetting
4
 *
5
 * Registers mutation for updating a single WooCommerce setting.
6
 *
7
 * @package WPGraphQL\WooCommerce\Mutation
8
 * @since   TBD
9
 */
10

11
namespace WPGraphQL\WooCommerce\Mutation;
12

13
use GraphQL\Error\UserError;
14
use WPGraphQL\WooCommerce\Data\Mutation\Settings_Mutation;
15

16
/**
17
 * Class Setting_Update
18
 */
19
class Setting_Update {
20
        /**
21
         * Registers mutation
22
         *
23
         * @return void
24
         */
25
        public static function register_mutation() {
26
                register_graphql_mutation(
110✔
27
                        'updateWCSetting',
110✔
28
                        [
110✔
29
                                'inputFields'         => self::get_input_fields(),
110✔
30
                                'outputFields'        => self::get_output_fields(),
110✔
31
                                'mutateAndGetPayload' => self::mutate_and_get_payload(),
110✔
32
                        ]
110✔
33
                );
110✔
34
        }
35

36
        /**
37
         * Defines the mutation input field configuration
38
         *
39
         * @return array
40
         */
41
        public static function get_input_fields() {
42
                return [
110✔
43
                        'group' => [
110✔
44
                                'type'        => [ 'non_null' => 'String' ],
110✔
45
                                'description' => __( 'Settings group ID.', 'wp-graphql-woocommerce' ),
110✔
46
                        ],
110✔
47
                        'id'    => [
110✔
48
                                'type'        => [ 'non_null' => 'String' ],
110✔
49
                                'description' => __( 'Setting ID.', 'wp-graphql-woocommerce' ),
110✔
50
                        ],
110✔
51
                        'value' => [
110✔
52
                                'type'        => [ 'non_null' => 'String' ],
110✔
53
                                'description' => __( 'Setting value.', 'wp-graphql-woocommerce' ),
110✔
54
                        ],
110✔
55
                ];
110✔
56
        }
57

58
        /**
59
         * Defines the mutation output field configuration
60
         *
61
         * @return array
62
         */
63
        public static function get_output_fields() {
64
                return [
110✔
65
                        'setting' => [
110✔
66
                                'type'        => 'WCSetting',
110✔
67
                                'description' => __( 'The updated setting.', 'wp-graphql-woocommerce' ),
110✔
68
                                'resolve'     => static function ( $payload ) {
110✔
UNCOV
69
                                        return $payload['setting'];
×
70
                                },
110✔
71
                        ],
110✔
72
                ];
110✔
73
        }
74

75
        /**
76
         * Defines the mutation data modification closure.
77
         *
78
         * @return callable
79
         */
80
        public static function mutate_and_get_payload() {
81
                return static function ( $input ) {
110✔
UNCOV
82
                        if ( ! \wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
×
UNCOV
83
                                throw new UserError( __( 'Sorry, you cannot update settings.', 'wp-graphql-woocommerce' ) );
×
84
                        }
85

UNCOV
86
                        $group_id   = $input['group'];
×
UNCOV
87
                        $setting_id = $input['id'];
×
UNCOV
88
                        $value      = $input['value'];
×
89

UNCOV
90
                        $controller = new \WC_REST_Setting_Options_Controller();
×
91

92
                        /** @var array|\WP_Error $setting */
UNCOV
93
                        $setting = $controller->get_setting( $group_id, $setting_id );
×
94

UNCOV
95
                        if ( is_wp_error( $setting ) ) {
×
UNCOV
96
                                throw new UserError( $setting->get_error_message() );
×
97
                        }
98

UNCOV
99
                        $validated = self::validate_value( $value, $setting );
×
UNCOV
100
                        self::save_setting( $setting, $validated );
×
101

102
                        /** @var array|\WP_Error $updated */
UNCOV
103
                        $updated = $controller->get_setting( $group_id, $setting_id );
×
UNCOV
104
                        if ( is_wp_error( $updated ) ) {
×
105
                                throw new UserError( $updated->get_error_message() );
×
106
                        }
107

UNCOV
108
                        return [ 'setting' => $updated ];
×
109
                };
110✔
110
        }
111

112
        /**
113
         * Validate a setting value based on its type.
114
         *
115
         * @param mixed $value   Value to validate.
116
         * @param array $setting Setting definition.
117
         *
118
         * @throws \GraphQL\Error\UserError If validation fails.
119
         *
120
         * @return mixed
121
         */
122
        public static function validate_value( $value, $setting ) {
UNCOV
123
                $type   = $setting['type'];
×
UNCOV
124
                $method = 'validate_setting_' . $type . '_field';
×
125

UNCOV
126
                if ( method_exists( Settings_Mutation::class, $method ) ) {
×
UNCOV
127
                        return Settings_Mutation::$method( $value, $setting );
×
128
                }
129

UNCOV
130
                return Settings_Mutation::validate_setting_text_field( $value, $setting );
×
131
        }
132

133
        /**
134
         * Save a validated setting value.
135
         *
136
         * @param array $setting Setting definition.
137
         * @param mixed $value   Validated value.
138
         *
139
         * @return void
140
         */
141
        public static function save_setting( $setting, $value ) {
UNCOV
142
                $option_key = $setting['option_key'];
×
143

UNCOV
144
                if ( is_array( $option_key ) ) {
×
145
                        $option = get_option( $option_key[0], [] );
×
146
                        if ( ! is_array( $option ) ) {
×
147
                                $option = [];
×
148
                        }
149
                        $option[ $option_key[1] ] = $value;
×
150
                        update_option( $option_key[0], $option );
×
151
                } else {
UNCOV
152
                        \WC_Admin_Settings::save_fields(
×
UNCOV
153
                                [ $setting ],
×
UNCOV
154
                                [ $setting['id'] => $value ]
×
UNCOV
155
                        );
×
156
                }
157
        }
158
}
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