sakura/app/controllers/configuration-controller.php

154 lines
3.9 KiB
PHP
Raw Normal View History

2021-07-14 14:55:41 +08:00
<?php
namespace Sakura\Controllers;
use WP_REST_Server;
use WP_REST_Request;
use WP_Error;
2021-07-14 16:54:49 +08:00
use Sakura\Lib\Exception;
2021-07-14 14:55:41 +08:00
use Sakura\Models\OptionModel;
class ConfigurationController extends BaseController
{
2021-07-14 16:54:49 +08:00
public function public_options()
{
$keys = [
// key => default value
'title' => 'Theme Sakura',
];
$res = [];
foreach ($keys as $key => $default) {
$res[$key] = $this->sakura_options($key, $default);
}
return $res;
}
2021-07-14 14:55:41 +08:00
/**
* Constructor.
*
* @since 5.0.0
*/
public function __construct()
{
$this->namespace = 'sakura/v1';
$this->rest_base = 'config';
}
/**
* Registers the routes for comments.
*
2021-07-14 16:54:49 +08:00
* @since 5.0.0
2021-07-14 14:55:41 +08:00
*
* @see register_rest_route()
*/
public function register_routes()
{
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'get_config'),
'permission_callback' => array($this, 'get_config_permissions_check'),
// 'args' => $this->get_collection_params(),
),
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array($this, 'update_config'),
'permission_callback' => array($this, 'update_config_permissions_check'),
// 'args' => $this->get_endpoint_args_for_item_schema(WP_REST_Server::CREATABLE),
),
// 'schema' => array($this, 'get_public_item_schema'),
)
);
}
public function get_config(WP_REST_Request $request)
{
$config = (array) OptionModel::get($this->rest_base);
if (!$config) {
return new WP_Error(
'no_such_option',
__('Maybe you should save the configuration bufore using it.', self::$text_domain),
array('status' => 500)
);
} else {
return $config;
}
}
public function get_config_permissions_check(WP_REST_Request $request)
{
return true;
}
2021-07-14 16:54:49 +08:00
public function update_config(WP_REST_Request $request)
2021-07-14 14:55:41 +08:00
{
$original = (array) $this->get_config($request);
$json = (array) self::json_validate($request->get_body());
if (empty(array_diff($original, $json))) {
return $original;
}
2021-07-14 16:54:49 +08:00
$config = OptionModel::update($this->rest_base, $json);
2021-07-14 14:55:41 +08:00
if (!$config) {
return new WP_Error(
'save_config_failure',
__('Unable to save configuration.', self::$text_domain),
array('status' => 500)
);
} else {
return $this->get_config($request);
}
}
2021-07-14 16:54:49 +08:00
public function update_config_permissions_check(WP_REST_Request $request)
2021-07-14 14:55:41 +08:00
{
return true;
}
2021-07-14 16:54:49 +08:00
public function inite_theme()
2021-07-14 14:55:41 +08:00
{
2021-07-14 16:54:49 +08:00
$config = OptionModel::create($this->rest_base, (array)[]);
2021-07-14 14:55:41 +08:00
}
public static function json_validate(string $string)
{
$json = json_decode($string);
return $json;
}
2021-07-14 16:54:49 +08:00
public function set_key_value(string $key, $value)
{
$json = (array) OptionModel::get($this->rest_base);
if (!$json) {
return new WP_Error(
'no_such_option',
__('Maybe you should save the configuration bufore using it.', self::$text_domain),
array('status' => 500)
);
}
$json[$key] = $value;
$config = OptionModel::update($this->rest_base, $json);
$config = $config ? $config : OptionModel::create($this->rest_base, $json);
return $config;
}
public function sakura_options(string $namespace, $default)
{
$config = (array) OptionModel::get($this->rest_base);
if (array_key_exists($namespace, $config)) {
return $config[$namespace];
} else {
$this->set_key_value($namespace, $default);
return $default;
}
// translators: %s: $namespace */
// throw new Exception(
// sprintf(__("No existing database saving value or default value for option '%s'.", self::$text_domain), $namespace)
// );
}
2021-07-14 14:55:41 +08:00
}