| @@ -28,12 +28,19 @@ | ||
| 28 | 28 | register_rest_route( |
| 29 | 29 | 'elasticpress/v1', |
| 30 | 30 | 'features', |
| 31 | 31 | [ |
| 32 | - 'args' => $this->get_args(), | |
| 33 | - 'callback' => [ $this, 'update_settings' ], | |
| 34 | - 'methods' => 'PUT', | |
| 35 | - 'permission_callback' => [ $this, 'check_permission' ], | |
| 32 | + [ | |
| 33 | + 'callback' => [ $this, 'get_features' ], | |
| 34 | + 'methods' => 'GET', | |
| 35 | + 'permission_callback' => [ $this, 'check_permission' ], | |
| 36 | + ], | |
| 37 | + [ | |
| 38 | + 'args' => $this->get_args(), | |
| 39 | + 'callback' => [ $this, 'update_settings' ], | |
| 40 | + 'methods' => 'PUT', | |
| 41 | + 'permission_callback' => [ $this, 'check_permission' ], | |
| 42 | + ], | |
| 36 | 43 | ] |
| 37 | 44 | ); |
| 38 | 45 | } |
| 39 | 46 | |
| @@ -70,8 +77,18 @@ | ||
| 70 | 77 | break; |
| 71 | 78 | case 'toggle': |
| 72 | 79 | $property['type'] = 'boolean'; |
| 73 | 80 | break; |
| 81 | + case 'number': | |
| 82 | + $property['type'] = 'number'; | |
| 83 | + break; | |
| 84 | + case 'url': | |
| 85 | + $property['type'] = 'string'; | |
| 86 | + $property['format'] = 'uri'; | |
| 87 | + break; | |
| 88 | + case 'field_group': | |
| 89 | + $property['type'] = 'object'; | |
| 90 | + $property['properties'] = []; | |
| 74 | 91 | } |
| 75 | 92 | |
| 76 | 93 | $properties[ $schema['key'] ] = $property; |
| 77 | 94 | } |
| @@ -80,8 +97,12 @@ | ||
| 80 | 97 | 'description' => $feature->get_title(), |
| 81 | 98 | 'properties' => $properties, |
| 82 | 99 | 'type' => 'object', |
| 83 | 100 | ]; |
| 101 | + | |
| 102 | + if ( method_exists( $feature, 'sanitize_settings_callback' ) ) { | |
| 103 | + $args[ $feature->slug ]['sanitize_callback'] = [ $feature, 'sanitize_settings_callback' ]; | |
| 104 | + } | |
| 84 | 105 | } |
| 85 | 106 | |
| 86 | 107 | return $args; |
| 87 | 108 | } |
| @@ -130,13 +151,33 @@ | ||
| 130 | 151 | |
| 131 | 152 | $schema = $feature->get_settings_schema(); |
| 132 | 153 | |
| 133 | 154 | foreach ( $schema as $schema ) { |
| 134 | - $key = $schema['key']; | |
| 155 | + $key = $schema['key']; | |
| 156 | + $type = $schema['type'] ?? ''; | |
| 135 | 157 | |
| 136 | 158 | if ( isset( $param[ $key ] ) ) { |
| 137 | - $new_settings[ $slug ][ $key ] = $param[ $key ]; | |
| 159 | + // Handle field group values | |
| 160 | + if ( 'field_group' === $type ) { | |
| 161 | + // Save the nested structure for the settings UI | |
| 162 | + $new_settings[ $slug ][ $key ] = $param[ $key ]; | |
| 138 | 163 | |
| 164 | + // Flatten the field group values into the main settings array | |
| 165 | + foreach ( $schema['fields'] as $field ) { | |
| 166 | + $field_key = $field['key']; | |
| 167 | + if ( isset( $param[ $key ][ $field_key ] ) ) { | |
| 168 | + $new_settings[ $slug ][ $field_key ] = $param[ $key ][ $field_key ]; | |
| 169 | + | |
| 170 | + // Only apply to current settings if no sync required | |
| 171 | + if ( empty( $schema['requires_sync'] ) ) { | |
| 172 | + $current_settings[ $slug ][ $field_key ] = $param[ $key ][ $field_key ]; | |
| 173 | + } | |
| 174 | + } | |
| 175 | + } | |
| 176 | + } else { | |
| 177 | + $new_settings[ $slug ][ $key ] = $param[ $key ]; | |
| 178 | + } | |
| 179 | + | |
| 139 | 180 | // Only apply to the current settings if does not require a sync or if it is activating it |
| 140 | 181 | if ( ! empty( $schema['requires_sync'] ) && ! empty( $param[ $key ] ) ) { |
| 141 | 182 | continue; |
| 142 | 183 | } |
| @@ -161,9 +202,19 @@ | ||
| 161 | 202 | } |
| 162 | 203 | |
| 163 | 204 | foreach ( $settings_that_requires_features as $feature => $fields ) { |
| 164 | 205 | foreach ( $fields as $field_key => $field_data ) { |
| 165 | - if ( ! empty( $current_settings[ $field_data['required_feature'] ]['active'] ) ) { | |
| 206 | + $required_features = (array) $field_data['required_feature']; | |
| 207 | + | |
| 208 | + $all_required_active = true; | |
| 209 | + foreach ( $required_features as $required_feature_slug ) { | |
| 210 | + if ( empty( $current_settings[ $required_feature_slug ]['active'] ) ) { | |
| 211 | + $all_required_active = false; | |
| 212 | + break; | |
| 213 | + } | |
| 214 | + } | |
| 215 | + | |
| 216 | + if ( $all_required_active ) { | |
| 166 | 217 | $current_settings[ $feature ][ $field_key ] = $field_data['value']; |
| 167 | 218 | } |
| 168 | 219 | } |
| 169 | 220 | } |
| @@ -176,9 +227,45 @@ | ||
| 176 | 227 | FeaturesStore::factory()->update_feature( $slug, $feature, true, 'draft' ); |
| 177 | 228 | } |
| 178 | 229 | |
| 179 | 230 | return [ |
| 180 | - 'data' => $current_settings, | |
| 181 | 231 | 'success' => true, |
| 182 | 232 | ]; |
| 233 | + } | |
| 234 | + | |
| 235 | + /** | |
| 236 | + * Return the current features payload along with persisted settings. | |
| 237 | + * | |
| 238 | + * @since 5.3.0 | |
| 239 | + * @return array | |
| 240 | + */ | |
| 241 | + public function get_features() { | |
| 242 | + $store = FeaturesStore::factory(); | |
| 243 | + | |
| 244 | + $settings = $store->get_feature_settings(); | |
| 245 | + $settings_draft = $store->get_feature_settings_draft(); | |
| 246 | + | |
| 247 | + return [ | |
| 248 | + 'features' => $this->get_features_payload(), | |
| 249 | + 'settings' => is_array( $settings ) ? $settings : [], | |
| 250 | + 'settingsDraft' => is_array( $settings_draft ) ? $settings_draft : null, | |
| 251 | + 'success' => true, | |
| 252 | + ]; | |
| 253 | + } | |
| 254 | + | |
| 255 | + /** | |
| 256 | + * Build the serialized features payload. | |
| 257 | + * | |
| 258 | + * @since 5.3.0 | |
| 259 | + * @return array | |
| 260 | + */ | |
| 261 | + protected function get_features_payload() { | |
| 262 | + $features_objects = FeaturesStore::factory()->registered_features; | |
| 263 | + | |
| 264 | + foreach ( $features_objects as $feature ) { | |
| 265 | + $feature->reset_settings_schema(); | |
| 266 | + } | |
| 267 | + | |
| 268 | + $features_data = array_map( fn( $feature ) => $feature->get_json(), $features_objects ); | |
| 269 | + return array_values( $features_data ); | |
| 183 | 270 | } |
| 184 | 271 | } |