更新到1.2.2版本

This commit is contained in:
LinRuiqi
2025-11-22 09:37:46 +08:00
parent e3a9931a16
commit 8c5a3e0ce1
7 changed files with 1007 additions and 157 deletions

44
CHANGELOG.md Normal file
View File

@@ -0,0 +1,44 @@
# 🎉 友情链接管理器 v1.2.2 发布
## ✨ 新增功能
- **链接描述支持**:为每个链接添加描述信息,让友情链接更加丰富
- **灵活显示设置**
- 自定义电脑端每行显示链接数2-6个可选
- 随机/顺序显示模式切换
- 描述信息显示开关
- **批量操作**:支持批量选中删除链接,提升管理效率
- **拖拽排序**:直观的拖拽排序功能,自动保存排序结果
## 🔧 功能优化
- **保存功能修复**:彻底解决链接信息无法保存的问题
- **CSV导出增强**:支持包含描述信息的完整数据导出
- **导入功能改进**:支持四列数据导入,向后兼容旧格式
- **表单结构优化**:解决表单冲突,提升操作稳定性
## 🛡️ 安全提升
- 增强权限验证机制
- 完善安全检查流程
- 优化数据处理安全性
## 💡 用户体验改进
- 添加数据库一键修复功能
- 简化操作提示信息
- 更友好的错误反馈
- 优化管理界面布局
## 📊 兼容性
- 支持从 v1.1.1 无缝升级
- 自动检测并修复数据库结构
- 保持原有数据完整性
---
## 升级说明
1. 直接覆盖安装即可,无需额外操作
2. 插件会自动检测并升级数据库结构
3. 如遇问题可使用"数据库修复"功能
4. 所有原有数据将完整保留
## 技术支持
- 如有问题请访问https://lhcy.org
- 或在WordPress插件目录提交反馈

155
README.md
View File

@@ -1,54 +1,151 @@
# Friend Links Manager: 一款简洁、美观的 WordPress 友情链接插件
# 友情链接管理器 WordPress插件
近日,我开发了一款全新 WordPress 友情链接插件。该插件简洁、美观、易于使用
一个功能强大且易于使用的WordPress友情链接管理插件,支持链接描述、批量操作、导入导出等功能
可以访问以下链接查看实际展示效果:[友情链接展示页](https://lhcy.org/friendlinks.html)
## 🌟 主要功能
## 插件特性
### 📝 链接管理
- **添加链接**支持网站名称、URL、图标、描述信息
- **编辑链接**:在线编辑所有链接信息
- **删除链接**:单个删除或批量删除
- **拖拽排序**:直观的拖拽排序,自动保存
- **响应式布局**:在桌面端默认每行展示 3 个链接,并能根据屏幕尺寸自动调整,在平板和手机上也能完美呈现。
- **信息展示丰富**:每个链接均会展示对方网站的 Favicon头像和博客名称一目了然。
- **灵活的排序方式**:支持固定排序和随机排序两种模式。设置为随机排序后,每次刷新页面都会带来全新的链接顺序。
- **智能头像获取**:自动通过 `favicon.im` 服务抓取网站头像。同时支持手动指定头像链接,或在留空时显示默认头像。
- **简洁的管理后台**:后台管理界面操作直观,美观大方。
- **纯净无残留**:启用时自动创建专属数据表,禁用时彻底删除,不在数据库留下任何冗余数据。
- **数据轻松迁移**:内置导入/导出功能,方便备份和恢复友链数据。
### 🎨 显示设置
- **布局控制**自定义电脑端每行显示链接数2-6个
- **显示模式**:随机显示或按顺序显示
- **描述控制**:可选择是否在前台显示链接描述
## 使用方法
### 📊 数据管理
- **CSV导出**导出所有链接数据为CSV文件
- **CSV导入**:批量导入链接数据,支持描述字段
- **数据备份**:定期导出备份链接数据
**1. 下载与安装**
### 🛡️ 安全特性
- **权限控制**:仅管理员可访问管理界面
- **数据验证**:严格的输入验证和安全检查
- **数据库保护**:安全的数据操作和错误处理
下载 `.zip` 压缩包后,在 WordPress 后台的“插件”—>“安装插件”页面上传并启用。
## 📦 安装方法
**2. 插入短代码 (Shortcode)**
### 方法一WordPress后台安装
1. 登录WordPress管理后台
2. 进入"插件" → "安装插件"
3. 搜索"友情链接管理器"
4. 点击"安装"然后"启用"
在任意页面或文章中插入短代码即可展示友情链接。
### 方法二:手动安装
1. 下载插件压缩包
2. 解压到 `wp-content/plugins/` 目录
3. 在WordPress后台启用插件
- 默认排序:
## 🚀 使用方法
```
### 1. 添加友情链接
1. 进入WordPress后台 → "友情链接"
2. 在"添加新链接"区域填写信息:
- **网站名称**:必填,显示的链接标题
- **网站URL**:必填,完整的网站地址
- **自动获取头像**勾选可自动获取网站favicon
- **网站图标URL**:可选,手动指定图标地址
- **链接描述**:可选,一句话描述该网站
3. 点击"添加链接"
### 2. 管理现有链接
1. 在"链接列表"中查看所有链接
2. 直接在列表中编辑链接信息
3. 拖拽链接调整显示顺序
4. 勾选链接可进行批量删除
5. 点击"保存更改"保存所有修改
### 3. 配置显示设置
1. 在"显示设置"区域配置:
- **电脑端每行显示链接数**选择2-6个
- **随机显示友情链接**:开启后随机显示,关闭后按排序显示
- **显示链接描述**:控制是否在前台显示描述
2. 点击"保存设置"
### 4. 在页面中显示友情链接
使用短代码在任意页面或文章中显示友情链接:
```html
[friend_links]
```
- 随机排序(每次刷新页面重新排序):
### 5. 导入导出数据
#### 导出链接
1. 点击"导出为CSV"按钮
2. 下载包含所有链接数据的CSV文件
3. 文件包含四列网站名称、网站URL、图标URL、链接描述
#### 导入链接
1. 准备CSV文件格式如下
```
[friend_links random="true"]
网站名称,网站URL,图标URL,链接描述
示例网站,https://example.com,https://example.com/icon.png,这是一个示例网站
```
2. 在"导入链接"区域选择CSV文件
3. 点击"导入链接"
4. 系统会自动处理并显示导入结果
**如何在编辑器中插入?**
## 🔧 高级功能
- **Gutenberg (区块编辑器)**:添加“自定义 HTML”区块将短代码粘贴进去即可。
- **Classic Editor (经典编辑器)**:直接在文本编辑模式下粘贴短代码。
### 数据库修复
如果遇到保存功能异常,可使用数据库修复功能:
1. 在管理页面顶部找到"数据库修复"提示
2. 点击"点击修复数据库表结构"按钮
3. 系统会自动检查并修复数据库结构
## 未来计划
### 批量操作
1. 在链接列表中勾选多个链接
2. 点击"批量删除"按钮
3. 确认删除操作
计划将此插件移植到 Typecho 平台。
## 📋 CSV文件格式
## 致谢
### 导出格式
导出的CSV文件包含以下四列
- **网站名称**:链接显示的标题
- **网站URL**:完整的网站地址
- **图标URL**:网站图标地址(可选)
- **链接描述**:网站描述信息(可选)
在插件的开发过程中AI 提供了巨大帮助,特此感谢科技的力量。
### 导入格式
导入CSV文件时
- 前两列网站名称、网站URL为必填
- 后两列图标URL、链接描述为可选
- 支持从旧版本三列格式升级
经过两天的密集调试,目前插件运行稳定,暂未发现明显 Bug。欢迎提出宝贵的意见和建议或是一同参与改进让它在保持简洁、美观、易于使用的同时可以变得更好。
## 🎨 前端显示效果
[我的博客链接](https://lhcy.org),欢迎访问并留言。
友情链接在前台以卡片形式显示:
- 网站图标
- 网站名称
- 链接描述(如果启用)
- 响应式布局,适配各种屏幕尺寸
## 🔒 安全说明
- 插件仅在管理员权限下运行
- 所有用户输入都经过严格验证和清理
- 使用WordPress标准的安全机制
- 不会收集或传输任何用户数据
## 📞 技术支持
- **作者网站**https://lhcy.org
- **插件版本**1.2.2
- **WordPress要求**5.0+
- **PHP要求**7.0+
## 🔄 更新日志
查看 `CHANGELOG.md` 了解详细的版本更新信息。
## 📄 许可证
本插件采用 GPL v2.0 许可证发布。
---
**提示**:建议定期导出链接数据进行备份,以防意外数据丢失。

View File

@@ -1,4 +1,39 @@
/* 后台样式 */
.flm-critical-warning {
font-size: 16px !important;
font-weight: bold !important;
border-left: 5px solid #dc3232 !important;
background-color: #fef7f7 !important;
padding: 15px 20px !important;
margin: 20px 0 25px 0 !important;
box-shadow: 0 2px 8px rgba(220, 50, 50, 0.2) !important;
border-radius: 4px !important;
animation: flm-warning-pulse 2s infinite !important;
}
.flm-critical-warning p {
margin: 0 !important;
line-height: 1.5 !important;
color: #dc3232 !important;
}
.flm-critical-warning strong {
font-size: 18px !important;
color: #b71c1c !important;
}
@keyframes flm-warning-pulse {
0% {
box-shadow: 0 2px 8px rgba(220, 50, 50, 0.2);
}
50% {
box-shadow: 0 2px 12px rgba(220, 50, 50, 0.4);
}
100% {
box-shadow: 0 2px 8px rgba(220, 50, 50, 0.2);
}
}
.flm-admin-container {
display: flex;
gap: 2rem;
@@ -33,11 +68,23 @@
}
.form-group input[type="text"],
.form-group input[type="url"] {
.form-group input[type="url"],
.form-group textarea {
width: 100%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
resize: vertical;
}
.form-group textarea {
min-height: 60px;
}
.form-group input::placeholder,
.form-group textarea::placeholder {
color: #999;
font-style: italic;
}
#flm-sortable-links {
@@ -57,11 +104,43 @@
border-left: 4px solid #6667ab;
}
.flm-link-checkbox {
display: flex;
align-items: center;
min-width: 30px;
}
.flm-link-checkbox input[type="checkbox"] {
width: 16px;
height: 16px;
margin: 0;
}
.flm-link-preview {
display: flex;
align-items: center;
gap: 10px;
min-width: 200px;
cursor: move;
padding: 8px;
border-radius: 4px;
transition: background-color 0.2s ease;
}
.flm-link-preview:hover {
background-color: #f9f9f9;
}
.flm-drag-handle {
color: #999;
font-size: 18px;
cursor: move;
user-select: none;
padding: 4px;
}
.flm-drag-handle:hover {
color: #666;
}
.flm-link-icon {
@@ -79,20 +158,80 @@
min-width: 100px;
}
.flm-import-export {
margin-top: 30px;
padding-top: 20px;
border-top: 1px solid #eee;
/* 拖拽排序样式 */
.flm-link-item-placeholder {
background-color: #f0f0f0;
border: 2px dashed #ccc;
height: 60px;
margin: 8px 0;
border-radius: 4px;
}
.flm-import-export h3 {
.ui-sortable-helper {
background-color: #fff;
box-shadow: 0 2px 8px rgba(0,0,0,0.15);
border-radius: 4px;
}
.flm-top-actions {
margin-bottom: 20px;
padding-bottom: 15px;
border-bottom: 1px solid #eee;
display: flex;
gap: 10px;
align-items: center;
flex-wrap: wrap;
}
.flm-top-actions .button {
margin-right: 10px;
}
.flm-top-section {
margin-bottom: 25px;
padding: 20px;
background: #f9f9f9;
border-radius: 4px;
border-left: 4px solid #6667ab;
}
.flm-top-section h3 {
margin-top: 0;
margin-bottom: 15px;
font-size: 1.1em;
}
.flm-top-section .form-group {
margin-bottom: 15px;
}
.flm-export, .flm-import {
margin-bottom: 15px;
.flm-top-section .form-group:last-child {
margin-bottom: 0;
}
.flm-top-section select {
width: 200px;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
}
.flm-top-section input[type="file"] {
width: 100%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
}
.flm-top-section .description {
font-size: 0.9em;
color: #666;
margin-top: 5px;
font-style: italic;
}
/* 前端展示样式 */
.flm-links-container {
display: grid;
@@ -110,8 +249,13 @@
}
.flm-link-card:hover {
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
transform: translateY(-2px);
box-shadow: 0 6px 12px rgba(0,0,0,0.15);
border-color: #555599;
}
/* 确保hover时横线仍然可见 */
.flm-link-card:hover .flm-link-description {
border-bottom-color: #ddd;
}
.flm-link-icon-container {
@@ -130,6 +274,37 @@
font-weight: bold;
color: inherit; /* 使用主题默认颜色 */
margin-top: 10px;
margin-bottom: 5px;
border: none !important;
border-bottom: none !important;
padding-bottom: 0 !important;
}
.flm-link-description {
font-size: 0.9em;
color: #666;
margin-top: 5px;
margin-bottom: 0;
line-height: 1.4;
padding-bottom: 8px;
border-bottom: 1px solid #eee !important;
}
/* 确保没有描述时完全不显示横线 */
.flm-link-card:not(:has(.flm-link-description)) .flm-link-name {
margin-bottom: 0;
}
/* 彻底移除链接卡片内所有不必要的边框 */
.flm-link-card a,
.flm-link-card a *,
.flm-link-icon-container,
.flm-link-icon {
border: none !important;
border-bottom: none !important;
border-top: none !important;
border-left: none !important;
border-right: none !important;
}
.flm-link-card a {
@@ -141,7 +316,7 @@
/* 响应式设计 */
@media (max-width: 1024px) {
.flm-links-container {
grid-template-columns: repeat(2, 1fr);
grid-template-columns: repeat(2, 1fr) !important;
}
}
@@ -155,7 +330,7 @@
}
.flm-links-container {
grid-template-columns: 1fr;
grid-template-columns: 1fr !important;
}
.flm-link-item {

View File

@@ -1,15 +1,168 @@
jQuery(document).ready(function($) {
// 显示消息函数
function flm_show_message(message, type) {
var className = type === 'success' ? 'notice-success' : 'notice-error';
var $notice = $('<div class="notice ' + className + ' is-dismissible"><p>' + message + '</p></div>');
$('.wrap h1').after($notice);
// 3秒后自动隐藏
setTimeout(function() {
$notice.fadeOut(300, function() {
$(this).remove();
});
}, 3000);
}
// 使链接可排序
$('#flm-sortable-links').sortable({
handle: '.flm-link-preview',
placeholder: 'flm-link-item-placeholder',
axis: 'y',
update: function() {
// 更新排序后可以在这里添加AJAX保存逻辑
update: function(event, ui) {
// 获取排序后的ID顺序
var linkIds = [];
$('#flm-sortable-links .flm-link-item').each(function(index) {
var linkId = $(this).find('input[name="link_ids[]"]').val();
linkIds.push(linkId);
});
// AJAX保存排序
$.ajax({
url: flm_vars.ajax_url,
type: 'POST',
data: {
action: 'flm_update_sort_order',
link_ids: linkIds,
nonce: flm_vars.nonce
},
success: function(response) {
if (response.success) {
// 显示成功消息
if (typeof flm_show_message === 'function') {
flm_show_message('排序已保存', 'success');
}
} else {
if (typeof flm_show_message === 'function') {
flm_show_message('排序保存失败', 'error');
}
}
},
error: function() {
if (typeof flm_show_message === 'function') {
flm_show_message('排序保存失败', 'error');
}
}
});
},
stop: function(event, ui) {
// 拖拽结束后,重新索引所有字段以确保数据一致性
$('#flm-sortable-links .flm-link-item').each(function(index) {
// 更新link_ids字段的值顺序确保与DOM顺序一致
// 这里不需要改变因为link_ids的值本身就是ID
// 但需要确保其他字段在提交时能正确对应
});
}
});
// 删除链接
// 自动获取头像切换
$('#auto_get_icon').on('change', function() {
if ($(this).is(':checked')) {
$('#icon_field').hide();
} else {
$('#icon_field').show();
}
});
// 多选删除功能
function updateBatchDeleteButton() {
var selectedCount = $('.flm-select-link:checked').length;
var $batchButton = $('.flm-batch-delete');
if (selectedCount > 0) {
$batchButton.prop('disabled', false).text('批量删除 (' + selectedCount + ')');
} else {
$batchButton.prop('disabled', true).text('批量删除');
}
}
// 复选框变化时更新批量删除按钮
$(document).on('change', '.flm-select-link', function() {
updateBatchDeleteButton();
});
// 表单提交前确保数据顺序正确
$('#flm-links-form').on('submit', function(e) {
// PHP代码现在使用 array_values() 来处理数组,所以不需要重新索引
// 让表单正常提交PHP会按顺序处理数组
});
// 批量删除
$('.flm-batch-delete').on('click', function(e) {
e.preventDefault();
var selectedIds = [];
$('.flm-select-link:checked').each(function() {
selectedIds.push($(this).val());
});
if (selectedIds.length === 0) {
alert('请先选择要删除的链接');
return;
}
if (!confirm('确定要删除选中的 ' + selectedIds.length + ' 个链接吗?此操作不可撤销!')) {
return;
}
var $button = $(this);
$.ajax({
url: flm_vars.ajax_url,
type: 'POST',
data: {
action: 'flm_batch_delete_links',
link_ids: selectedIds,
nonce: flm_vars.nonce
},
beforeSend: function() {
$button.prop('disabled', true).text('删除中...');
},
success: function(response) {
if (response.success) {
// 移除已删除的链接项
var $itemsToRemove = $('.flm-select-link:checked').closest('.flm-link-item');
var totalItems = $itemsToRemove.length;
var removedItems = 0;
$itemsToRemove.fadeOut(300, function() {
$(this).remove();
removedItems++;
// 只有当所有项目都被移除后才更新按钮状态
if (removedItems === totalItems) {
updateBatchDeleteButton();
}
});
var message = '成功删除 ' + response.data.deleted_count + ' 个链接';
if (response.data.error_count > 0) {
message += '' + response.data.error_count + ' 个删除失败';
}
alert(message);
} else {
alert('批量删除失败:' + (response.data || '请重试'));
$button.prop('disabled', false).text('批量删除');
}
},
error: function() {
alert('批量删除失败,请重试');
$button.prop('disabled', false).text('批量删除');
}
});
});
// 删除单个链接
$('.flm-delete-link').on('click', function(e) {
e.preventDefault();
@@ -31,10 +184,16 @@ jQuery(document).ready(function($) {
beforeSend: function() {
$button.prop('disabled', true).text('删除中...');
},
success: function() {
success: function(response) {
if (response.success) {
$button.closest('.flm-link-item').fadeOut(300, function() {
$(this).remove();
updateBatchDeleteButton();
});
} else {
alert('删除失败,请重试');
$button.prop('disabled', false).text('删除');
}
},
error: function() {
alert('删除失败,请重试');

View File

@@ -1,9 +1,8 @@
<?php
/*
Plugin Name: 友情链接管理器
Plugin URI: https://lhcy.org/archives/wordpress-friend-links-manager.html
Description: 管理并展示友情链接,支持导入导出
Version: 1.1.1
Version: 1.2.2
Author: 林海草原
Author URI: https://lhcy.org
Text Domain: friend-links-manager
@@ -12,7 +11,7 @@ Text Domain: friend-links-manager
defined('ABSPATH') or die('无权访问');
// 定义插件常量
define('FLM_VERSION', '1.0');
define('FLM_VERSION', '1.2.2');
define('FLM_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('FLM_PLUGIN_URL', plugin_dir_url(__FILE__));
@@ -29,6 +28,7 @@ function flm_create_table() {
name varchar(100) NOT NULL,
url varchar(255) NOT NULL,
icon varchar(255) DEFAULT '',
description text DEFAULT '',
sort_order int(11) DEFAULT 0,
PRIMARY KEY (id)
) $charset_collate;";
@@ -36,13 +36,24 @@ function flm_create_table() {
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
// 初始化插件版本
update_option('flm_version', FLM_VERSION);
// 检查是否需要升级数据库
$current_version = get_option('flm_version', '1.0');
if (version_compare($current_version, '1.2.0', '<')) {
// 检查description字段是否已存在使用更安全的方法
$table_columns = $wpdb->get_col("SHOW COLUMNS FROM $table_name");
if (!in_array('description', $table_columns)) {
// 添加description字段
$wpdb->query("ALTER TABLE $table_name ADD COLUMN description text DEFAULT '' AFTER icon");
}
}
// 动态卸载逻辑(替代 register_uninstall_hook
register_deactivation_hook(__FILE__, 'flm_cleanup');
function flm_cleanup() {
// 初始化插件版本
update_option('flm_version', '1.2.0');
}
// 卸载逻辑(只在卸载插件时删除数据)
register_uninstall_hook(__FILE__, 'flm_uninstall');
function flm_uninstall() {
global $wpdb;
// 删除数据库表
@@ -53,11 +64,130 @@ function flm_cleanup() {
// 清理插件选项
delete_option('flm_version');
delete_option('flm_desktop_columns');
delete_option('flm_random_display');
delete_option('flm_show_descriptions');
// 强制刷新缓存(针对某些缓存插件)
wp_cache_flush();
}
// AJAX处理函数
add_action('wp_ajax_flm_update_sort_order', 'flm_update_sort_order');
function flm_update_sort_order() {
check_ajax_referer('flm_nonce', 'nonce');
if (!current_user_can('manage_options')) {
wp_die('无权访问');
}
if (!empty($_POST['link_ids'])) {
global $wpdb;
$table_name = $wpdb->prefix . 'friend_links';
foreach ($_POST['link_ids'] as $index => $link_id) {
$wpdb->update($table_name,
array('sort_order' => $index),
array('id' => intval($link_id))
);
}
wp_send_json_success(array('message' => '排序已更新'));
}
wp_send_json_error(array('message' => '无效的请求数据'));
}
add_action('wp_ajax_flm_delete_link', 'flm_delete_link');
function flm_delete_link() {
check_ajax_referer('flm_nonce', 'nonce');
if (!current_user_can('manage_options')) {
wp_die('无权访问');
}
if (!empty($_POST['link_id'])) {
global $wpdb;
$table_name = $wpdb->prefix . 'friend_links';
$result = $wpdb->delete($table_name, array('id' => intval($_POST['link_id'])));
if ($result !== false) {
wp_send_json_success(array('message' => '链接已删除'));
}
}
wp_send_json_error(array('message' => '删除失败'));
}
add_action('wp_ajax_flm_batch_delete_links', 'flm_batch_delete_links');
function flm_batch_delete_links() {
check_ajax_referer('flm_nonce', 'nonce');
if (!current_user_can('manage_options')) {
wp_die('无权访问');
}
if (!empty($_POST['link_ids'])) {
global $wpdb;
$table_name = $wpdb->prefix . 'friend_links';
$deleted_count = 0;
$error_count = 0;
foreach ($_POST['link_ids'] as $link_id) {
$result = $wpdb->delete($table_name, array('id' => intval($link_id)));
if ($result !== false) {
$deleted_count++;
} else {
$error_count++;
}
}
wp_send_json_success(array(
'deleted_count' => $deleted_count,
'error_count' => $error_count
));
}
wp_send_json_error(array('message' => '无效的请求数据'));
}
// 手动数据库修复函数
add_action('admin_init', 'flm_manual_db_fix');
function flm_manual_db_fix() {
if (isset($_GET['flm_fix_db']) && current_user_can('manage_options')) {
check_admin_referer('flm_fix_db');
global $wpdb;
$table_name = $wpdb->prefix . 'friend_links';
// 检查表是否存在
if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
wp_die('友情链接表不存在');
}
// 检查description字段是否存在
$table_columns = $wpdb->get_col("SHOW COLUMNS FROM $table_name");
if (!in_array('description', $table_columns)) {
// 添加description字段
$result = $wpdb->query("ALTER TABLE $table_name ADD COLUMN description text DEFAULT '' AFTER icon");
if ($result !== false) {
// 修复成功后重定向到管理页面,不显示错误信息
wp_redirect(admin_url('admin.php?page=friend-links-manager&flm_fix_success=1'));
exit;
} else {
wp_redirect(admin_url('admin.php?page=friend-links-manager&flm_fix_error=1'));
exit;
}
} else {
// 字段已存在,重定向
wp_redirect(admin_url('admin.php?page=friend-links-manager&flm_fix_exists=1'));
exit;
}
}
}
// 加载插件功能文件
require_once FLM_PLUGIN_DIR . 'includes/admin-page.php';
require_once FLM_PLUGIN_DIR . 'includes/shortcode.php';

View File

@@ -14,6 +14,68 @@ function flm_add_admin_menu() {
);
}
// 处理导出请求
add_action('admin_init', 'flm_handle_export');
function flm_handle_export() {
if (isset($_GET['page']) && $_GET['page'] === 'friend-links-manager' &&
isset($_GET['action']) && $_GET['action'] === 'export' &&
isset($_GET['nonce']) && wp_verify_nonce($_GET['nonce'], 'flm_export_nonce')) {
global $wpdb;
$table_name = $wpdb->prefix . 'friend_links';
// 检查表是否存在
if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
wp_die('友情链接表不存在,请先添加一些链接。');
}
$links = $wpdb->get_results("SELECT * FROM $table_name ORDER BY sort_order ASC");
// 检查是否有数据
if (empty($links)) {
wp_die('没有找到任何链接数据,请先添加一些链接。');
}
// 清除所有输出缓冲
while (ob_get_level()) {
ob_end_clean();
}
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=friend-links-export-' . date('Y-m-d') . '.csv');
header('Pragma: no-cache');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Transfer-Encoding: binary');
$output = fopen('php://output', 'w');
// 添加BOM头解决中文乱码
fwrite($output, chr(0xEF).chr(0xBB).chr(0xBF));
// 写入四列标题
fputcsv($output, array(
'网站名称',
'网站URL',
'图标URL',
'链接描述'
));
foreach ($links as $link) {
// 对每列数据应用极端清理
fputcsv($output, array(
flm_sanitize_export_text($link->name),
flm_sanitize_export_url($link->url),
flm_sanitize_export_url($link->icon),
flm_sanitize_export_text($link->description)
));
}
fclose($output);
exit;
}
}
// 自动获取favicon
function flm_get_favicon($url) {
$domain = parse_url($url, PHP_URL_HOST);
@@ -66,8 +128,20 @@ function flm_admin_page() {
global $wpdb;
$table_name = $wpdb->prefix . 'friend_links';
// 显示警告信息
echo '<div class="notice notice-warning"><p><strong>警告</strong>禁用该插件将删除所有链接数据请在禁用前导出包含链接的CSV文件</p></div>';
// 显示提示信息
echo '<div class="notice notice-info"><p><strong>💡 提示</strong>卸载插件时会删除数据,禁用插件不会丢失数据。</p></div>';
// 显示数据库修复结果
if (isset($_GET['flm_fix_success'])) {
echo '<div class="notice notice-success"><p>✅ 数据库修复成功description字段已添加现在可以正常保存链接信息了。</p></div>';
} elseif (isset($_GET['flm_fix_error'])) {
echo '<div class="notice notice-error"><p>❌ 数据库修复失败,请检查数据库权限。</p></div>';
} elseif (isset($_GET['flm_fix_exists'])) {
echo '<div class="notice notice-info"><p> 数据库表结构正常,无需修复。</p></div>';
}
// 数据库修复按钮
echo '<div class="notice notice-warning"><p><strong>⚠️ 数据库修复:</strong><a href="' . wp_nonce_url(admin_url('admin.php?page=friend-links-manager&flm_fix_db=1'), 'flm_fix_db') . '" class="button">点击修复数据库表结构</a>(如果保存功能不工作)</p></div>';
// 处理表单提交
if (isset($_POST['flm_action'])) {
@@ -78,7 +152,22 @@ function flm_admin_page() {
if (!empty($_POST['name']) && !empty($_POST['url'])) {
$name = sanitize_text_field($_POST['name']);
$url = esc_url_raw($_POST['url']);
$icon = !empty($_POST['icon']) ? esc_url_raw($_POST['icon']) : flm_get_favicon($url);
// 验证URL格式
if (!filter_var($url, FILTER_VALIDATE_URL)) {
echo '<div class="notice notice-error"><p>请输入有效的URL地址</p></div>';
break;
}
$auto_get_icon = isset($_POST['auto_get_icon']) ? true : false;
$icon = '';
if ($auto_get_icon) {
$icon = flm_get_favicon($url);
} elseif (!empty($_POST['icon'])) {
$icon = esc_url_raw($_POST['icon']);
}
$description = !empty($_POST['description']) ? sanitize_textarea_field($_POST['description']) : '';
$existing = $wpdb->get_row($wpdb->prepare(
"SELECT id FROM $table_name WHERE url = %s",
@@ -86,11 +175,16 @@ function flm_admin_page() {
));
if (!$existing) {
// 获取当前最大的sort_order值
$max_sort_order = $wpdb->get_var("SELECT MAX(sort_order) FROM $table_name");
$new_sort_order = $max_sort_order ? intval($max_sort_order) + 1 : 0;
$wpdb->insert($table_name, array(
'name' => $name,
'url' => $url,
'icon' => $icon,
'sort_order' => 0
'description' => $description,
'sort_order' => $new_sort_order
));
echo '<div class="notice notice-success"><p>链接添加成功!</p></div>';
} else {
@@ -101,15 +195,58 @@ function flm_admin_page() {
case 'update_links':
if (!empty($_POST['link_ids'])) {
foreach ($_POST['link_ids'] as $index => $id) {
$wpdb->update($table_name, array(
'name' => sanitize_text_field($_POST['link_names'][$index]),
'url' => esc_url_raw($_POST['link_urls'][$index]),
'icon' => esc_url_raw($_POST['link_icons'][$index]),
'sort_order' => $index
), array('id' => intval($id)));
$update_errors = 0;
$update_count = 0;
// 直接使用数组格式
$link_ids = array_values($_POST['link_ids']);
$link_names = isset($_POST['link_names']) ? array_values($_POST['link_names']) : array();
$link_urls = isset($_POST['link_urls']) ? array_values($_POST['link_urls']) : array();
$link_icons = isset($_POST['link_icons']) ? array_values($_POST['link_icons']) : array();
$link_descriptions = isset($_POST['link_descriptions']) ? array_values($_POST['link_descriptions']) : array();
foreach ($link_ids as $index => $id) {
// 确保数组索引存在
if (!isset($link_names[$index]) || !isset($link_urls[$index])) {
$update_errors++;
continue;
}
$name = sanitize_text_field($link_names[$index]);
$url = esc_url_raw($link_urls[$index]);
// 验证URL格式
if (!filter_var($url, FILTER_VALIDATE_URL)) {
$update_errors++;
continue;
}
$icon = isset($link_icons[$index]) ? esc_url_raw($link_icons[$index]) : '';
$description = isset($link_descriptions[$index]) ? sanitize_textarea_field($link_descriptions[$index]) : '';
$update_data = array(
'name' => $name,
'url' => $url,
'icon' => $icon,
'description' => $description,
'sort_order' => $index
);
$result = $wpdb->update($table_name, $update_data, array('id' => intval($id)));
if ($result === false) {
echo '<div class="notice notice-error"><p><strong>数据库错误:</strong>ID ' . $id . ' 更新失败: ' . $wpdb->last_error . '</p></div>';
$update_errors++;
} else {
$update_count++;
}
}
if ($update_errors > 0) {
echo '<div class="notice notice-warning"><p>更新完成!成功更新 ' . $update_count . ' 条链接,跳过 ' . $update_errors . ' 条记录</p></div>';
} else {
echo '<div class="notice notice-success"><p>链接更新成功!共更新 ' . $update_count . ' 条记录</p></div>';
}
echo '<div class="notice notice-success"><p>链接更新成功!</p></div>';
}
break;
@@ -121,43 +258,24 @@ function flm_admin_page() {
break;
case 'export_links':
$links = $wpdb->get_results("SELECT name, url, icon FROM $table_name ORDER BY sort_order ASC");
// 清除所有输出缓冲
while (ob_get_level()) {
ob_end_clean();
}
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=friend-links-export-' . date('Y-m-d') . '.csv');
header('Pragma: no-cache');
header('Expires: 0');
$output = fopen('php://output', 'w');
// 添加BOM头解决中文乱码
fwrite($output, chr(0xEF).chr(0xBB).chr(0xBF));
// 只写入三列标题
fputcsv($output, array(
'网站名称',
'网站URL',
'图标URL'
));
foreach ($links as $link) {
// 对每列数据应用极端清理
fputcsv($output, array(
flm_sanitize_export_text($link->name),
flm_sanitize_export_url($link->url),
flm_sanitize_export_url($link->icon)
));
}
fclose($output);
// 使用WordPress的admin_url进行重定向导出
$export_url = admin_url('admin.php?page=friend-links-manager&action=export&nonce=' . wp_create_nonce('flm_export_nonce'));
wp_redirect($export_url);
exit;
break;
case 'save_settings':
$desktop_columns = isset($_POST['desktop_columns']) ? intval($_POST['desktop_columns']) : 3;
$random_display = isset($_POST['random_display']) ? 1 : 0;
$show_descriptions = isset($_POST['show_descriptions']) ? 1 : 0;
update_option('flm_desktop_columns', $desktop_columns);
update_option('flm_random_display', $random_display);
update_option('flm_show_descriptions', $show_descriptions);
echo '<div class="notice notice-success"><p>设置保存成功!</p></div>';
break;
case 'import_links':
if (!empty($_FILES['import_file']['tmp_name'])) {
$file = $_FILES['import_file']['tmp_name'];
@@ -169,6 +287,7 @@ function flm_admin_page() {
// 跳过标题行
fgetcsv($handle);
$sort_order = 0;
while (($data = fgetcsv($handle)) !== false) {
if (count($data) < 2 || empty($data[0]) || empty($data[1])) {
$error_count++;
@@ -178,6 +297,7 @@ function flm_admin_page() {
$name = sanitize_text_field($data[0]);
$url = esc_url_raw($data[1]);
$icon = isset($data[2]) ? esc_url_raw($data[2]) : flm_get_favicon($data[1]);
$description = isset($data[3]) ? sanitize_textarea_field($data[3]) : '';
if (!filter_var($url, FILTER_VALIDATE_URL)) {
$error_count++;
@@ -193,7 +313,8 @@ function flm_admin_page() {
if ($existing) {
$wpdb->update($table_name, array(
'name' => $name,
'icon' => $icon
'icon' => $icon,
'description' => $description
), array('id' => $existing->id));
$update_count++;
} else {
@@ -201,10 +322,13 @@ function flm_admin_page() {
'name' => $name,
'url' => $url,
'icon' => $icon,
'sort_order' => 0
'description' => $description,
'sort_order' => $sort_order
));
$import_count++;
}
$sort_order++;
}
}
@@ -223,6 +347,18 @@ function flm_admin_page() {
echo '<div class="notice notice-success"><p>' . $message . '</p></div>';
}
break;
case 'save_settings':
$desktop_columns = isset($_POST['desktop_columns']) ? intval($_POST['desktop_columns']) : 3;
$random_display = isset($_POST['random_display']) ? 1 : 0;
$show_descriptions = isset($_POST['show_descriptions']) ? 1 : 0;
update_option('flm_desktop_columns', $desktop_columns);
update_option('flm_random_display', $random_display);
update_option('flm_show_descriptions', $show_descriptions);
echo '<div class="notice notice-success"><p>设置保存成功!</p></div>';
break;
}
}
@@ -251,9 +387,21 @@ function flm_admin_page() {
</div>
<div class="form-group">
<label>
<input type="checkbox" id="auto_get_icon" name="auto_get_icon" checked>
自动获取网站头像
</label>
</div>
<div class="form-group" id="icon_field" style="display:none;">
<label for="icon">网站图标URL (可选)</label>
<input type="url" id="icon" name="icon">
<p class="description">留空将自动获取favicon</p>
<p class="description">手动指定图标URL</p>
</div>
<div class="form-group">
<label for="description">链接描述 (可选)</label>
<textarea id="description" name="description" rows="3" placeholder="一句话描述这个网站"></textarea>
</div>
<button type="submit" class="button button-primary">添加链接</button>
@@ -264,16 +412,66 @@ function flm_admin_page() {
<div class="flm-links-list">
<h2>链接列表</h2>
<!-- 显示设置 -->
<div class="flm-top-section flm-display-settings">
<h3>显示设置</h3>
<form method="post">
<?php wp_nonce_field('flm_nonce'); ?>
<input type="hidden" name="flm_action" value="save_settings">
<div class="form-group">
<label for="desktop_columns">电脑端每行显示链接数</label>
<select id="desktop_columns" name="desktop_columns">
<option value="2" <?php selected(get_option('flm_desktop_columns', 3), 2); ?>>2个</option>
<option value="3" <?php selected(get_option('flm_desktop_columns', 3), 3); ?>>3个</option>
<option value="4" <?php selected(get_option('flm_desktop_columns', 3), 4); ?>>4个</option>
<option value="5" <?php selected(get_option('flm_desktop_columns', 3), 5); ?>>5个</option>
<option value="6" <?php selected(get_option('flm_desktop_columns', 3), 6); ?>>6个</option>
</select>
</div>
<div class="form-group">
<label>
<input type="checkbox" name="random_display" <?php checked(get_option('flm_random_display', 1), 1); ?>>
随机显示友情链接
</label>
<p class="description">关闭后按照链接添加顺序或CSV文件中的顺序显示</p>
</div>
<div class="form-group">
<label>
<input type="checkbox" name="show_descriptions" <?php checked(get_option('flm_show_descriptions', 1), 1); ?>>
显示链接描述
</label>
<p class="description">关闭后,即使链接有描述也不会在前台显示</p>
</div>
<button type="submit" class="button button-primary">保存设置</button>
</form>
</div>
<form method="post" id="flm-links-form">
<?php wp_nonce_field('flm_nonce'); ?>
<input type="hidden" name="flm_action" value="update_links">
<div class="flm-top-actions">
<button type="submit" class="button button-primary">保存更改</button>
<a href="<?php echo admin_url('admin.php?page=friend-links-manager&action=export&nonce=' . wp_create_nonce('flm_export_nonce')); ?>" class="button">导出为CSV</a>
<button type="button" class="button flm-batch-delete" disabled>批量删除</button>
</div>
<ul id="flm-sortable-links">
<?php foreach ($links as $link): ?>
<li class="flm-link-item">
<input type="hidden" name="link_ids[]" value="<?php echo $link->id; ?>">
<div class="flm-link-checkbox">
<input type="checkbox" class="flm-select-link" value="<?php echo $link->id; ?>">
</div>
<div class="flm-link-preview">
<span class="flm-drag-handle">☰</span>
<?php if ($link->icon): ?>
<img src="<?php echo esc_url($link->icon); ?>" alt="<?php echo esc_attr($link->name); ?>" class="flm-link-icon">
<?php endif; ?>
@@ -295,6 +493,11 @@ function flm_admin_page() {
<label>网站图标URL</label>
<input type="url" name="link_icons[]" value="<?php echo esc_attr($link->icon); ?>">
</div>
<div class="form-group">
<label>链接描述</label>
<textarea name="link_descriptions[]" rows="2" placeholder="一句话描述这个网站"><?php echo esc_textarea($link->description); ?></textarea>
</div>
</div>
<div class="flm-link-actions">
@@ -309,20 +512,10 @@ function flm_admin_page() {
<?php endif; ?>
</form>
<!-- 导入导出 -->
<div class="flm-import-export">
<h3>导入/导出</h3>
<!-- 导入功能 -->
<div class="flm-top-section flm-import">
<h3>导入链接</h3>
<div class="flm-export">
<form method="post">
<?php wp_nonce_field('flm_nonce'); ?>
<input type="hidden" name="flm_action" value="export_links">
<button type="submit" class="button">导出为CSV</button>
<p class="description">导出的CSV文件将只包含三列数据网站名称、网站URL、图标URL</p>
</form>
</div>
<div class="flm-import">
<form method="post" enctype="multipart/form-data">
<?php wp_nonce_field('flm_nonce'); ?>
<input type="hidden" name="flm_action" value="import_links">
@@ -330,7 +523,7 @@ function flm_admin_page() {
<div class="form-group">
<label for="import_file">选择CSV文件</label>
<input type="file" id="import_file" name="import_file" accept=".csv" required>
<p class="description">请选择包含列数据的CSV文件网站名称、网站URL、图标URL</p>
<p class="description">请选择包含列数据的CSV文件网站名称、网站URL、图标URL、链接描述),描述列为可选</p>
</div>
<button type="submit" class="button button-primary">导入链接</button>
@@ -339,26 +532,6 @@ function flm_admin_page() {
</div>
</div>
</div>
</div>
<script>
jQuery(document).ready(function($) {
// 使链接可排序
$('#flm-sortable-links').sortable();
// 删除链接
$('.flm-delete-link').on('click', function() {
if (confirm('确定要删除这个链接吗?')) {
var linkId = $(this).data('link-id');
var $form = $('#flm-links-form');
$form.append('<input type="hidden" name="flm_action" value="delete_link">');
$form.append('<input type="hidden" name="link_id" value="' + linkId + '">');
$form.submit();
}
});
});
</script>
<?php
}
@@ -376,3 +549,66 @@ function flm_ajax_delete_link() {
wp_send_json_error();
}
// 处理AJAX排序更新请求
add_action('wp_ajax_flm_update_sort_order', 'flm_ajax_update_sort_order');
function flm_ajax_update_sort_order() {
check_ajax_referer('flm_nonce', 'nonce');
if (!empty($_POST['link_ids']) && is_array($_POST['link_ids'])) {
global $wpdb;
$table_name = $wpdb->prefix . 'friend_links';
foreach ($_POST['link_ids'] as $index => $link_id) {
$link_id = intval($link_id);
if ($link_id > 0) {
$wpdb->update($table_name,
array('sort_order' => $index),
array('id' => $link_id),
array('%d'),
array('%d')
);
}
}
wp_send_json_success();
}
wp_send_json_error();
}
// 处理AJAX批量删除请求
add_action('wp_ajax_flm_batch_delete_links', 'flm_ajax_batch_delete_links');
function flm_ajax_batch_delete_links() {
check_ajax_referer('flm_nonce', 'nonce');
if (!empty($_POST['link_ids']) && is_array($_POST['link_ids'])) {
global $wpdb;
$table_name = $wpdb->prefix . 'friend_links';
$deleted_count = 0;
$error_count = 0;
foreach ($_POST['link_ids'] as $link_id) {
$link_id = intval($link_id);
if ($link_id > 0) {
$result = $wpdb->delete($table_name, array('id' => $link_id), array('%d'));
if ($result !== false) {
$deleted_count++;
} else {
$error_count++;
}
}
}
if ($deleted_count > 0) {
wp_send_json_success(array(
'deleted_count' => $deleted_count,
'error_count' => $error_count
));
} else {
wp_send_json_error('没有链接被删除');
}
}
wp_send_json_error('无效的请求参数');
}

View File

@@ -5,11 +5,12 @@ function flm_display_friend_links($atts) {
global $wpdb;
$table_name = $wpdb->prefix . 'friend_links';
$atts = shortcode_atts(array(
'random' => 'true'
), $atts);
// 完全使用后台设置,忽略用户传递的参数
$random_display = get_option('flm_random_display', 1);
$desktop_columns = get_option('flm_desktop_columns', 3);
$show_descriptions = get_option('flm_show_descriptions', 1);
$order_by = ($atts['random'] === 'true') ? 'RAND()' : 'sort_order ASC';
$order_by = $random_display ? 'RAND()' : 'sort_order ASC';
$links = $wpdb->get_results("SELECT * FROM $table_name ORDER BY $order_by");
if (empty($links)) {
@@ -18,7 +19,7 @@ function flm_display_friend_links($atts) {
ob_start();
?>
<div class="flm-links-container">
<div class="flm-links-container" data-columns="<?php echo $desktop_columns; ?>">
<?php foreach ($links as $link): ?>
<div class="flm-link-card">
<a href="<?php echo esc_url($link->url); ?>" target="_blank" rel="noopener noreferrer">
@@ -28,10 +29,18 @@ function flm_display_friend_links($atts) {
class="flm-link-icon">
</div>
<div class="flm-link-name"><?php echo esc_html($link->name); ?></div>
<?php if ($show_descriptions && !empty($link->description)): ?>
<div class="flm-link-description"><?php echo esc_html($link->description); ?></div>
<?php endif; ?>
</a>
</div>
<?php endforeach; ?>
</div>
<style>
.flm-links-container {
grid-template-columns: repeat(<?php echo $desktop_columns; ?>, 1fr) !important;
}
</style>
<?php
return ob_get_clean();
}