- rewrite schemas for posts, tab_posts, oneline_posts, hot_posts, term_cards, term_aggregation, media_cards, combo_info, partners, recent_comments, posts_toc and slider_media_combo to mirror the modern reference modules field by field (options, defaults, dependencies, min/max/step, subheadings) - port reference renderers: hot ranking with TOP badges, category aggregation card, graphic cover cards, partners CSS marquee, combo info cards with auto band palette, recent comment list - rename 分类与标签聚合卡片 to 分类聚合卡片 and restrict it to categories (no tags/topics branch in this project) - drop topics filter fields and the topics query branch; align orderby value domain with supported post meta - add searchable sortable terms admin field type and spinner unit suffix rendering - unify widget pagination and lazy loading on the REST client with the runtime-owned ias reveal trigger; remove the legacy post_ajax_trigger dual path - align section-navs TOC with the reference contract (wznav ids, 3-heading minimum, zib-widget box)
180 lines
9.0 KiB
PHP
180 lines
9.0 KiB
PHP
<?php
|
|
|
|
namespace AKSRT\WidgetSystem;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
/**
|
|
* Applies the common widget contract before dispatching business markup.
|
|
*/
|
|
final class FrontendRenderer {
|
|
public static function render(WidgetDefinition $definition, array $args, array $instance, SchemaWidget $widget): void {
|
|
if (!WidgetVisibility::should_render($instance)) {
|
|
return;
|
|
}
|
|
|
|
$context = [
|
|
'widget_id' => (string) $widget->id,
|
|
'id_base' => $definition->id_base(),
|
|
];
|
|
$content = RendererRegistry::render($definition->renderer(), $instance, $context);
|
|
if ($content === '' && $definition->slug() !== 'layout_gap') {
|
|
return;
|
|
}
|
|
|
|
AssetManager::enqueue_for($definition);
|
|
if (in_array($definition->slug(), ['user_card', 'author_card'], true)) {
|
|
self::render_user_widget($definition, $instance, $widget, $content);
|
|
return;
|
|
}
|
|
if (in_array($definition->slug(), [
|
|
'post_pay', 'tab_posts', 'posts', 'oneline_posts', 'hot_posts',
|
|
'slider', 'slider_media_combo', 'icon_cards', 'icon_cover_cards',
|
|
'buttons', 'notice', 'term_cards', 'term_aggregation', 'media_cards',
|
|
'combo_info', 'partners', 'recent_comments', 'posts_toc',
|
|
], true)) {
|
|
echo (string) ($args['before_widget'] ?? '');
|
|
if ($definition->slug() !== 'post_pay') echo self::reference_title($instance);
|
|
$animation = '';
|
|
$content_classes = ['widget-content'];
|
|
$device_class = WidgetVisibility::device_class($instance);
|
|
if ($device_class !== '') {
|
|
$content_classes[] = $device_class;
|
|
}
|
|
if (!empty($instance['animation_in'])) {
|
|
$content_classes[] = 'obs-animate ani-' . sanitize_html_class((string) $instance['animation_in']);
|
|
if (!empty($instance['animation_repeat'])) {
|
|
$content_classes[] = 'obs-animate-repeat';
|
|
}
|
|
}
|
|
echo '<div class="' . esc_attr(implode(' ', $content_classes)) . '">' . $content . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
echo (string) ($args['after_widget'] ?? '');
|
|
return;
|
|
}
|
|
$classes = ['aksrt-widget', 'aksrt-panel-wrap', 'aksrt-widget--' . $definition->slug(), 'aksrt-widget--' . $definition->size()];
|
|
if ($definition->size() === 'sidebar') {
|
|
$classes[] = 'aksrt-panel';
|
|
}
|
|
$device_class = WidgetVisibility::device_class($instance);
|
|
if ($device_class !== '') {
|
|
$classes[] = $device_class;
|
|
}
|
|
if (!empty($instance['sidebar_affix'])) {
|
|
$classes[] = 'aksrt-widget--affix';
|
|
}
|
|
if (!empty($instance['animation_in'])) {
|
|
$classes[] = 'aksrt-widget--animate';
|
|
$classes[] = 'aksrt-widget--animate-' . sanitize_html_class((string) $instance['animation_in']);
|
|
}
|
|
if (!empty($instance['animation_repeat'])) {
|
|
$classes[] = 'aksrt-widget--animate-repeat';
|
|
}
|
|
|
|
$style = self::style($instance);
|
|
echo (string) ($args['before_widget'] ?? '<section>'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
echo '<div class="' . esc_attr(implode(' ', $classes)) . '" data-aksrt-widget="' . esc_attr($definition->slug()) . '" data-widget-id="' . esc_attr((string) $widget->id) . '"' . ($style !== '' ? ' style="' . esc_attr($style) . '"' : '') . '>';
|
|
if ($definition->size() !== 'sidebar') {
|
|
echo '<div class="aksrt-widget__inner container">';
|
|
}
|
|
echo self::title($instance); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
echo '<div class="aksrt-widget__content">' . $content . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
if ($definition->size() !== 'sidebar') {
|
|
echo '</div>';
|
|
}
|
|
echo '</div>';
|
|
echo (string) ($args['after_widget'] ?? '</section>'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
}
|
|
|
|
private static function render_user_widget(WidgetDefinition $definition, array $instance, SchemaWidget $widget, string $content): void {
|
|
$classes = ['zib-widget-wrap', 'aksrt-user-widget-wrap', 'aksrt-' . str_replace('_', '-', $definition->slug()) . '-wrap'];
|
|
$device_class = WidgetVisibility::device_class($instance);
|
|
if ($device_class !== '') {
|
|
$classes[] = $device_class;
|
|
}
|
|
if (!empty($instance['sidebar_affix'])) {
|
|
$classes[] = 'aksrt-widget--affix';
|
|
}
|
|
|
|
$content_classes = ['widget-content'];
|
|
if (!empty($instance['animation_in'])) {
|
|
$content_classes[] = 'aksrt-widget--animate';
|
|
$content_classes[] = 'aksrt-widget--animate-' . sanitize_html_class((string) $instance['animation_in']);
|
|
}
|
|
if (!empty($instance['animation_repeat'])) {
|
|
$content_classes[] = 'aksrt-widget--animate-repeat';
|
|
}
|
|
|
|
$style = self::style($instance);
|
|
echo '<div class="' . esc_attr(implode(' ', $classes)) . '" data-aksrt-widget="' . esc_attr($definition->slug()) . '" data-widget-id="' . esc_attr((string) $widget->id) . '"' . ($style !== '' ? ' style="' . esc_attr($style) . '"' : '') . '>';
|
|
echo '<div class="widget-container"><div>';
|
|
echo self::reference_title($instance); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
echo '<div class="' . esc_attr(implode(' ', $content_classes)) . '">' . $content . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
echo '</div></div></div>';
|
|
}
|
|
|
|
private static function title(array $instance): string {
|
|
$title = trim((string) ($instance['title'] ?? ''));
|
|
if ($title === '') {
|
|
return '';
|
|
}
|
|
$safe_title = wp_kses_post($title);
|
|
$highlight = trim((string) ($instance['title_highlight'] ?? ''));
|
|
if ($highlight !== '') {
|
|
$safe_highlight = esc_html($highlight);
|
|
$safe_title = str_replace($safe_highlight, '<mark class="' . esc_attr((string) ($instance['title_highlight_color'] ?? 'focus-color')) . '">' . $safe_highlight . '</mark>', $safe_title);
|
|
}
|
|
$html = '<header class="aksrt-widget__header' . (($instance['title_style'] ?? '') === 'big' ? ' aksrt-widget__header--big' : '') . '"><div><h2 class="aksrt-widget__title">' . nl2br($safe_title) . '</h2>';
|
|
if (!empty($instance['subtitle'])) {
|
|
$html .= '<p class="aksrt-widget__subtitle">' . nl2br(wp_kses_post((string) $instance['subtitle'])) . '</p>';
|
|
}
|
|
$html .= '</div><nav class="aksrt-widget__title-links" aria-label="' . esc_attr__('模块链接', 'aksrt') . '">';
|
|
foreach (['title_link', 'title_link2', 'title_link3'] as $index => $key) {
|
|
$link = RendererHelpers::link((array) ($instance[$key] ?? []));
|
|
if ($link['url'] !== '' && $link['text'] !== '') {
|
|
$color = (string) ($instance['title_link_style']['color_' . ($index + 1)] ?? '');
|
|
$html .= '<a class="' . esc_attr($color) . '" href="' . $link['url'] . '"' . RendererHelpers::link_attrs($link) . '>' . esc_html($link['text']) . '</a>';
|
|
}
|
|
}
|
|
return $html . '</nav></header>';
|
|
}
|
|
|
|
private static function reference_title(array $instance): string {
|
|
$title = trim((string) ($instance['title'] ?? ''));
|
|
if ($title === '') return '';
|
|
$title = wp_kses_post($title);
|
|
$highlight = trim((string) ($instance['title_highlight'] ?? ''));
|
|
if ($highlight !== '') $title = str_replace(esc_html($highlight), '<span class="title-highlight ' . esc_attr((string) ($instance['title_highlight_color'] ?? 'focus-color')) . '">' . esc_html($highlight) . '</span>', $title);
|
|
$subtitle = !empty($instance['subtitle']) ? wp_kses_post((string) $instance['subtitle']) : '';
|
|
if (($instance['title_style'] ?? '') === 'big') {
|
|
$links = '';
|
|
foreach (['title_link', 'title_link2', 'title_link3'] as $index => $key) { $link = RendererHelpers::link((array) ($instance[$key] ?? [])); if ($link['url'] && $link['text']) $links .= '<a class="but ' . esc_attr((string) ($instance['title_link_style']['color_' . ($index + 1)] ?? '')) . '" href="' . $link['url'] . '"' . RendererHelpers::link_attrs($link) . '>' . esc_html($link['text']) . '</a>'; }
|
|
return '<div class="big-title-wrap"><h2 class="big-title">' . $title . '</h2>' . ($subtitle ? '<p class="big-title-sub muted-2-color">' . $subtitle . '</p>' : '') . ($links ? '<div class="flex jc big-title-buts">' . $links . '</div>' : '') . '</div>';
|
|
}
|
|
$link = RendererHelpers::link((array) ($instance['title_link'] ?? []));
|
|
$more = $link['url'] ? '<div class="pull-right em09 mt3"><a class="muted-2-color" href="' . $link['url'] . '"' . RendererHelpers::link_attrs($link) . '>' . esc_html($link['text'] ?: __('更多', 'aksrt')) . '</a></div>' : '';
|
|
return '<div class="box-body notop"><div class="title-theme">' . $title . ($subtitle ? '<small class="ml10">' . $subtitle . '</small>' : '') . $more . '</div></div>';
|
|
}
|
|
|
|
private static function style(array $instance): string {
|
|
$properties = [];
|
|
foreach (['layout_pin_pc' => 'space', 'layout_pin_m' => 'space-m'] as $key => $prefix) {
|
|
foreach (['top', 'right', 'bottom', 'left'] as $edge) {
|
|
$value = $instance[$key][$edge] ?? '';
|
|
if ($value !== '' && is_numeric($value)) {
|
|
$properties[] = '--aksrt-widget-' . $prefix . '-' . $edge . ':' . (float) $value . 'px';
|
|
}
|
|
}
|
|
}
|
|
foreach (['light', 'dark'] as $mode) {
|
|
$background = (array) ($instance['layout_bg'][$mode] ?? []);
|
|
if (!empty($background['color'])) {
|
|
$properties[] = '--aksrt-widget-bg-' . $mode . ':' . (string) $background['color'];
|
|
}
|
|
if (!empty($background['image'])) {
|
|
$properties[] = '--aksrt-widget-bg-image-' . $mode . ':url(' . esc_url_raw((string) $background['image']) . ')';
|
|
}
|
|
}
|
|
return implode(';', $properties);
|
|
}
|
|
}
|