230 lines
7.1 KiB
PHP
230 lines
7.1 KiB
PHP
<?php
|
||
/*
|
||
Plugin Name: 友情链接管理器
|
||
Description: 管理并展示友情链接,支持导入导出
|
||
Version: 1.2.3
|
||
Author: 林海草原
|
||
Author URI: https://lhcy.org
|
||
Text Domain: friend-links-manager
|
||
*/
|
||
|
||
defined('ABSPATH') or die('无权访问');
|
||
|
||
// 定义插件常量
|
||
define('FLM_VERSION', '1.2.2');
|
||
define('FLM_PLUGIN_DIR', plugin_dir_path(__FILE__));
|
||
define('FLM_PLUGIN_URL', plugin_dir_url(__FILE__));
|
||
|
||
// 创建数据库表
|
||
register_activation_hook(__FILE__, 'flm_create_table');
|
||
function flm_create_table() {
|
||
global $wpdb;
|
||
$table_name = $wpdb->prefix . 'friend_links';
|
||
|
||
$charset_collate = $wpdb->get_charset_collate();
|
||
|
||
$sql = "CREATE TABLE $table_name (
|
||
id mediumint(9) NOT NULL AUTO_INCREMENT,
|
||
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;";
|
||
|
||
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', '1.2.0');
|
||
}
|
||
|
||
// 卸载逻辑(只在卸载插件时删除数据)
|
||
register_uninstall_hook(__FILE__, 'flm_uninstall');
|
||
function flm_uninstall() {
|
||
global $wpdb;
|
||
|
||
// 删除数据库表
|
||
$table_name = $wpdb->prefix . 'friend_links';
|
||
if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") == $table_name) {
|
||
$wpdb->query("DROP TABLE IF EXISTS $table_name");
|
||
}
|
||
|
||
// 清理插件选项
|
||
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';
|
||
|
||
// 加载样式和脚本
|
||
add_action('wp_enqueue_scripts', 'flm_enqueue_scripts');
|
||
function flm_enqueue_scripts() {
|
||
wp_enqueue_style(
|
||
'flm-style',
|
||
FLM_PLUGIN_URL . 'assets/css/style.css',
|
||
array(),
|
||
FLM_VERSION
|
||
);
|
||
}
|
||
|
||
// 后台脚本和样式
|
||
add_action('admin_enqueue_scripts', 'flm_admin_enqueue_scripts');
|
||
function flm_admin_enqueue_scripts($hook) {
|
||
if ('toplevel_page_friend-links-manager' === $hook) {
|
||
wp_enqueue_style(
|
||
'flm-admin-style',
|
||
FLM_PLUGIN_URL . 'assets/css/style.css',
|
||
array(),
|
||
FLM_VERSION
|
||
);
|
||
|
||
wp_enqueue_script(
|
||
'flm-admin-js',
|
||
FLM_PLUGIN_URL . 'assets/js/admin.js',
|
||
array('jquery', 'jquery-ui-sortable'),
|
||
FLM_VERSION,
|
||
true
|
||
);
|
||
|
||
wp_localize_script('flm-admin-js', 'flm_vars', array(
|
||
'ajax_url' => admin_url('admin-ajax.php'),
|
||
'nonce' => wp_create_nonce('flm_nonce')
|
||
));
|
||
}
|
||
} |