Files
wordpress-theme-aksrt/src/AKSRT/Widgets/WidgetTiltCases.php
T
kate522 2f81e2a0b5 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.
2026-07-19 00:35:00 +08:00

259 lines
9.2 KiB
PHP

<?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,
];
}
}