id !== 'widgets' || !current_user_can('edit_theme_options')) { return; } $sidebars = $GLOBALS['wp_registered_sidebars'] ?? []; $templates = WidgetTransfer::templates(); $notice = isset($_GET['aksrt_widgets_notice']) ? sanitize_key(wp_unslash($_GET['aksrt_widgets_notice'])) : ''; if ($notice !== '') { echo '

' . esc_html__('小工具配置已更新。', 'aksrt') . '

'; } ?>

2 * MB_IN_BYTES) { wp_die(esc_html__('导入文件无效或超过 2MB。', 'aksrt')); } $payload = json_decode((string) file_get_contents((string) $file['tmp_name']), true); if (!is_array($payload)) { wp_die(esc_html__('导入文件不是有效 JSON。', 'aksrt')); } self::run_import($payload); } public static function handle_template(): void { self::authorize(); $template = isset($_POST['template']) ? sanitize_key(wp_unslash($_POST['template'])) : ''; try { $payload = WidgetTransfer::template_payload($template); } catch (\InvalidArgumentException $error) { wp_die(esc_html($error->getMessage()), '', ['response' => 400]); } self::run_import($payload); } public static function ajax_preview(): void { check_ajax_referer(self::NONCE, 'nonce'); if (!current_user_can('edit_theme_options')) { wp_send_json_error(['message' => __('权限不足。', 'aksrt')], 403); } $template = isset($_POST['template']) ? sanitize_key(wp_unslash($_POST['template'])) : ''; try { wp_send_json_success(['html' => WidgetTransfer::preview($template)]); } catch (\InvalidArgumentException $error) { wp_send_json_error(['message' => $error->getMessage()], 400); } } private static function authorize(): void { if (!current_user_can('edit_theme_options')) { wp_die(esc_html__('权限不足。', 'aksrt'), '', ['response' => 403]); } check_admin_referer(self::NONCE); } private static function run_import(array $payload): void { $mode = isset($_POST['mode']) ? sanitize_key(wp_unslash($_POST['mode'])) : 'append'; $target = isset($_POST['target_sidebar']) ? sanitize_key(wp_unslash($_POST['target_sidebar'])) : ''; try { WidgetTransfer::import($payload, $mode, $target); } catch (\InvalidArgumentException $error) { wp_die(esc_html($error->getMessage()), '', ['response' => 400]); } wp_safe_redirect(add_query_arg('aksrt_widgets_notice', 'updated', admin_url('widgets.php'))); exit; } private static function sidebar_select(array $sidebars, string $name, bool $allow_empty, string $empty_label = ''): void { echo ''; } private static function mode_select(): void { echo ''; } }