PluginProbe
WPWaterMark 轻水印插件 / 5.1.7
WPWaterMark 轻水印插件 v5.1.7
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.1.7, at index.php

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