Rebuild vertical/horizontal icon card widgets on aksrt_icon_card and aksrt_icon_cover_card with row gutters and optional scroll animation.
199 lines
7.4 KiB
PHP
199 lines
7.4 KiB
PHP
<?php
|
|
/**
|
|
* Icon cover cards (zibll zib_widget_ui_icon_cover_card → aksrt_icon_cover_card).
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
namespace AKSRT\Widgets;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class WidgetIconCoverCards extends \WP_Widget {
|
|
|
|
public function __construct() {
|
|
parent::__construct(
|
|
'aksrt_icon_cover_cards',
|
|
esc_html__('AKSRT 图标封面卡', 'aksrt'),
|
|
[
|
|
'description' => esc_html__('横向图标 + 标题/描述的导航卡片。', 'aksrt'),
|
|
'classname' => 'widget_aksrt_icon_cover_cards',
|
|
]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param array<string,mixed> $args
|
|
* @param array<string,mixed> $instance
|
|
*/
|
|
public function widget($args, $instance): void {
|
|
$cards = $this->normalize($instance['cards'] ?? []);
|
|
if (!$cards) {
|
|
return;
|
|
}
|
|
$pc_row = max(1, min(4, (int) ($instance['pc_row'] ?? 2)));
|
|
$m_row = max(1, min(2, (int) ($instance['m_row'] ?? 1)));
|
|
$icon_size = max(16, min(48, (int) ($instance['icon_size'] ?? 25)));
|
|
$radius = !empty($instance['icon_radius4']);
|
|
$go_link = !empty($instance['go_link']);
|
|
$show_bg = !empty($instance['show_widget_bg']);
|
|
|
|
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
|
|
}
|
|
|
|
$is_row = count($cards) > 1;
|
|
$pc_span = max(1, (int) floor(12 / $pc_row));
|
|
$m_span = max(1, (int) floor(12 / $m_row));
|
|
$row_class = 'col-sm-' . $pc_span . ($m_row > 1 ? ' col-xs-' . $m_span : '');
|
|
|
|
echo $show_bg ? '<div class="zib-widget padding-10">' : '<div class="mb10">';
|
|
if ($is_row) {
|
|
echo '<div class="row gutters-5">';
|
|
}
|
|
|
|
foreach ($cards as $card) {
|
|
$url = (string) ($card['url'] ?? '');
|
|
if ($url !== '' && $go_link && function_exists('aksrt_go_link')) {
|
|
$url = aksrt_go_link($url, true);
|
|
}
|
|
$card_args = [
|
|
'class' => $show_bg ? 'padding-10' : 'zib-widget mb10',
|
|
'icon' => (string) ($card['icon'] ?? ''),
|
|
'icon_image' => (string) ($card['icon_image'] ?? ''),
|
|
'icon_size' => $icon_size,
|
|
'icon_class' => $radius ? 'radius4' : '',
|
|
'icon_color' => (string) ($card['icon_color'] ?? ''),
|
|
'title' => (string) ($card['title'] ?? ''),
|
|
'desc' => (string) ($card['desc'] ?? ''),
|
|
'link' => [
|
|
'url' => $url,
|
|
'target' => (string) ($card['target'] ?? ''),
|
|
],
|
|
];
|
|
|
|
if ($is_row) {
|
|
echo '<div class="' . esc_attr($row_class) . '">';
|
|
}
|
|
if (function_exists('aksrt_icon_cover_card')) {
|
|
echo aksrt_icon_cover_card($card_args); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
}
|
|
if ($is_row) {
|
|
echo '</div>';
|
|
}
|
|
}
|
|
|
|
if ($is_row) {
|
|
echo '</div>';
|
|
}
|
|
echo '</div>';
|
|
|
|
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
}
|
|
|
|
/**
|
|
* @param mixed $raw
|
|
* @return array<int, array<string,string>>
|
|
*/
|
|
private function normalize($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'] ?? ''));
|
|
$icon = sanitize_text_field((string) ($row['icon'] ?? ''));
|
|
$icon_image = esc_url_raw(trim((string) ($row['icon_image'] ?? $row['customize_icon'] ?? '')));
|
|
if ($title === '' && $icon === '' && $icon_image === '') {
|
|
continue;
|
|
}
|
|
$url = '';
|
|
$target = '';
|
|
if (is_array($row['link'] ?? null)) {
|
|
$url = esc_url_raw(trim((string) ($row['link']['url'] ?? '')));
|
|
$target = sanitize_text_field((string) ($row['link']['target'] ?? ''));
|
|
} else {
|
|
$url = esc_url_raw(trim((string) ($row['url'] ?? $row['link'] ?? '')));
|
|
$target = sanitize_text_field((string) ($row['target'] ?? '_blank'));
|
|
}
|
|
$out[] = [
|
|
'title' => $title,
|
|
'desc' => sanitize_text_field((string) ($row['desc'] ?? '')),
|
|
'icon' => $icon,
|
|
'icon_image' => $icon_image,
|
|
'icon_color' => sanitize_html_class((string) ($row['icon_color'] ?? '')),
|
|
'url' => $url,
|
|
'target' => $target,
|
|
];
|
|
if (count($out) >= 16) {
|
|
break;
|
|
}
|
|
}
|
|
return $out;
|
|
}
|
|
|
|
/**
|
|
* @param array<string,mixed> $instance
|
|
*/
|
|
public function form($instance): void {
|
|
$json = is_string($instance['cards'] ?? null)
|
|
? (string) $instance['cards']
|
|
: wp_json_encode($this->normalize($instance['cards'] ?? []), 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((string) ($instance['title'] ?? '')); ?>">
|
|
</label>
|
|
</p>
|
|
<p>
|
|
<label><?php esc_html_e('卡片 JSON', 'aksrt'); ?>
|
|
<textarea class="widefat" rows="7" name="<?php echo esc_attr($this->get_field_name('cards')); ?>" placeholder='[{"title":"文档","desc":"使用说明","icon":"fas fa-book","url":"https://"}]'><?php echo esc_textarea((string) $json); ?></textarea>
|
|
</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('pc_row')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['pc_row'] ?? 2))); ?>">
|
|
</label>
|
|
<label><?php esc_html_e('移动列', 'aksrt'); ?>
|
|
<input class="tiny-text" type="number" min="1" max="2" name="<?php echo esc_attr($this->get_field_name('m_row')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['m_row'] ?? 1))); ?>">
|
|
</label>
|
|
<label><?php esc_html_e('图标字号', 'aksrt'); ?>
|
|
<input class="tiny-text" type="number" min="16" max="48" name="<?php echo esc_attr($this->get_field_name('icon_size')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['icon_size'] ?? 25))); ?>">
|
|
</label>
|
|
</p>
|
|
<p>
|
|
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('icon_radius4')); ?>" value="1" <?php checked(!empty($instance['icon_radius4'])); ?>> <?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'])); ?>> <?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>
|
|
</p>
|
|
<?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 {
|
|
return [
|
|
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
|
|
'cards' => $this->normalize($new_instance['cards'] ?? []),
|
|
'pc_row' => max(1, min(4, (int) ($new_instance['pc_row'] ?? 2))),
|
|
'm_row' => max(1, min(2, (int) ($new_instance['m_row'] ?? 1))),
|
|
'icon_size' => max(16, min(48, (int) ($new_instance['icon_size'] ?? 25))),
|
|
'icon_radius4' => !empty($new_instance['icon_radius4']) ? 1 : 0,
|
|
'show_widget_bg' => !empty($new_instance['show_widget_bg']) ? 1 : 0,
|
|
'go_link' => !empty($new_instance['go_link']) ? 1 : 0,
|
|
];
|
|
}
|
|
}
|