feat(widgets): deepen categories, tag cloud, vertical icon cards
Add hierarchy/include-exclude for categories; multi-taxonomy colored tag cloud; vertical icon-card layout with go_link and mobile columns.
This commit is contained in:
1 parent
dce98f631c
commit
05200ce781
3 files changed
+374
-153
No files matched your search
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
/**
|
||||
* Vertical icon cards grid (zibll zib_widget_ui_icon_card).
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
@@ -9,91 +11,134 @@ defined('ABSPATH') || exit;
|
||||
|
||||
class IconCardsWidget extends \WP_Widget {
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct('aksrt_icon_cards', esc_html__('AKSRT 图标卡片', 'aksrt'), [
|
||||
'description' => esc_html__('显示图标+文字卡片网格', 'aksrt'),
|
||||
]);
|
||||
}
|
||||
public function __construct() {
|
||||
parent::__construct('aksrt_icon_cards', esc_html__('AKSRT 图标卡片', 'aksrt'), [
|
||||
'description' => esc_html__('图标在上、文案在下的竖版图标卡片网格。', 'aksrt'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
echo $args['before_widget'];
|
||||
$title = apply_filters('widget_title', $instance['title'] ?? '', $instance, $this->id_base);
|
||||
$cards = isset($instance['cards']) && is_array($instance['cards']) ? $instance['cards'] : [];
|
||||
$columns = max(1, min(4, (int) ($instance['columns'] ?? 2)));
|
||||
public function widget($args, $instance): void {
|
||||
$cards = isset($instance['cards']) && is_array($instance['cards']) ? $instance['cards'] : [];
|
||||
$columns = max(1, min(4, (int) ($instance['columns'] ?? $instance['pc_row'] ?? 2)));
|
||||
$m_cols = max(1, min(3, (int) ($instance['m_row'] ?? 2)));
|
||||
$go_link = !empty($instance['go_link']);
|
||||
$show_bg = !isset($instance['show_widget_bg']) || !empty($instance['show_widget_bg']);
|
||||
$icon_size = max(18, min(48, (int) ($instance['icon_size'] ?? 28)));
|
||||
|
||||
if ($title) echo $args['before_title'] . esc_html($title) . $args['after_title'];
|
||||
$valid = [];
|
||||
foreach ($cards as $card) {
|
||||
if (!is_array($card)) {
|
||||
continue;
|
||||
}
|
||||
$title = sanitize_text_field((string) ($card['title'] ?? ''));
|
||||
if ($title === '') {
|
||||
continue;
|
||||
}
|
||||
$url = esc_url_raw(trim((string) ($card['link'] ?? $card['url'] ?? '')));
|
||||
if ($url !== '' && $go_link && function_exists('aksrt_go_link')) {
|
||||
$url = aksrt_go_link($url, true);
|
||||
}
|
||||
$valid[] = [
|
||||
'title' => $title,
|
||||
'icon' => sanitize_text_field((string) ($card['icon'] ?? '')),
|
||||
'desc' => sanitize_text_field((string) ($card['description'] ?? $card['desc'] ?? '')),
|
||||
'url' => $url,
|
||||
];
|
||||
}
|
||||
if (!$valid) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!empty($cards)) : ?>
|
||||
<div class="widget-icon-cards" style="--ic-columns:<?php echo (int) $columns; ?>;">
|
||||
<?php foreach ($cards as $card) :
|
||||
$card_title = sanitize_text_field($card['title'] ?? '');
|
||||
$card_icon = sanitize_text_field($card['icon'] ?? '');
|
||||
$card_link = esc_url($card['link'] ?? '');
|
||||
$card_desc = sanitize_text_field($card['description'] ?? '');
|
||||
if ($card_title === '') continue;
|
||||
$tag = $card_link !== '' ? 'a' : 'div';
|
||||
$extra = $card_link !== '' ? ' href="' . $card_link . '" target="_blank" rel="noopener"' : '';
|
||||
?>
|
||||
<<?php echo $tag; ?> class="widget-icon-cards__item"<?php echo $extra; ?>>
|
||||
<?php if ($card_icon) : ?>
|
||||
<div class="widget-icon-cards__icon"><i class="<?php echo esc_attr($card_icon); ?>" aria-hidden="true"></i></div>
|
||||
<?php endif; ?>
|
||||
<div class="widget-icon-cards__text">
|
||||
<div class="widget-icon-cards__title"><?php echo esc_html($card_title); ?></div>
|
||||
<?php if ($card_desc) : ?>
|
||||
<div class="widget-icon-cards__desc"><?php echo esc_html($card_desc); ?></div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</<?php echo $tag; ?>>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php endif;
|
||||
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 $args['after_widget'];
|
||||
}
|
||||
$style = sprintf('--ic-columns:%d;--ic-m-columns:%d;--ic-icon:%dpx;', $columns, $m_cols, $icon_size);
|
||||
echo '<div class="widget-icon-cards' . ($show_bg ? '' : ' is-bare') . '" style="' . esc_attr($style) . '">';
|
||||
foreach ($valid as $card) {
|
||||
$tag = $card['url'] !== '' ? 'a' : 'div';
|
||||
$extra = $card['url'] !== '' ? ' href="' . esc_url($card['url']) . '" target="_blank" rel="noopener noreferrer"' : '';
|
||||
echo '<' . $tag . ' class="widget-icon-cards__item is-vertical"' . $extra . '>';
|
||||
if ($card['icon'] !== '') {
|
||||
echo '<div class="widget-icon-cards__icon"><i class="' . esc_attr($card['icon']) . '" aria-hidden="true"></i></div>';
|
||||
}
|
||||
echo '<div class="widget-icon-cards__text">';
|
||||
echo '<div class="widget-icon-cards__title">' . esc_html($card['title']) . '</div>';
|
||||
if ($card['desc'] !== '') {
|
||||
echo '<div class="widget-icon-cards__desc">' . esc_html($card['desc']) . '</div>';
|
||||
}
|
||||
echo '</div></' . $tag . '>';
|
||||
}
|
||||
echo '</div>';
|
||||
|
||||
public function form($instance): void {
|
||||
$cards = isset($instance['cards']) && is_array($instance['cards']) ? $instance['cards'] : [];
|
||||
while (count($cards) < 4) {
|
||||
$cards[] = ['title' => '', 'icon' => '', 'link' => '', 'description' => ''];
|
||||
}
|
||||
?>
|
||||
<p>
|
||||
<label for="<?php echo $this->get_field_id('title'); ?>"><?php esc_html_e('标题(可选):', 'aksrt'); ?></label>
|
||||
<input class="widefat" type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo esc_attr($instance['title'] ?? ''); ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo $this->get_field_id('columns'); ?>"><?php esc_html_e('列数:', 'aksrt'); ?></label>
|
||||
<input class="tiny-text" type="number" min="1" max="4" id="<?php echo $this->get_field_id('columns'); ?>" name="<?php echo $this->get_field_name('columns'); ?>" value="<?php echo (int) ($instance['columns'] ?? 2); ?>">
|
||||
</p>
|
||||
<hr>
|
||||
<?php foreach ($cards as $i => $card) : ?>
|
||||
<div style="border:1px solid #ddd;padding:6px;margin-bottom:6px;border-radius:4px;">
|
||||
<p style="margin:0 0 4px;"><input class="widefat" type="text" name="<?php echo $this->get_field_name("cards[{$i}][title]"); ?>" value="<?php echo esc_attr($card['title']); ?>" placeholder="<?php esc_attr_e('标题', 'aksrt'); ?>"></p>
|
||||
<p style="margin:0 0 4px;"><input class="widefat" type="text" name="<?php echo $this->get_field_name("cards[{$i}][icon]"); ?>" value="<?php echo esc_attr($card['icon']); ?>" placeholder="<?php esc_attr_e('图标类名 (如 fas fa-star)', 'aksrt'); ?>"></p>
|
||||
<p style="margin:0 0 4px;"><input class="widefat" type="text" name="<?php echo $this->get_field_name("cards[{$i}][description]"); ?>" value="<?php echo esc_attr($card['description']); ?>" placeholder="<?php esc_attr_e('描述(可选)', 'aksrt'); ?>"></p>
|
||||
<p style="margin:0;"><input class="widefat" type="url" name="<?php echo $this->get_field_name("cards[{$i}][link]"); ?>" value="<?php echo esc_attr($card['link']); ?>" placeholder="<?php esc_attr_e('链接(可选)', 'aksrt'); ?>"></p>
|
||||
</div>
|
||||
<?php endforeach;
|
||||
}
|
||||
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
$cards = [];
|
||||
if (isset($new_instance['cards']) && is_array($new_instance['cards'])) {
|
||||
foreach ($new_instance['cards'] as $card) {
|
||||
if (!is_array($card)) continue;
|
||||
$cards[] = [
|
||||
'title' => sanitize_text_field($card['title'] ?? ''),
|
||||
'icon' => sanitize_text_field($card['icon'] ?? ''),
|
||||
'link' => esc_url_raw($card['link'] ?? ''),
|
||||
'description' => sanitize_text_field($card['description'] ?? ''),
|
||||
];
|
||||
}
|
||||
}
|
||||
return [
|
||||
'title' => sanitize_text_field($new_instance['title'] ?? ''),
|
||||
'columns' => max(1, min(4, (int) ($new_instance['columns'] ?? 2))),
|
||||
'cards' => $cards,
|
||||
];
|
||||
}
|
||||
public function form($instance): void {
|
||||
$cards = isset($instance['cards']) && is_array($instance['cards']) ? $instance['cards'] : [];
|
||||
while (count($cards) < 4) {
|
||||
$cards[] = ['title' => '', 'icon' => '', 'link' => '', 'description' => ''];
|
||||
}
|
||||
?>
|
||||
<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('PC 列数:', 'aksrt'); ?>
|
||||
<input class="tiny-text" type="number" min="1" max="4" name="<?php echo esc_attr($this->get_field_name('columns')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['columns'] ?? 2))); ?>">
|
||||
</label>
|
||||
<label><?php esc_html_e('移动列数', 'aksrt'); ?>
|
||||
<input class="tiny-text" type="number" min="1" max="3" name="<?php echo esc_attr($this->get_field_name('m_row')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['m_row'] ?? 2))); ?>">
|
||||
</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>
|
||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_widget_bg')); ?>" value="1" <?php checked(!isset($instance['show_widget_bg']) || !empty($instance['show_widget_bg'])); ?>> <?php esc_html_e('卡片背景', 'aksrt'); ?></label>
|
||||
</p>
|
||||
<hr>
|
||||
<?php foreach ($cards as $i => $card) : ?>
|
||||
<div style="border:1px solid #ddd;padding:6px;margin-bottom:6px;border-radius:4px;">
|
||||
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name("cards[{$i}][title]")); ?>" value="<?php echo esc_attr((string) ($card['title'] ?? '')); ?>" placeholder="<?php esc_attr_e('标题', 'aksrt'); ?>">
|
||||
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name("cards[{$i}][icon]")); ?>" value="<?php echo esc_attr((string) ($card['icon'] ?? '')); ?>" placeholder="<?php esc_attr_e('图标类名', 'aksrt'); ?>">
|
||||
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name("cards[{$i}][description]")); ?>" value="<?php echo esc_attr((string) ($card['description'] ?? '')); ?>" placeholder="<?php esc_attr_e('描述', 'aksrt'); ?>">
|
||||
<input class="widefat" type="url" name="<?php echo esc_attr($this->get_field_name("cards[{$i}][link]")); ?>" value="<?php echo esc_attr((string) ($card['link'] ?? '')); ?>" placeholder="<?php esc_attr_e('链接', 'aksrt'); ?>">
|
||||
</div>
|
||||
<?php endforeach;
|
||||
}
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
$cards = [];
|
||||
if (isset($new_instance['cards']) && is_array($new_instance['cards'])) {
|
||||
foreach ($new_instance['cards'] as $card) {
|
||||
if (!is_array($card)) {
|
||||
continue;
|
||||
}
|
||||
$title = sanitize_text_field((string) ($card['title'] ?? ''));
|
||||
if ($title === '') {
|
||||
continue;
|
||||
}
|
||||
$cards[] = [
|
||||
'title' => $title,
|
||||
'icon' => sanitize_text_field((string) ($card['icon'] ?? '')),
|
||||
'link' => esc_url_raw(trim((string) ($card['link'] ?? ''))),
|
||||
'description' => sanitize_text_field((string) ($card['description'] ?? '')),
|
||||
];
|
||||
if (count($cards) >= 12) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return [
|
||||
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
|
||||
'columns' => max(1, min(4, (int) ($new_instance['columns'] ?? 2))),
|
||||
'm_row' => max(1, min(3, (int) ($new_instance['m_row'] ?? 2))),
|
||||
'go_link' => !empty($new_instance['go_link']) ? 1 : 0,
|
||||
'show_widget_bg' => !empty($new_instance['show_widget_bg']) ? 1 : 0,
|
||||
'cards' => $cards,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user