feat(widgets): OnelinePosts via swiper-scroll + shared post renderer
Replace private track DOM with slider.js Type A shell and aksrt_render_post cards/mini/list slides, matching product oneline and zibll oneline posts.
This commit is contained in:
1 parent
3fd02a9338
commit
1fdd2301fd
1 file changed
+79
-15
@@ -1,6 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Horizontal one-line posts scroller (zibll oneline + remote HTML).
|
* Horizontal one-line posts scroller (zibll zib_widget_ui_oneline_posts).
|
||||||
|
*
|
||||||
|
* Uses shared post renderer + slider.js Type A (.swiper-scroll).
|
||||||
*
|
*
|
||||||
* @package AKSRT
|
* @package AKSRT
|
||||||
*/
|
*/
|
||||||
@@ -16,12 +18,16 @@ class WidgetOnelinePosts extends \WP_Widget {
|
|||||||
'aksrt_oneline_posts',
|
'aksrt_oneline_posts',
|
||||||
esc_html__('AKSRT 横向文章', 'aksrt'),
|
esc_html__('AKSRT 横向文章', 'aksrt'),
|
||||||
[
|
[
|
||||||
'description' => esc_html__('横向滚动的文章卡片/迷你列表。', 'aksrt'),
|
'description' => esc_html__('横向滚动的文章卡片/迷你列表(共享 swiper-scroll)。', 'aksrt'),
|
||||||
'classname' => 'widget_aksrt_oneline_posts',
|
'classname' => 'widget_aksrt_oneline_posts',
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string,mixed> $instance
|
||||||
|
* @return array<string,mixed>
|
||||||
|
*/
|
||||||
public static function build_query_args(array $instance): array {
|
public static function build_query_args(array $instance): array {
|
||||||
$count = max(1, min(20, (int) ($instance['count'] ?? 8)));
|
$count = max(1, min(20, (int) ($instance['count'] ?? 8)));
|
||||||
$orderby = sanitize_key((string) ($instance['orderby'] ?? 'date'));
|
$orderby = sanitize_key((string) ($instance['orderby'] ?? 'date'));
|
||||||
@@ -60,11 +66,15 @@ class WidgetOnelinePosts extends \WP_Widget {
|
|||||||
return $query;
|
return $query;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string,mixed> $instance
|
||||||
|
*/
|
||||||
public static function render_html(array $instance): string {
|
public static function render_html(array $instance): string {
|
||||||
$style = sanitize_key((string) ($instance['style'] ?? 'card'));
|
$style = sanitize_key((string) ($instance['style'] ?? 'card'));
|
||||||
if (!in_array($style, ['card', 'mini', 'list'], true)) {
|
if (!in_array($style, ['card', 'mini', 'list'], true)) {
|
||||||
$style = 'card';
|
$style = 'card';
|
||||||
}
|
}
|
||||||
|
|
||||||
$q = new \WP_Query(self::build_query_args($instance));
|
$q = new \WP_Query(self::build_query_args($instance));
|
||||||
if (!$q->have_posts()) {
|
if (!$q->have_posts()) {
|
||||||
wp_reset_postdata();
|
wp_reset_postdata();
|
||||||
@@ -72,29 +82,75 @@ class WidgetOnelinePosts extends \WP_Widget {
|
|||||||
? aksrt_get_ajax_null(__('暂无文章', 'aksrt'))
|
? aksrt_get_ajax_null(__('暂无文章', 'aksrt'))
|
||||||
: '<p class="widget-empty ajax-item">' . esc_html__('暂无文章', 'aksrt') . '</p>';
|
: '<p class="widget-empty ajax-item">' . esc_html__('暂无文章', 'aksrt') . '</p>';
|
||||||
}
|
}
|
||||||
ob_start();
|
|
||||||
echo '<div class="aksrt-oneline style-' . esc_attr($style) . ' ajax-item"><div class="aksrt-oneline__track">';
|
$mode = $style === 'mini' ? 'mini' : ($style === 'list' ? 'list' : 'card');
|
||||||
|
$cfg = [
|
||||||
|
'mode' => $mode,
|
||||||
|
'context' => 'widget',
|
||||||
|
'show_thumb' => true,
|
||||||
|
'show_meta' => $style !== 'mini',
|
||||||
|
'show_excerpt' => false,
|
||||||
|
'show_author' => false,
|
||||||
|
'show_comments' => false,
|
||||||
|
'show_likes' => false,
|
||||||
|
'show_tags' => false,
|
||||||
|
'show_badges' => $style === 'card',
|
||||||
|
'skip_wrapper' => true,
|
||||||
|
'extra_class' => 'posts-item',
|
||||||
|
];
|
||||||
|
|
||||||
|
$slides = '';
|
||||||
|
if (function_exists('aksrt_render_post')) {
|
||||||
|
while ($q->have_posts()) {
|
||||||
|
$q->the_post();
|
||||||
|
$item = aksrt_render_post($cfg);
|
||||||
|
if ($item === '') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// Ensure freeMode width calc has a posts-item root when renderer wraps differently.
|
||||||
|
if (strpos($item, 'posts-item') === false && strpos($item, 'posts-mini') === false) {
|
||||||
|
$item = '<div class="posts-item">' . $item . '</div>';
|
||||||
|
}
|
||||||
|
$slides .= '<div class="swiper-slide ajax-item">' . $item . '</div>';
|
||||||
|
}
|
||||||
|
wp_reset_postdata();
|
||||||
|
} else {
|
||||||
|
// Fallback: bare thumb+title (should rarely hit).
|
||||||
while ($q->have_posts()) {
|
while ($q->have_posts()) {
|
||||||
$q->the_post();
|
$q->the_post();
|
||||||
$thumb = get_the_post_thumbnail_url(get_the_ID(), $style === 'card' ? 'medium' : 'thumbnail');
|
$thumb = get_the_post_thumbnail_url(get_the_ID(), $style === 'card' ? 'medium' : 'thumbnail');
|
||||||
echo '<a class="aksrt-oneline__item" href="' . esc_url(get_permalink()) . '">';
|
$slides .= '<div class="swiper-slide ajax-item"><a class="posts-item aksrt-oneline-fallback" href="' . esc_url(get_permalink()) . '">';
|
||||||
if ($thumb) {
|
if ($thumb) {
|
||||||
echo '<div class="aksrt-oneline__thumb"><img src="' . esc_url($thumb) . '" alt="" loading="lazy" /></div>';
|
$slides .= '<div class="item-thumbnail"><img src="' . esc_url($thumb) . '" alt="" loading="lazy" /></div>';
|
||||||
} else {
|
|
||||||
echo '<div class="aksrt-oneline__thumb is-empty"></div>';
|
|
||||||
}
|
}
|
||||||
echo '<div class="aksrt-oneline__body">';
|
$slides .= '<div class="item-body"><div class="item-heading">' . esc_html(get_the_title()) . '</div></div></a></div>';
|
||||||
echo '<div class="aksrt-oneline__title">' . esc_html(get_the_title()) . '</div>';
|
|
||||||
if ($style !== 'mini') {
|
|
||||||
echo '<div class="aksrt-oneline__meta">' . esc_html(get_the_date()) . '</div>';
|
|
||||||
}
|
}
|
||||||
echo '</div></a>';
|
|
||||||
}
|
|
||||||
echo '</div></div>';
|
|
||||||
wp_reset_postdata();
|
wp_reset_postdata();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($slides === '') {
|
||||||
|
return function_exists('aksrt_get_ajax_null')
|
||||||
|
? aksrt_get_ajax_null(__('暂无文章', 'aksrt'))
|
||||||
|
: '<p class="widget-empty ajax-item">' . esc_html__('暂无文章', 'aksrt') . '</p>';
|
||||||
|
}
|
||||||
|
|
||||||
|
// zibll: .widget-main-post.style-{card|mini|list} + .swiper-container.swiper-scroll data-slideClass="posts-item".
|
||||||
|
ob_start();
|
||||||
|
echo '<div class="widget-main-post style-' . esc_attr($style) . ' aksrt-oneline-posts ajax-item">';
|
||||||
|
echo '<div class="swiper-container swiper-scroll aksrt-oneline-swiper" data-slideClass="posts-item">';
|
||||||
|
echo '<div class="swiper-wrapper">';
|
||||||
|
echo $slides; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||||
|
echo '</div>';
|
||||||
|
echo '<div class="swiper-button-prev" aria-label="' . esc_attr__('上一页', 'aksrt') . '"></div>';
|
||||||
|
echo '<div class="swiper-button-next" aria-label="' . esc_attr__('下一页', 'aksrt') . '"></div>';
|
||||||
|
echo '</div></div>';
|
||||||
return (string) ob_get_clean();
|
return (string) ob_get_clean();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string,mixed> $args
|
||||||
|
* @param array<string,mixed> $instance
|
||||||
|
*/
|
||||||
public function widget($args, $instance): void {
|
public function widget($args, $instance): void {
|
||||||
$html = self::render_html($instance);
|
$html = self::render_html($instance);
|
||||||
if ($html === '') {
|
if ($html === '') {
|
||||||
@@ -112,6 +168,9 @@ class WidgetOnelinePosts extends \WP_Widget {
|
|||||||
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string,mixed> $instance
|
||||||
|
*/
|
||||||
public function form($instance): void {
|
public function form($instance): void {
|
||||||
?>
|
?>
|
||||||
<p>
|
<p>
|
||||||
@@ -156,6 +215,11 @@ class WidgetOnelinePosts extends \WP_Widget {
|
|||||||
<?php
|
<?php
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string,mixed> $new_instance
|
||||||
|
* @param array<string,mixed> $old_instance
|
||||||
|
* @return array<string,mixed>
|
||||||
|
*/
|
||||||
public function update($new_instance, $old_instance): array {
|
public function update($new_instance, $old_instance): array {
|
||||||
$style = sanitize_key((string) ($new_instance['style'] ?? 'card'));
|
$style = sanitize_key((string) ($new_instance['style'] ?? 'card'));
|
||||||
if (!in_array($style, ['card', 'mini', 'list'], true)) {
|
if (!in_array($style, ['card', 'mini', 'list'], true)) {
|
||||||
|
|||||||
Reference in new issue
Block a user