From 8c5a3e0ce163f366ab8fa9724a7e8db6added536 Mon Sep 17 00:00:00 2001 From: LinRuiqi Date: Sat, 22 Nov 2025 09:37:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=88=B01.2.2=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 44 ++++ README.md | 163 ++++++++++++--- assets/css/style.css | 199 ++++++++++++++++-- assets/js/admin.js | 173 +++++++++++++++- friend-links-manager.php | 144 ++++++++++++- includes/admin-page.php | 422 ++++++++++++++++++++++++++++++--------- includes/shortcode.php | 19 +- 7 files changed, 1007 insertions(+), 157 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..fc05564 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,44 @@ +# 🎉 友情链接管理器 v1.2.2 发布 + +## ✨ 新增功能 +- **链接描述支持**:为每个链接添加描述信息,让友情链接更加丰富 +- **灵活显示设置**: + - 自定义电脑端每行显示链接数(2-6个可选) + - 随机/顺序显示模式切换 + - 描述信息显示开关 +- **批量操作**:支持批量选中删除链接,提升管理效率 +- **拖拽排序**:直观的拖拽排序功能,自动保存排序结果 + +## 🔧 功能优化 +- **保存功能修复**:彻底解决链接信息无法保存的问题 +- **CSV导出增强**:支持包含描述信息的完整数据导出 +- **导入功能改进**:支持四列数据导入,向后兼容旧格式 +- **表单结构优化**:解决表单冲突,提升操作稳定性 + +## 🛡️ 安全提升 +- 增强权限验证机制 +- 完善安全检查流程 +- 优化数据处理安全性 + +## 💡 用户体验改进 +- 添加数据库一键修复功能 +- 简化操作提示信息 +- 更友好的错误反馈 +- 优化管理界面布局 + +## 📊 兼容性 +- 支持从 v1.1.1 无缝升级 +- 自动检测并修复数据库结构 +- 保持原有数据完整性 + +--- + +## 升级说明 +1. 直接覆盖安装即可,无需额外操作 +2. 插件会自动检测并升级数据库结构 +3. 如遇问题可使用"数据库修复"功能 +4. 所有原有数据将完整保留 + +## 技术支持 +- 如有问题请访问:https://lhcy.org +- 或在WordPress插件目录提交反馈 \ No newline at end of file diff --git a/README.md b/README.md index 7f8384c..f636512 100644 --- a/README.md +++ b/README.md @@ -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后台启用插件 -- 默认排序: +## 🚀 使用方法 - ``` - [friend_links] - ``` +### 1. 添加友情链接 +1. 进入WordPress后台 → "友情链接" +2. 在"添加新链接"区域填写信息: + - **网站名称**:必填,显示的链接标题 + - **网站URL**:必填,完整的网站地址 + - **自动获取头像**:勾选可自动获取网站favicon + - **网站图标URL**:可选,手动指定图标地址 + - **链接描述**:可选,一句话描述该网站 +3. 点击"添加链接" -- 随机排序(每次刷新页面重新排序): +### 2. 管理现有链接 +1. 在"链接列表"中查看所有链接 +2. 直接在列表中编辑链接信息 +3. 拖拽链接调整显示顺序 +4. 勾选链接可进行批量删除 +5. 点击"保存更改"保存所有修改 - ``` - [friend_links random="true"] - ``` +### 3. 配置显示设置 +1. 在"显示设置"区域配置: + - **电脑端每行显示链接数**:选择2-6个 + - **随机显示友情链接**:开启后随机显示,关闭后按排序显示 + - **显示链接描述**:控制是否在前台显示描述 +2. 点击"保存设置" -**如何在编辑器中插入?** +### 4. 在页面中显示友情链接 +使用短代码在任意页面或文章中显示友情链接: -- **Gutenberg (区块编辑器)**:添加“自定义 HTML”区块,将短代码粘贴进去即可。 -- **Classic Editor (经典编辑器)**:直接在文本编辑模式下粘贴短代码。 +```html +[friend_links] +``` -## 未来计划 +### 5. 导入导出数据 -计划将此插件移植到 Typecho 平台。 +#### 导出链接 +1. 点击"导出为CSV"按钮 +2. 下载包含所有链接数据的CSV文件 +3. 文件包含四列:网站名称、网站URL、图标URL、链接描述 -## 致谢 +#### 导入链接 +1. 准备CSV文件,格式如下: + ``` + 网站名称,网站URL,图标URL,链接描述 + 示例网站,https://example.com,https://example.com/icon.png,这是一个示例网站 + ``` +2. 在"导入链接"区域选择CSV文件 +3. 点击"导入链接" +4. 系统会自动处理并显示导入结果 -在插件的开发过程中,AI 提供了巨大帮助,特此感谢科技的力量。 +## 🔧 高级功能 -经过两天的密集调试,目前插件运行稳定,暂未发现明显 Bug。欢迎提出宝贵的意见和建议,或是一同参与改进,让它在保持简洁、美观、易于使用的同时可以变得更好。 +### 数据库修复 +如果遇到保存功能异常,可使用数据库修复功能: +1. 在管理页面顶部找到"数据库修复"提示 +2. 点击"点击修复数据库表结构"按钮 +3. 系统会自动检查并修复数据库结构 -[我的博客链接](https://lhcy.org),欢迎访问并留言。 \ No newline at end of file +### 批量操作 +1. 在链接列表中勾选多个链接 +2. 点击"批量删除"按钮 +3. 确认删除操作 + +## 📋 CSV文件格式 + +### 导出格式 +导出的CSV文件包含以下四列: +- **网站名称**:链接显示的标题 +- **网站URL**:完整的网站地址 +- **图标URL**:网站图标地址(可选) +- **链接描述**:网站描述信息(可选) + +### 导入格式 +导入CSV文件时: +- 前两列(网站名称、网站URL)为必填 +- 后两列(图标URL、链接描述)为可选 +- 支持从旧版本三列格式升级 + +## 🎨 前端显示效果 + +友情链接在前台以卡片形式显示: +- 网站图标 +- 网站名称 +- 链接描述(如果启用) +- 响应式布局,适配各种屏幕尺寸 + +## 🔒 安全说明 + +- 插件仅在管理员权限下运行 +- 所有用户输入都经过严格验证和清理 +- 使用WordPress标准的安全机制 +- 不会收集或传输任何用户数据 + +## 📞 技术支持 + +- **作者网站**:https://lhcy.org +- **插件版本**:1.2.2 +- **WordPress要求**:5.0+ +- **PHP要求**:7.0+ + +## 🔄 更新日志 + +查看 `CHANGELOG.md` 了解详细的版本更新信息。 + +## 📄 许可证 + +本插件采用 GPL v2.0 许可证发布。 + +--- + +**提示**:建议定期导出链接数据进行备份,以防意外数据丢失。 \ No newline at end of file diff --git a/assets/css/style.css b/assets/css/style.css index 33bca5d..c302020 100644 --- a/assets/css/style.css +++ b/assets/css/style.css @@ -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 { diff --git a/assets/js/admin.js b/assets/js/admin.js index 2983c44..1d94213 100644 --- a/assets/js/admin.js +++ b/assets/js/admin.js @@ -1,15 +1,168 @@ jQuery(document).ready(function($) { + // 显示消息函数 + function flm_show_message(message, type) { + var className = type === 'success' ? 'notice-success' : 'notice-error'; + var $notice = $('

' + message + '

'); + + $('.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() { - $button.closest('.flm-link-item').fadeOut(300, function() { - $(this).remove(); - }); + 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('删除失败,请重试'); diff --git a/friend-links-manager.php b/friend-links-manager.php index 5294a03..a848397 100644 --- a/friend-links-manager.php +++ b/friend-links-manager.php @@ -1,9 +1,8 @@ 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"); + } + } + // 初始化插件版本 - update_option('flm_version', FLM_VERSION); + update_option('flm_version', '1.2.0'); } -// 动态卸载逻辑(替代 register_uninstall_hook) -register_deactivation_hook(__FILE__, 'flm_cleanup'); -function flm_cleanup() { +// 卸载逻辑(只在卸载插件时删除数据) +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'; diff --git a/includes/admin-page.php b/includes/admin-page.php index c530005..6c66d6f 100644 --- a/includes/admin-page.php +++ b/includes/admin-page.php @@ -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 '

警告:禁用该插件将删除所有链接数据,请在禁用前导出包含链接的CSV文件!

'; + // 显示提示信息 + echo '

💡 提示:卸载插件时会删除数据,禁用插件不会丢失数据。

'; + + // 显示数据库修复结果 + if (isset($_GET['flm_fix_success'])) { + echo '

✅ 数据库修复成功!description字段已添加,现在可以正常保存链接信息了。

'; + } elseif (isset($_GET['flm_fix_error'])) { + echo '

❌ 数据库修复失败,请检查数据库权限。

'; + } elseif (isset($_GET['flm_fix_exists'])) { + echo '

ℹ️ 数据库表结构正常,无需修复。

'; + } + + // 数据库修复按钮 + echo '

⚠️ 数据库修复:点击修复数据库表结构(如果保存功能不工作)

'; // 处理表单提交 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 '

请输入有效的URL地址!

'; + 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 '

链接添加成功!

'; } 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]), + $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 - ), array('id' => intval($id))); + ); + + $result = $wpdb->update($table_name, $update_data, array('id' => intval($id))); + + if ($result === false) { + echo '

数据库错误:ID ' . $id . ' 更新失败: ' . $wpdb->last_error . '

'; + $update_errors++; + } else { + $update_count++; + } + } + + if ($update_errors > 0) { + echo '

更新完成!成功更新 ' . $update_count . ' 条链接,跳过 ' . $update_errors . ' 条记录

'; + } else { + echo '

链接更新成功!共更新 ' . $update_count . ' 条记录

'; } - echo '

链接更新成功!

'; } 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 '

设置保存成功!

'; + 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 '

' . $message . '

'; } 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 '

设置保存成功!

'; + break; } } @@ -251,9 +387,21 @@ function flm_admin_page() {
+ +
+ + + +
+ +
@@ -264,16 +412,66 @@ function flm_admin_page() { - - 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('无效的请求参数'); } \ No newline at end of file diff --git a/includes/shortcode.php b/includes/shortcode.php index 22929d7..e5c4ecc 100644 --- a/includes/shortcode.php +++ b/includes/shortcode.php @@ -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(); ?> - +