PluginProbe ʕ •ᴥ•ʔ
Strong Testimonials / 3.3.2
Strong Testimonials v3.3.2
3.3.2 3.3.1 trunk 1.0.1 2.30.9 2.31.10 2.32 2.32.1 2.32.2 2.32.3 2.32.4 2.33 2.34 2.35 2.36 2.37 2.38 2.38.1 2.39 2.39.1 2.39.2 2.39.3 2.40.0 2.40.1 2.40.2 2.40.3 2.40.4 2.40.5 2.40.6 2.40.7 2.41.0 2.41.1 2.50.0 2.50.1 2.50.2 2.50.3 2.50.4 2.51.0 2.51.1 2.51.2 2.51.3 2.51.4 2.51.5 2.51.6 2.51.7 2.51.8 2.51.9 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 3.1.19 3.1.2 3.1.20 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.10 3.2.11 3.2.12 3.2.13 3.2.14 3.2.15 3.2.16 3.2.17 3.2.18 3.2.19 3.2.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 3.3.0
strong-testimonials / admin / rest-api / class-strong-testimonials-rest-api-base.php
strong-testimonials / admin / rest-api Last commit date
class-strong-testimonials-extensions-base.php 1 week ago class-strong-testimonials-rest-api-base.php 2 weeks ago class-strong-testimonials-settings-sanitizer.php 1 month ago
class-strong-testimonials-rest-api-base.php
299 lines
1 <?php
2 require_once WPMTST_ADMIN . 'rest-api/class-strong-testimonials-extensions-base.php';
3 require_once WPMTST_ADMIN . 'rest-api/class-strong-testimonials-settings-sanitizer.php';
4 require_once WPMTST_ADMIN . 'settings/class-strong-testimonials-general-settings-react.php';
5
6 class Strong_Testimonials_Rest_Api_Base {
7
8 private $namespace = 'strong-testimonials/v1';
9 private $settings = null;
10
11 public function __construct() {
12 add_action( 'rest_api_init', array( $this, 'register_routes' ) );
13
14 Strong_Testimonials_Extensions_Base::get_instance();
15
16 $this->settings = new Strong_Testimonials_General_Settings_React();
17 }
18
19 public function register_routes() {
20
21 register_rest_route(
22 $this->namespace,
23 '/general-settings',
24 array(
25 'methods' => 'GET',
26 'callback' => array( $this, 'get_settings' ),
27 'permission_callback' => array( $this, 'settings_permissions_check' ),
28 )
29 );
30 register_rest_route(
31 $this->namespace,
32 '/general-settings',
33 array(
34 'methods' => 'POST',
35 'callback' => array( $this, 'update_settings' ),
36 'permission_callback' => array( $this, 'settings_permissions_check' ),
37 )
38 );
39 register_rest_route(
40 $this->namespace,
41 '/general-settings-tabs',
42 array(
43 'methods' => 'GET',
44 'callback' => array( $this, 'get_tabs' ),
45 'permission_callback' => array( $this, 'settings_permissions_check' ),
46 )
47 );
48
49 register_rest_route(
50 $this->namespace,
51 '/license',
52 array(
53 'methods' => 'POST',
54 'callback' => array( $this, 'license_action' ),
55 'permission_callback' => array( $this, 'settings_permissions_check' ),
56 )
57 );
58
59 register_rest_route(
60 $this->namespace,
61 '/extensions',
62 array(
63 'methods' => 'GET',
64 'callback' => array( $this, 'get_extensions' ),
65 'permission_callback' => array( $this, 'settings_permissions_check' ),
66 )
67 );
68
69 register_rest_route(
70 $this->namespace,
71 '/aggregate-rating',
72 array(
73 'methods' => 'GET',
74 'callback' => array( $this, 'get_aggregate_rating' ),
75 'permission_callback' => array( $this, 'settings_permissions_check' ),
76 )
77 );
78
79 register_rest_route(
80 $this->namespace,
81 '/recalculate-aggregate',
82 array(
83 'methods' => 'POST',
84 'callback' => array( $this, 'recalculate_aggregate' ),
85 'permission_callback' => array( $this, 'settings_permissions_check' ),
86 )
87 );
88
89 register_rest_route(
90 $this->namespace,
91 '/client-section-options',
92 array(
93 'methods' => 'GET',
94 'callback' => array( $this, 'get_client_section_options' ),
95 'permission_callback' => array( $this, 'settings_permissions_check' ),
96 )
97 );
98 }
99
100 public function get_settings() {
101 return new \WP_REST_Response( $this->settings->get_settings(), 200 );
102 }
103
104 public function update_settings( $request ) {
105 $settings = $request->get_json_params();
106
107 if ( empty( $settings ) || ! is_array( $settings ) ) {
108 return new \WP_REST_Response( 'No settings to save.', 400 );
109 }
110
111 $sanitization_schema = $this->settings->settings_sanitization();
112 $sanitizer = Strong_Testimonials_Settings_Sanitizer::get_instance();
113
114 $sanitized_settings = array();
115
116 foreach ( $settings as $option => $value ) {
117 if ( ! isset( $sanitization_schema[ $option ] ) || ! is_array( $sanitization_schema[ $option ] ) ) {
118 continue;
119 }
120
121 $value = $this->sanitize_setting_value( $value, $sanitization_schema[ $option ], $sanitizer );
122
123 // Merge incoming array values with the existing option so that
124 // saving a partial set of fields (e.g. from one accordion subtab)
125 // does not wipe out fields managed by a different subtab.
126 if ( is_array( $value ) ) {
127 $existing = get_option( $option, array() );
128 if ( is_array( $existing ) ) {
129 $value = array_merge( $existing, $value );
130 }
131 }
132
133 update_option( $option, $value );
134
135 do_action( 'strong_testimonials_settings_api_update_' . $option, $value );
136
137 $sanitized_settings[ $option ] = $value;
138 }
139
140 return new \WP_REST_Response( $sanitized_settings, 200 );
141 }
142
143 public function get_tabs() {
144 return new \WP_REST_Response( $this->settings->get_tabs(), 200 );
145 }
146
147 public function settings_permissions_check() {
148
149 // Check if the user has the capability to manage options
150 return current_user_can( 'manage_options' );
151 }
152
153 public function license_action( $request ) {
154 $body = $request->get_json_params();
155 $license_key = isset( $body['license_key'] ) ? $body['license_key'] : '';
156 $action = isset( $body['action'] ) ? $body['action'] : '';
157 $saved = get_option( 'strong_testimonials_license_key', '' );
158 if ( empty( $license_key ) && empty( $saved ) ) {
159 return new \WP_REST_Response(
160 array(
161 'message' => 'no_license_key',
162 'status' => 'error',
163 ),
164 200
165 );
166 }
167
168 if ( ! class_exists( 'Strong_Testimonials_Pro\Extensions\Licensing' ) ) {
169 return new \WP_REST_Response( 'Strong Testimonials Pro is not installed.', 400 );
170 }
171
172 $license = Strong_Testimonials_Pro\Extensions\Licensing::get_instance();
173
174 if ( 'activate' === $action ) {
175 return new \WP_REST_Response( $license->activate_license( $license_key ), 200 );
176 }
177
178 if ( 'deactivate' === $action ) {
179 return new \WP_REST_Response( $license->deactivate_license( $license_key ), 200 );
180 }
181
182 return new \WP_REST_Response( $license->check_license(), 200 );
183 }
184
185 public function get_aggregate_rating() {
186 return new \WP_REST_Response( $this->settings->get_aggregate_rating(), 200 );
187 }
188
189 public function recalculate_aggregate() {
190 return new \WP_REST_Response( $this->settings->recalculate_aggregate(), 200 );
191 }
192
193 public function get_client_section_options() {
194 return new \WP_REST_Response( $this->settings->get_client_section_options(), 200 );
195 }
196
197 public function get_extensions() {
198 $instance = class_exists( 'Strong_Testimonials_Pro\Extensions\Extensions' )
199 ? Strong_Testimonials_Pro\Extensions\Extensions::get_instance()
200 : Strong_Testimonials_Extensions_Base::get_instance();
201
202 return new \WP_REST_Response( $instance->get_extensions(), 200 );
203 }
204
205 /**
206 * Sanitize settings payload based on provided sanitization schema.
207 *
208 * @param mixed $value Value to sanitize.
209 * @param array|string $schema Sanitization schema for the value.
210 * @param Strong_Testimonials_Settings_Sanitizer $sanitizer Sanitizer instance.
211 *
212 * @return mixed
213 */
214 private function sanitize_setting_value( $value, $schema, $sanitizer ) {
215 // Handle indexed array with per-item schema: schema = ['_each' => [...item schema...]]
216 if ( is_array( $value ) && is_array( $schema ) && isset( $schema['_each'] ) ) {
217 $item_schema = $schema['_each'];
218 $sanitized = array();
219 foreach ( $value as $item ) {
220 if ( is_array( $item ) ) {
221 $sanitized[] = $this->sanitize_setting_value( $item, $item_schema, $sanitizer );
222 }
223 }
224 return $sanitized;
225 }
226
227 if ( is_array( $value ) && $this->is_associative_array( $schema ) ) {
228 $sanitized = array();
229
230 foreach ( $value as $key => $sub_value ) {
231 if ( isset( $schema[ $key ] ) ) {
232 $sanitized[ $key ] = $this->sanitize_setting_value( $sub_value, $schema[ $key ], $sanitizer );
233 } else {
234 $sanitized[ $key ] = $sub_value;
235 }
236 }
237
238 return $sanitized;
239 }
240
241 if ( ! is_array( $value ) && is_array( $schema ) ) {
242 $is_numeric_indexed = array_keys( $schema ) === range( 0, count( $schema ) - 1 );
243
244 if ( $is_numeric_indexed && isset( $schema[0] ) && is_string( $schema[0] ) ) {
245 return $this->run_sanitizer( $schema[0], $value, $sanitizer );
246 }
247
248 if ( ! $is_numeric_indexed && 1 === count( $schema ) ) {
249 $sanitizer_key = array_keys( $schema )[0];
250 $args = $schema[ $sanitizer_key ];
251
252 return $this->run_sanitizer( $sanitizer_key, $value, $sanitizer, $args );
253 }
254 }
255
256 return $value;
257 }
258
259 /**
260 * Run a sanitizer method or custom handler based on schema.
261 *
262 * @param string $sanitizer_key Sanitizer key.
263 * @param mixed $value Value to sanitize.
264 * @param Strong_Testimonials_Settings_Sanitizer $sanitizer Sanitizer instance.
265 * @param mixed $args Optional args for sanitizer (used for enum).
266 *
267 * @return mixed
268 */
269 private function run_sanitizer( $sanitizer_key, $value, $sanitizer, $args = array() ) {
270 if ( 'enum' === $sanitizer_key && is_array( $args ) && ! empty( $args ) ) {
271 return in_array( $value, $args, true ) ? $value : reset( $args );
272 }
273
274 if ( method_exists( $sanitizer, $sanitizer_key ) ) {
275 return $sanitizer->$sanitizer_key( $value );
276 }
277
278 return $value;
279 }
280
281 /**
282 * Check if an array is associative.
283 *
284 * @param array $unknown_array Array to check.
285 *
286 * @return bool
287 */
288 private function is_associative_array( $unknown_array ) {
289 if ( ! is_array( $unknown_array ) || array() === $unknown_array ) {
290 return false;
291 }
292
293 return array_keys( $unknown_array ) !== range( 0, count( $unknown_array ) - 1 );
294 }
295
296 }
297
298 new Strong_Testimonials_Rest_Api_Base();
299