Deepen existing widgets toward zibll: WP bookmarks source for friend links, optional hot-post thumbnails, and video backgrounds on cover cards.
151 lines
6.7 KiB
PHP
151 lines
6.7 KiB
PHP
<?php
|
|
/**
|
|
* Friend links / bookmarks widget (settings list + WP bookmarks).
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
namespace AKSRT\Widgets;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class FriendLinksWidget extends \WP_Widget {
|
|
|
|
public function __construct() {
|
|
parent::__construct('aksrt_friend_links', esc_html__('AKSRT 友情链接', 'aksrt'), [
|
|
'description' => esc_html__('显示友情链接(设置项或 WordPress 书签)。', 'aksrt'),
|
|
]);
|
|
}
|
|
|
|
public function widget($args, $instance): void {
|
|
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
$title = apply_filters('widget_title', (string) ($instance['title'] ?? __('友情链接', 'aksrt')), $instance, $this->id_base);
|
|
$show_icon = !empty($instance['show_icon']) || !isset($instance['show_icon']);
|
|
$columns = max(1, min(4, (int) ($instance['columns'] ?? 2)));
|
|
$source = sanitize_key((string) ($instance['source'] ?? 'settings'));
|
|
$go_link = !empty($instance['go_link']);
|
|
$limit = max(1, min(100, (int) ($instance['limit'] ?? 24)));
|
|
|
|
if ($title) {
|
|
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
}
|
|
|
|
$links = $this->resolve_links($source, $instance, $limit);
|
|
if (!$links) {
|
|
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;
|
|
}
|
|
?>
|
|
<div class="widget-friend-links" style="--fl-columns:<?php echo (int) $columns; ?>;">
|
|
<?php foreach ($links as $link) :
|
|
$name = sanitize_text_field((string) ($link['name'] ?? ''));
|
|
$url = (string) ($link['url'] ?? '');
|
|
$desc = sanitize_text_field((string) ($link['description'] ?? $link['desc'] ?? ''));
|
|
$icon = (string) ($link['icon'] ?? $link['image'] ?? '');
|
|
if ($name === '' || $url === '') {
|
|
continue;
|
|
}
|
|
if ($go_link && function_exists('aksrt_go_link')) {
|
|
$url = aksrt_go_link($url, true);
|
|
}
|
|
?>
|
|
<a href="<?php echo esc_url($url); ?>" class="widget-friend-links__item" target="_blank" rel="noopener noreferrer" title="<?php echo esc_attr($desc); ?>">
|
|
<?php if ($show_icon && $icon !== '') : ?>
|
|
<img src="<?php echo esc_url($icon); ?>" alt="" class="widget-friend-links__icon" width="16" height="16" loading="lazy">
|
|
<?php endif; ?>
|
|
<span class="widget-friend-links__name"><?php echo esc_html($name); ?></span>
|
|
</a>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php
|
|
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
}
|
|
|
|
/**
|
|
* @param array<string,mixed> $instance
|
|
* @return array<int, array<string,string>>
|
|
*/
|
|
private function resolve_links(string $source, array $instance, int $limit): array {
|
|
if ($source === 'bookmarks') {
|
|
$bm_args = [
|
|
'orderby' => sanitize_key((string) ($instance['orderby'] ?? 'name')) ?: 'name',
|
|
'order' => strtoupper((string) ($instance['order'] ?? 'ASC')) === 'DESC' ? 'DESC' : 'ASC',
|
|
'limit' => $limit,
|
|
];
|
|
$cats = trim((string) ($instance['cats'] ?? ''));
|
|
if ($cats !== '') {
|
|
$bm_args['category'] = $cats;
|
|
}
|
|
$out = [];
|
|
foreach (get_bookmarks($bm_args) as $b) {
|
|
$out[] = [
|
|
'name' => (string) $b->link_name,
|
|
'url' => (string) $b->link_url,
|
|
'description' => (string) $b->link_description,
|
|
'icon' => (string) $b->link_image,
|
|
];
|
|
}
|
|
return $out;
|
|
}
|
|
|
|
$links = class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::get_friend_links() : [];
|
|
if (!is_array($links) || !$links) {
|
|
// Fallback to WP bookmarks when settings empty.
|
|
return $this->resolve_links('bookmarks', $instance, $limit);
|
|
}
|
|
return array_slice(array_values($links), 0, $limit);
|
|
}
|
|
|
|
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'] ?? __('友情链接', 'aksrt'))); ?>">
|
|
</label>
|
|
</p>
|
|
<p>
|
|
<label><?php esc_html_e('数据源', 'aksrt'); ?>
|
|
<select class="widefat" name="<?php echo esc_attr($this->get_field_name('source')); ?>">
|
|
<option value="settings" <?php selected(($instance['source'] ?? 'settings'), 'settings'); ?>><?php esc_html_e('后台友链设置', 'aksrt'); ?></option>
|
|
<option value="bookmarks" <?php selected(($instance['source'] ?? ''), 'bookmarks'); ?>><?php esc_html_e('WordPress 链接', 'aksrt'); ?></option>
|
|
</select>
|
|
</label>
|
|
</p>
|
|
<p>
|
|
<label><?php esc_html_e('列数:', '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="100" name="<?php echo esc_attr($this->get_field_name('limit')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['limit'] ?? 24))); ?>">
|
|
</label>
|
|
</p>
|
|
<p>
|
|
<label><?php esc_html_e('书签分类 ID(书签模式)', 'aksrt'); ?>
|
|
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('cats')); ?>" value="<?php echo esc_attr((string) ($instance['cats'] ?? '')); ?>">
|
|
</label>
|
|
</p>
|
|
<p>
|
|
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_icon')); ?>" value="1" <?php checked(!empty($instance['show_icon']) || !isset($instance['show_icon'])); ?>> <?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'] ?? '')),
|
|
'source' => ($new_instance['source'] ?? '') === 'bookmarks' ? 'bookmarks' : 'settings',
|
|
'columns' => max(1, min(4, (int) ($new_instance['columns'] ?? 2))),
|
|
'limit' => max(1, min(100, (int) ($new_instance['limit'] ?? 24))),
|
|
'cats' => sanitize_text_field((string) ($new_instance['cats'] ?? '')),
|
|
'show_icon' => !empty($new_instance['show_icon']) ? 1 : 0,
|
|
'go_link' => !empty($new_instance['go_link']) ? 1 : 0,
|
|
'orderby' => sanitize_key((string) ($new_instance['orderby'] ?? 'name')),
|
|
'order' => strtoupper((string) ($new_instance['order'] ?? 'ASC')) === 'DESC' ? 'DESC' : 'ASC',
|
|
];
|
|
}
|
|
}
|