| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Controls Site options |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace Extendify\PageCreator\Controllers; |
| 8 |
|
| 9 |
defined('ABSPATH') || die('No direct access.'); |
| 10 |
|
| 11 |
use Extendify\Shared\Services\Sanitizer; |
| 12 |
|
| 13 |
/** |
| 14 |
* The controller for persisting site data |
| 15 |
*/ |
| 16 |
|
| 17 |
class SiteController |
| 18 |
{ |
| 19 |
/** |
| 20 |
* Get option data by name. |
| 21 |
* |
| 22 |
* @param \WP_REST_Request $request - The request. |
| 23 |
* @return \WP_REST_Response - Sends JSON response with the option data. |
| 24 |
*/ |
| 25 |
public static function get($request) |
| 26 |
{ |
| 27 |
$data = \get_option('extendify_' . $request->get_param('name')); |
| 28 |
if (empty($data)) { |
| 29 |
return new \WP_REST_Response([]); |
| 30 |
} |
| 31 |
|
| 32 |
if ($request->get_param('item')) { |
| 33 |
$data = ($data['state'][$request->get_param('item')] ?? []); |
| 34 |
} |
| 35 |
|
| 36 |
return new \WP_REST_Response($data); |
| 37 |
} |
| 38 |
/** |
| 39 |
* Persist single data |
| 40 |
* |
| 41 |
* @param \WP_REST_Request $request - The request. |
| 42 |
* @return \WP_REST_Response |
| 43 |
*/ |
| 44 |
public static function single($request) |
| 45 |
{ |
| 46 |
$key = $request->get_param('key'); |
| 47 |
$value = $request->get_param('value'); |
| 48 |
// Remove the 'extendify_' prefix if it exists. |
| 49 |
if (strpos($key, 'extendify_') === 0) { |
| 50 |
$key = substr($key, 10); |
| 51 |
} |
| 52 |
|
| 53 |
\update_option('extendify_' . $key, Sanitizer::sanitizeUnknown($value)); |
| 54 |
return new \WP_REST_Response($value); |
| 55 |
} |
| 56 |
} |
| 57 |
|