feat(widgets): oneline posts scroller + tilt showcase cases

Add remaining high-traffic zibll marketing widgets: horizontal posts
rail and left-copy/right-image tilt cases, with CSS registration.
This commit is contained in:
2026-07-19 00:35:00 +08:00
parent bf28ab64b8
commit 2f81e2a0b5
5 changed files with 581 additions and 0 deletions
+163
View File
@@ -0,0 +1,163 @@
<?php
/**
* Horizontal one-line posts scroller (zibll widget_ui_oneline_posts lite).
*
* @package AKSRT
*/
namespace AKSRT\Widgets;
defined('ABSPATH') || exit;
class WidgetOnelinePosts extends \WP_Widget {
public function __construct() {
parent::__construct(
'aksrt_oneline_posts',
esc_html__('AKSRT 横向文章', 'aksrt'),
[
'description' => esc_html__('横向滚动的文章卡片/迷你列表。', 'aksrt'),
'classname' => 'widget_aksrt_oneline_posts',
]
);
}
public function widget($args, $instance): void {
$count = max(1, min(20, (int) ($instance['count'] ?? 8)));
$style = sanitize_key((string) ($instance['style'] ?? 'card'));
if (!in_array($style, ['card', 'mini', 'list'], true)) {
$style = 'card';
}
$orderby = sanitize_key((string) ($instance['orderby'] ?? 'date'));
$cat = (int) ($instance['cat'] ?? 0);
$limit_day = max(0, (int) ($instance['limit_day'] ?? 0));
$query = [
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $count,
'ignore_sticky_posts' => true,
'no_found_rows' => true,
];
if ($cat > 0) {
$query['cat'] = $cat;
}
if ($limit_day > 0) {
$query['date_query'] = [['after' => $limit_day . ' days ago']];
}
switch ($orderby) {
case 'views':
$query['meta_key'] = 'aksrt_post_views';
$query['orderby'] = 'meta_value_num';
$query['order'] = 'DESC';
break;
case 'comments':
$query['orderby'] = 'comment_count';
$query['order'] = 'DESC';
break;
case 'rand':
$query['orderby'] = 'rand';
break;
default:
$query['orderby'] = 'date';
$query['order'] = 'DESC';
}
$q = new \WP_Query($query);
if (!$q->have_posts()) {
wp_reset_postdata();
return;
}
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base);
if ($title !== '') {
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
echo '<div class="aksrt-oneline style-' . esc_attr($style) . '"><div class="aksrt-oneline__track">';
while ($q->have_posts()) {
$q->the_post();
$thumb = get_the_post_thumbnail_url(get_the_ID(), $style === 'card' ? 'medium' : 'thumbnail');
echo '<a class="aksrt-oneline__item" href="' . esc_url(get_permalink()) . '">';
if ($thumb) {
echo '<div class="aksrt-oneline__thumb"><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">';
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();
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
public function form($instance): void {
?>
<p>
<label><?php esc_html_e('标题', 'aksrt'); ?>
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr((string) ($instance['title'] ?? '')); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('数量', 'aksrt'); ?>
<input class="tiny-text" type="number" min="1" max="20" name="<?php echo esc_attr($this->get_field_name('count')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['count'] ?? 8))); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('样式', 'aksrt'); ?>
<select name="<?php echo esc_attr($this->get_field_name('style')); ?>">
<option value="card" <?php selected(($instance['style'] ?? 'card'), 'card'); ?>><?php esc_html_e('卡片', 'aksrt'); ?></option>
<option value="mini" <?php selected(($instance['style'] ?? ''), 'mini'); ?>><?php esc_html_e('迷你', 'aksrt'); ?></option>
<option value="list" <?php selected(($instance['style'] ?? ''), 'list'); ?>><?php esc_html_e('列表', 'aksrt'); ?></option>
</select>
</label>
</p>
<p>
<label><?php esc_html_e('排序', 'aksrt'); ?>
<select name="<?php echo esc_attr($this->get_field_name('orderby')); ?>">
<option value="date" <?php selected(($instance['orderby'] ?? 'date'), 'date'); ?>><?php esc_html_e('时间', 'aksrt'); ?></option>
<option value="views" <?php selected(($instance['orderby'] ?? ''), 'views'); ?>><?php esc_html_e('浏览', 'aksrt'); ?></option>
<option value="comments" <?php selected(($instance['orderby'] ?? ''), 'comments'); ?>><?php esc_html_e('评论', 'aksrt'); ?></option>
<option value="rand" <?php selected(($instance['orderby'] ?? ''), 'rand'); ?>><?php esc_html_e('随机', 'aksrt'); ?></option>
</select>
</label>
</p>
<p>
<label><?php esc_html_e('分类 ID(0=全部)', 'aksrt'); ?>
<input class="tiny-text" type="number" min="0" name="<?php echo esc_attr($this->get_field_name('cat')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['cat'] ?? 0))); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('限制最近 N 天(0=不限)', 'aksrt'); ?>
<input class="tiny-text" type="number" min="0" name="<?php echo esc_attr($this->get_field_name('limit_day')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['limit_day'] ?? 0))); ?>">
</label>
</p>
<?php
}
public function update($new_instance, $old_instance): array {
$style = sanitize_key((string) ($new_instance['style'] ?? 'card'));
if (!in_array($style, ['card', 'mini', 'list'], true)) {
$style = 'card';
}
$orderby = sanitize_key((string) ($new_instance['orderby'] ?? 'date'));
if (!in_array($orderby, ['date', 'views', 'comments', 'rand'], true)) {
$orderby = 'date';
}
return [
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
'count' => max(1, min(20, (int) ($new_instance['count'] ?? 8))),
'style' => $style,
'orderby' => $orderby,
'cat' => max(0, (int) ($new_instance['cat'] ?? 0)),
'limit_day' => max(0, (int) ($new_instance['limit_day'] ?? 0)),
];
}
}
+258
View File
@@ -0,0 +1,258 @@
<?php
/**
* Tilt cases / showcase intro (zibll zib_widget_ui_tilt_cases lite, no JS tilt).
*
* @package AKSRT
*/
namespace AKSRT\Widgets;
defined('ABSPATH') || exit;
class WidgetTiltCases extends \WP_Widget {
public function __construct() {
parent::__construct(
'aksrt_tilt_cases',
esc_html__('AKSRT 案例展示', 'aksrt'),
[
'description' => esc_html__('左文案 + 右侧叠层图片案例展示。', 'aksrt'),
'classname' => 'widget_aksrt_tilt_cases',
]
);
}
public function widget($args, $instance): void {
$title = trim((string) ($instance['intro_title'] ?? ''));
$subtitle = trim((string) ($instance['intro_subtitle'] ?? ''));
$badge = trim((string) ($instance['intro_badge_text'] ?? ''));
$images = $this->normalize_images($instance['intro_showcase_images'] ?? []);
$features = $this->normalize_features($instance['intro_features'] ?? []);
$buttons = $this->normalize_buttons($instance['intro_buttons'] ?? []);
if ($title === '' && $subtitle === '' && !$images) {
return;
}
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '<div class="aksrt-tilt">';
echo '<div class="aksrt-tilt__text">';
if ($badge !== '') {
$icon = sanitize_text_field((string) ($instance['intro_badge_icon'] ?? ''));
echo '<div class="aksrt-tilt__badge">';
if ($icon !== '') {
echo '<i class="' . esc_attr($icon) . '" aria-hidden="true"></i> ';
}
echo esc_html($badge) . '</div>';
}
if ($title !== '') {
$highlight = trim((string) ($instance['intro_title_highlight'] ?? ''));
$title_html = esc_html($title);
if ($highlight !== '') {
$h = esc_html($highlight);
$title_html = str_replace($h, '<em class="aksrt-tilt__hl">' . $h . '</em>', $title_html);
}
echo '<h2 class="aksrt-tilt__title">' . $title_html . '</h2>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
if ($subtitle !== '') {
echo '<p class="aksrt-tilt__subtitle">' . esc_html($subtitle) . '</p>';
}
if ($features) {
echo '<ul class="aksrt-tilt__features">';
foreach ($features as $f) {
echo '<li>';
if ($f['icon'] !== '') {
echo '<i class="' . esc_attr($f['icon']) . '" aria-hidden="true"></i> ';
}
echo esc_html($f['text']) . '</li>';
}
echo '</ul>';
}
if ($buttons) {
echo '<div class="aksrt-tilt__actions">';
foreach ($buttons as $b) {
$url = $b['url'];
if ($url !== '' && !empty($instance['go_link']) && function_exists('aksrt_go_link')) {
$url = aksrt_go_link($url, true);
}
echo '<a class="aksrt-tilt__btn" href="' . esc_url($url !== '' ? $url : '#') . '">' . esc_html($b['text']) . '</a>';
}
echo '</div>';
}
echo '</div>';
echo '<div class="aksrt-tilt__showcase">';
if ($images) {
foreach ($images as $i => $img) {
$cls = $i === 0 ? ' is-main' : '';
echo '<div class="aksrt-tilt__img' . esc_attr($cls) . '"><img src="' . esc_url($img) . '" alt="" loading="lazy" /></div>';
}
} else {
echo '<div class="aksrt-tilt__img is-main is-empty"></div>';
}
echo '</div></div>';
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/** @param mixed $raw @return array<int,string> */
private function normalize_images($raw): array {
if (is_string($raw)) {
$decoded = json_decode($raw, true);
if (is_array($decoded)) {
$raw = $decoded;
} else {
$parts = preg_split('/\r\n|\r|\n|,/', $raw) ?: [];
$raw = $parts;
}
}
if (!is_array($raw)) {
return [];
}
$out = [];
foreach ($raw as $row) {
if (is_array($row)) {
$url = esc_url_raw(trim((string) ($row['image'] ?? $row['url'] ?? '')));
} else {
$url = esc_url_raw(trim((string) $row));
}
if ($url !== '') {
$out[] = $url;
}
if (count($out) >= 5) {
break;
}
}
return $out;
}
/** @param mixed $raw @return array<int, array{text:string,icon:string}> */
private function normalize_features($raw): array {
if (is_string($raw)) {
$decoded = json_decode($raw, true);
$raw = is_array($decoded) ? $decoded : [];
}
if (!is_array($raw)) {
return [];
}
$out = [];
foreach ($raw as $row) {
if (is_string($row)) {
$text = sanitize_text_field($row);
$icon = '';
} elseif (is_array($row)) {
$text = sanitize_text_field((string) ($row['text'] ?? ''));
$icon = sanitize_text_field((string) ($row['icon'] ?? ''));
} else {
continue;
}
if ($text === '') {
continue;
}
$out[] = ['text' => $text, 'icon' => $icon];
if (count($out) >= 8) {
break;
}
}
return $out;
}
/** @param mixed $raw @return array<int, array{text:string,url:string}> */
private function normalize_buttons($raw): array {
if (is_string($raw)) {
$decoded = json_decode($raw, true);
$raw = is_array($decoded) ? $decoded : [];
}
if (!is_array($raw)) {
return [];
}
$out = [];
foreach ($raw as $row) {
if (!is_array($row)) {
continue;
}
$text = sanitize_text_field((string) ($row['text'] ?? $row['btn']['text'] ?? ''));
$url = esc_url_raw(trim((string) ($row['url'] ?? $row['btn']['url'] ?? '')));
if ($text === '') {
continue;
}
$out[] = ['text' => $text, 'url' => $url];
if (count($out) >= 4) {
break;
}
}
return $out;
}
public function form($instance): void {
$images = is_string($instance['intro_showcase_images'] ?? null)
? (string) $instance['intro_showcase_images']
: wp_json_encode($this->normalize_images($instance['intro_showcase_images'] ?? []), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$features = is_string($instance['intro_features'] ?? null)
? (string) $instance['intro_features']
: wp_json_encode($this->normalize_features($instance['intro_features'] ?? []), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$buttons = is_string($instance['intro_buttons'] ?? null)
? (string) $instance['intro_buttons']
: wp_json_encode($this->normalize_buttons($instance['intro_buttons'] ?? []), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
?>
<p>
<label><?php esc_html_e('角标文案', 'aksrt'); ?>
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('intro_badge_text')); ?>" value="<?php echo esc_attr((string) ($instance['intro_badge_text'] ?? '')); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('角标图标 class', 'aksrt'); ?>
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('intro_badge_icon')); ?>" value="<?php echo esc_attr((string) ($instance['intro_badge_icon'] ?? '')); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('标题', 'aksrt'); ?>
<textarea class="widefat" rows="2" name="<?php echo esc_attr($this->get_field_name('intro_title')); ?>"><?php echo esc_textarea((string) ($instance['intro_title'] ?? '')); ?></textarea>
</label>
</p>
<p>
<label><?php esc_html_e('标题高亮词', 'aksrt'); ?>
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('intro_title_highlight')); ?>" value="<?php echo esc_attr((string) ($instance['intro_title_highlight'] ?? '')); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('副标题', 'aksrt'); ?>
<textarea class="widefat" rows="2" name="<?php echo esc_attr($this->get_field_name('intro_subtitle')); ?>"><?php echo esc_textarea((string) ($instance['intro_subtitle'] ?? '')); ?></textarea>
</label>
</p>
<p>
<label><?php esc_html_e('特性 JSON', 'aksrt'); ?>
<textarea class="widefat" rows="3" name="<?php echo esc_attr($this->get_field_name('intro_features')); ?>"><?php echo esc_textarea((string) $features); ?></textarea>
</label>
</p>
<p>
<label><?php esc_html_e('按钮 JSON', 'aksrt'); ?>
<textarea class="widefat" rows="3" name="<?php echo esc_attr($this->get_field_name('intro_buttons')); ?>"><?php echo esc_textarea((string) $buttons); ?></textarea>
</label>
</p>
<p>
<label><?php esc_html_e('展示图 JSON/URL 列表', 'aksrt'); ?>
<textarea class="widefat" rows="4" name="<?php echo esc_attr($this->get_field_name('intro_showcase_images')); ?>"><?php echo esc_textarea((string) $images); ?></textarea>
</label>
</p>
<p>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('go_link')); ?>" value="1" <?php checked(!empty($instance['go_link'])); ?>> <?php esc_html_e('外链跳转页', 'aksrt'); ?></label>
</p>
<?php
}
public function update($new_instance, $old_instance): array {
return [
'intro_badge_text' => sanitize_text_field((string) ($new_instance['intro_badge_text'] ?? '')),
'intro_badge_icon' => sanitize_text_field((string) ($new_instance['intro_badge_icon'] ?? '')),
'intro_title' => sanitize_textarea_field((string) ($new_instance['intro_title'] ?? '')),
'intro_title_highlight' => sanitize_text_field((string) ($new_instance['intro_title_highlight'] ?? '')),
'intro_subtitle' => sanitize_textarea_field((string) ($new_instance['intro_subtitle'] ?? '')),
'intro_features' => $this->normalize_features($new_instance['intro_features'] ?? []),
'intro_buttons' => $this->normalize_buttons($new_instance['intro_buttons'] ?? []),
'intro_showcase_images' => $this->normalize_images($new_instance['intro_showcase_images'] ?? []),
'go_link' => !empty($new_instance['go_link']) ? 1 : 0,
];
}
}