feat(widgets): complete phase 3 atomic integration
This commit is contained in:
@@ -144,177 +144,6 @@ trait ImportExportControllerPart1 {
|
||||
]);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Widget Template Handlers */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
/**
|
||||
* GET /widgets/export — Export current widget configuration.
|
||||
*/
|
||||
public function export_widgets(\WP_REST_Request $request): \WP_REST_Response {
|
||||
$sidebars_widgets = get_option('sidebars_widgets', []);
|
||||
$widget_options = $this->collect_widget_options();
|
||||
|
||||
return $this->success([
|
||||
'data' => [
|
||||
'version' => AKSRT_VERSION ?? '3.4.0',
|
||||
'exported' => gmdate('Y-m-d H:i:s'),
|
||||
'sidebars_widgets' => $sidebars_widgets,
|
||||
'widget_options' => $widget_options,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /widgets/import — Import widget configuration.
|
||||
*/
|
||||
public function import_widgets(\WP_REST_Request $request): \WP_REST_Response {
|
||||
$params = $request->get_json_params() ?? [];
|
||||
$sidebars = $params['sidebars_widgets'] ?? null;
|
||||
$options = $params['widget_options'] ?? null;
|
||||
|
||||
if (!$sidebars || !is_array($sidebars)) {
|
||||
return $this->error('缺少 sidebars_widgets 数据');
|
||||
}
|
||||
|
||||
// Validate sidebar IDs exist
|
||||
$valid_sidebars = array_keys(get_registered_sidebars());
|
||||
$valid_sidebars[] = 'wp_inactive_widgets';
|
||||
|
||||
$imported = 0;
|
||||
foreach ($sidebars as $sidebar_id => $widget_ids) {
|
||||
if (!in_array($sidebar_id, $valid_sidebars, true) && $sidebar_id !== 'array_version') {
|
||||
continue;
|
||||
}
|
||||
$imported++;
|
||||
}
|
||||
|
||||
// Import widget options if provided
|
||||
if ($options && is_array($options)) {
|
||||
foreach ($options as $option_key => $option_value) {
|
||||
if (preg_match('/^widget_aksrt_/', $option_key)) {
|
||||
update_option($option_key, $option_value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Import sidebar assignments
|
||||
update_option('sidebars_widgets', $sidebars);
|
||||
|
||||
return $this->success(['message' => "已导入 {$imported} 个侧栏配置。"]);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /widgets/templates — List saved widget templates.
|
||||
*/
|
||||
public function list_widget_templates(\WP_REST_Request $request): \WP_REST_Response {
|
||||
$templates = get_option('aksrt_widget_templates', []);
|
||||
return $this->success(['data' => ['templates' => $templates]]);
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /widgets/templates — Save current layout as a named template.
|
||||
*/
|
||||
public function save_widget_template(\WP_REST_Request $request): \WP_REST_Response {
|
||||
$params = $request->get_json_params() ?? [];
|
||||
$name = sanitize_text_field((string) ($params['name'] ?? ''));
|
||||
if (!$name) {
|
||||
return $this->error('请输入模板名称');
|
||||
}
|
||||
|
||||
$templates = get_option('aksrt_widget_templates', []);
|
||||
$template = [
|
||||
'id' => count($templates) + 1,
|
||||
'name' => $name,
|
||||
'created_at' => gmdate('Y-m-d H:i:s'),
|
||||
'sidebars_widgets' => get_option('sidebars_widgets', []),
|
||||
'widget_options' => $this->collect_widget_options(),
|
||||
];
|
||||
|
||||
$templates[] = $template;
|
||||
update_option('aksrt_widget_templates', $templates);
|
||||
|
||||
return $this->success(['message' => '模板已保存', 'data' => $template]);
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /widgets/templates/{id}/apply — Apply a saved template.
|
||||
*/
|
||||
public function apply_widget_template(\WP_REST_Request $request): \WP_REST_Response {
|
||||
$id = (int) $request->get_param('id');
|
||||
$templates = get_option('aksrt_widget_templates', []);
|
||||
|
||||
$template = null;
|
||||
foreach ($templates as $t) {
|
||||
if ((int) ($t['id'] ?? 0) === $id) {
|
||||
$template = $t;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$template) {
|
||||
return $this->error('模板不存在', 404);
|
||||
}
|
||||
|
||||
// Apply widget options
|
||||
if (!empty($template['widget_options']) && is_array($template['widget_options'])) {
|
||||
foreach ($template['widget_options'] as $key => $value) {
|
||||
update_option($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
// Apply sidebar assignments
|
||||
if (!empty($template['sidebars_widgets'])) {
|
||||
update_option('sidebars_widgets', $template['sidebars_widgets']);
|
||||
}
|
||||
|
||||
return $this->success(['message' => '模板已应用']);
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE /widgets/templates/{id} — Delete a saved template.
|
||||
*/
|
||||
public function delete_widget_template(\WP_REST_Request $request): \WP_REST_Response {
|
||||
$id = (int) $request->get_param('id');
|
||||
$templates = get_option('aksrt_widget_templates', []);
|
||||
|
||||
$found = false;
|
||||
$filtered = [];
|
||||
foreach ($templates as $t) {
|
||||
if ((int) ($t['id'] ?? 0) === $id) {
|
||||
$found = true;
|
||||
continue;
|
||||
}
|
||||
$filtered[] = $t;
|
||||
}
|
||||
|
||||
if (!$found) {
|
||||
return $this->error('模板不存在', 404);
|
||||
}
|
||||
|
||||
update_option('aksrt_widget_templates', $filtered);
|
||||
return $this->success(['message' => '模板已删除']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect all widget options from the database.
|
||||
*/
|
||||
private function collect_widget_options(): array {
|
||||
global $wpdb;
|
||||
$rows = $wpdb->get_results(
|
||||
"SELECT option_name, option_value FROM {$wpdb->options} WHERE option_name LIKE 'widget_aksrt_%' ORDER BY option_name",
|
||||
ARRAY_A
|
||||
);
|
||||
|
||||
$options = [];
|
||||
if ($rows) {
|
||||
foreach ($rows as $row) {
|
||||
$value = maybe_unserialize($row['option_value']);
|
||||
$options[$row['option_name']] = $value;
|
||||
}
|
||||
}
|
||||
return $options;
|
||||
}
|
||||
|
||||
private function persist_setting_backup(array $backup): void {
|
||||
$backups = get_option('aksrt_settings_backups', []);
|
||||
|
||||
Reference in New Issue
Block a user