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

wp-graphql / wp-graphql / 14525905632

17 Apr 2025 10:27PM UTC coverage: 82.607% (-0.03%) from 82.64%
14525905632

push

github

actions-user
chore: update changeset for PR #3355

13916 of 16846 relevant lines covered (82.61%)

300.67 hits per line

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

95.83
/src/Type/WPEnumType.php
1
<?php
2
namespace WPGraphQL\Type;
3

4
use GraphQL\Type\Definition\EnumType;
5

6
/**
7
 * Class WPEnumType
8
 *
9
 * EnumTypes should extend this class to have filters and sorting applied, etc.
10
 *
11
 * @package WPGraphQL\Type
12
 */
13
class WPEnumType extends EnumType {
14

15
        /**
16
         * WPEnumType constructor.
17
         *
18
         * @param array<string,mixed> $config
19
         */
20
        public function __construct( $config ) {
409✔
21
                $name             = ucfirst( $config['name'] );
409✔
22
                $config['name']   = apply_filters( 'graphql_type_name', $name, $config, $this );
409✔
23
                $config['values'] = self::prepare_values( $config['values'], $config['name'] );
409✔
24
                parent::__construct( $config );
409✔
25
        }
26

27
        /**
28
         * Generate a safe / sanitized Enum value from a string.
29
         *
30
         * @param  string $value Enum value.
31
         * @return string
32
         */
33
        public static function get_safe_name( string $value ) {
594✔
34
                $sanitized_enum_name = graphql_format_name( $value, '_' );
594✔
35

36
                // If the sanitized name is empty, we want to return the original value so it displays in the error.
37
                if ( ! empty( $sanitized_enum_name ) ) {
594✔
38
                        $value = $sanitized_enum_name;
594✔
39
                }
40

41
                $safe_name = strtoupper( $value );
594✔
42

43
                // Enum names must start with a letter or underscore.
44
                if ( ! preg_match( '#^[_a-zA-Z]#', $safe_name ) ) {
594✔
45
                        return '_' . $safe_name;
593✔
46
                }
47

48
                return $safe_name;
594✔
49
        }
50

51
        /**
52
         * This function sorts the values and applies a filter to allow for easily
53
         * extending/modifying the shape of the Schema for the enum.
54
         *
55
         * @param array<string,mixed> $values
56
         * @param string              $type_name
57
         * @return array<string,mixed>
58
         * @since 0.0.5
59
         */
60
        private static function prepare_values( $values, $type_name ) {
409✔
61

62
                // map over the values and if the description is a callable, call it
63
                foreach ( $values as $key => $value ) {
409✔
64
                        if ( ! empty( $value['description'] ) && is_callable( $value['description'] ) ) {
409✔
65
                                $description = $value['description']();
×
66
                        } else {
67
                                $description = is_string( $value['description'] ) ? $value['description'] : '';
409✔
68
                        }
69
                        $values[ $key ]['description'] = $description;
409✔
70
                }
71

72
                /**
73
                 * Filter all object fields, passing the $typename as a param
74
                 *
75
                 * This is useful when several different types need to be easily filtered at once. . .for example,
76
                 * if ALL types with a field of a certain name needed to be adjusted, or something to that tune
77
                 *
78
                 * @param array<string,mixed> $values
79
                 * @param string              $type_name
80
                 */
81
                $values = apply_filters( 'graphql_enum_values', $values, $type_name );
409✔
82

83
                /**
84
                 * Pass the values through a filter
85
                 *
86
                 * Filter for lcfirst( $type_name ) was added for backward compatibility
87
                 *
88
                 * This is useful for more targeted filtering, and is applied after the general filter, to allow for
89
                 * more specific overrides
90
                 *
91
                 * @param array<string,mixed> $values
92
                 * @param string              $type_name
93
                 *
94
                 * @since 0.0.5
95
                 */
96
                $values = apply_filters( 'graphql_' . lcfirst( $type_name ) . '_values', $values, $type_name );
409✔
97
                $values = apply_filters( 'graphql_' . $type_name . '_values', $values, $type_name );
409✔
98

99
                /**
100
                 * Sort the values alphabetically by key. This makes reading through docs much easier
101
                 *
102
                 * @since 0.0.5
103
                 */
104
                ksort( $values );
409✔
105

106
                /**
107
                 * Return the filtered, sorted $fields
108
                 *
109
                 * @since 0.0.5
110
                 */
111
                return $values;
409✔
112
        }
113
}
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