PluginProbe
WPWaterMark 轻水印插件 / 5.0.1
WPWaterMark 轻水印插件 v5.0.1
5.2.4 5.2.2 5.2.1 5.1.7 5.1.6 trunk 4.3 5.0.0 5.0.1 5.1.2 5.1.3 5.1.4 5.1.5
wpwatermark / index.php

index.php in WPWaterMark 轻水印插件 5.0.1, at index.php

288 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: WPWaterMark
4 * Plugin URI: https://www.laojiang.me/5993.html
5 * Description: WordPress轻水印插件,支持文字水印和图片水印,支持批量添加水印,支持自定义水印位置、大小、颜色、透明度等。
6 * Version: 5.0.1
7 * Requires at least: 5.0
8 * Requires PHP: 7.4
9 * Author: 老蒋和他的伙伴们
10 * Author URI: https://www.laojiang.me
11 * License: GPL v2 or later
12 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
13 * Text Domain: wpwatermark
14 */
15
16 // 防止直接访问
17 if (!defined('ABSPATH')) {
18 exit;
19 }
20
21 // 检查PHP版本要求
22 if (version_compare(PHP_VERSION, '7.4', '<')) {
23 add_action('admin_notices', function() {
24 echo '<div class="notice notice-error"><p>' .
25 sprintf(__('WPWaterMark 插件需要 PHP %s 或更高版本。您当前的PHP版本是 %s,请升级您的PHP版本。', 'wpwatermark'),
26 '7.4', PHP_VERSION) .
27 '</p></div>';
28 });
29 return;
30 }
31
32 // 定义插件版本和路径常量
33 define('WPWaterMark_VERSION', '4.2.0');
34 define('WPWaterMark_PLUGIN_DIR', plugin_dir_path(__FILE__));
35 define('WPWaterMark_PLUGIN_URL', plugin_dir_url(__FILE__));
36 define('WPWaterMark_BASENAME', plugin_basename(__FILE__));
37
38 // 加载�
39 要的类
40 require_once(WPWaterMark_PLUGIN_DIR . 'WaterMarkConfig.php');
41 require_once(WPWaterMark_PLUGIN_DIR . 'WaterMarkHandler.php');
42 require_once(WPWaterMark_PLUGIN_DIR . 'WaterMarkPerformance.php');
43
44 class WPWaterMark {
45 private $config;
46 private $handler;
47 private $performance;
48
49 /**
50 * 构造函数
51 */
52 public function __construct() {
53 // 初始化组件
54 $this->config = WaterMarkConfig::loadFromWordPress();
55 $this->handler = new WaterMarkHandler($this->config->getOptions());
56 $this->performance = new WaterMarkPerformance();
57
58 // 注册钩子
59 add_action('admin_menu', array($this, 'addAdminMenu'));
60 add_action('admin_init', array($this, 'registerSettings'));
61 add_filter('wp_handle_upload', array($this, 'handleImageUpload'));
62 add_action('admin_enqueue_scripts', array($this, 'enqueueAdminScripts'));
63
64 // 添加定期�
65 理日志的计划任务
66 if (!wp_next_scheduled('wpwatermark_clean_logs')) {
67 wp_schedule_event(time(), 'daily', 'wpwatermark_clean_logs');
68 }
69 add_action('wpwatermark_clean_logs', array($this, 'cleanLogs'));
70 }
71
72 /**
73 * 加载管理界面所需的脚本和样式
74 */
75 public function enqueueAdminScripts($hook) {
76 if ($hook != 'settings_page_wpwatermark') {
77 return;
78 }
79
80 // 加载WordPress原生的媒体上传器
81 wp_enqueue_media();
82
83 // 加载WordPress原生的颜色选择器
84 wp_enqueue_style('wp-color-picker');
85 wp_enqueue_script('wp-color-picker');
86
87 // 加载自定义脚本和样式
88 wp_enqueue_style(
89 'wpwatermark-admin',
90 WPWaterMark_PLUGIN_URL . 'css/admin.css',
91 array(),
92 WPWaterMark_VERSION
93 );
94
95 wp_enqueue_script(
96 'wpwatermark-admin',
97 WPWaterMark_PLUGIN_URL . 'js/admin.js',
98 array('jquery', 'wp-color-picker'),
99 WPWaterMark_VERSION,
100 true
101 );
102 }
103
104 /**
105 * 添加管理菜单
106 */
107 public function addAdminMenu() {
108 add_options_page(
109 'WPWaterMark设置',
110 'WPWaterMark',
111 'manage_options',
112 'wpwatermark',
113 array($this, 'displaySettingsPage')
114 );
115 }
116
117 /**
118 * 注册设置
119 */
120 public function registerSettings() {
121 register_setting('wpwatermark_options', 'wpwatermark_options', array($this, 'validateSettings'));
122 }
123
124 /**
125 * 验证设置
126 */
127 public function validateSettings($input) {
128 $config = new WaterMarkConfig($input);
129 return $config->getOptions();
130 }
131
132 /**
133 * 处理图片上传
134 */
135 public function handleImageUpload($file) {
136 // 检查是否为图片
137 if (strpos($file['type'], 'image') === false) {
138 return $file;
139 }
140
141 // 获取图片信息
142 $image_size = getimagesize($file['file']);
143 if (!$image_size) {
144 return $file;
145 }
146
147 // 检查图片尺寸是否满足要求
148 if ($image_size[0] < $this->config->getOption('watermark_min_width') ||
149 $image_size[1] < $this->config->getOption('watermark_min_height')) {
150 return $file;
151 }
152
153 // 开始性能监控
154 $this->performance->startMonitoring();
155
156 try {
157 // 添加水印
158 if ($this->config->getOption('watermark_type') === 'text_watermark') {
159 $this->handler->createTextWatermark(
160 $file['file'],
161 $file['file'],
162 $this->config->getOption('text_content')
163 );
164 } else {
165 $this->handler->createImageWatermark(
166 $file['file'],
167 $this->config->getOption('watermark_mark_image'),
168 $file['file']
169 );
170 }
171
172 // 记录性能数据
173 $this->performance->endMonitoring('image_upload', [
174 'file_size' => filesize($file['file']),
175 'image_dimensions' => $image_size[0] . 'x' . $image_size[1]
176 ]);
177
178 } catch (Exception $e) {
179 error_log('WPWaterMark Error: ' . $e->getMessage());
180 }
181
182 return $file;
183 }
184
185 /**
186 * 显示设置页面
187 */
188 public function displaySettingsPage() {
189 if (!current_user_can('manage_options')) {
190 wp_die(__('Insufficient privileges!', 'wpwatermark'));
191 }
192
193 // 确保选项存在
194 $wpwatermark_options = get_option('wpwatermark_options');
195 if (!is_array($wpwatermark_options)) {
196 $config = new WaterMarkConfig();
197 $wpwatermark_options = $config->getOptions();
198 update_option('wpwatermark_options', $wpwatermark_options);
199 }
200
201 // 加载设置页面模板
202 require_once(WPWaterMark_PLUGIN_DIR . 'setting_page.php');
203 wpwatermark_setting_page();
204 }
205
206 /**
207 * �
208 理日志
209 */
210 public function cleanLogs() {
211 $this->performance->cleanOldLogs(30); // 保留30天的日志
212 }
213
214 /**
215 * 插件激活时的处理
216 */
217 public static function activate() {
218 // 创建�
219 要的目录
220 $dirs = array('cache', 'logs');
221 foreach ($dirs as $dir) {
222 $path = WPWaterMark_PLUGIN_DIR . $dir;
223 if (!file_exists($path)) {
224 wp_mkdir_p($path);
225 }
226 }
227
228 // 设置默认选项
229 if (!get_option('wpwatermark_options')) {
230 $config = new WaterMarkConfig();
231 update_option('wpwatermark_options', $config->getOptions());
232 }
233 }
234
235 /**
236 * 插件停用时的处理
237 */
238 public static function deactivate() {
239 wp_clear_scheduled_hook('wpwatermark_clean_logs');
240 }
241
242 /**
243 * 插件卸载时的处理
244 */
245 public static function uninstall() {
246 // �
247 理选项
248 delete_option('wpwatermark_options');
249
250 // �
251 理文件
252 $dirs = array('cache', 'logs');
253 foreach ($dirs as $dir) {
254 $path = WPWaterMark_PLUGIN_DIR . $dir;
255 if (file_exists($path)) {
256 self::removeDirectory($path);
257 }
258 }
259 }
260
261 /**
262 * 递归删除目录
263 */
264 private static function removeDirectory($dir) {
265 if (is_dir($dir)) {
266 $objects = scandir($dir);
267 foreach ($objects as $object) {
268 if ($object != "." && $object != "..") {
269 if (is_dir($dir . "/" . $object)) {
270 self::removeDirectory($dir . "/" . $object);
271 } else {
272 unlink($dir . "/" . $object);
273 }
274 }
275 }
276 rmdir($dir);
277 }
278 }
279 }
280
281 // 注册激活、停用和卸载钩子
282 register_activation_hook(__FILE__, array('WPWaterMark', 'activate'));
283 register_deactivation_hook(__FILE__, array('WPWaterMark', 'deactivate'));
284 register_uninstall_hook(__FILE__, array('WPWaterMark', 'uninstall'));
285
286 // 初始化插件
287 $wpwatermark = new WPWaterMark();
288