Add hierarchy/include-exclude for categories; multi-taxonomy colored tag cloud; vertical icon-card layout with go_link and mobile columns.
145 lines
6.7 KiB
PHP
145 lines
6.7 KiB
PHP
<?php
|
|
/**
|
|
* Vertical icon cards grid (zibll zib_widget_ui_icon_card).
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
namespace AKSRT\Widgets;
|
|
|
|
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 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)));
|
|
|
|
$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;
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
$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>';
|
|
|
|
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
}
|
|
|
|
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,
|
|
];
|
|
}
|
|
}
|