Autogenerated
4 weeks ago
GraphQLEndpointRegistrar.php
4 weeks ago
OpcacheFileExpiry.php
4 weeks ago
QueryCache.php
4 weeks ago
QueryComplexityRule.php
4 weeks ago
QueryDepthRule.php
4 weeks ago
Settings.php
4 weeks ago
StatusResolverFailedException.php
4 weeks ago
Settings.php
188 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\Api; |
| 6 | |
| 7 | use Automattic\WooCommerce\Api\Infrastructure\GraphQLControllerBase; |
| 8 | use Automattic\WooCommerce\Api\Infrastructure\Main; |
| 9 | |
| 10 | /** |
| 11 | * Settings handling for the GraphQL API. |
| 12 | * |
| 13 | * Registers the "GraphQL" section under WooCommerce - Settings - Advanced. |
| 14 | * Only active when Main::is_enabled() returns true (feature flag on and |
| 15 | * PHP 8.1+), so the section is hidden when the feature is disabled. |
| 16 | */ |
| 17 | class Settings { |
| 18 | /** |
| 19 | * Identifier for the GraphQL section under the Advanced settings tab. |
| 20 | */ |
| 21 | public const SECTION_ID = 'graphql'; |
| 22 | |
| 23 | /** |
| 24 | * Register the filter hooks that expose the GraphQL settings section. |
| 25 | */ |
| 26 | public function register(): void { |
| 27 | add_filter( 'woocommerce_get_sections_advanced', array( $this, 'add_section' ) ); |
| 28 | add_filter( 'woocommerce_get_settings_advanced', array( $this, 'add_settings' ), 10, 2 ); |
| 29 | add_filter( |
| 30 | 'woocommerce_admin_settings_sanitize_option_' . Main::OPTION_ENDPOINT_URL, |
| 31 | array( $this, 'sanitize_endpoint_url' ), |
| 32 | 10, |
| 33 | 3 |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Append the GraphQL section to the Advanced settings tab. |
| 39 | * |
| 40 | * @param array $sections Existing sections keyed by id. |
| 41 | * @return array |
| 42 | */ |
| 43 | public function add_section( array $sections ): array { |
| 44 | if ( Main::is_enabled() ) { |
| 45 | $sections[ self::SECTION_ID ] = __( 'GraphQL', 'woocommerce' ); |
| 46 | } |
| 47 | return $sections; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Provide the settings fields for the GraphQL section. |
| 52 | * |
| 53 | * @param array $settings Existing settings for the current section. |
| 54 | * @param string $section_id Current section id. |
| 55 | * @return array |
| 56 | */ |
| 57 | public function add_settings( array $settings, string $section_id ): array { |
| 58 | if ( self::SECTION_ID !== $section_id || ! Main::is_enabled() ) { |
| 59 | return $settings; |
| 60 | } |
| 61 | |
| 62 | return array( |
| 63 | array( |
| 64 | 'title' => __( 'GraphQL', 'woocommerce' ), |
| 65 | 'desc' => __( 'Configure the WooCommerce GraphQL API.', 'woocommerce' ), |
| 66 | 'type' => 'title', |
| 67 | 'id' => 'woocommerce_graphql_options', |
| 68 | ), |
| 69 | array( |
| 70 | 'title' => __( 'Endpoint URL', 'woocommerce' ), |
| 71 | 'desc' => __( 'Path relative to /wp-json/ where the GraphQL endpoint is exposed. Needs at least two segments (namespace/route), e.g. wc/graphql.', 'woocommerce' ), |
| 72 | 'desc_tip' => true, |
| 73 | 'id' => Main::OPTION_ENDPOINT_URL, |
| 74 | 'default' => GraphQLControllerBase::DEFAULT_ENDPOINT_URL, |
| 75 | 'type' => 'text', |
| 76 | ), |
| 77 | array( |
| 78 | 'title' => __( 'Enable GET endpoint', 'woocommerce' ), |
| 79 | 'desc' => __( 'Allow GraphQL queries over GET in addition to POST', 'woocommerce' ), |
| 80 | 'id' => Main::OPTION_GET_ENDPOINT_ENABLED, |
| 81 | 'default' => 'yes', |
| 82 | 'type' => 'checkbox', |
| 83 | ), |
| 84 | array( |
| 85 | 'title' => __( 'Maximum query depth', 'woocommerce' ), |
| 86 | 'desc' => __( 'Reject queries whose selection nesting exceeds this depth.', 'woocommerce' ), |
| 87 | 'id' => Main::OPTION_MAX_QUERY_DEPTH, |
| 88 | 'default' => (string) GraphQLControllerBase::DEFAULT_MAX_QUERY_DEPTH, |
| 89 | 'type' => 'number', |
| 90 | 'custom_attributes' => array( 'min' => '1' ), |
| 91 | ), |
| 92 | array( |
| 93 | 'title' => __( 'Maximum query complexity', 'woocommerce' ), |
| 94 | 'desc' => __( 'Reject queries whose computed complexity score exceeds this value.', 'woocommerce' ), |
| 95 | 'id' => Main::OPTION_MAX_QUERY_COMPLEXITY, |
| 96 | 'default' => (string) GraphQLControllerBase::DEFAULT_MAX_QUERY_COMPLEXITY, |
| 97 | 'type' => 'number', |
| 98 | 'custom_attributes' => array( 'min' => '1' ), |
| 99 | ), |
| 100 | array( |
| 101 | 'title' => __( 'Enable OPcache-based caching', 'woocommerce' ), |
| 102 | 'desc' => __( 'Cache parsed queries on disk as PHP files so OPcache can serve them from shared memory. Falls back to the object cache when the filesystem is not writable.', 'woocommerce' ), |
| 103 | 'id' => Main::OPTION_OPCACHE_ENABLED, |
| 104 | 'default' => 'yes', |
| 105 | 'type' => 'checkbox', |
| 106 | ), |
| 107 | array( |
| 108 | 'title' => __( 'Enable ObjectCache-based caching', 'woocommerce' ), |
| 109 | 'desc' => __( 'Cache parsed queries in the WP object cache', 'woocommerce' ), |
| 110 | 'id' => Main::OPTION_OBJECT_CACHE_ENABLED, |
| 111 | 'default' => 'yes', |
| 112 | 'type' => 'checkbox', |
| 113 | ), |
| 114 | array( |
| 115 | 'title' => __( 'Enable APQ caching', 'woocommerce' ), |
| 116 | 'desc' => __( 'Cache parsed queries using the Apollo Automatic Persisted Queries protocol', 'woocommerce' ), |
| 117 | 'id' => Main::OPTION_APQ_ENABLED, |
| 118 | 'default' => 'yes', |
| 119 | 'type' => 'checkbox', |
| 120 | ), |
| 121 | array( |
| 122 | 'title' => __( 'Parsed query cache TTL', 'woocommerce' ), |
| 123 | 'desc' => __( 'Time in seconds before cached parsed queries expire.', 'woocommerce' ), |
| 124 | 'id' => Main::OPTION_QUERY_CACHE_TTL, |
| 125 | 'default' => (string) QueryCache::DEFAULT_CACHE_TTL, |
| 126 | 'type' => 'number', |
| 127 | 'custom_attributes' => array( 'min' => '1' ), |
| 128 | ), |
| 129 | array( |
| 130 | 'type' => 'sectionend', |
| 131 | 'id' => 'woocommerce_graphql_options', |
| 132 | ), |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Validate and normalize the endpoint URL on save. |
| 138 | * |
| 139 | * Rejects empty input and inputs without at least two path segments, since |
| 140 | * register_rest_route() needs both a namespace and a route. Rejects any |
| 141 | * character outside of what WordPress REST routes accept (alphanumerics, |
| 142 | * underscores, hyphens). On rejection, adds a settings error message and |
| 143 | * returns the previously stored value so the option is not overwritten. |
| 144 | * |
| 145 | * @param mixed $value The sanitized value passed by earlier filters. |
| 146 | * @param array $option The option config from add_settings(). |
| 147 | * @param mixed $raw_value The raw value submitted by the form. Typed as mixed because POST data can be null or an array (e.g. when the field name is submitted as `name[]`). |
| 148 | * @return string |
| 149 | */ |
| 150 | public function sanitize_endpoint_url( $value, array $option, $raw_value ): string { |
| 151 | unset( $value, $option ); |
| 152 | |
| 153 | $fallback = (string) get_option( Main::OPTION_ENDPOINT_URL, GraphQLControllerBase::DEFAULT_ENDPOINT_URL ); |
| 154 | |
| 155 | if ( ! is_string( $raw_value ) ) { |
| 156 | return $fallback; |
| 157 | } |
| 158 | |
| 159 | $normalized = trim( $raw_value, '/' ); |
| 160 | |
| 161 | if ( '' === $normalized ) { |
| 162 | \WC_Admin_Settings::add_error( __( 'GraphQL endpoint URL cannot be empty.', 'woocommerce' ) ); |
| 163 | return $fallback; |
| 164 | } |
| 165 | |
| 166 | $parts = explode( '/', $normalized ); |
| 167 | if ( count( $parts ) < 2 ) { |
| 168 | \WC_Admin_Settings::add_error( __( 'GraphQL endpoint URL needs at least two segments, e.g. wc/graphql.', 'woocommerce' ) ); |
| 169 | return $fallback; |
| 170 | } |
| 171 | |
| 172 | foreach ( $parts as $part ) { |
| 173 | if ( '' === $part || ! preg_match( GraphQLControllerBase::ENDPOINT_URL_SEGMENT_PATTERN, $part ) ) { |
| 174 | \WC_Admin_Settings::add_error( |
| 175 | sprintf( |
| 176 | /* translators: %s: the invalid path segment */ |
| 177 | __( 'GraphQL endpoint URL segment "%s" contains invalid characters. Use letters, digits, underscores, and hyphens only.', 'woocommerce' ), |
| 178 | $part |
| 179 | ) |
| 180 | ); |
| 181 | return $fallback; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | return $normalized; |
| 186 | } |
| 187 | } |
| 188 |