PluginProbe
WPWaterMark 轻水印插件 / 5.1.3
WPWaterMark 轻水印插件 v5.1.3
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 / WaterMarkConfig.php

WaterMarkConfig.php in WPWaterMark 轻水印插件 5.1.3, at WaterMarkConfig.php

168 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WaterMark Configuration Class
4 *
5 * Manages watermark settings with validation and defaults
6 */
7 class WaterMarkConfig {
8 // Default settings
9 private static $defaults = [
10 'watermark_enabled' => '0',
11 'watermark_type' => 'text_watermark',
12 'text_content' => '',
13 'text_font' => 'simhei.ttf',
14 'text_angle' => '0',
15 'text_size' => '14',
16 'text_color' => '#790000',
17 'watermark_mark_image' => '',
18 'watermark_position' => 'bottom-right',
19 'watermark_margin' => '50',
20 'watermark_diaphaneity' => '100',
21 'watermark_min_width' => '300',
22 'watermark_min_height' => '300'
23 ];
24
25 private $options;
26
27 /**
28 * Constructor
29 *
30 * @param array $options Optional custom options
31 */
32 public function __construct($options = []) {
33 $this->options = wp_parse_args($options, self::$defaults);
34 $this->validateOptions();
35 }
36
37 /**
38 * Validate all options
39 */
40 private function validateOptions() {
41 // Validate watermark type
42 if (!in_array($this->options['watermark_type'], ['text_watermark', 'image_watermark'])) {
43 $this->options['watermark_type'] = self::$defaults['watermark_type'];
44 }
45
46 // Validate numeric values
47 $this->options['text_angle'] = $this->validateNumeric('text_angle', 0, 360);
48 $this->options['text_size'] = $this->validateNumeric('text_size', 8, 72);
49 $this->options['watermark_margin'] = $this->validateNumeric('watermark_margin', 0, 200);
50 $this->options['watermark_diaphaneity'] = $this->validateNumeric('watermark_diaphaneity', 0, 100);
51 $this->options['watermark_min_width'] = $this->validateNumeric('watermark_min_width', 100, 9999);
52 $this->options['watermark_min_height'] = $this->validateNumeric('watermark_min_height', 100, 9999);
53
54 // Validate watermark position
55 $valid_positions = [
56 'top-left', 'top-center', 'top-right',
57 'middle-left', 'middle-center', 'middle-right',
58 'bottom-left', 'bottom-center', 'bottom-right'
59 ];
60 if (!in_array($this->options['watermark_position'], $valid_positions)) {
61 $this->options['watermark_position'] = self::$defaults['watermark_position'];
62 }
63
64 // Validate color
65 $this->options['text_color'] = $this->validateColor($this->options['text_color']);
66
67 // Validate text content
68 $this->options['text_content'] = sanitize_text_field($this->options['text_content']);
69
70 // Validate font file
71 if (!$this->validateFont($this->options['text_font'])) {
72 $this->options['text_font'] = self::$defaults['text_font'];
73 }
74
75 // Validate image URL if using image watermark
76 if ($this->options['watermark_type'] === 'image_watermark') {
77 $this->options['watermark_mark_image'] = $this->validateImageUrl($this->options['watermark_mark_image']);
78 }
79 }
80
81 /**
82 * Validate numeric value
83 */
84 private function validateNumeric($key, $min, $max) {
85 $value = intval($this->options[$key]);
86 return max($min, min($max, $value));
87 }
88
89 /**
90 * Validate color value
91 */
92 private function validateColor($color) {
93 if (preg_match('/^#[a-fA-F0-9]{6}$/', $color)) {
94 return $color;
95 }
96 return self::$defaults['text_color'];
97 }
98
99 /**
100 * Validate font file
101 */
102 private function validateFont($font) {
103 $font_path = plugin_dir_path(__FILE__) . 'fonts/' . $font;
104 return file_exists($font_path) && is_readable($font_path);
105 }
106
107 /**
108 * Validate image URL
109 */
110 private function validateImageUrl($url) {
111 if (empty($url)) {
112 return '';
113 }
114
115 // Basic URL validation
116 if (!filter_var($url, FILTER_VALIDATE_URL)) {
117 return '';
118 }
119
120 // Check if image exists and is accessible
121 $headers = get_headers($url, 1);
122 if (strpos($headers[0], '200') === false ||
123 !preg_match('/^image\/(jpeg|png|gif)/', $headers['Content-Type'])) {
124 return '';
125 }
126
127 return esc_url_raw($url);
128 }
129
130 /**
131 * Get all options
132 */
133 public function getOptions() {
134 return $this->options;
135 }
136
137 /**
138 * Get single option
139 */
140 public function getOption($key) {
141 return isset($this->options[$key]) ? $this->options[$key] : null;
142 }
143
144 /**
145 * Set single option
146 */
147 public function setOption($key, $value) {
148 if (array_key_exists($key, self::$defaults)) {
149 $this->options[$key] = $value;
150 $this->validateOptions();
151 }
152 }
153
154 /**
155 * Save options to WordPress
156 */
157 public function saveOptions() {
158 return update_option('wpwatermark_options', $this->options);
159 }
160
161 /**
162 * Load options from WordPress
163 */
164 public static function loadFromWordPress() {
165 $options = get_option('wpwatermark_options', []);
166 return new self($options);
167 }
168 }