• 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

89.29
/src/Type/ObjectType/Settings.php
1
<?php
2

3
namespace WPGraphQL\Type\ObjectType;
4

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

9
/**
10
 * Class Settings
11
 *
12
 * @package WPGraphQL\Type\Object
13
 */
14
class Settings {
15

16
        /**
17
         * Registers a Settings Type with fields for all settings based on settings
18
         * registered using the core register_setting API
19
         *
20
         * @param \WPGraphQL\Registry\TypeRegistry $type_registry The WPGraphQL TypeRegistry
21
         *
22
         * @return void
23
         */
24
        public static function register_type( TypeRegistry $type_registry ) {
593✔
25
                $fields = self::get_fields( $type_registry );
593✔
26

27
                if ( empty( $fields ) ) {
593✔
28
                        return;
×
29
                }
30

31
                register_graphql_object_type(
593✔
32
                        'Settings',
593✔
33
                        [
593✔
34
                                'description' => static function () {
593✔
35
                                        return __( 'All of the registered settings', 'wp-graphql' );
18✔
36
                                },
593✔
37
                                'fields'      => $fields,
593✔
38
                        ]
593✔
39
                );
593✔
40
        }
41

42
        /**
43
         * Returns an array of fields for all settings based on the `register_setting` WordPress API
44
         *
45
         * @param \WPGraphQL\Registry\TypeRegistry $type_registry The WPGraphQL TypeRegistry
46
         *
47
         * @return array<string,array<string,mixed>>
48
         */
49
        public static function get_fields( TypeRegistry $type_registry ) {
593✔
50
                $registered_settings = DataSource::get_allowed_settings( $type_registry );
593✔
51
                $fields              = [];
593✔
52

53
                if ( ! empty( $registered_settings ) && is_array( $registered_settings ) ) {
593✔
54

55
                        /**
56
                         * Loop through the $settings_array and build thevar
57
                         * setting with
58
                         * proper fields
59
                         */
60
                        foreach ( $registered_settings as $key => $setting_field ) {
593✔
61
                                if ( ! isset( $setting_field['type'] ) || ! $type_registry->get_type( $setting_field['type'] ) ) {
593✔
62
                                        continue;
×
63
                                }
64

65
                                /**
66
                                 * Determine if the individual setting already has a
67
                                 * REST API name, if not use the option name.
68
                                 * Then, sanitize the field name to be camelcase
69
                                 */
70
                                if ( ! empty( $setting_field['show_in_rest']['name'] ) ) {
593✔
71
                                        $field_key = $setting_field['show_in_rest']['name'];
593✔
72
                                } else {
73
                                        $field_key = $key;
593✔
74
                                }
75

76
                                $group = DataSource::format_group_name( $setting_field['group'] );
593✔
77

78
                                $field_key = lcfirst( graphql_format_name( $field_key, ' ', '/[^a-zA-Z0-9 -]/' ) );
593✔
79
                                $field_key = lcfirst( str_replace( '_', ' ', ucwords( $field_key, '_' ) ) );
593✔
80
                                $field_key = lcfirst( str_replace( '-', ' ', ucwords( $field_key, '_' ) ) );
593✔
81
                                $field_key = lcfirst( str_replace( ' ', '', ucwords( $field_key, ' ' ) ) );
593✔
82

83
                                $field_key = $group . 'Settings' . ucfirst( $field_key );
593✔
84

85
                                if ( ! empty( $key ) ) {
593✔
86

87
                                        /**
88
                                         * Dynamically build the individual setting and it's fields
89
                                         * then add it to $fields
90
                                         */
91
                                        $fields[ $field_key ] = [
593✔
92
                                                'type'        => $setting_field['type'],
593✔
93
                                                // translators: %s is the name of the setting group.
94
                                                'description' => static function () use ( $setting_field ) {
593✔
95
                                                        // translators: %s is the name of the setting group.
96
                                                        return sprintf( __( 'Settings of the the %s Settings Group', 'wp-graphql' ), $setting_field['type'] );
18✔
97
                                                },
593✔
98
                                                'resolve'     => static function () use ( $setting_field, $key ) {
593✔
99
                                                        /**
100
                                                         * Check to see if the user querying the email field has the 'manage_options' capability
101
                                                         * All other options should be public by default
102
                                                         */
103
                                                        if ( 'admin_email' === $key && ! current_user_can( 'manage_options' ) ) {
4✔
104
                                                                throw new UserError( esc_html__( 'Sorry, you do not have permission to view this setting.', 'wp-graphql' ) );
×
105
                                                        }
106

107
                                                        $option = get_option( (string) $key );
4✔
108

109
                                                        switch ( $setting_field['type'] ) {
4✔
110
                                                                case 'integer':
4✔
111
                                                                        $option = absint( $option );
3✔
112
                                                                        break;
3✔
113
                                                                case 'string':
4✔
114
                                                                        $option = ! empty( $option ) ? (string) $option : '';
4✔
115
                                                                        break;
4✔
116
                                                                case 'boolean':
3✔
117
                                                                        $option = (bool) $option;
3✔
118
                                                                        break;
3✔
119
                                                                case 'number':
×
120
                                                                        $option = (float) $option;
×
121
                                                                        break;
×
122
                                                        }
123

124
                                                        return isset( $option ) ? $option : null;
4✔
125
                                                },
593✔
126
                                        ];
593✔
127
                                }
128
                        }
129
                }
130

131
                return $fields;
593✔
132
        }
133
}
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