更新到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

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);
// 检查是否需要升级数据库
$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");
}
}
// 初始化插件版本
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';