Files
wordpress-theme-aksrt/src/WidgetSystem/AssetManager.php
T
kate522andClaude Opus 5 f80ca1e27d fix(widgets): restore single-card reference UI for search and quote widgets
The 搜索框 and 一言 sidebar widgets fell into the generic renderer branch, so
they rendered a .jio-panel card wrapping a second bespoke card
(.jio-widget-search / .jio-widget-quote). In-article 一言 also used
divergent modifier classes and an extra hover rule.

- search: render through the shared jio_get_search_box() (legacy keyword,
  history and category pickers) inside one reference card, mirroring the
  reference widget's markup and title default 「搜索」
- quote: render the reference .yiyan-box card itself as the click root; the
  shell now emits only the reference widget-container/title wrapper
- article 一言: adopt the reference class lists for header, footer and the
  standalone after-article box; drop the invented yiyan--* modifiers and
  the duplicate hover/opacity rules
- client: fill .cn/.en in the reference order (first line .cn, second .en)
  and drop the stale window.JioYiyan bridge plus the dead qv-yiyan.php AJAX
  path in runtime main.js
- schema: replace the ad-hoc search_style radio with the reference switches
  (keywords, history, input category, more_cats) and default title
- assets: enqueue assets/js/yiyan.js through the quote widget's asset list

Tests: widget-system suite 820/821 (the one failure is the pre-existing
frontend-lifecycle theme-init stub); new search/quote card contracts pass.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-11-03 15:56:52 +08:00

85 lines
3.5 KiB
PHP

<?php
namespace Jio\WidgetSystem;
defined('ABSPATH') || exit;
/** Registers the isolated widget bundle and optional interaction libraries. */
final class AssetManager {
private const STYLE_HANDLE = 'jio-widget-system';
private const SCRIPT_HANDLE = 'jio-widgets';
private static bool $registered = false;
private static bool $configured = false;
/** Enqueue before wp_head whenever at least one new widget is assigned. */
public static function enqueue_active(): void {
foreach (WidgetRegistry::all() as $definition) {
if (is_active_widget(false, false, $definition->id_base(), true)) {
self::enqueue_for($definition);
}
}
}
public static function enqueue_for(WidgetDefinition $definition): void {
self::register();
if ($definition->slug() === 'post_pay') {
wp_enqueue_style('jio-pay-box');
}
foreach ($definition->assets() as $asset) {
self::enqueue_optional($asset);
}
wp_enqueue_style(self::STYLE_HANDLE);
wp_enqueue_script(self::SCRIPT_HANDLE);
if (!self::$configured) {
$config = [
'restUrl' => esc_url_raw(rest_url('jio/v1/widgets/')),
'i18n' => [
'loading' => __('正在加载…', 'jio'),
'error' => __('加载失败,请重试。', 'jio'),
'retry' => __('重试', 'jio'),
],
];
wp_add_inline_script(self::SCRIPT_HANDLE, 'window.jioWidgets=' . wp_json_encode($config) . ';', 'before');
self::$configured = true;
}
}
private static function register(): void {
if (self::$registered) {
return;
}
$root = get_template_directory();
$uri = get_template_directory_uri();
$css = $root . '/assets/widgets/frontend.css';
$pay_css = $root . '/assets/css/frontend/pay-box.css';
$js = $root . '/assets/widgets/frontend.js';
wp_register_style(self::STYLE_HANDLE, $uri . '/assets/widgets/frontend.css', ['jio-public'], is_file($css) ? (string) filemtime($css) : null);
wp_register_style('jio-pay-box', $uri . '/assets/css/frontend/pay-box.css', ['jio-public'], is_file($pay_css) ? (string) filemtime($pay_css) : null);
$yiyan_js = $root . '/assets/js/yiyan.js';
wp_register_script('jio-yiyan', $uri . '/assets/js/yiyan.js', ['loader_js'], is_file($yiyan_js) ? (string) filemtime($yiyan_js) : null, true);
wp_register_script(self::SCRIPT_HANDLE, $uri . '/assets/widgets/frontend.js', ['loader_js'], is_file($js) ? (string) filemtime($js) : null, true);
$third_party = $uri . '/assets/widgets/third-party/';
wp_register_script('jio-widget-hls', $third_party . 'hls.min.js', [], 'reference-pinned', true);
wp_register_script('jio-widget-flv', $third_party . 'flv.min.js', [], 'reference-pinned', true);
wp_register_script('jio-widget-dash', $third_party . 'dash.all.min.js', [], 'reference-pinned', true);
wp_register_script('jio-widget-dplayer', $third_party . 'DPlayer.min.js', ['jio-widget-hls', 'jio-widget-flv', 'jio-widget-dash'], 'reference-pinned', true);
wp_register_script('jio-widget-tilt', $third_party . 'vanilla-tilt.min.js', [], 'reference-pinned', true);
self::$registered = true;
}
private static function enqueue_optional(string $asset): void {
if ($asset === 'swiper') {
// legacy main.js owns the single Swiper runtime and discovers .new-swiper.
} elseif ($asset === 'dplayer') {
wp_enqueue_script('jio-widget-dplayer');
} elseif (in_array($asset, ['hls', 'flv', 'dash'], true)) {
wp_enqueue_script('jio-widget-' . $asset);
} elseif ($asset === 'tilt') {
wp_enqueue_script('jio-widget-tilt');
} elseif ($asset === 'yiyan') {
wp_enqueue_script('jio-yiyan');
}
}
}