| 1 |
<?php |
| 2 |
/** |
| 3 |
* 插件设置页面 |
| 4 |
* |
| 5 |
* @package WPWaterMark |
| 6 |
* @version 5.1.7 |
| 7 |
*/ |
| 8 |
// require_once('WaterMarkFunctions.php'); |
| 9 |
|
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
// 确保 WPWaterMark_VERSION 常量可用 |
| 15 |
if (!defined('WPWaterMark_VERSION')) { |
| 16 |
define('WPWaterMark_VERSION', '5.0.0'); |
| 17 |
} |
| 18 |
|
| 19 |
function wpwatermark_setting_page() { |
| 20 |
global $wpwatermark_options; |
| 21 |
|
| 22 |
// 如果没有� |
| 23 |
�局变量,尝试获取选项 |
| 24 |
if (!isset($wpwatermark_options) || !is_array($wpwatermark_options)) { |
| 25 |
$wpwatermark_options = get_option('wpwatermark_options', array()); |
| 26 |
} |
| 27 |
|
| 28 |
// 确保选项是数组 |
| 29 |
if (!is_array($wpwatermark_options)) { |
| 30 |
$config = new WaterMarkConfig(); |
| 31 |
$wpwatermark_options = $config->getOptions(); |
| 32 |
} |
| 33 |
|
| 34 |
// 处理表单提交 |
| 35 |
if (isset($_POST['submit']) && check_admin_referer('wpwatermark_settings')) { |
| 36 |
// 更新选项 |
| 37 |
$wpwatermark_options['watermark_enabled'] = isset($_POST['watermark_enabled']) ? '1' : '0'; |
| 38 |
$wpwatermark_options['watermark_type'] = sanitize_text_field($_POST['watermark_type'] ?? 'text_watermark'); |
| 39 |
$wpwatermark_options['text_content'] = sanitize_text_field($_POST['text_content'] ?? ''); |
| 40 |
$wpwatermark_options['text_font'] = sanitize_text_field($_POST['text_font'] ?? 'simhei.ttf'); |
| 41 |
$wpwatermark_options['text_angle'] = absint($_POST['text_angle'] ?? 0); |
| 42 |
$wpwatermark_options['text_size'] = absint($_POST['text_size'] ?? 14); |
| 43 |
$wpwatermark_options['text_color'] = sanitize_hex_color($_POST['text_color'] ?? '#790000'); |
| 44 |
$wpwatermark_options['watermark_mark_image'] = esc_url_raw($_POST['watermark_mark_image'] ?? ''); |
| 45 |
// 位置:优� |
| 46 |
�根据单选项(与隐藏域双保险,避� |
| 47 |
�� |
| 48 |
依赖 JS 时未提交 random) |
| 49 |
$position_mode = isset($_POST['wpwatermark_position_mode']) |
| 50 |
? sanitize_text_field(wp_unslash($_POST['wpwatermark_position_mode'])) |
| 51 |
: 'fixed'; |
| 52 |
if ($position_mode === 'random') { |
| 53 |
$wpwatermark_options['watermark_position'] = 'random'; |
| 54 |
} else { |
| 55 |
$pos = isset($_POST['watermark_position']) ? sanitize_text_field(wp_unslash($_POST['watermark_position'])) : 'bottom-right'; |
| 56 |
$wpwatermark_options['watermark_position'] = ($pos === 'random') ? 'bottom-right' : $pos; |
| 57 |
} |
| 58 |
$wpwatermark_options['watermark_margin'] = absint($_POST['watermark_margin'] ?? 50); |
| 59 |
$wpwatermark_options['watermark_diaphaneity'] = absint($_POST['watermark_diaphaneity'] ?? 100); |
| 60 |
$wpwatermark_options['watermark_min_width'] = absint($_POST['watermark_min_width'] ?? 300); |
| 61 |
$wpwatermark_options['watermark_min_height'] = absint($_POST['watermark_min_height'] ?? 300); |
| 62 |
$wpwatermark_options['watermark_extension_whitelist'] = sanitize_text_field( |
| 63 |
wp_unslash($_POST['watermark_extension_whitelist'] ?? '') |
| 64 |
); |
| 65 |
|
| 66 |
update_option('wpwatermark_options', $wpwatermark_options); |
| 67 |
echo '<div class="notice notice-success is-dismissible"><p><strong>' . |
| 68 |
__('设置已保存。', 'wpwatermark') . |
| 69 |
'</strong></p></div>'; |
| 70 |
} |
| 71 |
|
| 72 |
// 处理预览 |
| 73 |
if (isset($_POST['preview']) && check_admin_referer('wpwatermark_settings')) { |
| 74 |
$handler = new WaterMarkHandler($wpwatermark_options); |
| 75 |
$demo_img_path = plugin_dir_path(__FILE__); |
| 76 |
$im_url = $demo_img_path . 'demo.jpg'; |
| 77 |
$new_im_url = $demo_img_path . 'preview.jpg'; |
| 78 |
|
| 79 |
if ($wpwatermark_options['watermark_type'] === 'text_watermark') { |
| 80 |
$handler->createTextWatermark( |
| 81 |
$im_url, |
| 82 |
$new_im_url, |
| 83 |
$wpwatermark_options['text_content'], |
| 84 |
$wpwatermark_options |
| 85 |
); |
| 86 |
} elseif ($wpwatermark_options['watermark_type'] === 'image_watermark') { |
| 87 |
$handler->createImageWatermark( |
| 88 |
$im_url, |
| 89 |
$wpwatermark_options['watermark_mark_image'], |
| 90 |
$new_im_url, |
| 91 |
$wpwatermark_options |
| 92 |
); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
// 加载WordPress的颜色选择器 |
| 97 |
wp_enqueue_style('wp-color-picker'); |
| 98 |
wp_enqueue_script('wp-color-picker'); |
| 99 |
|
| 100 |
// 加载媒体上传器 |
| 101 |
wp_enqueue_media(); |
| 102 |
|
| 103 |
// 添加自定义样式和脚本 |
| 104 |
wp_enqueue_style('wpwatermark-admin', plugin_dir_url(__FILE__) . 'css/admin.css', array(), WPWaterMark_VERSION); |
| 105 |
wp_enqueue_script('wpwatermark-admin', plugin_dir_url(__FILE__) . 'js/admin.js', array('jquery', 'wp-color-picker'), WPWaterMark_VERSION, true); |
| 106 |
|
| 107 |
// 输出设置页面HTML |
| 108 |
?> |
| 109 |
<div class="wrap wpwatermark-wrap"> |
| 110 |
<h1>WPWaterMark 水印插件设置</h1> |
| 111 |
<p>在这里,我们要对水印插件设置。<a href="https://www.laojiang.me/5993.html" target="_blank">插件介绍</a>(� |
| 112 |
�注� |
| 113 |
�众号:<span style="color: red;">老蒋朋友圈</span>)</p> |
| 114 |
<form method="post" action="" class="wpwatermark-form"> |
| 115 |
<?php wp_nonce_field('wpwatermark_settings'); ?> |
| 116 |
|
| 117 |
<table class="form-table"> |
| 118 |
<tr> |
| 119 |
<th scope="row">启用水印</th> |
| 120 |
<td> |
| 121 |
<fieldset> |
| 122 |
<label> |
| 123 |
<input type="checkbox" name="watermark_enabled" value="1" <?php checked($wpwatermark_options['watermark_enabled'], '1'); ?>> |
| 124 |
启用水印功能 |
| 125 |
</label> |
| 126 |
<p class="description">勾选此项后,水印功能才会生效</p> |
| 127 |
</fieldset> |
| 128 |
</td> |
| 129 |
</tr> |
| 130 |
|
| 131 |
<tr> |
| 132 |
<th scope="row">水印类型</th> |
| 133 |
<td> |
| 134 |
<fieldset> |
| 135 |
<label> |
| 136 |
<input type="radio" name="watermark_type" value="text_watermark" <?php checked($wpwatermark_options['watermark_type'], 'text_watermark'); ?>> |
| 137 |
文字水印 |
| 138 |
</label> |
| 139 |
<br> |
| 140 |
<label> |
| 141 |
<input type="radio" name="watermark_type" value="image_watermark" <?php checked($wpwatermark_options['watermark_type'], 'image_watermark'); ?>> |
| 142 |
图片水印(推荐) |
| 143 |
</label> |
| 144 |
</fieldset> |
| 145 |
</td> |
| 146 |
</tr> |
| 147 |
|
| 148 |
<tr class="text-watermark-options" <?php echo $wpwatermark_options['watermark_type'] !== 'text_watermark' ? 'style="display:none;"' : ''; ?>> |
| 149 |
<th scope="row">水印文字</th> |
| 150 |
<td> |
| 151 |
<input type="text" name="text_content" value="<?php echo esc_attr($wpwatermark_options['text_content']); ?>" class="regular-text"> |
| 152 |
</td> |
| 153 |
</tr> |
| 154 |
|
| 155 |
<tr class="text-watermark-options" <?php echo $wpwatermark_options['watermark_type'] !== 'text_watermark' ? 'style="display:none;"' : ''; ?>> |
| 156 |
<th scope="row">字体</th> |
| 157 |
<td> |
| 158 |
<select name="text_font"> |
| 159 |
<?php |
| 160 |
$fonts_dir = plugin_dir_path(__FILE__) . 'fonts/'; |
| 161 |
$fonts = scandir($fonts_dir); |
| 162 |
foreach ($fonts as $font) { |
| 163 |
if ($font != "." && $font != "..") { |
| 164 |
echo '<option value="' . esc_attr($font) . '" ' . selected($wpwatermark_options['text_font'], $font, false) . '>' . esc_html($font) . '</option>'; |
| 165 |
} |
| 166 |
} |
| 167 |
?> |
| 168 |
</select> |
| 169 |
<p class="description">可以将字体ttf文件放在fonts文件夹中,然后选择字体。</p> |
| 170 |
</td> |
| 171 |
</tr> |
| 172 |
|
| 173 |
<tr class="text-watermark-options" <?php echo $wpwatermark_options['watermark_type'] !== 'text_watermark' ? 'style="display:none;"' : ''; ?>> |
| 174 |
<th scope="row">字体大小</th> |
| 175 |
<td> |
| 176 |
<input type="number" name="text_size" value="<?php echo esc_attr($wpwatermark_options['text_size']); ?>" class="small-text" min="1" max="100"> |
| 177 |
<p class="description">像素大小</p> |
| 178 |
</td> |
| 179 |
</tr> |
| 180 |
|
| 181 |
<tr class="text-watermark-options" <?php echo $wpwatermark_options['watermark_type'] !== 'text_watermark' ? 'style="display:none;"' : ''; ?>> |
| 182 |
<th scope="row">字体颜色</th> |
| 183 |
<td> |
| 184 |
<input type="text" name="text_color" value="<?php echo esc_attr($wpwatermark_options['text_color']); ?>" class="wpwatermark-color-picker"> |
| 185 |
</td> |
| 186 |
</tr> |
| 187 |
|
| 188 |
<tr class="text-watermark-options" <?php echo $wpwatermark_options['watermark_type'] !== 'text_watermark' ? 'style="display:none;"' : ''; ?>> |
| 189 |
<th scope="row">文字角度</th> |
| 190 |
<td> |
| 191 |
<input type="number" name="text_angle" value="<?php echo esc_attr($wpwatermark_options['text_angle']); ?>" class="small-text" min="0" max="360"> |
| 192 |
<p class="description">0-360度之间</p> |
| 193 |
</td> |
| 194 |
</tr> |
| 195 |
|
| 196 |
<tr class="image-watermark-options" <?php echo $wpwatermark_options['watermark_type'] !== 'image_watermark' ? 'style="display:none;"' : ''; ?>> |
| 197 |
<th scope="row">水印图片</th> |
| 198 |
<td> |
| 199 |
<div class="wpwatermark-media-preview"> |
| 200 |
<?php if (!empty($wpwatermark_options['watermark_mark_image'])): ?> |
| 201 |
<img src="<?php echo esc_url($wpwatermark_options['watermark_mark_image']); ?>" alt="水印预览"> |
| 202 |
<?php endif; ?> |
| 203 |
</div> |
| 204 |
<input type="hidden" name="watermark_mark_image" id="watermark_mark_image" value="<?php echo esc_attr($wpwatermark_options['watermark_mark_image']); ?>"> |
| 205 |
<button type="button" class="button wpwatermark-upload-button">选择图片</button> |
| 206 |
</td> |
| 207 |
</tr> |
| 208 |
|
| 209 |
<tr> |
| 210 |
<th scope="row">水印位置</th> |
| 211 |
<td> |
| 212 |
<?php |
| 213 |
$position_is_random = isset($wpwatermark_options['watermark_position']) && $wpwatermark_options['watermark_position'] === 'random'; |
| 214 |
?> |
| 215 |
<fieldset class="wpwatermark-position-mode" style="margin-bottom:12px;"> |
| 216 |
<label style="display:inline-block;margin-right:16px;"> |
| 217 |
<input type="radio" name="wpwatermark_position_mode" value="fixed" <?php checked(!$position_is_random); ?>> |
| 218 |
固定九宫格 |
| 219 |
</label> |
| 220 |
<label style="display:inline-block;"> |
| 221 |
<input type="radio" name="wpwatermark_position_mode" value="random" <?php checked($position_is_random); ?>> |
| 222 |
随机九宫格(每次上传在九宫格中随机一格) |
| 223 |
</label> |
| 224 |
</fieldset> |
| 225 |
<div class="wpwatermark-position-selector"<?php echo $position_is_random ? ' style="opacity:0.5;"' : ''; ?>> |
| 226 |
<?php |
| 227 |
$positions = array( |
| 228 |
'top-left' => '左上', |
| 229 |
'top-center' => '中上', |
| 230 |
'top-right' => '右上', |
| 231 |
'middle-left' => '左中', |
| 232 |
'middle-center' => '中心', |
| 233 |
'middle-right' => '右中', |
| 234 |
'bottom-left' => '左下', |
| 235 |
'bottom-center' => '中下', |
| 236 |
'bottom-right' => '右下' |
| 237 |
); |
| 238 |
|
| 239 |
foreach ($positions as $value => $label) { |
| 240 |
echo '<button type="button" data-position="' . esc_attr($value) . '" ' . |
| 241 |
(!$position_is_random && isset($wpwatermark_options['watermark_position']) && $wpwatermark_options['watermark_position'] === $value ? 'class="active"' : '') . '>' . |
| 242 |
esc_html($label) . '</button>'; |
| 243 |
} |
| 244 |
?> |
| 245 |
</div> |
| 246 |
<input type="hidden" name="watermark_position" id="watermark_position" value="<?php echo esc_attr($wpwatermark_options['watermark_position']); ?>"> |
| 247 |
<p class="description">固定九宫格:水印始终在所选格子� |
| 248 |
;随机九宫格:每张图处理时从九个格子中随机选一个(与固定模式二选一)。</p> |
| 249 |
</td> |
| 250 |
</tr> |
| 251 |
|
| 252 |
<tr> |
| 253 |
<th scope="row">边距</th> |
| 254 |
<td> |
| 255 |
<input type="number" name="watermark_margin" value="<?php echo esc_attr($wpwatermark_options['watermark_margin']); ?>" class="small-text" min="0"> |
| 256 |
<p class="description">水印距离边缘的距离(像素)</p> |
| 257 |
</td> |
| 258 |
</tr> |
| 259 |
|
| 260 |
<tr> |
| 261 |
<th scope="row">透明度</th> |
| 262 |
<td> |
| 263 |
<input type="number" name="watermark_diaphaneity" value="<?php echo esc_attr($wpwatermark_options['watermark_diaphaneity']); ?>" class="small-text" min="0" max="100"> |
| 264 |
<p class="description">0-100之间,100为完� |
| 265 |
�不透明。此设置同时对文字水印和图片水印生效。</p> |
| 266 |
</td> |
| 267 |
</tr> |
| 268 |
|
| 269 |
<tr> |
| 270 |
<th scope="row">最小尺寸限制</th> |
| 271 |
<td> |
| 272 |
<label> |
| 273 |
宽度: |
| 274 |
<input type="number" name="watermark_min_width" value="<?php echo esc_attr($wpwatermark_options['watermark_min_width']); ?>" class="small-text" min="0"> |
| 275 |
</label> |
| 276 |
<label> |
| 277 |
高度: |
| 278 |
<input type="number" name="watermark_min_height" value="<?php echo esc_attr($wpwatermark_options['watermark_min_height']); ?>" class="small-text" min="0"> |
| 279 |
</label> |
| 280 |
<p class="description">只有� |
| 281 |
过这个尺寸的图片才会添加水印</p> |
| 282 |
</td> |
| 283 |
</tr> |
| 284 |
|
| 285 |
<tr> |
| 286 |
<th scope="row">水印白名单后缀</th> |
| 287 |
<td> |
| 288 |
<input type="text" name="watermark_extension_whitelist" value="<?php echo esc_attr($wpwatermark_options['watermark_extension_whitelist'] ?? ''); ?>" class="regular-text" placeholder="例如:webp,png"> |
| 289 |
<?php |
| 290 |
$supported_extensions = WaterMarkHandler::getSupportedExtensions(); |
| 291 |
$webp_supported = in_array('webp', $supported_extensions, true); |
| 292 |
?> |
| 293 |
<p class="description">使用英文逗号分隔,填写后这些后缀上传时不添加水印(例如:webp,png)。</p> |
| 294 |
<p class="description">当前支持添加水印的后缀:<?php echo esc_html(implode(', ', $supported_extensions)); ?><?php echo $webp_supported ? '' : '(当前环境未启用 WebP 处理能力)'; ?>。</p> |
| 295 |
</td> |
| 296 |
</tr> |
| 297 |
</table> |
| 298 |
|
| 299 |
<p class="submit"> |
| 300 |
<input type="submit" name="submit" class="button button-primary" value="保存更改"> |
| 301 |
<input type="submit" name="preview" class="button" value="预览效果"> |
| 302 |
</p> |
| 303 |
</form> |
| 304 |
<p><img width="150" height="150" src="<?php echo plugins_url('/images/wechat.png', __FILE__); ?>" alt="扫码� |
| 305 |
�注� |
| 306 |
�众号" /></p> |
| 307 |
<?php if (isset($_POST['preview']) && file_exists(plugin_dir_path(__FILE__) . 'preview.jpg')): ?> |
| 308 |
<div class="wpwatermark-preview"> |
| 309 |
<h2>预览效果</h2> |
| 310 |
<img src="<?php echo esc_url(plugin_dir_url(__FILE__) . 'preview.jpg?' . time()); ?>" alt="水印预览"> |
| 311 |
</div> |
| 312 |
<?php endif; ?> |
| 313 |
</div> |
| 314 |
<?php |
| 315 |
} |
| 316 |
?> |