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

wp-graphql / wp-graphql-woocommerce / 23661591439

27 Mar 2026 06:30PM UTC coverage: 89.39% (+0.2%) from 89.165%
23661591439

Pull #1002

github

web-flow
Merge 440d92338 into 66f840efc
Pull Request #1002: feat: WC Settings API, compatibility refactor, HPOS fix

575 of 643 new or added lines in 21 files covered. (89.42%)

15806 of 17682 relevant lines covered (89.39%)

140.84 hits per line

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

98.8
/includes/type/interface/class-wc-setting.php
1
<?php
2
/**
3
 * WPInterface Type - WCSetting
4
 *
5
 * Registers WCSetting interface with common fields shared
6
 * by all WC setting concrete types.
7
 *
8
 * @package WPGraphQL\WooCommerce\Type\WPInterface
9
 * @since   TBD
10
 */
11

12
namespace WPGraphQL\WooCommerce\Type\WPInterface;
13

14
/**
15
 * Class WC_Setting
16
 */
17
class WC_Setting {
18
        /**
19
         * Mapping of WC setting types to normalized enum values.
20
         *
21
         * @var array
22
         */
23
        private static $type_map = [
24
                'multi_select_countries'         => 'multiselect',
25
                'single_select_country'          => 'select',
26
                'single_select_page'             => 'select',
27
                'single_select_page_with_search' => 'select',
28
                'thumbnail_cropping'             => 'text',
29
        ];
30

31
        /**
32
         * Default setting type to GraphQL concrete type mapping.
33
         *
34
         * @var array
35
         */
36
        private static $default_graphql_type_map = [
37
                'multiselect'            => 'WCArraySetting',
38
                'relative_date_selector' => 'WCRelativeDateSetting',
39
                'image_width'            => 'WCImageWidthSetting',
40
        ];
41

42
        /**
43
         * Registers the WCSetting interface.
44
         *
45
         * @return void
46
         */
47
        public static function register_interface() {
48
                register_graphql_interface_type(
277✔
49
                        'WCSetting',
277✔
50
                        [
277✔
51
                                'eagerlyLoadType' => true,
277✔
52
                                'description'     => __( 'A WC setting object', 'wp-graphql-woocommerce' ),
277✔
53
                                'resolveType'     => [ self::class, 'resolve_type' ],
277✔
54
                                'fields'          => [
277✔
55
                                        'id'          => [
277✔
56
                                                'type'        => [ 'non_null' => 'ID' ],
277✔
57
                                                'description' => __( 'The globally unique identifier for the WC setting.', 'wp-graphql-woocommerce' ),
277✔
58
                                                'resolve'     => static function ( $source ) {
277✔
59
                                                        return $source['id'] ?? null;
12✔
60
                                                },
277✔
61
                                        ],
277✔
62
                                        'label'       => [
277✔
63
                                                'type'        => 'String',
277✔
64
                                                'description' => __( 'A human readable label for the setting used in user interfaces.', 'wp-graphql-woocommerce' ),
277✔
65
                                                'resolve'     => static function ( $source ) {
277✔
66
                                                        return $source['label'] ?? $source['title'] ?? null;
5✔
67
                                                },
277✔
68
                                        ],
277✔
69
                                        'groupId'     => [
277✔
70
                                                'type'        => 'String',
277✔
71
                                                'description' => __( 'The ID of the settings group this setting belongs to.', 'wp-graphql-woocommerce' ),
277✔
72
                                                'resolve'     => static function ( $source ) {
277✔
73
                                                        return $source['group_id'] ?? null;
1✔
74
                                                },
277✔
75
                                        ],
277✔
76
                                        'description' => [
277✔
77
                                                'type'        => 'String',
277✔
78
                                                'description' => __( 'A human readable description for the setting used in user interfaces.', 'wp-graphql-woocommerce' ),
277✔
79
                                                'resolve'     => static function ( $source ) {
277✔
80
                                                        return ! empty( $source['description'] ) ? $source['description'] : null;
4✔
81
                                                },
277✔
82
                                        ],
277✔
83
                                        'type'        => [
277✔
84
                                                'type'        => 'WCSettingTypeEnum',
277✔
85
                                                'description' => __( 'Type of setting.', 'wp-graphql-woocommerce' ),
277✔
86
                                                'resolve'     => static function ( $source ) {
277✔
87
                                                        $raw_type = $source['type'] ?? '';
6✔
88
                                                        return self::$type_map[ $raw_type ] ?? ( ! empty( $raw_type ) ? $raw_type : null );
6✔
89
                                                },
277✔
90
                                        ],
277✔
91
                                        'tip'         => [
277✔
92
                                                'type'        => 'String',
277✔
93
                                                'description' => __( 'Additional help text shown to the user about the setting', 'wp-graphql-woocommerce' ),
277✔
94
                                                'resolve'     => static function ( $source ) {
277✔
95
                                                        return ! empty( $source['desc_tip'] ) ? $source['desc_tip'] : ( ! empty( $source['tip'] ) ? $source['tip'] : null );
4✔
96
                                                },
277✔
97
                                        ],
277✔
98
                                        'placeholder' => [
277✔
99
                                                'type'        => 'String',
277✔
100
                                                'description' => __( 'Placeholder text to be displayed in text inputs.', 'wp-graphql-woocommerce' ),
277✔
101
                                                'resolve'     => static function ( $source ) {
277✔
102
                                                        return ! empty( $source['placeholder'] ) ? $source['placeholder'] : null;
4✔
103
                                                },
277✔
104
                                        ],
277✔
105
                                        'options'     => [
277✔
106
                                                'type'        => [ 'list_of' => 'String' ],
277✔
107
                                                'description' => __( 'Array of option key/value pairs for select and multiselect types.', 'wp-graphql-woocommerce' ),
277✔
108
                                                'resolve'     => static function ( $source ) {
277✔
109
                                                        if ( empty( $source['options'] ) || ! is_array( $source['options'] ) ) {
1✔
110
                                                                return null;
1✔
111
                                                        }
112

113
                                                        $options = [];
1✔
114
                                                        foreach ( $source['options'] as $key => $label ) {
1✔
115
                                                                $options[] = $key . ':' . $label;
1✔
116
                                                        }
117

118
                                                        return $options;
1✔
119
                                                },
277✔
120
                                        ],
277✔
121
                                ],
277✔
122
                        ]
277✔
123
                );
277✔
124
        }
125

126
        /**
127
         * Resolves a setting array to the correct concrete GraphQL type.
128
         *
129
         * @param array $source The setting data array.
130
         *
131
         * @return string The GraphQL type name.
132
         */
133
        public static function resolve_type( $source ) {
134
                $raw_type        = $source['type'] ?? '';
9✔
135
                $normalized_type = self::$type_map[ $raw_type ] ?? $raw_type;
9✔
136
                if ( empty( $normalized_type ) ) {
9✔
NEW
137
                        $normalized_type = 'text';
×
138
                }
139

140
                /**
141
                 * Filters the mapping of WC setting types to GraphQL concrete type names.
142
                 *
143
                 * Allows third-party plugins to register custom setting types and map them
144
                 * to their own GraphQL types that implement the WCSetting interface.
145
                 *
146
                 * @param array  $type_map        Map of WC setting type => GraphQL type name.
147
                 * @param string $normalized_type  The normalized setting type.
148
                 * @param array  $source           The raw setting data.
149
                 */
150
                $graphql_type_map = apply_filters(
9✔
151
                        'graphql_woocommerce_setting_type_map',
9✔
152
                        self::$default_graphql_type_map,
9✔
153
                        $normalized_type,
9✔
154
                        $source
9✔
155
                );
9✔
156

157
                return $graphql_type_map[ $normalized_type ] ?? 'WCStringSetting';
9✔
158
        }
159
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc