feat(widgets): icon cover cards + WP bookmark links list

Close remaining zibll more-widgets gaps: horizontal icon-cover cards
and native WordPress bookmark/links list with go_link/nofollow.
This commit is contained in:
2026-07-19 01:09:07 +08:00
parent 7a91eb7321
commit ff919da342
4 changed files with 404 additions and 0 deletions
+161
View File
@@ -0,0 +1,161 @@
<?php
/**
* WordPress bookmark / links list (zibll widget_ui_links_lists_2).
*
* @package AKSRT
*/
namespace AKSRT\Widgets;
defined('ABSPATH') || exit;
class WidgetBookmarkLinks extends \WP_Widget {
public function __construct() {
parent::__construct(
'aksrt_bookmark_links',
esc_html__('AKSRT 链接列表', 'aksrt'),
[
'description' => esc_html__('WordPress 链接(书签)列表,适合友链/导航。', 'aksrt'),
'classname' => 'widget_aksrt_bookmark_links',
]
);
}
public function widget($args, $instance): void {
$limit = max(0, min(100, (int) ($instance['links_limit'] ?? 12)));
$orderby = sanitize_key((string) ($instance['links_orderby'] ?? 'name'));
if (!in_array($orderby, ['name', 'updated', 'rating', 'rand', 'id'], true)) {
$orderby = 'name';
}
$order = strtoupper((string) ($instance['links_order'] ?? 'ASC')) === 'DESC' ? 'DESC' : 'ASC';
$type = sanitize_key((string) ($instance['type'] ?? 'card'));
if (!in_array($type, ['card', 'image', 'simple'], true)) {
$type = 'card';
}
$go_link = !empty($instance['go_link']);
$blank = !isset($instance['blank']) || !empty($instance['blank']);
$nofollow = !empty($instance['nofollow']);
$show_box = !empty($instance['show_box']);
$bm_args = [
'orderby' => $orderby === 'rand' ? 'rand' : $orderby,
'order' => $order,
'limit' => $limit > 0 ? $limit : -1,
];
$cats = trim((string) ($instance['links_cats'] ?? ''));
if ($cats !== '' && $cats !== 'all') {
$bm_args['category'] = $cats;
}
$bookmarks = get_bookmarks($bm_args);
if (!$bookmarks) {
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
}
$wrap = 'aksrt-bookmark-links style-' . $type . ($show_box ? ' has-box' : '');
echo '<div class="' . esc_attr($wrap) . '">';
foreach ($bookmarks as $b) {
$url = (string) $b->link_url;
if ($url !== '' && $go_link && function_exists('aksrt_go_link')) {
$url = aksrt_go_link($url, true);
}
$rel = [];
if ($nofollow) {
$rel[] = 'nofollow';
}
if ($blank) {
$rel[] = 'noopener';
$rel[] = 'noreferrer';
}
$rel_attr = $rel ? ' rel="' . esc_attr(implode(' ', $rel)) . '"' : '';
$tgt = $blank ? ' target="_blank"' : '';
$img = (string) $b->link_image;
$name = (string) $b->link_name;
$desc = (string) $b->link_description;
echo '<a class="aksrt-bookmark-links__item" href="' . esc_url($url) . '"' . $tgt . $rel_attr . '>';
if ($type !== 'simple' && $img !== '') {
echo '<img class="aksrt-bookmark-links__img" src="' . esc_url($img) . '" alt="" loading="lazy" />';
}
echo '<span class="aksrt-bookmark-links__text">';
echo '<span class="aksrt-bookmark-links__name">' . esc_html($name) . '</span>';
if ($type === 'card' && $desc !== '') {
echo '<span class="aksrt-bookmark-links__desc">' . esc_html($desc) . '</span>';
}
echo '</span></a>';
}
echo '</div>';
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
public function form($instance): void {
?>
<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('链接分类 ID(逗号,空=全部)', 'aksrt'); ?>
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('links_cats')); ?>" value="<?php echo esc_attr((string) ($instance['links_cats'] ?? '')); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('数量(0=不限)', 'aksrt'); ?>
<input class="tiny-text" type="number" min="0" max="100" name="<?php echo esc_attr($this->get_field_name('links_limit')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['links_limit'] ?? 12))); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('样式', 'aksrt'); ?>
<select name="<?php echo esc_attr($this->get_field_name('type')); ?>">
<option value="card" <?php selected(($instance['type'] ?? 'card'), 'card'); ?>><?php esc_html_e('图文卡片', 'aksrt'); ?></option>
<option value="image" <?php selected(($instance['type'] ?? ''), 'image'); ?>><?php esc_html_e('图片', 'aksrt'); ?></option>
<option value="simple" <?php selected(($instance['type'] ?? ''), 'simple'); ?>><?php esc_html_e('纯文字', 'aksrt'); ?></option>
</select>
</label>
</p>
<p>
<label><?php esc_html_e('排序', 'aksrt'); ?>
<select name="<?php echo esc_attr($this->get_field_name('links_orderby')); ?>">
<option value="name" <?php selected(($instance['links_orderby'] ?? 'name'), 'name'); ?>><?php esc_html_e('名称', 'aksrt'); ?></option>
<option value="updated" <?php selected(($instance['links_orderby'] ?? ''), 'updated'); ?>><?php esc_html_e('更新', 'aksrt'); ?></option>
<option value="rating" <?php selected(($instance['links_orderby'] ?? ''), 'rating'); ?>><?php esc_html_e('评分', 'aksrt'); ?></option>
<option value="rand" <?php selected(($instance['links_orderby'] ?? ''), 'rand'); ?>><?php esc_html_e('随机', 'aksrt'); ?></option>
</select>
</label>
<select name="<?php echo esc_attr($this->get_field_name('links_order')); ?>">
<option value="ASC" <?php selected(($instance['links_order'] ?? 'ASC'), 'ASC'); ?>><?php esc_html_e('升序', 'aksrt'); ?></option>
<option value="DESC" <?php selected(($instance['links_order'] ?? ''), 'DESC'); ?>><?php esc_html_e('降序', 'aksrt'); ?></option>
</select>
</p>
<p>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('blank')); ?>" value="1" <?php checked(!isset($instance['blank']) || !empty($instance['blank'])); ?>> <?php esc_html_e('新窗口', 'aksrt'); ?></label><br>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('nofollow')); ?>" value="1" <?php checked(!empty($instance['nofollow'])); ?>> <?php esc_html_e('nofollow', '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_box')); ?>" value="1" <?php checked(!empty($instance['show_box'])); ?>> <?php esc_html_e('外框盒子', 'aksrt'); ?></label>
</p>
<?php
}
public function update($new_instance, $old_instance): array {
return [
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
'links_cats' => sanitize_text_field((string) ($new_instance['links_cats'] ?? '')),
'links_limit' => max(0, min(100, (int) ($new_instance['links_limit'] ?? 12))),
'links_orderby' => sanitize_key((string) ($new_instance['links_orderby'] ?? 'name')),
'links_order' => strtoupper((string) ($new_instance['links_order'] ?? 'ASC')) === 'DESC' ? 'DESC' : 'ASC',
'type' => in_array(($new_instance['type'] ?? ''), ['card', 'image', 'simple'], true) ? $new_instance['type'] : 'card',
'blank' => !empty($new_instance['blank']) ? 1 : 0,
'nofollow' => !empty($new_instance['nofollow']) ? 1 : 0,
'go_link' => !empty($new_instance['go_link']) ? 1 : 0,
'show_box' => !empty($new_instance['show_box']) ? 1 : 0,
];
}
}
+163
View File
@@ -0,0 +1,163 @@
<?php
/**
* Icon cover cards (zibll zib_widget_ui_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',
]
);
}
public function widget($args, $instance): void {
$cards = $this->normalize($instance['cards'] ?? []);
if (!$cards) {
return;
}
$cols = max(1, min(4, (int) ($instance['pc_row'] ?? 2)));
$m_cols = 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']);
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('--aksrt-icc-pc:%d;--aksrt-icc-m:%d;--aksrt-icc-icon:%dpx;', $cols, $m_cols, $icon_size);
echo '<div class="aksrt-icon-cover' . ($radius ? ' is-radius' : '') . '" style="' . esc_attr($style) . '">';
foreach ($cards as $card) {
$url = $card['url'];
if ($url !== '' && $go_link && function_exists('aksrt_go_link')) {
$url = aksrt_go_link($url, true);
}
$tag = $url !== '' ? 'a' : 'div';
$href = $url !== '' ? ' href="' . esc_url($url) . '"' . ($card['target'] !== '' ? ' target="' . esc_attr($card['target']) . '" rel="noopener noreferrer"' : '') : '';
echo '<' . $tag . ' class="aksrt-icon-cover__item"' . $href . '>';
echo '<div class="aksrt-icon-cover__icon">';
if ($card['icon_image'] !== '') {
echo '<img src="' . esc_url($card['icon_image']) . '" alt="" loading="lazy" />';
} elseif ($card['icon'] !== '') {
echo '<i class="' . esc_attr($card['icon']) . '" aria-hidden="true"></i>';
}
echo '</div>';
echo '<div class="aksrt-icon-cover__text">';
if ($card['title'] !== '') {
echo '<div class="aksrt-icon-cover__title">' . esc_html($card['title']) . '</div>';
}
if ($card['desc'] !== '') {
echo '<div class="aksrt-icon-cover__desc">' . esc_html($card['desc']) . '</div>';
}
echo '</div></' . $tag . '>';
}
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 = '';
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,
'url' => $url,
'target' => $target,
];
if (count($out) >= 16) {
break;
}
}
return $out;
}
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('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 [
'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,
'go_link' => !empty($new_instance['go_link']) ? 1 : 0,
];
}
}