feat(widgets): friend-links bookmarks source, hot thumbs, cover video
Deepen existing widgets toward zibll: WP bookmarks source for friend links, optional hot-post thumbnails, and video backgrounds on cover cards.
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
/**
|
||||
* Friend links / bookmarks widget (settings list + WP bookmarks).
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
@@ -9,68 +11,140 @@ defined('ABSPATH') || exit;
|
||||
|
||||
class FriendLinksWidget extends \WP_Widget {
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct('aksrt_friend_links', esc_html__('AKSRT 友情链接', 'aksrt'), [
|
||||
'description' => esc_html__('显示友情链接列表', 'aksrt'),
|
||||
]);
|
||||
}
|
||||
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'];
|
||||
$title = apply_filters('widget_title', $instance['title'] ?? __('友情链接', 'aksrt'), $instance, $this->id_base);
|
||||
$show_icon = !empty($instance['show_icon']);
|
||||
$columns = max(1, min(4, (int) ($instance['columns'] ?? 2)));
|
||||
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'];
|
||||
if ($title) {
|
||||
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
$links = class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::get_friend_links() : [];
|
||||
$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
|
||||
}
|
||||
|
||||
if (!empty($links)) : ?>
|
||||
<div class="widget-friend-links" style="--fl-columns:<?php echo (int) $columns; ?>;">
|
||||
<?php foreach ($links as $link) :
|
||||
$name = sanitize_text_field($link['name'] ?? '');
|
||||
$url = esc_url($link['url'] ?? '');
|
||||
$desc = sanitize_text_field($link['description'] ?? '');
|
||||
$icon = esc_url($link['icon'] ?? '');
|
||||
if ($name === '' || $url === '') continue;
|
||||
?>
|
||||
<a href="<?php echo $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 $icon; ?>" alt="" class="widget-friend-links__icon" width="16" height="16"<?php echo function_exists('aksrt_lazy_loading') ? aksrt_lazy_loading('widget') : ' loading="lazy"'; ?>>
|
||||
<?php endif; ?>
|
||||
<span class="widget-friend-links__name"><?php echo esc_html($name); ?></span>
|
||||
</a>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php else : ?>
|
||||
<?php echo function_exists('aksrt_get_widget_null') ? aksrt_get_widget_null(__('暂无友情链接', 'aksrt')) : '<p class="widget-empty">' . esc_html__('暂无友情链接', 'aksrt') . '</p>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
|
||||
<?php endif;
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
|
||||
echo $args['after_widget'];
|
||||
}
|
||||
$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 for="<?php echo $this->get_field_id('title'); ?>"><?php esc_html_e('标题:', 'aksrt'); ?></label>
|
||||
<input class="widefat" type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo esc_attr($instance['title'] ?? __('友情链接', 'aksrt')); ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo $this->get_field_id('columns'); ?>"><?php esc_html_e('列数:', 'aksrt'); ?></label>
|
||||
<input class="tiny-text" type="number" min="1" max="4" id="<?php echo $this->get_field_id('columns'); ?>" name="<?php echo $this->get_field_name('columns'); ?>" value="<?php echo (int) ($instance['columns'] ?? 2); ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label><input type="checkbox" name="<?php echo $this->get_field_name('show_icon'); ?>" value="1" <?php checked(!empty($instance['show_icon'])); ?>> <?php esc_html_e('显示站点图标', 'aksrt'); ?></label>
|
||||
</p>
|
||||
<p style="color:#666;font-size:12px;"><?php esc_html_e('链接数据在后台"友链管理"中配置。', 'aksrt'); ?></p>
|
||||
<?php
|
||||
}
|
||||
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($new_instance['title'] ?? ''),
|
||||
'columns' => max(1, min(4, (int) ($new_instance['columns'] ?? 2))),
|
||||
'show_icon'=> !empty($new_instance['show_icon']),
|
||||
];
|
||||
}
|
||||
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',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ class HotPostsWidget extends \WP_Widget {
|
||||
$count = absint($instance['count'] ?? 5);
|
||||
$order_by = $instance['order_by'] ?? 'views';
|
||||
$days = absint($instance['days'] ?? 30);
|
||||
$show_thumb = !empty($instance['show_thumb']);
|
||||
|
||||
if ($title) {
|
||||
echo $args['before_title'] . esc_html($title) . $args['after_title'];
|
||||
@@ -85,6 +86,13 @@ class HotPostsWidget extends \WP_Widget {
|
||||
?>
|
||||
<div class="hot-post-item <?php echo esc_attr($rank_class); ?>">
|
||||
<span class="hot-post-item__rank"><?php echo esc_html($index); ?></span>
|
||||
<?php if ($show_thumb) :
|
||||
$thumb = get_the_post_thumbnail_url(get_the_ID(), 'thumbnail');
|
||||
?>
|
||||
<a class="hot-post-item__thumb<?php echo $thumb ? '' : ' is-empty'; ?>" href="<?php the_permalink(); ?>">
|
||||
<?php if ($thumb) : ?><img src="<?php echo esc_url($thumb); ?>" alt="" loading="lazy" /><?php endif; ?>
|
||||
</a>
|
||||
<?php endif; ?>
|
||||
<div class="hot-post-item__info">
|
||||
<a href="<?php the_permalink(); ?>" class="hot-post-item__title"><?php the_title(); ?></a>
|
||||
<div class="hot-post-item__meta">
|
||||
@@ -132,7 +140,11 @@ class HotPostsWidget extends \WP_Widget {
|
||||
<label for="<?php echo $this->get_field_id('days'); ?>"><?php esc_html_e('时间范围(天,0=不限):', 'aksrt'); ?></label>
|
||||
<input class="tiny-text" id="<?php echo $this->get_field_id('days'); ?>" name="<?php echo $this->get_field_name('days'); ?>" type="number" min="0" max="365" value="<?php echo $days; ?>">
|
||||
</p>
|
||||
<?php
|
||||
|
||||
<p>
|
||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_thumb')); ?>" value="1" <?php checked(!empty($instance['show_thumb'])); ?>> <?php esc_html_e('显示缩略图', 'aksrt'); ?></label>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
|
||||
@@ -35,6 +35,7 @@ class WidgetCoverCards extends \WP_Widget {
|
||||
|
||||
foreach ($items as $item) {
|
||||
$image = esc_url($item['image'] ?? '');
|
||||
$video = esc_url($item['video'] ?? '');
|
||||
$link = esc_url($item['link'] ?? '');
|
||||
$card_title = sanitize_text_field($item['title'] ?? '');
|
||||
$desc = sanitize_text_field($item['desc'] ?? '');
|
||||
@@ -53,7 +54,11 @@ class WidgetCoverCards extends \WP_Widget {
|
||||
|
||||
echo '<div class="aksrt-cover-card"' . ($card_style ? ' style="' . esc_attr($card_style) . '"' : '') . '>';
|
||||
|
||||
if ($image) {
|
||||
if ($video) {
|
||||
echo '<div class="aksrt-cover-card__bg is-video">';
|
||||
echo '<video src="' . esc_url($video) . '" muted loop autoplay playsinline></video>';
|
||||
echo '</div>';
|
||||
} elseif ($image) {
|
||||
echo '<div class="aksrt-cover-card__bg">';
|
||||
echo '<img src="' . esc_url($image) . '" alt="' . esc_attr($card_title) . '"' . (function_exists('aksrt_lazy_loading') ? aksrt_lazy_loading('widget') : ' loading="lazy"') . '>';
|
||||
echo '</div>';
|
||||
|
||||
Reference in New Issue
Block a user