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

wp-graphql / wp-graphql-woocommerce / 9122745284

17 May 2024 04:03AM UTC coverage: 85.048% (+0.4%) from 84.647%
9122745284

push

github

web-flow
feat: Queries and mutations for shipping zones, tax classes, and tax rates. (#856)

* fix: General bugfixes and improvements

* devops: New mutations and types tested and compliance with Linter and PHPStan

* chore: hooks added to the mutation resolvers

* feat: permission checks added

* chore: Linter and compliance met

* chore: Linter and compliance met

1252 of 1399 new or added lines in 39 files covered. (89.49%)

9 existing lines in 2 files now uncovered.

12423 of 14607 relevant lines covered (85.05%)

70.56 hits per line

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

4.35
/includes/data/mutation/class-settings-mutation.php
1
<?php
2
/**
3
 * Defines helper functions for executing mutations related to the WC Settings API.
4
 *
5
 * @package WPGraphQL\WooCommerce\Data\Mutation
6
 * @since TBD
7
 */
8

9
namespace WPGraphQL\WooCommerce\Data\Mutation;
10

11
use GraphQL\Error\UserError;
12

13
/**
14
 * Class - Settings_Mutation
15
 */
16
class Settings_Mutation {
17
        /**
18
         * Validate a text value for a text based setting.
19
         *
20
         * @param string $value Value.
21
         * @param array  $setting Setting.
22
         * @return string
23
         */
24
        public static function validate_setting_text_field( $value, $setting ) {
25
                $value = is_null( $value ) ? '' : $value;
2✔
26
                return wp_kses_post( trim( stripslashes( $value ) ) );
2✔
27
        }
28

29
        /**
30
         * Validate select based settings.
31
         *
32
         * @param string $value Value.
33
         * @param array  $setting Setting.
34
         *
35
         * @throws \GraphQL\Error\UserError If an invalid setting value was passed.
36
         *
37
         * @return string
38
         */
39
        public static function validate_setting_select_field( $value, $setting ) {
NEW
40
                if ( array_key_exists( $value, $setting['options'] ) ) {
×
NEW
41
                        return $value;
×
42
                } else {
NEW
43
                        throw new UserError( __( 'An invalid setting value was passed.', 'wp-graphql-woocommerce' ), 400 );
×
44
                }
45
        }
46

47
        /**
48
         * Validate multiselect based settings.
49
         *
50
         * @param array $values Values.
51
         * @param array $setting Setting.
52
         *
53
         * @throws \GraphQL\Error\UserError If an invalid setting value was passed.
54
         *
55
         * @return array
56
         */
57
        public static function validate_setting_multiselect_field( $values, $setting ) {
NEW
58
                if ( empty( $values ) ) {
×
NEW
59
                        return [];
×
60
                }
61

NEW
62
                if ( ! is_array( $values ) ) {
×
NEW
63
                        throw new UserError( __( 'An invalid setting value was passed.', 'wp-graphql-woocommerce' ), 400 );
×
64
                }
65

NEW
66
                $final_values = [];
×
NEW
67
                foreach ( $values as $value ) {
×
NEW
68
                        if ( array_key_exists( $value, $setting['options'] ) ) {
×
NEW
69
                                $final_values[] = $value;
×
70
                        }
71
                }
72

NEW
73
                return $final_values;
×
74
        }
75

76
        /**
77
         * Validate image_width based settings.
78
         *
79
         * @param array $values Values.
80
         * @param array $setting Setting.
81
         *
82
         * @throws \GraphQL\Error\UserError If an invalid setting value was passed.
83
         *
84
         * @return string
85
         */
86
        public static function validate_setting_image_width_field( $values, $setting ) {
NEW
87
                if ( ! is_array( $values ) ) {
×
NEW
88
                        throw new UserError( __( 'An invalid setting value was passed.', 'wp-graphql-woocommerce' ), 400 );
×
89
                }
90

NEW
91
                $current = $setting['value'];
×
NEW
92
                if ( isset( $values['width'] ) ) {
×
NEW
93
                        $current['width'] = intval( $values['width'] );
×
94
                }
NEW
95
                if ( isset( $values['height'] ) ) {
×
NEW
96
                        $current['height'] = intval( $values['height'] );
×
97
                }
NEW
98
                if ( isset( $values['crop'] ) ) {
×
NEW
99
                        $current['crop'] = (bool) $values['crop'];
×
100
                }
NEW
101
                return $current;
×
102
        }
103

104
        /**
105
         * Validate radio based settings.
106
         *
107
         * @param string $value Value.
108
         * @param array  $setting Setting.
109
         *
110
         * @throws \GraphQL\Error\UserError If an invalid setting value was passed.
111
         *
112
         * @return string
113
         */
114
        public static function validate_setting_radio_field( $value, $setting ) {
NEW
115
                return self::validate_setting_select_field( $value, $setting );
×
116
        }
117

118
        /**
119
         * Validate checkbox based settings.
120
         *
121
         * @param string $value Value.
122
         * @param array  $setting Setting.
123
         *
124
         * @throws \GraphQL\Error\UserError If an invalid setting value was passed.
125
         *
126
         * @return string
127
         */
128
        public function validate_setting_checkbox_field( $value, $setting ) {
NEW
129
                if ( in_array( $value, [ 'yes', 'no' ], true ) ) {
×
NEW
130
                        return $value;
×
NEW
131
                } elseif ( empty( $value ) ) {
×
NEW
132
                        $value = isset( $setting['default'] ) ? $setting['default'] : 'no';
×
NEW
133
                        return $value;
×
134
                } else {
NEW
135
                        throw new UserError( __( 'An invalid setting value was passed.', 'wp-graphql-woocommerce' ), 400 );
×
136
                }
137
        }
138

139
        /**
140
         * Validate textarea based settings.
141
         *
142
         * @param string $value Value.
143
         * @param array  $setting Setting.
144
         *
145
         * @return string
146
         */
147
        public static function validate_setting_textarea_field( $value, $setting ) {
NEW
148
                $value = is_null( $value ) ? '' : $value;
×
NEW
149
                return wp_kses(
×
NEW
150
                        trim( stripslashes( $value ) ),
×
NEW
151
                        array_merge(
×
NEW
152
                                [
×
NEW
153
                                        'iframe' => [
×
NEW
154
                                                'src'   => true,
×
NEW
155
                                                'style' => true,
×
NEW
156
                                                'id'    => true,
×
NEW
157
                                                'class' => true,
×
NEW
158
                                        ],
×
NEW
159
                                ],
×
NEW
160
                                wp_kses_allowed_html( 'post' )
×
NEW
161
                        )
×
NEW
162
                );
×
163
        }
164
}
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