PluginProbe
ezCache / 1.3.11
ezCache v1.3.11
2.6.5 2.6.4 2.6.2 2.6.3 2.6.1 2.6.0 2.5.6 2.5.5 2.5.4 2.5.3 2.5.2 2.5.1 2.5 2.2.1 2.2.2 trunk 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.3 1.3.1 1.3.10 1.3.11 All 49 releases
ezcache / includes / Rest / SettingsController.php

SettingsController.php in ezCache 1.3.11, at includes/Rest/SettingsController.php

99 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Upress\EzCache\Rest;
3
4 use Upress\EzCache\Cache;
5 use Upress\EzCache\Settings;
6 use WP_REST_Request;
7
8 class SettingsController {
9
10 function show() {
11 $settings = Settings::get_settings();
12
13 return wp_send_json_success( $settings );
14 }
15
16 /**
17 * @param WP_REST_Request $request
18 */
19 function update( $request ) {
20 $default_settings = (array) Settings::get_default_settings();
21
22 $input = (array) $request->get_json_params();
23 $updated_settings = $this->sanitize_settings( $input, $default_settings );
24
25 Settings::set_settings( $updated_settings );
26 Cache::instance()->clear_cache();
27
28 return wp_send_json_success();
29 }
30
31 /**
32 * Sanitize the settings based on the predefined $default_settings
33 *
34 * @param array $settings
35 * @param array $default_settings
36 *
37 * @return array
38 */
39 protected function sanitize_settings( $settings, $default_settings ) {
40 $sanitized = [];
41
42 foreach( $settings as $key => $value ) {
43 if ( ! isset( $default_settings[ $key ] ) ) {
44 continue;
45 }
46
47 $type = gettype( $default_settings[ $key ] );
48
49 if ( 'array' === $type || 'object' === $type ) {
50 $value = $this->sanitize_settings( ((array) $value), $default_settings[ $key ] );
51 } elseif ( method_exists( $this, "sanitize_{$type}" ) ) {
52 $value = call_user_func( [ $this, "sanitize_{$type}" ], $value );
53 } else {
54 continue;
55 }
56
57 $sanitized[ $key ] = $value;
58 }
59
60 return $sanitized;
61 }
62
63 /**
64 * @param mixed $bool
65 *
66 * @return bool
67 */
68 protected function sanitize_boolean( $bool ) {
69 return !! $bool;
70 }
71
72 /**
73 * @param mixed $int
74 *
75 * @return int
76 */
77 protected function sanitize_integer( $int ) {
78 return intval( $int );
79 }
80
81 /**
82 * @param mixed $double
83 *
84 * @return float
85 */
86 protected function sanitize_double( $double ) {
87 return doubleval( $double );
88 }
89
90 /**
91 * @param mixed $string
92 *
93 * @return string
94 */
95 protected function sanitize_string( $string ) {
96 return sanitize_textarea_field( $string );
97 }
98 }
99