Files
wordpress-theme-aksrt/src/AKSRT/Widgets/FriendLinksWidget.php
T
kate522 aaf458af1c refactor(autoload): migrate theme classes to Composer PSR-4
Move 288 types from global AKSRT_* + inc/classes manual requires into
namespace AKSRT\{Domain} under src/AKSRT/, wire composer.json psr-4,
and slim bootstrap to helpers/modules/hooks only. Split multi-class
widget/gateway files, update admin contract audit paths, and document
the layout in ADR/README.
2026-07-17 13:29:22 +08:00

77 lines
3.9 KiB
PHP

<?php
/**
* @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__('显示友情链接列表', '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)));
if ($title) echo $args['before_title'] . esc_html($title) . $args['after_title'];
$links = class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::get_friend_links() : [];
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;
echo $args['after_widget'];
}
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 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']),
];
}
}