Files
wordpress-theme-aksrt/src/Admin/ToolsController.php
T

96 lines
3.1 KiB
PHP

<?php
/**
* @package AKSRT
*/
namespace AKSRT\Admin;
defined('ABSPATH') || exit;
class ToolsController extends ControllerBase {
private static $instance = null;
public static function get_instance(): self {
if (null === self::$instance) self::$instance = new self();
return self::$instance;
}
public function register_routes(): void {
// Tools: update center
register_rest_route(self::API_NAMESPACE, '/tools/update-info', [
'methods' => 'GET',
'callback' => [$this, 'get_update_info'],
'permission_callback' => [$this, 'check_permission'],
]);
register_rest_route(self::API_NAMESPACE, '/tools/update-check', [
'methods' => 'POST',
'callback' => [$this, 'detect_update'],
'permission_callback' => [$this, 'check_permission'],
]);
register_rest_route(self::API_NAMESPACE, '/tools/update-dismiss', [
'methods' => 'POST',
'callback' => [$this, 'dismiss_update'],
'permission_callback' => [$this, 'check_permission'],
]);
}
public function get_update_info(): \WP_REST_Response {
$theme = wp_get_theme();
$info = [
'current_version' => $theme->get('Version') ?: '0.0.0',
'name' => $theme->get('Name') ?: 'AKSRT',
'update_available'=> false,
'remote_version' => '',
'download_url' => '',
'changelog' => '',
'last_check' => 0,
'dismissed' => '',
];
if (class_exists('\AKSRT\Core\UpdateChecker')) {
$stored = \AKSRT\Core\UpdateChecker::get_instance()->get_update_info();
if (is_array($stored)) {
$info = array_merge($info, $stored);
}
}
$info['dismissed'] = (string) get_user_meta(get_current_user_id(), 'aksrt_dismissed_update', true);
return $this->success(['data' => $info]);
}
public function detect_update(): \WP_REST_Response {
if (!class_exists('\AKSRT\Core\UpdateChecker')) {
return $this->error(__('更新检查器不可用', 'aksrt'), 500);
}
\AKSRT\Core\UpdateChecker::get_instance()->do_check();
$stored = \AKSRT\Core\UpdateChecker::get_instance()->get_update_info();
return $this->success([
'message' => __('已完成更新检测', 'aksrt'),
'data' => is_array($stored) ? $stored : [],
]);
}
public function dismiss_update(\WP_REST_Request $request): \WP_REST_Response {
$params = $this->get_json_params($request);
$version = sanitize_text_field((string) ($params['version'] ?? ''));
if ($version === '') {
return $this->error(__('缺少版本号', 'aksrt'), 422);
}
update_user_meta(get_current_user_id(), 'aksrt_dismissed_update', $version);
return $this->success([
'message' => __('已忽略此版本提醒', 'aksrt'),
'data' => ['version' => $version],
]);
}
}