feat(widgets): BookmarkLinks via aksrt_links_box bigcard/popover

Delegate render to shared links helper for card/bigcard/image/simple
styles, popover data attrs, empty admin guidance.
This commit is contained in:
2026-07-21 08:36:00 +08:00
parent 08e63c07f4
commit af8f97ad3c
+32 -35
View File
@@ -1,6 +1,6 @@
<?php
/**
* WordPress bookmark / links list (zibll widget_ui_links_lists_2).
* WordPress bookmark / links list (zibll widget_ui_links_lists via aksrt_links_box).
*
* @package AKSRT
*/
@@ -16,7 +16,7 @@ class WidgetBookmarkLinks extends \WP_Widget {
'aksrt_bookmark_links',
esc_html__('AKSRT 链接列表', 'aksrt'),
[
'description' => esc_html__('WordPress 链接(书签)列表,适合友链/导航。', 'aksrt'),
'description' => esc_html__('WordPress 链接(书签)列表:card / bigcard / image / simple。', 'aksrt'),
'classname' => 'widget_aksrt_bookmark_links',
]
);
@@ -30,12 +30,12 @@ class WidgetBookmarkLinks extends \WP_Widget {
}
$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)) {
if (!in_array($type, ['card', 'bigcard', 'image', 'simple'], true)) {
$type = 'card';
}
$go_link = !empty($instance['go_link']);
$blank = !isset($instance['blank']) || !empty($instance['blank']);
$nofollow = !empty($instance['nofollow']);
$nofollow = !isset($instance['nofollow']) || !empty($instance['nofollow']);
$show_box = !empty($instance['show_box']);
$bm_args = [
@@ -49,6 +49,13 @@ class WidgetBookmarkLinks extends \WP_Widget {
}
$bookmarks = get_bookmarks($bm_args);
if (!$bookmarks) {
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;
}
@@ -58,37 +65,22 @@ class WidgetBookmarkLinks extends \WP_Widget {
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' : '');
$wrap = 'aksrt-bookmark-links links-box 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);
if (function_exists('aksrt_links_box')) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo aksrt_links_box($bookmarks, $type, $nofollow, $go_link, $blank);
} else {
// Minimal fallback.
foreach ($bookmarks as $b) {
$url = (string) $b->link_url;
if ($url !== '' && $go_link && function_exists('aksrt_go_link')) {
$url = aksrt_go_link($url, true);
}
echo '<a class="aksrt-bookmark-links__item" href="' . esc_url($url) . '"' . ($blank ? ' target="_blank" rel="noopener noreferrer"' : '') . '>';
echo esc_html((string) $b->link_name);
echo '</a>';
}
$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>';
@@ -116,6 +108,7 @@ class WidgetBookmarkLinks extends \WP_Widget {
<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="bigcard" <?php selected(($instance['type'] ?? ''), 'bigcard'); ?>><?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>
@@ -137,7 +130,7 @@ class WidgetBookmarkLinks extends \WP_Widget {
</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('nofollow')); ?>" value="1" <?php checked(!isset($instance['nofollow']) || !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>
@@ -145,13 +138,17 @@ class WidgetBookmarkLinks extends \WP_Widget {
}
public function update($new_instance, $old_instance): array {
$type = sanitize_key((string) ($new_instance['type'] ?? 'card'));
if (!in_array($type, ['card', 'bigcard', 'image', 'simple'], true)) {
$type = 'card';
}
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',
'type' => $type,
'blank' => !empty($new_instance['blank']) ? 1 : 0,
'nofollow' => !empty($new_instance['nofollow']) ? 1 : 0,
'go_link' => !empty($new_instance['go_link']) ? 1 : 0,