PluginProbe
IMGspider – 图片采集抓取插件 / trunk
IMGspider – 图片采集抓取插件 vtrunk
trunk 1.0.0 1.0.1 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0 2.3.1 2.3.10 2.3.11 All 36 releases
imgspider / classes / conf.class.php

conf.class.php in IMGspider – 图片采集抓取插件 trunk, at classes/conf.class.php

524 lines 15.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 class WB_IMGSPY_Conf
5 {
6 public static $name = 'imgspider_pack';
7 public static $optionName = 'wb_imgspider_option';
8
9 public static $cnf_fields = array(
10 'filter' => array(
11 'type' => array(
12 array(
13 'code' => 'jpg',
14 'name' => 'jpg'
15 ),
16 array(
17 'code' => 'jpeg',
18 'name' => 'jpeg'
19 ),
20 array(
21 'code' => 'png',
22 'name' => 'png'
23 ),
24 array(
25 'code' => 'gif',
26 'name' => 'gif'
27 ),
28 array(
29 'code' => 'bmp',
30 'name' => 'bmp'
31 ),
32 array(
33 'code' => 'webp',
34 'name' => 'webp'
35 ),
36 )
37
38 )
39 );
40
41 public static function init()
42 {
43 if (is_admin()) {
44 add_action('admin_menu', array(__CLASS__, 'admin_menu'));
45 add_action('admin_enqueue_scripts', array(__CLASS__, 'admin_enqueue_scripts'), 1);
46 add_filter('plugin_action_links', array(__CLASS__, 'actionLinks'), 10, 2);
47 add_filter('plugin_row_meta', array(__CLASS__, 'plugin_row_meta'), 10, 2);
48 add_action('admin_notices', array(__CLASS__, 'admin_notices'));
49 }
50 }
51
52
53 public static function admin_notices()
54 {
55 global $current_screen;
56 if (!current_user_can('update_plugins')) {
57 return;
58 }
59
60 if (!preg_match('#imgspider_pack#', $current_screen->base)) {
61 return;
62 }
63 $current = get_site_transient('update_plugins');
64 if (!$current) {
65 return;
66 }
67 $plugin_file = plugin_basename(IMGSPY_BASE_FILE);
68 if (!isset($current->response[$plugin_file])) {
69 return;
70 }
71 $all_plugins = get_plugins();
72 if (!$all_plugins || !isset($all_plugins[$plugin_file])) {
73 return;
74 }
75 $plugin_data = $all_plugins[$plugin_file];
76 $update = $current->response[$plugin_file];
77
78 //print_r($update);
79 $update_url = wp_nonce_url(self_admin_url('update.php?action=upgrade-plugin&plugin=') . $plugin_file, 'upgrade-plugin_' . $plugin_file);
80
81
82 echo '<div class="update-message notice inline notice-warning notice-alt"><p>'.esc_html($plugin_data['Name'].'有新版本可用。');
83 echo '<a href="'.esc_url($update->url).'" target="_blank" aria-label="'.esc_attr('查看'.$plugin_data['Name'].'版本'.$update->new_version.'详�
84 ').'">查看版本'.esc_html($update->new_version).'详�
85 </a>';
86 echo '<a href="'.esc_url($update_url).'" class="update-link" aria-label="'.esc_attr('现在更新'.$plugin_data['Name']).'">现在更新</a>';
87 echo '</p></div>';
88
89 }
90
91
92
93 public static function plugin_row_meta($links, $file)
94 {
95
96 $base = plugin_basename(IMGSPY_BASE_FILE);
97 if ($file == $base) {
98
99
100 $links[] = '<a href="https://www.wbolt.com/plugins/imgspider/">插件主页</a>';
101 $links[] = '<a href="https://www.wbolt.com/imgspider-plugin-documentation.html">FAQ</a>';
102 $links[] = '<a href="https://wordpress.org/support/plugin/imgspider/">反馈</a>';
103 }
104 return $links;
105 }
106
107 public static function actionLinks($links, $file)
108 {
109
110 if ($file != plugin_basename(IMGSPY_BASE_FILE))
111 return $links;
112 if (!get_option('wb_imgspider_ver', 0)) {
113 $a_link = '<a href="https://www.wbolt.com/plugins/imgspider-pro" target="_blank"><span style="color: #FCB214;">升至Pro版</span></a>';
114 array_unshift($links, $a_link);
115 }
116
117 $settings_link = '<a href="' . menu_page_url('imgspider_pack', false) . '">设置</a>';
118
119 array_unshift($links, $settings_link);
120
121 return $links;
122 }
123
124 public static function admin_menu()
125 {
126 add_options_page(
127 'IMGspider图片蜘蛛',
128 '图片蜘蛛',
129 'manage_options',
130 'imgspider_pack',
131 [__CLASS__, 'admin_settings']
132 );
133 }
134
135 public static function insert_assets()
136 {
137 $ver = rawurlencode(IMGSPY_VERSION);
138 echo '<link rel="stylesheet" href="' . esc_url(IMGSPY_URI . 'setting/vendor/wbs-lab-base.css?ver=' . $ver) . '">' . "\n";
139 echo '<link rel="stylesheet" href="' . esc_url(IMGSPY_URI . 'setting/vendor/element-plus.css?ver=' . $ver) . '">' . "\n";
140 echo IMGSPY_Vite::vite(
141 'src/main.js',
142 IMGSPY_PATH . '/setting/assets/',
143 IMGSPY_URI . 'setting/assets/'
144 );
145 }
146
147 public static function admin_enqueue_scripts($hook)
148 {
149 if (!preg_match('#imgspider_pack#', $hook)) {
150 return;
151 }
152
153 $prompt_items = array();
154 if (file_exists(__DIR__ . '/_prompt.php')) {
155 include __DIR__ . '/_prompt.php';
156 }
157
158 wp_register_script('wbp-imgscrapy', false, null, false);
159 wp_enqueue_script('wbp-imgscrapy');
160
161 wp_enqueue_media();
162
163 // wp_enqueue_script('wbp-imgscrapy-js-vendors', IMGSPY_URI . 'setting/assets/js/chunk-vendors.js', array(), IMGSPY_VERSION, true);
164 // wp_enqueue_script('wbp-imgscrapy-js', IMGSPY_URI . 'setting/assets/js/app.js', array('wbp-imgscrapy-js-vendors'), IMGSPY_VERSION, true);
165 // wp_enqueue_style('wbs-style-imgscrapy', IMGSPY_URI . 'assets/wbp_setting.css', array(), IMGSPY_VERSION);
166 // wp_enqueue_style('wbs-style-imgscrapy-vendors', IMGSPY_URI . 'setting/assets/css/chunk-vendors.css', array(), IMGSPY_VERSION);
167
168 global $wp_post_types;
169
170 $post_types = array();
171 if ($wp_post_types && is_array($wp_post_types)) foreach ($wp_post_types as $type) {
172 if ($type->public) {
173 $post_types[$type->name] = $type->labels->name;
174 }
175 }
176
177 $ajax_nonce = wp_create_nonce('wp_ajax_wb_imgspider');
178 $imgspider_ver = intval(get_option('wb_imgspider_ver', 0));
179
180 $editor = WB_IMGSPY_Image::get_image_editor(null, null);
181 $wb_cnf = array(
182 'base_url' => admin_url(),
183 'ajax_url' => admin_url('admin-ajax.php'),
184 'dir_url' => IMGSPY_URI,
185 'pd_code' => IMGSPY_CODE,
186 'pd_title' => 'IMGspider-图片蜘蛛',
187 'pd_version' => IMGSPY_VERSION,
188 'locale' => get_locale(),
189 'is_pro' => $imgspider_ver,
190 'doc_url' => 'https://www.wbolt.com/imgspider-plugin-documentation.html',
191 'action' => array(
192 'act' => 'wb_scrapy_image',
193 'fetch' => 'get_setting',
194 'push' => 'set_setting'
195 ),
196 'watermark' => preg_match('/Imagick/i', $editor),
197 'watermark_preview' => '',
198 'prompt' => $prompt_items,
199 'supports_avif' => WB_IMGSPY_Post::supports_avif(),
200 'supports_webp' => function_exists('wp_image_editor_supports') && wp_image_editor_supports(array('mime_type' => 'image/webp')),
201 'cdn_presets' => array(
202 'alicdn.com',
203 'aliyuncs.com',
204 'qiniucdn.com',
205 'myqcloud.com',
206 'upyun.com',
207 'byteimg.com',
208 'zhimg.com',
209 ),
210 );
211
212 $category = array_column(get_terms(['taxonomy' => 'category', 'parent' => 0,]), 'name', 'term_id');
213 $post_status = get_post_statuses();
214 $inline_script = 'var wb_ajaxurl="' . admin_url('admin-ajax.php') . '",
215 wb_vue_path="' . IMGSPY_URI . 'setting/",
216 imgspider_ver=' . $imgspider_ver . ',
217 wb_cnf=' . wp_json_encode($wb_cnf) . ',
218 post_types=' . wp_json_encode($post_types, JSON_UNESCAPED_UNICODE) . ',
219 post_status=' . wp_json_encode($post_status, JSON_UNESCAPED_UNICODE) . ',
220 category=' . wp_json_encode($category, JSON_UNESCAPED_UNICODE) . ',
221 _wb_imgspider_ajax_nonce="' . $ajax_nonce . '";';
222
223 wp_add_inline_script('wbp-imgscrapy', $inline_script, 'before');
224
225 wp_enqueue_style('wbs-style-imgscrapy-font', IMGSPY_URI . 'assets/font-face.css', array(), IMGSPY_VERSION);
226 add_filter('style_loader_tag', function ($tag, $handle, $href, $media) {
227 if (!preg_match('#^vue-#', $media)) {
228 return $tag;
229 }
230
231 $media = htmlspecialchars_decode($media);
232 $r = [];
233 parse_str(str_replace('vue-', '', $media), $r);
234 $rel = '';
235 $attr = [];
236 if ($r && is_array($r)) {
237 if (isset($r['rel'])) {
238 $rel = $r['rel'];
239 unset($r['rel']);
240 }
241 foreach ($r as $attr_k => $attr_v) {
242 $attr[] = sprintf('%s="%s"', $attr_k, esc_attr($attr_v));
243 }
244 }
245
246 $tag = sprintf(
247 '<link href="%s" rel="%s" %s/>' . "\n",
248 $href,
249 $rel,
250 implode(" ", $attr)
251 );
252 return $tag;
253 }, 10, 4);
254 add_filter('script_loader_tag', function ($tag, $handle, $src) {
255 if (!preg_match('#-vue-js-#', $handle)) {
256 return $tag;
257 }
258 $parts = explode('?', $src, 2);
259 $src = $parts[0];
260 $type = '';
261 $attr = '';
262 if (isset($parts[1])) {
263 $r = [];
264 parse_str(htmlspecialchars_decode($parts[1]), $r);
265 if ($r) {
266 if (isset($r['type'])) {
267 $type = sprintf(' type="%s"', esc_attr($r['type']));
268 unset($r['type']);
269 }
270 $attr_txt = '';
271 if (isset($r['attr'])) {
272 $attr_txt = $r['attr'];
273 unset($r['attr']);
274 }
275 foreach ($r as $k => $v) {
276 $attr .= sprintf(' %s="%s"', $k, esc_attr($v));
277 }
278 if ($attr_txt) {
279 $attr .= sprintf(' %s', esc_attr($attr_txt));
280 }
281 }
282 }
283
284 $tag = sprintf('<script%s src="%s"%s id="%s-js"></script>' . "\n", $type, $src, $attr, $handle);
285 return $tag;
286 }, 10, 3);
287 self::insert_assets();
288 }
289
290 public static function get_proxy()
291 {
292
293 $cnf = WB_IMGSPY_Conf::opt();
294
295 if (!isset($cnf['df_mode'])) {
296 return false;
297 }
298 if (in_array($cnf['df_mode'], array('none', 'proxy', 'ext'))) {
299 return false;
300 }
301 $proxy_type = $cnf['df_mode'];
302
303
304 /*if(!isset($cnf['proxy']) || empty($cnf['proxy']) || !is_array($cnf['proxy'])){
305 return false;
306 }
307
308
309 $proxy_type = '';
310
311 foreach($cnf['proxy'] as $type=>$active){
312 if($active){
313 $proxy_type = $type;
314 break;
315 }
316 }*/
317
318 if (!$proxy_type) {
319 return false;
320 }
321
322 /*if($proxy_type=='wb'){
323 return array('type'=>$proxy_type);
324 }*/
325
326
327 if (!isset($cnf['proxy_manual']) || empty($cnf['proxy_manual']) || !is_array($cnf['proxy_manual'])) {
328 return false;
329 }
330
331 if (!isset($cnf['proxy_manual'][$proxy_type])) {
332 return false;
333 }
334
335 $ret = $cnf['proxy_manual'][$proxy_type];
336 $ret['type'] = 'ip';
337
338 return $ret;
339 }
340
341 public static function update_cnf($opt_data)
342 {
343
344 if (!empty($opt_data['filter']['domain'])) {
345 $filter_domain = trim(str_replace("\r\n", "\n", sanitize_textarea_field($opt_data['filter']['domain'])));
346 $filter_domain = explode("\n", $filter_domain);
347 $domain = [];
348 foreach ($filter_domain as $v) {
349 $v = trim($v);
350 if (!$v) continue;
351 $domain[] = $v;
352 }
353 $opt_data['filter']['domain'] = $domain;
354 } else {
355 $opt_data['filter']['domain'] = [];
356 }
357 if (!empty($opt_data['filter']['type'])) {
358 $filter_type = explode(',', trim($opt_data['filter']['type']));
359 $types = [];
360 foreach ($filter_type as $v) {
361 $v = trim($v);
362 if (!$v) continue;
363 $types[$v] = '1';
364 }
365 } else {
366 $opt_data['filter']['type'] = [];
367 }
368 if (isset($opt_data['convert_webp'])) {
369 $opt_data['convert_webp'] = (int) $opt_data['convert_webp'] ? 1 : 0;
370 }
371 if (isset($opt_data['allow_apng'])) {
372 $opt_data['allow_apng'] = (int) $opt_data['allow_apng'] ? 1 : 0;
373 }
374 if (isset($opt_data['auto']) && is_array($opt_data['auto'])) {
375 $opt_data['auto']['max_per_run'] = max(1, min(100, absint(isset($opt_data['auto']['max_per_run']) ? $opt_data['auto']['max_per_run'] : 20)));
376 $opt_data['auto']['max_per_post'] = max(1, min(200, absint(isset($opt_data['auto']['max_per_post']) ? $opt_data['auto']['max_per_post'] : 30)));
377 $opt_data['auto']['fail_cool_hours'] = max(1, min(168, absint(isset($opt_data['auto']['fail_cool_hours']) ? $opt_data['auto']['fail_cool_hours'] : 24)));
378 }
379
380 update_option(self::$optionName, $opt_data, false);
381 }
382
383 public static function opt()
384 {
385
386 static $opt = null;
387 if ($opt) {
388 return $opt;
389 }
390 $def = array(
391 'switch' => '1',
392 'mode' => '1',
393 'df_mode' => 'none',
394 'proxy' => array(
395 'wb' => 0,
396 ),
397 'proxy_manual' => array(
398 //array('name'=>'test','ip'=>'127.0.0.1','port'=>'1080','user'=>'','pwd'=>'')
399 ),
400
401 /*
402 'proxy_type'=>'none',
403 'proxy_ip'=>'',
404 'proxy_port'=>'',
405 'proxy_user'=>'',
406 'proxy_pwd'=>'',*/
407
408 'del_src_url' => 0,
409 'thumbnail' => 0, //第一张图作为缩略图
410 'convert_webp' => 0,
411 'allow_apng' => 1,
412 'auto' => array(
413 'max_per_run' => 20,
414 'max_per_post' => 30,
415 'fail_cool_hours' => 24,
416 ),
417 'rule' => array(
418 'size' => 0,
419 'custom_size' => '',
420 'file_name' => 0,
421 'custom_name' => '',
422 'title_alt' => 0,
423 'custom_title' => '',
424 'align' => 'none'
425 ),
426
427 'filter' => array(
428 'except_index' => '',
429 'min_width' => '',
430 'domain' => [],
431 'type' => []
432 ),
433 'watermark' => array(
434 'type' => 0,
435 'image' => '',
436 'text' => '',
437 'font' => '',
438 'font_file' => '',
439 'size' => 32,
440 'color' => '',
441 'alpha' => 30,
442 'pos' => 1,
443 'x' => '',
444 'y' => '',
445 'apply' => 'new',
446 'min_width' => '700',
447 'min_height' => '500',
448 ),
449 );
450 $opt = get_option(self::$optionName, array());
451 foreach ($def as $k => $v) {
452 if (!isset($opt[$k])) {
453 $opt[$k] = $v;
454 } else {
455 if (is_array($v)) foreach ($v as $sk => $sv) {
456 if (!isset($opt[$k][$sk])) {
457 $opt[$k][$sk] = $sv;
458 }
459 }
460 }
461 }
462 foreach (['size', 'file_name', 'title_alt'] as $f) {
463 if (!isset($opt['rule'])) break;
464 if (isset($opt['rule'][$f])) {
465 $opt['rule'][$f] = intval($opt['rule'][$f]);
466 }
467 }
468 foreach (['type', 'size', 'alpha', 'pos'] as $f) {
469 if (!isset($opt['watermark'])) break;
470 if (isset($opt['watermark'][$f])) {
471 $opt['watermark'][$f] = intval($opt['watermark'][$f]);
472 }
473 }
474 foreach (['max_per_run', 'max_per_post', 'fail_cool_hours'] as $f) {
475 if (!isset($opt['auto'])) {
476 break;
477 }
478 if (isset($opt['auto'][$f])) {
479 $opt['auto'][$f] = absint($opt['auto'][$f]);
480 }
481 }
482 $opt['convert_webp'] = isset($opt['convert_webp']) ? (string) intval($opt['convert_webp']) : '0';
483 $opt['allow_apng'] = isset($opt['allow_apng']) ? (string) intval($opt['allow_apng']) : '1';
484
485 return apply_filters('wb_imgspy_cnf', $opt);
486 }
487
488 public static function cnf($key, $default = null)
489 {
490 static $option = array();
491 if (!$option) {
492 $option = self::opt();
493 }
494 $keys = explode('.', $key);
495 $find = false;
496 $cnf = $option;
497 foreach ($keys as $_k) {
498 if (isset($cnf[$_k])) {
499 $cnf = $cnf[$_k];
500 $find = true;
501 continue;
502 }
503 $find = false;
504 }
505 if ($find) {
506 return $cnf;
507 }
508
509 return $default;
510 }
511
512
513
514 public static function admin_settings()
515 {
516 echo '<div id="optionsframework-wrap">
517 <div id="app"></div>
518 <div style="display:none;">
519 <button type="button" id="wb-wbsm-btn-spy-batch-ext"></button>
520 </div>
521 </div>';
522 }
523 }
524