Files
wordpress-theme-aksrt/src/AKSRT/Widgets/WidgetBookmarkLinks.php
T
kate522 ff919da342 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.
2026-07-19 01:09:07 +08:00

162 lines
7.5 KiB
PHP

<?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,
];
}
}