• 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

83.64
/src/Type/ObjectType/SettingGroup.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
class SettingGroup {
10

11
        /**
12
         * Register each settings group to the GraphQL Schema
13
         *
14
         * @param string                           $group_name    The name of the setting group
15
         * @param string                           $group         The settings group config
16
         * @param \WPGraphQL\Registry\TypeRegistry $type_registry The WPGraphQL TypeRegistry
17
         *
18
         * @return string|null
19
         * @throws \Exception
20
         */
21
        public static function register_settings_group( string $group_name, string $group, TypeRegistry $type_registry ) {
593✔
22
                $fields = self::get_settings_group_fields( $group_name, $group, $type_registry );
593✔
23

24
                // if the settings group doesn't have any fields that
25
                // will map to the WPGraphQL Schema,
26
                // don't register the settings group Type to the schema
27
                if ( empty( $fields ) ) {
593✔
28
                        return null;
×
29
                }
30

31
                register_graphql_object_type(
593✔
32
                        ucfirst( $group_name ) . 'Settings',
593✔
33
                        [
593✔
34
                                // translators: %s is the name of the setting group.
35
                                'description' => static function () use ( $group_name ) {
593✔
36
                                        // translators: %s is the name of the setting group.
37
                                        return sprintf( __( 'The %s setting type', 'wp-graphql' ), $group_name );
20✔
38
                                },
593✔
39
                                'fields'      => $fields,
593✔
40
                        ]
593✔
41
                );
593✔
42

43
                return ucfirst( $group_name ) . 'Settings';
593✔
44
        }
45

46
        /**
47
         * Given the name of a registered settings group, retrieve GraphQL fields for the group
48
         *
49
         * @param string                           $group_name Name of the settings group to retrieve fields for
50
         * @param string                           $group      The settings group config
51
         * @param \WPGraphQL\Registry\TypeRegistry $type_registry The WPGraphQL TypeRegistry
52
         *
53
         * @return array<string,array<string,mixed>>|null
54
         */
55
        public static function get_settings_group_fields( string $group_name, string $group, TypeRegistry $type_registry ) {
593✔
56
                $setting_fields = DataSource::get_setting_group_fields( $group, $type_registry );
593✔
57
                $fields         = [];
593✔
58

59
                if ( ! empty( $setting_fields ) && is_array( $setting_fields ) ) {
593✔
60
                        foreach ( $setting_fields 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
                                $field_key = graphql_format_name( $field_key, ' ', '/[^a-zA-Z0-9 -]/' );
593✔
77
                                $field_key = lcfirst( str_replace( '_', ' ', ucwords( $field_key, '_' ) ) );
593✔
78
                                $field_key = lcfirst( str_replace( '-', ' ', ucwords( $field_key, '_' ) ) );
593✔
79
                                $field_key = lcfirst( str_replace( ' ', '', ucwords( $field_key, ' ' ) ) );
593✔
80

81
                                if ( ! empty( $key ) && ! empty( $field_key ) ) {
593✔
82

83
                                        /**
84
                                         * Dynamically build the individual setting and it's fields
85
                                         * then add it to the fields array
86
                                         */
87
                                        $fields[ $field_key ] = [
593✔
88
                                                'type'        => $type_registry->get_type( $setting_field['type'] ),
593✔
89
                                                // translators: %s is the name of the setting group.
90
                                                'description' => static function () use ( $setting_field ) {
593✔
91
                                                        // translators: %s is the name of the setting group.
92
                                                        return isset( $setting_field['description'] ) && ! empty( $setting_field['description'] ) ? $setting_field['description'] : sprintf( __( 'The %s Settings Group', 'wp-graphql' ), $setting_field['type'] );
20✔
93
                                                },
593✔
94
                                                'resolve'     => static function () use ( $setting_field ) {
593✔
95

96
                                                        /**
97
                                                         * Check to see if the user querying the email field has the 'manage_options' capability
98
                                                         * All other options should be public by default
99
                                                         */
100
                                                        if ( 'admin_email' === $setting_field['key'] ) {
10✔
101
                                                                if ( ! current_user_can( 'manage_options' ) ) {
×
102
                                                                        throw new UserError( esc_html__( 'Sorry, you do not have permission to view this setting.', 'wp-graphql' ) );
×
103
                                                                }
104
                                                        }
105

106
                                                        $option = ! empty( $setting_field['key'] ) ? get_option( $setting_field['key'] ) : null;
10✔
107

108
                                                        switch ( $setting_field['type'] ) {
10✔
109
                                                                case 'integer':
10✔
110
                                                                case 'int':
9✔
111
                                                                        return absint( $option );
5✔
112
                                                                case 'string':
9✔
113
                                                                        return (string) $option;
9✔
114
                                                                case 'boolean':
3✔
115
                                                                case 'bool':
×
116
                                                                        return (bool) $option;
3✔
117
                                                                case 'number':
×
118
                                                                case 'float':
×
119
                                                                        return (float) $option;
×
120
                                                        }
121

122
                                                        return ! empty( $option ) ? $option : null;
×
123
                                                },
593✔
124
                                        ];
593✔
125
                                }
126
                        }
127
                }
128

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