PluginProbe
ElasticPress / 5.0.2
ElasticPress v5.0.2
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
elasticpress / includes / classes / REST / Features.php

Features.php in ElasticPress 5.0.2, at includes/classes/REST/Features.php

185 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Features REST API Controller
4 *
5 * @since 5.0.0
6 * @package elasticpress
7 */
8
9 namespace ElasticPress\REST;
10
11 use ElasticPress\Features as FeaturesStore;
12 use ElasticPress\Utils;
13
14 /**
15 * Features API controller class.
16 *
17 * @since 5.0.0
18 * @package elasticpress
19 */
20 class Features {
21
22 /**
23 * Register routes.
24 *
25 * @return void
26 */
27 public function register_routes() {
28 register_rest_route(
29 'elasticpress/v1',
30 'features',
31 [
32 'args' => $this->get_args(),
33 'callback' => [ $this, 'update_settings' ],
34 'methods' => 'PUT',
35 'permission_callback' => [ $this, 'check_permission' ],
36 ]
37 );
38 }
39
40 /**
41 * Get args schema.
42 *
43 * @return array
44 */
45 public function get_args() {
46 $args = [];
47
48 $features = \ElasticPress\Features::factory()->registered_features;
49
50 foreach ( $features as $feature ) {
51 $properties = [];
52
53 $schema = $feature->get_settings_schema();
54
55 foreach ( $schema as $schema ) {
56 if ( ! isset( $schema['label'] ) ) {
57 continue;
58 }
59
60 $type = $schema['type'] ?? '';
61 $property = [
62 'description' => $schema['label'],
63 'type' => 'string',
64 ];
65
66 switch ( $type ) {
67 case 'select':
68 case 'radio':
69 $property['enum'] = array_map( fn( $o ) => $o['value'], $schema['options'] );
70 break;
71 case 'toggle':
72 $property['type'] = 'boolean';
73 break;
74 }
75
76 $properties[ $schema['key'] ] = $property;
77 }
78
79 $args[ $feature->slug ] = [
80 'description' => $feature->get_title(),
81 'properties' => $properties,
82 'type' => 'object',
83 ];
84 }
85
86 return $args;
87 }
88
89 /**
90 * Check that the request has permission to save features.
91 *
92 * @return boolean
93 */
94 public function check_permission() {
95 $capability = Utils\get_capability();
96
97 return current_user_can( $capability );
98 }
99
100 /**
101 * Update features settings.
102 *
103 * @param \WP_REST_Request $request Full details about the request.
104 * @return array
105 */
106 public function update_settings( \WP_REST_Request $request ) {
107 if ( Utils\is_indexing() ) {
108 wp_send_json_error( 'is_syncing', 400 );
109 exit;
110 }
111
112 $current_settings = FeaturesStore::factory()->get_feature_settings();
113 $new_settings = $current_settings;
114
115 $features = \ElasticPress\Features::factory()->registered_features;
116
117 $settings_that_requires_features = [];
118
119 foreach ( $features as $slug => $feature ) {
120 $param = $request->get_param( $slug );
121
122 if ( ! $param ) {
123 continue;
124 }
125
126 if ( empty( $current_settings[ $slug ] ) ) {
127 $current_settings[ $slug ] = [];
128 $new_settings[ $slug ] = [];
129 }
130
131 $schema = $feature->get_settings_schema();
132
133 foreach ( $schema as $schema ) {
134 $key = $schema['key'];
135
136 if ( isset( $param[ $key ] ) ) {
137 $new_settings[ $slug ][ $key ] = $param[ $key ];
138
139 // Only apply to the current settings if does not require a sync or if it is activating it
140 if ( ! empty( $schema['requires_sync'] ) && ! empty( $param[ $key ] ) ) {
141 continue;
142 }
143
144 /*
145 * If a setting requires another feature, we have to check for it after running through everything,
146 * as it is possible that the feature will be active after this foreach.
147 */
148 if ( empty( $schema['requires_feature'] ) ) {
149 $current_settings[ $slug ][ $key ] = $param[ $key ];
150 } else {
151 if ( ! isset( $settings_that_requires_features[ $slug ] ) ) {
152 $settings_that_requires_features[ $slug ] = [];
153 }
154 $settings_that_requires_features[ $slug ][ $key ] = [
155 'required_feature' => $schema['requires_feature'],
156 'value' => $param[ $key ],
157 ];
158 }
159 }
160 }
161 }
162
163 foreach ( $settings_that_requires_features as $feature => $fields ) {
164 foreach ( $fields as $field_key => $field_data ) {
165 if ( ! empty( $current_settings[ $field_data['required_feature'] ]['active'] ) ) {
166 $current_settings[ $feature ][ $field_key ] = $field_data['value'];
167 }
168 }
169 }
170
171 foreach ( $current_settings as $slug => $feature ) {
172 FeaturesStore::factory()->update_feature( $slug, $feature );
173 }
174
175 foreach ( $new_settings as $slug => $feature ) {
176 FeaturesStore::factory()->update_feature( $slug, $feature, true, 'draft' );
177 }
178
179 return [
180 'data' => $current_settings,
181 'success' => true,
182 ];
183 }
184 }
185