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

294 lines 8.6 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.lezaiyun.com/792.html
5 * Description: WordPress轻水印插件,支持文字水印和图片水印,支持批量添加水印,支持自定义水印位置、大小、颜色、透明度等。�
6 �众号:老蒋朋友圈
7 * Version: 5.1.2
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.2');
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 public function __construct() {
54 // 初始化组件
55 $this->config = WaterMarkConfig::loadFromWordPress();
56 $this->handler = new WaterMarkHandler($this->config->getOptions());
57 $this->performance = new WaterMarkPerformance();
58
59 // 注册钩子
60 add_action('admin_menu', array($this, 'addAdminMenu'));
61 add_action('admin_init', array($this, 'registerSettings'));
62 add_filter('wp_handle_upload', array($this, 'handleImageUpload'));
63 add_action('admin_enqueue_scripts', array($this, 'enqueueAdminScripts'));
64
65 // 添加定期�
66 理日志的计划任务
67 if (!wp_next_scheduled('wpwatermark_clean_logs')) {
68 wp_schedule_event(time(), 'daily', 'wpwatermark_clean_logs');
69 }
70 add_action('wpwatermark_clean_logs', array($this, 'cleanLogs'));
71 }
72
73 /**
74 * 加载管理界面所需的脚本和样式
75 */
76 public function enqueueAdminScripts($hook) {
77 if ($hook != 'settings_page_wpwatermark') {
78 return;
79 }
80
81 // 加载WordPress原生的媒体上传器
82 wp_enqueue_media();
83
84 // 加载WordPress原生的颜色选择器
85 wp_enqueue_style('wp-color-picker');
86 wp_enqueue_script('wp-color-picker');
87
88 // 加载自定义脚本和样式
89 wp_enqueue_style(
90 'wpwatermark-admin',
91 WPWaterMark_PLUGIN_URL . 'css/admin.css',
92 array(),
93 WPWaterMark_VERSION
94 );
95
96 wp_enqueue_script(
97 'wpwatermark-admin',
98 WPWaterMark_PLUGIN_URL . 'js/admin.js',
99 array('jquery', 'wp-color-picker'),
100 WPWaterMark_VERSION,
101 true
102 );
103 }
104
105 /**
106 * 添加管理菜单
107 */
108 public function addAdminMenu() {
109 add_options_page(
110 'WPWaterMark设置',
111 'WPWaterMark',
112 'manage_options',
113 'wpwatermark',
114 array($this, 'displaySettingsPage')
115 );
116 }
117
118 /**
119 * 注册设置
120 */
121 public function registerSettings() {
122 register_setting('wpwatermark_options', 'wpwatermark_options', array($this, 'validateSettings'));
123 }
124
125 /**
126 * 验证设置
127 */
128 public function validateSettings($input) {
129 $config = new WaterMarkConfig($input);
130 return $config->getOptions();
131 }
132
133 /**
134 * 处理图片上传
135 */
136 public function handleImageUpload($file) {
137 // 检查是否为图片
138 if (strpos($file['type'], 'image') === false) {
139 return $file;
140 }
141
142 // 检查水印功能是否启用
143 if ($this->config->getOption('watermark_enabled') !== '1') {
144 return $file;
145 }
146
147 // 获取图片信息
148 $image_size = getimagesize($file['file']);
149 if (!$image_size) {
150 return $file;
151 }
152
153 // 检查图片尺寸是否满足要求
154 if ($image_size[0] < $this->config->getOption('watermark_min_width') ||
155 $image_size[1] < $this->config->getOption('watermark_min_height')) {
156 return $file;
157 }
158
159 // 开始性能监控
160 $this->performance->startMonitoring();
161
162 try {
163 // 添加水印
164 if ($this->config->getOption('watermark_type') === 'text_watermark') {
165 $this->handler->createTextWatermark(
166 $file['file'],
167 $file['file'],
168 $this->config->getOption('text_content')
169 );
170 } else {
171 $this->handler->createImageWatermark(
172 $file['file'],
173 $this->config->getOption('watermark_mark_image'),
174 $file['file']
175 );
176 }
177
178 // 记录性能数据
179 $this->performance->endMonitoring('image_upload', [
180 'file_size' => filesize($file['file']),
181 'image_dimensions' => $image_size[0] . 'x' . $image_size[1]
182 ]);
183
184 } catch (Exception $e) {
185 error_log('WPWaterMark Error: ' . $e->getMessage());
186 }
187
188 return $file;
189 }
190
191 /**
192 * 显示设置页面
193 */
194 public function displaySettingsPage() {
195 if (!current_user_can('manage_options')) {
196 wp_die(__('Insufficient privileges!', 'wpwatermark'));
197 }
198
199 // 确保选项存在
200 $wpwatermark_options = get_option('wpwatermark_options');
201 if (!is_array($wpwatermark_options)) {
202 $config = new WaterMarkConfig();
203 $wpwatermark_options = $config->getOptions();
204 update_option('wpwatermark_options', $wpwatermark_options);
205 }
206
207 // 加载设置页面模板
208 require_once(WPWaterMark_PLUGIN_DIR . 'setting_page.php');
209 wpwatermark_setting_page();
210 }
211
212 /**
213 * �
214 理日志
215 */
216 public function cleanLogs() {
217 $this->performance->cleanOldLogs(30); // 保留30天的日志
218 }
219
220 /**
221 * 插件激活时的处理
222 */
223 public static function activate() {
224 // 创建�
225 要的目录
226 $dirs = array('cache', 'logs');
227 foreach ($dirs as $dir) {
228 $path = WPWaterMark_PLUGIN_DIR . $dir;
229 if (!file_exists($path)) {
230 wp_mkdir_p($path);
231 }
232 }
233
234 // 设置默认选项
235 if (!get_option('wpwatermark_options')) {
236 $config = new WaterMarkConfig();
237 update_option('wpwatermark_options', $config->getOptions());
238 }
239 }
240
241 /**
242 * 插件停用时的处理
243 */
244 public static function deactivate() {
245 wp_clear_scheduled_hook('wpwatermark_clean_logs');
246 }
247
248 /**
249 * 插件卸载时的处理
250 */
251 public static function uninstall() {
252 // �
253 理选项
254 delete_option('wpwatermark_options');
255
256 // �
257 理文件
258 $dirs = array('cache', 'logs');
259 foreach ($dirs as $dir) {
260 $path = WPWaterMark_PLUGIN_DIR . $dir;
261 if (file_exists($path)) {
262 self::removeDirectory($path);
263 }
264 }
265 }
266
267 /**
268 * 递归删除目录
269 */
270 private static function removeDirectory($dir) {
271 if (is_dir($dir)) {
272 $objects = scandir($dir);
273 foreach ($objects as $object) {
274 if ($object != "." && $object != "..") {
275 if (is_dir($dir . "/" . $object)) {
276 self::removeDirectory($dir . "/" . $object);
277 } else {
278 unlink($dir . "/" . $object);
279 }
280 }
281 }
282 rmdir($dir);
283 }
284 }
285 }
286
287 // 注册激活、停用和卸载钩子
288 register_activation_hook(__FILE__, array('WPWaterMark', 'activate'));
289 register_deactivation_hook(__FILE__, array('WPWaterMark', 'deactivate'));
290 register_uninstall_hook(__FILE__, array('WPWaterMark', 'uninstall'));
291
292 // 初始化插件
293 $wpwatermark = new WPWaterMark();
294