Files
wordpress-theme-aksrt/src/Widgets/WidgetPartnersScroll.php
T
kate522 8c5b1555c9 refactor: flatten src/AKSRT → src (drop theme-identifier dir)
Move all domain folders from src/AKSRT/{Domain} to src/{Domain}; remap
Composer PSR-4 "AKSRT\\": "src/"; update audit script + README + ADR
paths; remove dead tools/build-psr4-map.php (scanned deleted inc/classes).
2026-08-02 22:06:06 +08:00

361 lines
16 KiB
PHP

<?php
/**
* Horizontal partners / friends scroll (zibll zib_widget_ui_partners_scroll).
*
* @package AKSRT
*/
namespace AKSRT\Widgets;
use AKSRT\Widgets\Traits\CommonWidgetFields;
defined('ABSPATH') || exit;
class WidgetPartnersScroll extends \WP_Widget {
use CommonWidgetFields;
public function __construct() {
parent::__construct(
'aksrt_partners_scroll',
esc_html__('AKSRT 合作伙伴滚动', 'aksrt'),
[
'description' => esc_html__('横向无缝滚动的合作伙伴/友情链接条。', 'aksrt'),
'classname' => 'widget_aksrt_partners_scroll',
]
);
}
public function widget($args, $instance): void {
$partners = $this->resolve_partners($instance);
if (!$partners) {
if (current_user_can('manage_options')) {
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo function_exists('aksrt_get_widget_null')
? aksrt_get_widget_null(__('暂无合作伙伴,请在模块中添加', 'aksrt')) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
: '<p class="widget-empty">' . esc_html__('暂无合作伙伴', 'aksrt') . '</p>';
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
return;
}
$rows = max(1, min(4, (int) ($instance['rows'] ?? 1)));
$m_rows = max(1, min(4, (int) ($instance['m_rows'] ?? 1)));
$style_type = sanitize_key((string) ($instance['style_type'] ?? 'card'));
if (!in_array($style_type, ['card', 'logo', 'chip'], true)) {
$style_type = 'card';
}
$card_w = max(60, min(400, (int) ($instance['card_width'] ?? 220)));
$card_h = max(40, min(200, (int) ($instance['card_height'] ?? 90)));
$speed = max(1, min(20, (int) ($instance['speed'] ?? 3)));
$direction = sanitize_key((string) ($instance['direction'] ?? 'alternate'));
$pause = !empty($instance['pause_hover']) || !isset($instance['pause_hover']);
$card_bg = !empty($instance['card_bg']) || !isset($instance['card_bg']);
$end_fade = !empty($instance['end_fade']) || !isset($instance['end_fade']);
$go_link = !empty($instance['go_link']);
$show_bg = !empty($instance['show_widget_bg']) || !isset($instance['show_widget_bg']);
$radius = max(0, min(40, (int) ($instance['card_radius'] ?? 10)));
$use_rows = wp_is_mobile() ? $m_rows : $rows;
$rows_data = array_fill(0, $use_rows, []);
foreach (array_values($partners) as $i => $p) {
$rows_data[$i % $use_rows][] = $p;
}
$rows_data = array_values(array_filter($rows_data, static fn($r) => !empty($r)));
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
}
$wrap_class = 'aksrt-partners-scroll aksrt-ps-style-' . $style_type;
if ($pause) {
$wrap_class .= ' aksrt-ps-pause-hover';
}
if ($card_bg) {
$wrap_class .= ' aksrt-ps-card-bg';
}
if ($end_fade) {
$wrap_class .= ' aksrt-ps-end-fade';
}
$style = sprintf(
'--aksrt-ps-w:%dpx;--aksrt-ps-h:%dpx;--aksrt-ps-radius:%dpx;--aksrt-ps-speed:%d;',
$card_w,
$card_h,
$radius,
$speed
);
$outer = $show_bg ? 'aksrt-partners-scroll-wrap' : 'aksrt-partners-scroll-wrap is-bare';
echo '<div class="' . esc_attr($outer) . '">';
echo '<div class="' . esc_attr($wrap_class) . '" style="' . esc_attr($style) . '">';
foreach ($rows_data as $row_index => $row_partners) {
switch ($direction) {
case 'left':
$dir = 'left';
break;
case 'right':
$dir = 'right';
break;
default:
$dir = ($row_index % 2 === 0) ? 'left' : 'right';
}
echo '<div class="aksrt-ps-row aksrt-ps-dir-' . esc_attr($dir) . '">';
echo '<div class="aksrt-ps-track">';
// Duplicate group for seamless CSS marquee.
for ($dup = 0; $dup < 2; $dup++) {
echo '<div class="aksrt-ps-group">';
foreach ($row_partners as $partner) {
echo $this->render_card($partner, $style_type, $go_link); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
echo '</div>';
}
echo '</div></div>';
}
echo '</div></div>';
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* @param array<string,mixed> $partner
*/
private function render_card(array $partner, string $style_type, bool $go_link): string {
$title = trim((string) ($partner['title'] ?? ''));
$desc = trim((string) ($partner['desc'] ?? ''));
$image = (string) ($partner['image'] ?? '');
$url = (string) ($partner['url'] ?? '');
$target = (string) ($partner['target'] ?? '');
if ($image === '' && $title === '') {
return '';
}
$alt = esc_attr($title !== '' ? $title : __('合作伙伴', 'aksrt'));
$img_tag = $image !== ''
? '<img class="aksrt-ps-img aksrt-lazy lazyload" data-src="' . esc_url($image) . '" src="' . esc_url(get_template_directory_uri() . '/assets/img/placeholder.svg') . '" alt="' . $alt . '" loading="lazy" />'
: '';
$inner = '';
if ($style_type === 'logo') {
if ($img_tag === '') {
return '';
}
$inner = $img_tag;
} elseif ($style_type === 'chip') {
if ($img_tag !== '') {
$inner .= '<span class="aksrt-ps-chip-icon">' . $img_tag . '</span>';
}
$inner .= '<span class="aksrt-ps-chip-title">' . esc_html($title) . '</span>';
} else {
if ($img_tag !== '') {
$inner .= '<div class="aksrt-ps-card-icon">' . $img_tag . '</div>';
}
$inner .= '<div class="aksrt-ps-card-text">';
if ($title !== '') {
$inner .= '<div class="aksrt-ps-card-title">' . esc_html($title) . '</div>';
}
if ($desc !== '') {
$inner .= '<div class="aksrt-ps-card-desc">' . esc_html($desc) . '</div>';
}
$inner .= '</div>';
}
if ($url !== '') {
if ($go_link && function_exists('aksrt_go_link')) {
$url = aksrt_go_link($url, true);
}
$tgt = $target !== '' ? ' target="' . esc_attr($target) . '" rel="noopener noreferrer"' : '';
return '<a class="aksrt-ps-item" href="' . esc_url($url) . '"' . $tgt . '>' . $inner . '</a>';
}
return '<div class="aksrt-ps-item">' . $inner . '</div>';
}
/**
* @param array<string,mixed> $instance
* @return array<int, array{title:string,desc:string,image:string,url:string,target:string}>
*/
private function resolve_partners(array $instance): array {
$source = sanitize_key((string) ($instance['link_type'] ?? 'manual'));
if ($source === 'link') {
$args = [
'orderby' => sanitize_key((string) ($instance['link_orderby'] ?? 'name')) ?: 'name',
'order' => strtoupper((string) ($instance['link_order'] ?? 'ASC')) === 'DESC' ? 'DESC' : 'ASC',
'limit' => max(1, min(100, (int) ($instance['link_limit'] ?? 50))),
];
$cats = $instance['link_cats'] ?? '';
if (is_array($cats)) {
$cats = implode(',', array_map('intval', $cats));
}
$cats = trim((string) $cats);
if ($cats !== '') {
$args['category'] = $cats;
}
$bookmarks = get_bookmarks($args);
$out = [];
foreach ($bookmarks as $b) {
$out[] = [
'title' => (string) $b->link_name,
'desc' => (string) $b->link_description,
'image' => (string) $b->link_image,
'url' => (string) $b->link_url,
'target' => (string) $b->link_target,
];
}
return $out;
}
return $this->normalize_manual($instance['partners'] ?? []);
}
/**
* @param mixed $raw
* @return array<int, array{title:string,desc:string,image:string,url:string,target:string}>
*/
private function normalize_manual($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;
}
$title = sanitize_text_field((string) ($row['title'] ?? ''));
$image = esc_url_raw(trim((string) ($row['image'] ?? '')));
$url = esc_url_raw(trim((string) ($row['url'] ?? $row['link'] ?? '')));
if ($title === '' && $image === '') {
continue;
}
$out[] = [
'title' => $title,
'desc' => sanitize_text_field((string) ($row['desc'] ?? '')),
'image' => $image,
'url' => $url,
'target' => sanitize_text_field((string) ($row['target'] ?? '_blank')),
];
if (count($out) >= 40) {
break;
}
}
return $out;
}
public function form($instance): void {
$title = (string) ($instance['title'] ?? '');
$link_type = (string) ($instance['link_type'] ?? 'manual');
$partners_json = is_string($instance['partners'] ?? null)
? (string) $instance['partners']
: wp_json_encode($this->normalize_manual($instance['partners'] ?? []), 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('title')); ?>" value="<?php echo esc_attr($title); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('数据来源', 'aksrt'); ?>
<select class="widefat" name="<?php echo esc_attr($this->get_field_name('link_type')); ?>">
<option value="manual" <?php selected($link_type, 'manual'); ?>><?php esc_html_e('手动 JSON', 'aksrt'); ?></option>
<option value="link" <?php selected($link_type, 'link'); ?>><?php esc_html_e('WordPress 链接(书签)', 'aksrt'); ?></option>
</select>
</label>
</p>
<p>
<label><?php esc_html_e('合作伙伴 JSON', 'aksrt'); ?>
<textarea class="widefat" rows="6" name="<?php echo esc_attr($this->get_field_name('partners')); ?>" placeholder='[{"title":"Name","image":"https://...","url":"https://...","desc":""}]'><?php echo esc_textarea((string) $partners_json); ?></textarea>
</label>
<small><?php esc_html_e('字段: title, image, url, desc, target', 'aksrt'); ?></small>
</p>
<p>
<label><?php esc_html_e('链接分类 ID(逗号分隔,书签模式)', 'aksrt'); ?>
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('link_cats')); ?>" value="<?php echo esc_attr((string) ($instance['link_cats'] ?? '')); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('书签数量上限', 'aksrt'); ?>
<input class="tiny-text" type="number" min="1" max="100" name="<?php echo esc_attr($this->get_field_name('link_limit')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['link_limit'] ?? 50))); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('样式', 'aksrt'); ?>
<select name="<?php echo esc_attr($this->get_field_name('style_type')); ?>">
<option value="card" <?php selected(($instance['style_type'] ?? 'card'), 'card'); ?>><?php esc_html_e('图文卡片', 'aksrt'); ?></option>
<option value="logo" <?php selected(($instance['style_type'] ?? ''), 'logo'); ?>><?php esc_html_e('纯 Logo', 'aksrt'); ?></option>
<option value="chip" <?php selected(($instance['style_type'] ?? ''), 'chip'); ?>><?php esc_html_e('胶囊', 'aksrt'); ?></option>
</select>
</label>
</p>
<p>
<label><?php esc_html_e('PC 行数', 'aksrt'); ?>
<input class="tiny-text" type="number" min="1" max="4" name="<?php echo esc_attr($this->get_field_name('rows')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['rows'] ?? 1))); ?>">
</label>
<label><?php esc_html_e('移动行数', 'aksrt'); ?>
<input class="tiny-text" type="number" min="1" max="4" name="<?php echo esc_attr($this->get_field_name('m_rows')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['m_rows'] ?? 1))); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('卡片宽', 'aksrt'); ?>
<input class="tiny-text" type="number" min="60" max="400" name="<?php echo esc_attr($this->get_field_name('card_width')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['card_width'] ?? 220))); ?>">
</label>
<label><?php esc_html_e('高', 'aksrt'); ?>
<input class="tiny-text" type="number" min="40" max="200" name="<?php echo esc_attr($this->get_field_name('card_height')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['card_height'] ?? 90))); ?>">
</label>
<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('speed')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['speed'] ?? 3))); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('方向', 'aksrt'); ?>
<select name="<?php echo esc_attr($this->get_field_name('direction')); ?>">
<option value="alternate" <?php selected(($instance['direction'] ?? 'alternate'), 'alternate'); ?>><?php esc_html_e('交替', 'aksrt'); ?></option>
<option value="left" <?php selected(($instance['direction'] ?? ''), 'left'); ?>><?php esc_html_e('向左', 'aksrt'); ?></option>
<option value="right" <?php selected(($instance['direction'] ?? ''), 'right'); ?>><?php esc_html_e('向右', 'aksrt'); ?></option>
</select>
</label>
</p>
<p>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('pause_hover')); ?>" value="1" <?php checked(!empty($instance['pause_hover']) || !isset($instance['pause_hover'])); ?>> <?php esc_html_e('悬停暂停', 'aksrt'); ?></label><br>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('card_bg')); ?>" value="1" <?php checked(!empty($instance['card_bg']) || !isset($instance['card_bg'])); ?>> <?php esc_html_e('卡片背景', 'aksrt'); ?></label><br>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('end_fade')); ?>" value="1" <?php checked(!empty($instance['end_fade']) || !isset($instance['end_fade'])); ?>> <?php esc_html_e('两端渐隐', 'aksrt'); ?></label><br>
<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><br>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_widget_bg')); ?>" value="1" <?php checked(!empty($instance['show_widget_bg']) || !isset($instance['show_widget_bg'])); ?>> <?php esc_html_e('显示背景盒子', 'aksrt'); ?></label>
</p>
<?php
$this->render_common_form_section($instance);
}
public function update($new_instance, $old_instance): array {
$business = [
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
'link_type' => in_array(($new_instance['link_type'] ?? ''), ['manual', 'link'], true) ? $new_instance['link_type'] : 'manual',
'partners' => $this->normalize_manual($new_instance['partners'] ?? []),
'link_cats' => sanitize_text_field((string) ($new_instance['link_cats'] ?? '')),
'link_limit' => max(1, min(100, (int) ($new_instance['link_limit'] ?? 50))),
'link_orderby' => sanitize_key((string) ($new_instance['link_orderby'] ?? 'name')),
'link_order' => strtoupper((string) ($new_instance['link_order'] ?? 'ASC')) === 'DESC' ? 'DESC' : 'ASC',
'style_type' => in_array(($new_instance['style_type'] ?? ''), ['card', 'logo', 'chip'], true) ? $new_instance['style_type'] : 'card',
'rows' => max(1, min(4, (int) ($new_instance['rows'] ?? 1))),
'm_rows' => max(1, min(4, (int) ($new_instance['m_rows'] ?? 1))),
'card_width' => max(60, min(400, (int) ($new_instance['card_width'] ?? 220))),
'card_height' => max(40, min(200, (int) ($new_instance['card_height'] ?? 90))),
'card_radius' => max(0, min(40, (int) ($new_instance['card_radius'] ?? 10))),
'speed' => max(1, min(20, (int) ($new_instance['speed'] ?? 3))),
'direction' => in_array(($new_instance['direction'] ?? ''), ['alternate', 'left', 'right'], true) ? $new_instance['direction'] : 'alternate',
'pause_hover' => !empty($new_instance['pause_hover']) ? 1 : 0,
'card_bg' => !empty($new_instance['card_bg']) ? 1 : 0,
'end_fade' => !empty($new_instance['end_fade']) ? 1 : 0,
'go_link' => !empty($new_instance['go_link']) ? 1 : 0,
'show_widget_bg' => !empty($new_instance['show_widget_bg']) ? 1 : 0,
]; return $this->update_common_fields($new_instance, $old_instance, $business);
}
}