| 1 |
<?php |
| 2 |
namespace PixelavoOpt\Api; |
| 3 |
|
| 4 |
use WP_REST_Controller; |
| 5 |
use PixelavoOpt\SanitizeTrail\Sanitize_Trait; |
| 6 |
|
| 7 |
if (!class_exists('\PixelavoOpt\Admin\Options_Field')) { |
| 8 |
require_once PIXELAVO_INCLUDES . '/classes/Admin/Options_field.php'; |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* REST_API Handler |
| 13 |
*/ |
| 14 |
class Settings extends WP_REST_Controller { |
| 15 |
|
| 16 |
use Sanitize_Trait; |
| 17 |
|
| 18 |
protected $namespace; |
| 19 |
protected $rest_base; |
| 20 |
protected $errors; |
| 21 |
|
| 22 |
/** |
| 23 |
* All registered settings. |
| 24 |
* |
| 25 |
* @var array |
| 26 |
*/ |
| 27 |
protected $settings; |
| 28 |
|
| 29 |
/** |
| 30 |
* [__construct Settings constructor] |
| 31 |
*/ |
| 32 |
public function __construct() { |
| 33 |
$this->namespace = 'pixelavoopt/v1'; |
| 34 |
$this->rest_base = 'settings'; |
| 35 |
$this->errors = new \WP_Error(); |
| 36 |
$this->settings = \PixelavoOpt\Admin\Options_Field::instance()->get_registered_settings(); |
| 37 |
|
| 38 |
add_filter('pixelavo_settings_sanitize', [$this, 'sanitize_settings'], 3, 10); |
| 39 |
|
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Register the routes |
| 44 |
* |
| 45 |
* @return void |
| 46 |
*/ |
| 47 |
public function register_routes() { |
| 48 |
register_rest_route( |
| 49 |
$this->namespace, |
| 50 |
'/' . $this->rest_base, |
| 51 |
[ |
| 52 |
[ |
| 53 |
'methods' => \WP_REST_Server::READABLE, |
| 54 |
'callback' => [$this, 'get_items'], |
| 55 |
'permission_callback' => [$this, 'permissions_check'], |
| 56 |
'args' => $this->get_collection_params(), |
| 57 |
], |
| 58 |
|
| 59 |
[ |
| 60 |
'methods' => \WP_REST_Server::CREATABLE, |
| 61 |
'callback' => [$this, 'create_items'], |
| 62 |
'permission_callback' => [$this, 'permissions_check'], |
| 63 |
'args' => $this->get_collection_params(), |
| 64 |
] |
| 65 |
] |
| 66 |
); |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Checks if a given request has access to read the items. |
| 72 |
* |
| 73 |
* @param WP_REST_Request $request Full details about the request. |
| 74 |
* |
| 75 |
* @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
| 76 |
*/ |
| 77 |
public function permissions_check($request) { |
| 78 |
|
| 79 |
if (!current_user_can('manage_options')) { |
| 80 |
return new \WP_Error('rest_forbidden', 'PIXELAVO OPT: Permission Denied.', ['status' => 401]); |
| 81 |
} |
| 82 |
|
| 83 |
return true; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Retrieves the query params for the items collection. |
| 88 |
* |
| 89 |
* @return array Collection parameters. |
| 90 |
*/ |
| 91 |
public function get_collection_params() { |
| 92 |
return []; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Retrieves a collection of items. |
| 97 |
* |
| 98 |
* @param WP_REST_Request $request Full details about the request. |
| 99 |
* |
| 100 |
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 101 |
*/ |
| 102 |
public function get_items($request) { |
| 103 |
$items = []; |
| 104 |
|
| 105 |
$section = (string) $request['section']; |
| 106 |
if (!empty($section)) { |
| 107 |
$items = get_option($section, true); |
| 108 |
} |
| 109 |
|
| 110 |
$response = rest_ensure_response($items); |
| 111 |
return $response; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Create item response |
| 116 |
*/ |
| 117 |
public function create_items($request) { |
| 118 |
|
| 119 |
if (!wp_verify_nonce($request['settings']['verifynonce'], 'pixelavoopt_verifynonce')) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
if (!current_user_can('manage_options')) { |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
$section = (!empty($request['section']) ? sanitize_text_field($request['section']) : ''); |
| 128 |
$settings_received = (!empty($request['settings']) ? pixelavo_data_clean($request['settings']) : ''); |
| 129 |
$settings_reset = (!empty($request['reset']) ? rest_sanitize_boolean($request['reset']) : ''); |
| 130 |
|
| 131 |
// Data reset |
| 132 |
if ($settings_reset == true) { |
| 133 |
$reseted = delete_option($section); |
| 134 |
return rest_ensure_response($reseted); |
| 135 |
} |
| 136 |
|
| 137 |
if (empty($section) || empty($settings_received)) { |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
$get_settings = $this->settings[$section]; |
| 142 |
$data_to_save = []; |
| 143 |
|
| 144 |
if (is_array($get_settings) && !empty($get_settings)) { |
| 145 |
foreach ($get_settings as $setting) { |
| 146 |
|
| 147 |
// Skip if no setting type. |
| 148 |
if (!$setting['type']) { |
| 149 |
continue; |
| 150 |
} |
| 151 |
|
| 152 |
// Skip if setting type is html. |
| 153 |
if ($setting['type'] === 'html') { |
| 154 |
continue; |
| 155 |
} |
| 156 |
|
| 157 |
// Skip if setting field is pro. |
| 158 |
if (isset($setting['is_pro']) && $setting['is_pro']) { |
| 159 |
continue; |
| 160 |
} |
| 161 |
|
| 162 |
// Skip if the ID doesn't exist in the data received. |
| 163 |
if (!array_key_exists($setting['id'], $settings_received)) { |
| 164 |
continue; |
| 165 |
} |
| 166 |
|
| 167 |
// Sanitize the input. |
| 168 |
$setting_type = $setting['type']; |
| 169 |
|
| 170 |
$output = apply_filters('pixelavo_settings_sanitize', $settings_received[$setting['id']], $this->errors, $setting); |
| 171 |
|
| 172 |
$output = apply_filters('pixelavo_settings_sanitize_' . $setting['id'], $output, $this->errors, $setting); |
| 173 |
|
| 174 |
if ($setting_type == 'checkbox' && $output == false) { |
| 175 |
continue; |
| 176 |
} |
| 177 |
|
| 178 |
// Add the option to the list of ones that we need to save. |
| 179 |
if (!empty($output) && !is_wp_error($output)) { |
| 180 |
$data_to_save[$setting['id']] = $settings_received[$setting['id']]; |
| 181 |
} |
| 182 |
|
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
if (!empty($this->errors->get_error_codes())) { |
| 187 |
return new \WP_REST_Response($this->errors, 422); |
| 188 |
} |
| 189 |
|
| 190 |
update_option($section, $data_to_save); |
| 191 |
|
| 192 |
return rest_ensure_response($data_to_save); |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Sanitize callback for Settings Data |
| 198 |
* |
| 199 |
* @return mixed |
| 200 |
*/ |
| 201 |
public function sanitize_settings($setting_value, $errors, $setting) { |
| 202 |
|
| 203 |
if (!empty($setting['sanitize_callback']) && is_callable($setting['sanitize_callback'])) { |
| 204 |
$setting_value = call_user_func($setting['sanitize_callback'], $setting_value); |
| 205 |
} else { |
| 206 |
$setting_value = $this->default_sanitizer($setting_value, $errors, $setting); |
| 207 |
} |
| 208 |
|
| 209 |
return $setting_value; |
| 210 |
|
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* If no Sanitize callback function from option field. |
| 215 |
* |
| 216 |
* @return mixed |
| 217 |
*/ |
| 218 |
public function default_sanitizer($setting_value, $errors, $setting) { |
| 219 |
|
| 220 |
switch ($setting['type']) { |
| 221 |
case 'text': |
| 222 |
case 'radio': |
| 223 |
case 'select': |
| 224 |
$finalvalue = $this->sanitize_text_field($setting_value, $errors, $setting); |
| 225 |
break; |
| 226 |
|
| 227 |
case 'textarea': |
| 228 |
$finalvalue = $this->sanitize_textarea_field($setting_value, $errors, $setting); |
| 229 |
break; |
| 230 |
|
| 231 |
case 'checkbox': |
| 232 |
case 'switcher': |
| 233 |
case 'element': |
| 234 |
$finalvalue = $this->sanitize_checkbox_field($setting_value, $errors, $setting); |
| 235 |
break; |
| 236 |
|
| 237 |
case 'table': |
| 238 |
case 'event-table': |
| 239 |
case 'multiselect': |
| 240 |
case 'multicheckbox': |
| 241 |
$finalvalue = $this->sanitize_multiple_field($setting_value, $errors, $setting); |
| 242 |
break; |
| 243 |
|
| 244 |
case 'file': |
| 245 |
$finalvalue = $this->sanitize_file_field($setting_value, $errors, $setting); |
| 246 |
break; |
| 247 |
|
| 248 |
default: |
| 249 |
$finalvalue = sanitize_text_field($setting_value); |
| 250 |
break; |
| 251 |
} |
| 252 |
|
| 253 |
return $finalvalue; |
| 254 |
|
| 255 |
} |
| 256 |
|
| 257 |
} |