PluginProbe
WPJAM Basic / trunk
WPJAM Basic vtrunk
wpjam-basic / public / wpjam-compat.php

wpjam-compat.php in WPJAM Basic trunk, at public/wpjam-compat.php

1,569 lines 43.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 class_alias('WPJAM_Option_Items', 'WPJAM_Option');
3 class_alias('WPJAM_Items', 'WPJAM_Item');
4 class_alias('WPJAM_Post', 'WPJAM_PostType');
5 class_alias('WPJAM_Cache_Items', 'WPJAM_List_Cache');
6 class_alias('WPJAM_Cache_Items', 'WPJAM_ListCache');
7 class_alias('WPJAM_Cache', 'WPJAM_Cache_Group');
8 class_alias('WPJAM_Crypt', 'WPJAM_OPENSSL_Crypt');
9 class_alias('WPJAM_Option_Setting', 'WPJAM_Setting');
10
11 if(!function_exists('function_alias')){
12 function function_alias($original, ...$args){
13 foreach($args as $alias){
14 if(!function_exists($alias)){
15 eval('function '.$alias.'(...$args){
16 return function_exists(\''.$original.'\') ? '.$original.'(...$args) : null;
17 }');
18 }
19 }
20
21 return true;
22 }
23 }
24
25 if(!function_exists('clamp')){
26 function clamp($value, $min, $max){
27 return min(max($value, $min), $max);
28 }
29 }
30
31 if(!function_exists('wp_hash')){
32 function wp_hash($data, $scheme='auth', $algo='md5') {
33 return hash_hmac($algo, $data, wp_salt($scheme));
34 }
35 }
36
37 if(!function_exists('wp_cache_get_salted')){
38 function wp_cache_get_salted($key, $group, $salt){
39 $salt = is_array($salt) ? implode(':', $salt) : $salt;
40 $cache = wp_cache_get($key, $group);
41
42 return (!is_array($cache) || !isset($cache['salt'], $cache['data']) || $salt !== $cache['salt']) ? false : $cache['data'];
43 }
44 }
45
46 if(!function_exists('wp_cache_set_salted')){
47 function wp_cache_set_salted($key, $data, $group, $salt, $expire=0){
48 $salt = is_array($salt) ? implode(':', $salt) : $salt;
49
50 return wp_cache_set($key, ['data'=>$data, 'salt'=>$salt], $group, $expire);
51 }
52 }
53
54 if(!function_exists('wp_cache_get_with_cas')){
55 function wp_cache_get_with_cas($key, $group='', &$token=null){
56 return wp_cache_get($key, $group);
57 }
58 }
59
60 if(!function_exists('wp_cache_cas')){
61 function wp_cache_cas($cas_token, $key, $data, $group='', $expire=0){
62 return wp_cache_set($key, $data, $group, $expire);
63 }
64 }
65
66 if(!function_exists('array_find_index')){
67 function array_find_index($arr, $callback){
68 return wpjam_find($arr, $callback, 'index');
69 }
70 }
71
72 if(!function_exists('mb_strwidth')){
73 function mb_strwidth($str){
74 preg_match_all('/./us', $str, $match);
75
76 return array_sum(wpjam_map($match[0], fn($char)=> ord($char) >=224 ? 2 : 1));
77 }
78 }
79
80 if(!function_exists('mb_strimwidth')){
81 function mb_strimwidth($str, $start, $width, $trimmarker=''){
82 preg_match_all('/./us', $str, $match);
83
84 $count = count($match[0]);
85 $start = $start < 0 ? $count-1+$start : $start;
86 $chars = array_slice($match[0], $start);
87
88 if($width >= array_sum(wpjam_map($chars, fn($char)=> ord($char) >=224 ? 2 : 1))){
89 return implode('', $chars);
90 }
91
92 $length = 0;
93 $result = '';
94 $count -= $start;
95 $width -= strlen($trimmarker);
96
97 for($i=0; $i<$count; $i++){
98 $char = $chars[$i];
99 $w = ord($char) >= 224 ? 2 : 1;
100
101 if($length + $w > $width){
102 break;
103 }
104
105 $result .= $char;
106 $length += $w;
107 }
108
109 return $result.$trimmarker;
110 }
111 }
112
113 if(!function_exists('user_can_for_blog')){
114 function user_can_for_blog($user, $blog_id, $capability, ...$args){
115 return wpjam_call_for_blog($blog_id, 'user_can', $user, $capability, ...$args);
116 }
117 }
118
119 if(!function_exists('get_metadata_by_value')){
120 function get_metadata_by_value($type, $value, $key=''){
121 return ($data = wpjam_get_by_meta($type, $key, $value)) ? (object)array_first($data) : false;
122 }
123 }
124
125 if(!function_exists('update_usermeta_cache')){
126 function update_usermeta_cache($ids){
127 return update_meta_cache('user', $ids);
128 }
129 }
130
131 if(!function_exists('filter_deep')){
132 function filter_deep($arr, $cb){
133 return wpjam_filter($arr, $cb, true);
134 }
135 }
136
137 if(!function_exists('filter_null')){
138 function filter_null($arr, $deep=false){
139 return wpjam_filter($arr, fn($v)=> !is_null($v), $deep);
140 }
141 }
142
143 if(!function_exists('filter_blank')){
144 function filter_blank($arr, $deep=false){
145 return wpjam_filter($arr, fn($v)=> $v || is_numeric($v), $deep);
146 }
147 }
148
149 if(!function_exists('is_blank')){
150 function is_blank($v){
151 return !$v && !is_numeric($v);
152 }
153 }
154
155 if(!function_exists('is_populated')){
156 function is_populated($v){
157 return !is_blank($v);
158 }
159 }
160
161 if(!function_exists('is_exists')){
162 function is_exists($v){
163 return isset($v);
164 }
165 }
166
167 function wpjam_bind($cb, $args){
168 return is_closure($cb) ? $cb->bindTo(...array_fill(0, 2, is_object($args) ? $args : wpjam_args($args))) : $cb;
169 }
170
171 function wpjam_timer($cb, ...$args){
172 try{
173 $timestart = microtime(true);
174
175 return $cb(...$args);
176 }finally{
177 $log[] = "Callback: ".var_export($cb, true);
178 $log[] = "Time: ".number_format(microtime(true)-$timestart, 5);
179
180 if(is_closure($cb)){
181 $log[] = "File: ".wpjam_get_reflection($cb, 'FileName');
182 $log[] = "Line: ".wpjam_get_reflection($cb, 'StartLine');
183 }
184
185 trigger_error(implode("\n", $log)."\n\n");
186 }
187 }
188
189 function wpjam_timer_hook($value){
190 $name = current_filter();
191 $object = $GLOBALS['wp_filter'][$name] ?? null;
192
193 if($object){
194 foreach($object->callbacks as &$hooks){
195 foreach($hooks as &$hook){
196 $hook['function'] = fn(...$args)=> wpjam_timer($hook['function'], ...$args);
197 }
198 }
199 }
200
201 return $value;
202 }
203
204 function wpjam_parse_query($query, $args=[], $parse=true){
205 return wpjam_query($parse, $query, $args);
206 }
207
208 function wpjam_render_query($query, $args=[]){
209 return wpjam_query('render', $query, $args);
210 }
211
212 function wpjam_register_config(...$args){
213 return wpjam_config(...$args);
214 }
215
216 function wpjam_get_config($group=''){
217 return wpjam_config($group);
218 }
219
220 function wpjam_value($model, $name, ...$args){
221 return is_string($model) ? wpjam_call_method($model.'::get_'.$name, ...$args) : wpjam_value_callback($model, $name);
222 }
223
224 function wpjam_dynamic_method($class, $name, ...$args){
225 if($class && $name){
226 $group = 'dynamic_method';
227 $key = $class.'['.$name.']';
228
229 if(!$args){
230 return wpjam($group, $key) ?? wpjam_dynamic_method(get_parent_class($class), $name);
231 }
232
233 (!$args[0] || is_closure($args[0])) && wpjam($group.'[]', $key, $args[0] ?: null);
234 }
235 }
236
237 function wpjam_parse_method($model, $method, &$args=[]){
238 $cb = [$model, $method];
239
240 try{
241 [$cb, $args] = wpjam_callback($cb, $args);
242
243 return $cb;
244 }catch(Exception $e){
245 return wpjam_catch($e);
246 }
247 }
248
249 function wpjam_build_callback_unique_id($cb){
250 return wpjam_callback('uniqid', $cb);
251 }
252
253 function wpjam_die_if_error($result){
254 return wpjam_if_error($result, 'die');
255 }
256
257 function wpjam_throw_if_error($result){
258 return wpjam_if_error($result, 'throw');
259 }
260
261 function wpjam_remove_prefix($str, $fix){
262 return wpjam_prefix($str, '-', $fix);
263 }
264
265 function wpjam_remove_suffix($str, $fix){
266 return wpjam_suffix($str, '-', $fix);
267 }
268
269 if(!function_exists('try_remove_prefix')){
270 function try_remove_prefix(&$str, $fix){
271 return try_prefix($str, '-', $fix);
272 }
273 }
274
275 if(!function_exists('try_remove_suffix')){
276 function try_remove_suffix(&$str, $fix){
277 return try_suffix($str, '-', $fix);
278 }
279 }
280
281 function wpjam_remove_postfix($str, $postfix){
282 return wpjam_remove_suffix($str, $postfix);
283 }
284
285 function wpjam_get_plain_text($text){
286 return $text ? trim(preg_replace('/\s+/', ' ', str_replace(['"', '\'', "\r\n", "\n"], ['', '', ' ', ' '], wp_strip_all_tags($text)))) : $text;
287 }
288
289 function wpjam_toggle_url_scheme($url){
290 return WPJAM_CDN::scheme_replace($url);
291 }
292
293 function wpjam_hex2rgba($color, $opacity=null){
294 $color = wpjam_prefix($color, '-', '#');
295 $len = strlen($color);
296
297 if($len != 3 && $len != 6){
298 return $color;
299 }
300
301 $hex = array_map(fn($i)=> $len == 6 ? $color[$i*2].$color[$i*2+1] : $color[$i].$color[$i], [0, 1, 2]);
302 $rgb = implode(',', array_map('hexdec', $hex));
303 $rgb .= isset($opacity) ? ','.($opacity > 1 ? 1.0 : $opacity) : '';
304
305 return 'rgb('.$rgb.')';
306 }
307
308 function wpjam_get_current_priority($name=null){
309 $name = $name ?: current_filter();
310 $hook = $GLOBALS['wp_filter'][$name] ?? null;
311
312 return $hook ? $hook->current_priority() : null;
313 }
314
315 function wpjam_exception($errmsg, $errcode=null){
316 throw new WPJAM_Exception($errmsg, $errcode);
317 }
318
319 function wpjam_sum($items, $keys){
320 return array_reduce($items, fn($sum, $item)=> array_map(fn($k)=> $sum[$k]+(is_numeric($v = str_replace(',', '', ($item[$k] ?? 0))) ? $v : 0), $keys), array_fill_keys($keys, 0));
321 }
322
323 function wpjam_migrate_option($from, $to, $default=null){
324 if(get_option($to, $default) === $default){
325 $data = get_option($from) ?: [];
326 $data = array_merge($data, ['migrate_form'=>$from]);
327
328 update_option($to, $data);
329
330 delete_option($from);
331 }
332 }
333
334 function wpjam_get_verify_txt($name, $key=''){
335 return wpjam_txt($name, $key);
336 }
337
338 function wpjam_set_verify_txt($name, $data){
339 return wpjam_txt($name, $data);
340 }
341
342 function wpjam_generate_random_string($length){
343 return wp_generate_password($length, false);
344 }
345
346 function wpjam_http_request($url, $args=[], $err=[], &$headers=null){
347 $result = WPJAM_Http::request($url, $args, $err);
348
349 if(!is_wp_error($result)){
350 $headers = $result['headers'];
351 $result = $result['body'];
352 }
353
354 return $result;
355 }
356
357 function wpjam_generate_query_data($args, $type='data'){
358 return wpjam_get_parameter($args, [], $type);
359 }
360
361 function wpjam_register_extend_type($option, $dir, $args=[]){
362 return wpjam_load_extends($dir, array_merge($args, compact('option')));
363 }
364
365 function wpjam_is_module($module='', $action=''){
366 $current = wpjam_get_query_var('module');
367
368 return $module ? ($module == $current && (!$action || $action == wpjam_get_query_var('action'))) : (bool)$current;
369 }
370
371 function wpjam_get_current_module($wp=null){
372 return wpjam_get_query_var('module', $wp);
373 }
374
375 function wpjam_get_current_action($wp=null){
376 return wpjam_get_query_var('action', $wp);
377 }
378
379 function wpjam_is_webp_supported(){
380 return wpjam_ua('browser') == 'chrome' || wpjam_ua('os') == 'Android' || (wpjam_ua('os') == 'iOS' && version_compare(wpjam_ua('os_version'), 14) >= 0);
381 }
382
383 function wpjam_get_permastruct($name){
384 return $GLOBALS['wp_rewrite']->get_extra_permastruct($name);
385 }
386
387 function wpjam_set_permastruct($name, $value){
388 return $GLOBALS['wp_rewrite']->extra_permastructs[$name]['struct'] = $value;
389 }
390
391 function wpjam_get_items($group){
392 return wpjam($group);
393 }
394
395 function wpjam_get_item($group, $key){
396 return wpjam($group, $key);
397 }
398
399 function wpjam_add_item($group, $key, ...$args){
400 return wpjam($group.'[]', $key, ...$args);
401 }
402
403 function wpjam_get_current_var($name){
404 return wpjam_var($name);
405 }
406
407 function wpjam_set_current_var($name, $value){
408 return wpjam_var($name, $value);
409 }
410
411 function wpjam_set_current_user($user){
412 wpjam_var('user', $user);
413 }
414
415 function wpjam_get_current_commenter(){
416 $commenter = wp_get_current_commenter();
417
418 return empty($commenter['comment_author_email']) ? new WP_Error('access_denied') : $commenter;
419 }
420
421 function wpjam_get_instance($group, $id, $cb=null){
422 return wpjam('instance', $group.'.'.$id) ?? ($cb ? wpjam_add_instance($group, $id, $cb($id)) : null);
423 }
424
425 function wpjam_add_instance($group, $id, $object){
426 !is_wp_error($object) && !is_null($object) && wpjam('instance', $group.'.'.$id, $object);
427
428 return $object;
429 }
430
431 function wpjam_localize_script($handle, $name, $l10n ){
432 wp_localize_script($handle, $name, ['l10n_print_after' => $name.' = '.wpjam_json_encode($l10n)]);
433 }
434
435 function wpjam_wrap_tag($text, $tag='', $attr=[]){
436 return wpjam_tag($tag, $attr, $text);
437 }
438
439 function wpjam_ajax_enqueue_scripts(){
440 wp_enqueue_script('wpjam-ajax');
441 }
442
443 function wpjam_sanitize_option_value($value){
444 return WPJAM_Option_Setting::sanitize_option($value);
445 }
446
447 function wpjam_strip_data_type($args){
448 return array_diff_key($args, wpjam_admin('data_type', $args));
449 }
450
451 function wpjam_parse_data_type($args, $output='args'){
452 return wpjam_admin('data_type', $args, $output);
453 }
454
455 function wpjam_slice_data_type(&$args, $strip=false){
456 $result = wpjam_admin('data_type', $args);
457 $args = ($strip && $result) ? wpjam_except($args, wpjam_pull($args, 'data_type')) : $args;
458
459 return $result;
460 }
461
462 function wpjam_option($name, ...$args){
463 return WPJAM_Option_Setting::get_instance($name, ...$args);
464 }
465
466 function wpjam_get_option_object($option, $by=''){
467 return WPJAM_Option_Setting::get($option, $by);
468 }
469
470 function wpjam_get_option_setting($option){
471 return wpjam_get_option_object($option)->to_array();
472 }
473
474 function wpjam_option_get_setting($option, $setting='', ...$args){
475 return wpjam_get_setting($option, $setting, ...$args);
476 }
477
478 function wpjam_option_update_setting($option, $setting, $value){
479 return wpjam_update_setting($option, $setting, $value);
480 }
481
482 function wpjam_add_option_section_fields($option, $id, $fields){
483 return wpjam_add_option_section($option, $id, $fields);
484 }
485
486 function wpjam_get_admin_prefix(){
487 return wpjam_admin('prefix');
488 }
489
490 function wpjam_get_page_summary($type='page'){
491 return get_screen_option($type.'_summary');
492 }
493
494 function wpjam_set_page_summary($summary, $type='page', $append=true){
495 add_screen_option($type.'_summary', ($append ? get_screen_option($type.'_summary') : '').$summary);
496 }
497
498 function wpjam_call_list_table_model_method($method, ...$args){
499 return;
500 }
501
502 function wpjam_get_referer(){
503 return remove_query_arg([...wp_removable_query_args(), '_wp_http_referer', 'action', 'action2', '_wpnonce'], wp_get_original_referer() ?: wp_get_referer());
504 }
505
506 function wpjam_admin_tooltip($text, $tooltip){
507 return $text ? '<span class="tooltip" data-tooltip="'.esc_attr($tooltip).'">'.$text.'</span>' : '<span class="dashicons dashicons-editor-help tooltip" data-tooltip="'.esc_attr($tooltip).'"></span>';
508 }
509
510 function wpjam_add_admin_inline_script($data){
511 wpjam_script($data);
512 }
513
514 function wpjam_add_admin_inline_style($data){
515 wpjam_style($data);
516 }
517
518 function wpjam_register_builtin_page_load(...$args){
519 $args = is_array($args[0]) ? $args[0] : $args[1];
520
521 return wpjam_add_admin_load(['type'=>'builtin_page']+$args);
522 }
523
524 function wpjam_register_plugin_page_load(...$args){
525 $args = is_array($args[0]) ? $args[0] : $args[1];
526
527 return wpjam_add_admin_load(['type'=>'plugin_page']+$args);
528 }
529
530 function wpjam_page_action_compat($data){
531 $cb = wpjam_get_filter_name($GLOBALS['plugin_page'], 'ajax_response');
532 $cb = is_callable($cb) ? $cb : wp_die('invalid_callback');
533 $result = wpjam_if_error($cb($data['page_action']), 'send');
534
535 wpjam_send_json(is_array($result) ? $result : []);
536 }
537
538 function wpjam_get_ajax_button($args, $type='button'){
539 if($name = wpjam_pull($args, 'action')){
540 $object = wpjam_get_page_action($name) ?: wpjam_register_page_action($name, $args);
541
542 return $type == 'form' ? $object->get_form() : $object->get_button($args);
543 }
544 }
545
546 function wpjam_get_ajax_form($args){
547 return wpjam_get_ajax_button($args, 'form');
548 }
549
550 function wpjam_ajax_button($args){
551 echo wpjam_get_ajax_button($args);
552 }
553
554 function wpjam_ajax_form($args){
555 echo wpjam_get_ajax_form($args);
556 }
557
558 function wpjam_get_nonce_action($key){
559 return ($GLOBALS['plugin_page'] ?? $GLOBALS['current_screen']->id).'-'.$key;
560 }
561
562 function wpjam_get_plugin_page_setting($key='', $tab=false){
563 if($key == 'query_data'){
564 return wpjam_admin('query_data');
565 }
566
567 if($object = wpjam_admin('plugin_page')){
568 [$tab, $default] = str_ends_with($key, '_name') ? [true, $object->menu_slug] : [$tab, null];
569
570 if($tab && $object->type == 'tab'){
571 $object = wpjam_admin('current_tab');
572
573 if(!$object){
574 return null;
575 }
576 }
577
578 return $key ? ($object->$key ?: $default) : $object->to_array();
579 }
580 }
581
582 function wpjam_get_current_tab_setting($key=''){
583 return wpjam_get_plugin_page_setting($key, true);
584 }
585
586 function wpjam_get_plugin_page_query_data(){
587 return wpjam_admin('query_data');
588 }
589
590 function wpjam_set_plugin_page_summary($summary, $append=true){
591 wpjam_set_page_summary($summary, 'page', $append);
592 }
593
594 function wpjam_set_builtin_page_summary($summary, $append=true){
595 wpjam_set_page_summary($summary, 'page', $append);
596 }
597
598 function wpjam_get_plugin_page_type(){
599 return wpjam_get_plugin_page_setting('function');
600 }
601
602 function wpjam_register_plugin_page_tab($name, $args){
603 return wpjam_add_menu_page(['tab_slug'=>$name]+$args);
604 }
605
606 function wpjam_get_list_table_setting($key){
607 return isset($GLOBALS['wpjam_list_table']) ? $GLOBALS['wpjam_list_table']->$key : null;
608 }
609
610 function wpjam_get_list_table_filter_link($filters, $title, $class=''){
611 return $GLOBALS['wpjam_list_table']->get_filter_link($filters, $title, $class);
612 }
613
614 function wpjam_get_list_table_row_action($name, $args=[]){
615 return $GLOBALS['wpjam_list_table']->get_action($name, $args);
616 }
617
618 function wpjam_render_list_table_column_items($id, $items, $args=[]){
619 return $GLOBALS['wpjam_list_table']->get_column_value($id, '', ['items'=>$items, 'args'=>$args]);
620 }
621
622 function wpjam_register_list_table($name, $args=[]){
623 add_filter(wpjam_get_filter_name($name, 'list_table'), fn()=> $args);
624 }
625
626 function wpjam_register_dashboard($name, $args){
627 add_action('wpjam_plugin_page', fn($object, $type)=> $type == 'dashboard' ? ($object->$type = $args) : null, 10, 2);
628 }
629
630 function wpjam_post($post, $wp_error=false){
631 return WPJAM_Post::get_instance($post, null, $wp_error);
632 }
633
634 function wpjam_term($term, $wp_error=false){
635 return WPJAM_Term::get_instance($term, null, $wp_error);
636 }
637
638 function wpjam_user($user, $wp_error=false){
639 return WPJAM_User::get_instance($user, $wp_error);
640 }
641
642 function wpjam_get_post_option_fields($post_type, $post_id=null){
643 return [];
644 }
645
646 function wpjam_get_term_level($term){
647 return get_term_level($term->term_id);
648 }
649
650 if(!function_exists('array_pulls')){
651 function array_pulls(&$array, $keys){
652 return wpjam_pull($array, $keys);
653 }
654 }
655
656 function wpjam_found($arr, $cb){
657 return wpjam_find($arr, true, $cb);
658 }
659
660 function wpjam_slice($arr, $keys){
661 return wpjam_filter($arr, $keys);
662 }
663
664 function wpjam_array_pull(&$array, $key, $default=null){
665 return array_pull($array, $key, $default);
666 }
667
668 function wpjam_array_except($array, ...$keys){
669 return array_except($array, ...$keys);
670 }
671
672 function wpjam_array_push(&$array, $data, $key=null){
673 $index = is_null($key) ? false : array_search($key, array_keys($array), true);
674 $array = $index !== false ? wpjam_add_at($array, $index, $data) : array_merge($array, $data);
675
676 return true;
677 }
678
679 function wpjam_list_sort($list, $orderby='order', $order='DESC'){
680 return wp_list_sort($list, $orderby, $order, true);
681 }
682
683 function wpjam_list_filter($list, $args=[], $operator='AND'){
684 return wpjam_filter($list, $args, $operator);
685 }
686
687 function wpjam_list_flatten($list, $args=[]){
688 $name = $args['name'] ?? 'name';
689 $key = $args['children'] ?? 'children';
690
691 return wpjam_reduce($list, fn($carry, $v, $k, $d)=> array_merge($carry, [array_merge($v, [$name=>str_repeat('&emsp;', $d).$v[$name]])]), [], $key);
692 }
693
694 //中文截取方式
695 function wpjam_mb_strimwidth($text, $start=0, $width=40, $trimmarker='...', $encoding='utf-8'){
696 $text = wpjam_get_plain_text($text);
697
698 return $text ? mb_strimwidth($text, $start, $width, $trimmarker, $encoding) : '';
699 }
700
701 function wpjam_parse_fields($fields, $flat=false){
702 return wpjam_fields($fields, $flat);
703 }
704
705 function wpjam_field_get_icon($name){
706 if($name == 'multiply'){
707 return '✖️';
708 }
709 }
710
711 function wpjam_form_field_tmpls(){
712 return;
713 }
714
715 function wpjam_urlencode_img_cn_name($img_url){
716 return $img_url;
717 }
718
719 function wpjam_image_hwstring($size){
720 return image_hwstring((int)($size['width']), (int)($size['height']));
721 }
722
723 function wpjam_get_taxonomy_levels($taxonomy){
724 return wpjam_get_taxonomy_setting($taxonomy, 'levels', 0);
725 }
726
727 function wpjam_get_taxonomy_fields($taxonomy){
728 $object = WPJAM_Taxonomy::get($taxonomy);
729
730 return $object ? $object->get_fields() : [];
731 }
732
733 function wpjam_get_json_object($name){
734 return WPJAM_JSON::get($name);
735 }
736
737 function wpjam_get_current_json($output='name'){
738 return wpjam_get_json($output);
739 }
740
741 function wpjam_is_json($json=''){
742 return ($name = wpjam_get_json()) ? ($json ? $name == $json : true) : false;
743 }
744
745 function wpjam_send_error_json($errcode, $errmsg=''){
746 wpjam_send_json(compact('errcode', 'errmsg'));
747 }
748
749 function wpjam_is_platform($name){
750 return (WPJAM_Platform::get($name))->verify();
751 }
752
753 function wpjam_get_current_platforms($output='names'){
754 return WPJAM_Platform::get_by(['path'=>true], $output);
755 }
756
757 function wpjam_get_platform_options($output='bit'){
758 return WPJAM_Platform::get_options($output);
759 }
760
761 function wpjam_has_path($platform, $page_key, $strict=false){
762 $object = WPJAM_Platform::get($platform);
763
764 return $object ? $object->has_path($page_key, $strict) : false;
765 }
766
767 function wpjam_get_platform_object($name){
768 return WPJAM_Platform::get($name);
769 }
770
771 function wpjam_get_tabbar_options($platform){
772 return wp_list_pluck(wpjam_get_tabbar($platform), 'text');
773 }
774
775 function wpjam_get_path_object($page_key){
776 return WPJAM_Path::get_instance($page_key);
777 }
778
779 function wpjam_get_paths($platform){
780 return WPJAM_Path::get_by(['platform'=>$platform]);
781 }
782
783 function wpjam_get_path_item_link_tag($parsed, $text){
784 if($parsed['type'] == 'none'){
785 return $text;
786 }elseif($parsed['type'] == 'external'){
787 return '<a href_type="web_view" href="'.$parsed['url'].'">'.$text.'</a>';
788 }elseif($parsed['type'] == 'web_view'){
789 return '<a href_type="web_view" href="'.$parsed['src'].'">'.$text.'</a>';
790 }elseif($parsed['type'] == 'mini_program'){
791 return '<a href_type="mini_program" href="'.$parsed['path'].'" appid="'.$parsed['appid'].'">'.$text.'</a>';
792 }elseif($parsed['type'] == 'contact'){
793 return '<a href_type="contact" href="" tips="'.$parsed['tips'].'">'.$text.'</a>';
794 }elseif($parsed['type'] == ''){
795 return '<a href_type="path" page_key="'.$parsed['page_key'].'" href="'.$parsed['path'].'">'.$text.'</a>';
796 }
797 }
798
799 function wpjam_get_paths_by_post_type(){}
800 function wpjam_get_paths_by_taxonomy(){}
801 function wpjam_generate_path(){}
802 function wpjam_render_path_item(){}
803
804 function wpjam_is_over($name, $max, $time, $group=false, $action='increment'){
805 $times = wp_cache_get($name, $group) ?: 0;
806
807 return ($times > $max) || ($action == 'increment' && wp_cache_set($name, $times+1, $group, ($max == $times && $time > 60) ? $time : 60) && false);
808 }
809
810 function wpjam_related_posts($args=[]){
811 echo wpjam_get_related_posts(null, $args, false);
812 }
813
814 function wpjam_new_posts($args=[]){
815 echo wpjam_get_new_posts($args);
816 }
817
818 function wpjam_top_viewd_posts($args=[]){
819 echo wpjam_get_top_viewd_posts($args);
820 }
821
822 function wpjam_get_post_type_fields($post_type){
823 $object = WPJAM_Post_Type::get($post_type);
824
825 return $object ? $object->get_fields() : [];
826 }
827
828 function wpjam_attachment_url_to_postid($url){
829 $id = wp_cache_get($url, 'attachment_url_to_postid');
830
831 if($id === false){
832 global $wpdb;
833
834 $path = str_replace(parse_url(wp_get_upload_dir()['baseurl'], PHP_URL_PATH).'/', '', parse_url($url, PHP_URL_PATH));
835 $id = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s", $path));
836
837 wp_cache_set($url, $id, 'attachment_url_to_postid', DAY_IN_SECONDS);
838 }
839
840 return (int)apply_filters('attachment_url_to_postid', $id, $url);
841 }
842
843 function wpjam_get_filesystem(){
844 if(empty($GLOBALS['wp_filesystem'])){
845 require_once(ABSPATH.'wp-admin/includes/file.php');
846
847 WP_Filesystem();
848 }
849
850 return $GLOBALS['wp_filesystem'];
851 }
852
853 function wpjam_get_attachment_value($id, $field='file'){
854 return wpjam_file($id, $field);
855 }
856
857 function wpjam_restore_attachment_file($id, $url=''){
858 $file = wpjam_file($id, 'file');
859
860 return $file && !file_exists($file) ? wpjam_file('restore', $id, $url) : $file;
861 }
862
863 function wpjam_accept_to_mime_types($accept){
864 return wpjam_file('mimes', $accept);
865 }
866
867 function wpjam_upload_bits($bits, $name, $media=true){
868 $upload = wpjam_upload($name, ['bits'=>$bits]);
869
870 return $media ? wpjam_file('attach', $upload['file'], $upload+['post_id'=>is_numeric($media) ? $media : 0]) : $upload;
871 }
872
873 function wpjam_parse_image_query($url){
874 return wpjam_image($url, 'query');
875 }
876
877 function wpjam_get_image_size($img){
878 return wpjam_image($img, 'size');
879 }
880
881 function wpjam_get_content_remote_image_url($img_url, $post_id=null){
882 return $img_url;
883 }
884
885 function wpjam_get_content_remote_img_url($img_url, $post_id=0){
886 return wpjam_get_content_remote_image_url($img_url, $post_id);
887 }
888
889 function wpjam_image_remote_method($img_url=''){
890 return '';
891 }
892
893 function wpjam_is_remote_image($img_url, $strict=true){
894 return $strict ? !wpjam_is_cdn_url($img_url) : wpjam_is_external_url($img_url);
895 }
896
897 function wpjam_get_content_width(){
898 return (int)wpjam_cdn_get_setting('width');
899 }
900
901 function wpjam_cdn_content($content){
902 return wpjam_content_images($content);
903 }
904
905 function wpjam_content_images($content){
906 return WPJAM_CDN::filter_content($content);
907 }
908
909 function wpjam_bit($bit=0){
910 return new WPJAM_Bit($bit);
911 }
912
913 function wpjam_get_post_image_url($image_id, $size='full'){
914 $thumb = wp_get_attachment_image_src($image_id, $size);
915
916 return $thumb ? $thumb[0] : false;
917 }
918
919 function wpjam_has_post_thumbnail(){
920 return wpjam_get_post_thumbnail_url() ? true : false;
921 }
922
923 function wpjam_post_thumbnail($size='thumbnail', $crop=1, $class='wp-post-image', $ratio=2){
924 echo wpjam_get_post_thumbnail(null, $size, $crop, $class, $ratio);
925 }
926
927 function wpjam_get_post_thumbnail($post=null, $size='thumbnail', $crop=1, $class='wp-post-image', $ratio=2){
928 $size = wpjam_parse_size($size, $ratio);
929 if($post_thumbnail_url = wpjam_get_post_thumbnail_url($post, $size, $crop)){
930 $image_hwstring = image_hwstring($size['width']/$ratio, $size['height']/$ratio);
931 return '<img src="'.$post_thumbnail_url.'" alt="'.the_title_attribute(['echo'=>false]).'" class="'.$class.'"'.$image_hwstring.' />';
932 }else{
933 return '';
934 }
935 }
936
937 function wpjam_has_term_thumbnail(){
938 return wpjam_get_term_thumbnail_url() ? true : false;
939 }
940
941 function wpjam_term_thumbnail($size='thumbnail', $crop=1, $class="wp-term-image", $ratio=2){
942 echo wpjam_get_term_thumbnail(null, $size, $crop, $class);
943 }
944
945 function wpjam_get_term_thumbnail($term=null, $size='thumbnail', $crop=1, $class="wp-term-image", $ratio=2){
946 $size = wpjam_parse_size($size, $ratio);
947
948 if($term_thumbnail_url = wpjam_get_term_thumbnail_url($term, $size, $crop)){
949 $image_hwstring = image_hwstring($size['width']/$ratio, $size['height']/$ratio);
950
951 return '<img src="'.$term_thumbnail_url.'" class="'.$class.'"'.$image_hwstring.' />';
952 }else{
953 return '';
954 }
955 }
956
957 function wpjam_register_bind($type, $appid, $model=''){
958 return WPJAM_Bind::get($type.':'.$appid) ?: WPJAM_Bind::register($model ? new $model($appid) : new WPJAM_Bind($type, $appid));
959 }
960
961 function wpjam_parse_field_value($field, $args=[]){
962 return wpjam_field($field)->value_callback($args);
963 }
964
965 function wpjam_get_field_value($field, $args=[]){
966 return wpjam_parse_field_value($field, $args);
967 }
968
969 function wpjam_get_form_fields($admin_column=false){
970 return [];
971 }
972
973 function wpjam_validate_fields_value($fields, $values=null){
974 return wpjam_fields($fields)->validate($values);
975 }
976
977 function wpjam_validate_field_value($field, $value){
978 return wpjam_field($field)->validate($value);
979 }
980
981 function wpjam_prepare_fields_value($fields, $args=[]){
982 return wpjam_fields($fields)->prepare($args);
983 }
984
985 function wpjam_get_fields_defaults($fields){
986 return wpjam_fields($fields)->get_defaults();
987 }
988
989 function wpjam_get_form_post($fields, $nonce_action='', $capability='manage_options'){
990 check_admin_referer($nonce_action);
991
992 if(!current_user_can($capability)){
993 ob_clean();
994 wp_die('无权限');
995 }
996
997 return wpjam_validate_fields_value($fields);
998 }
999
1000 function wpjam_line_chart($data, $labels, $args=[]){
1001 echo wpjam_chart('line', $data, ['labels'=>$labels]+$args);
1002 }
1003
1004 function wpjam_bar_chart($data, $labels, $args=[]){
1005 echo wpjam_chart('bar', $data, ['labels'=>$labels]+$args);
1006 }
1007
1008 function wpjam_donut_chart($data, ...$args){
1009 echo wpjam_chart('donut', $data, (count($args) >= 2 ? ['labels'=>array_shift($args)] : [])+($args[0] ?? []));
1010 }
1011
1012 function wpjam_get_chart_parameter(...$args){
1013 return wpjam_chart(...$args);
1014 }
1015
1016 function wpjam_method_allow($method){
1017 return ($m = $_SERVER['REQUEST_METHOD']) == strtoupper($method) ? true : wp_die('method_not_allow', '接口不支持 '.$m.' 方法,请使用 '.$method.' 方法!');
1018 }
1019
1020 function wpjam_get_parameter($name='', $args=[], $method=''){
1021 $args = is_string($args) ? ['method'=>$args] : array_filter(['method'=>$method])+$args;
1022
1023 if($name){ // �
1024 �容
1025 if($type = $args['type'] ?? ''){
1026 $args = ['type'=>$type == 'int' ? 'number' : $type]+$args;
1027 }
1028
1029 if($fallback = wpjam_pull($args, 'fallback')){
1030 $default = wpjam_get_parameter($fallback, $method);
1031 $args = (isset($default) ? ['default'=>$default] : [])+$args;
1032 }
1033 }
1034
1035 return wpjam_param($name, $args);
1036 }
1037
1038 function wpjam_get_post_parameter($name='', $args=[]){
1039 return wpjam_get_parameter($name, $args, 'POST');
1040 }
1041
1042 function wpjam_get_data_parameter($name='', $args=[]){
1043 return wpjam_get_parameter($name, $args, 'data');
1044 }
1045
1046 function wpjam_get_request_parameter($name='', $args=[]){
1047 return wpjam_get_parameter($name, $args, 'REQUEST');
1048 }
1049
1050 function wpjam_stats_header($args=[]){
1051 global $wpjam_stats_labels;
1052
1053 $wpjam_stats_labels = [];
1054
1055 echo (WPJAM_Chart::get_instance($args))->render();
1056
1057 // do_action('wpjam_stats_header');
1058
1059 foreach(['start_date', 'start_timestamp', 'end_date', 'end_timestamp', 'date', 'timestamp', 'start_date_2', 'start_timestamp_2', 'end_date_2', 'end_timestamp_2', 'date_type', 'date_format', 'compare'] as $key){
1060 $wpjam_stats_labels['wpjam_'.$key] = wpjam_get_chart_parameter($key, $args);
1061 }
1062
1063 $wpjam_stats_labels['compare_label'] = $wpjam_stats_labels['wpjam_start_date'].' '.$wpjam_stats_labels['wpjam_end_date'];
1064 $wpjam_stats_labels['compare_label_2'] = $wpjam_stats_labels['wpjam_start_date_2'].' '.$wpjam_stats_labels['wpjam_end_date_2'];
1065 }
1066
1067 function wpjam_sub_summary($tabs){}
1068
1069 function wpjam_register_theme_upgrader(){}
1070
1071 function wpjam_register_plugin_updater($hostname, $url){
1072 return wpjam_updater('plugin', $hostname, $url);
1073 }
1074
1075 function wpjam_register_theme_updater($hostname, $url){
1076 return wpjam_updater('theme', $hostname, $url);
1077 }
1078
1079 function wpjam_data_attribute_string($attr){
1080 return wpjam_attr($attr, 'data');
1081 }
1082
1083 function wpjam_parse_attr($attr){
1084 return WPJAM_Attr::parse($attr);
1085 }
1086
1087 function wpjam_get_ajax_data_attr($name, $data=[], $output=null){
1088 $attr = WPJAM_AJAX::get($name) ? wpjam_ajax($name, $data) : null;
1089
1090 return $output ? wpjam_array($attr) : $attr;
1091 }
1092
1093 function wpjam_get_ajax_attribute_string($name, $data=[]){
1094 return wpjam_get_ajax_data_attr($name, $data);
1095 }
1096
1097 function wpjam_get_ajax_attributes($name, $data=[]){
1098 return wpjam_get_ajax_data_attr($name, $data, '[]');
1099 }
1100
1101 function wpjam_add_admin_error($msg='', $type='success'){
1102 wpjam_admin('error', $msg, $type);
1103 }
1104
1105 function wpjam_get_admin_post_id(){
1106 return wpjam_admin('post_id');
1107 }
1108
1109 function wpjam_add_once_filter($name, ...$args){
1110 return wpjam_once($name, ...$args);
1111 }
1112
1113 function wpjam_add_dashboard_widget($name, $args){
1114 wpjam_dashboard('add_widget', $name, $args);
1115 }
1116
1117 function_alias('wpjam_add_dashboard_widget', 'wpjam_register_dashboard_widget');
1118
1119 function_alias('wpjam_every', 'array_all');
1120 function_alias('wpjam_some', 'array_any');
1121 function_alias('wpjam_array', 'array_wrap');
1122 function_alias('wpjam_get', 'array_get');
1123 function_alias('wpjam_set', 'array_set');
1124 function_alias('wpjam_merge', 'merge_deep');
1125
1126 function_alias('wpjam_hook', 'wpjam_add_filter');
1127
1128 function_alias('wpjam_ob', 'wpjam_ob_get_contents');
1129 function_alias('wpjam_get_data_type', 'wpjam_get_data_type_object');
1130
1131 function_alias('is_login', 'wpjam_is_login');
1132 function_alias('wpjam_ajax', 'wpjam_register_ajax');
1133 function_alias('wpjam_route', 'wpjam_register_route', 'wpjam_register_route_module');
1134 function_alias('wpjam_options', 'wpjam_parse_options');
1135 function_alias('wpjam_error', 'wpjam_parse_error', 'wpjam_register_error_setting');
1136
1137 function_alias('array_first', 'array_value_first');
1138 function_alias('array_last', 'array_value_last');
1139
1140 function_alias('wpjam_lazyloader', 'wpjam_register_lazyloader');
1141 function_alias('wpjam_activation', 'wpjam_register_activation');
1142 function_alias('wpjam_json_source', 'wpjam_register_source');
1143
1144 function_alias('wpjam_strip_control_chars', 'wpjam_strip_control_characters');
1145 function_alias('wpjam_get_registered', 'wpjam_get_registered_object');
1146
1147 function_alias('wpjam_merge', 'wpjam_array_merge');
1148 function_alias('wpjam_filter', 'wpjam_array_filter');
1149 function_alias('wpjam_is_assoc_array', 'is_assoc_array');
1150
1151 function_alias('wpjam_add_admin_error', 'wpjam_admin_add_error');
1152
1153 function_alias('wpjam_map_meta_cap', 'wpjam_register_capability');
1154 function_alias('wpjam_setting', 'wpjam_get_setting_object');
1155 function_alias('wpjam_get_post_excerpt', 'get_post_excerpt');
1156 function_alias('wpjam_attr', 'wpjam_attribute_string');
1157 function_alias('wpjam_download_url', 'wpjam_download_image');
1158 function_alias('wpjam_is_external_url', 'wpjam_is_external_image');
1159
1160 function_alias('wpjam_get_post_option_fields', 'wpjam_get_post_fields');
1161
1162 function_alias('wpjam_get_items', 'wpjam_get_current_items');
1163 function_alias('wpjam_add_item', 'wpjam_add_current_item');
1164
1165 function_alias('wpjam_parse_ip', 'wpjam_get_ipdata');
1166 function_alias('wpjam_get_user_agent', 'wpjam_get_ua');
1167 function_alias('is_macintosh', 'is_mac');
1168 function_alias('wp_is_mobile', 'wpjam_is_mobile');
1169
1170 function_alias('wpjam_list_flatten', 'wpjam_flatten_terms');
1171 function_alias('wpjam_list_sort', 'wpjam_sort_items');
1172
1173 function_alias('wpjam_is_module', 'is_module');
1174
1175 function_alias('wpjam_get_taxonomy', 'wpjam_get_taxonomy_object');
1176
1177 function_alias('wpjam_get_json_object', 'wpjam_get_api_setting', 'wpjam_get_api');
1178
1179 function_alias('wpjam_get_bind', 'wpjam_get_bind_object');
1180 function_alias('wpjam_get_user_signup', 'wpjam_get_user_signup_object');
1181
1182 function_alias('wpjam_get_path_object', 'wpjam_get_path_obj');
1183 function_alias('wpjam_get_paths', 'wpjam_get_path_objs');
1184
1185 function_alias('wpjam_get_post_first_image_url', 'wpjam_get_post_first_image', 'get_post_first_image');
1186
1187 function_alias('wpjam_cdn_host_replace', 'wpjam_cdn_replace_local_hosts');
1188 function_alias('wpjam_field', 'wpjam_get_field_html', 'wpjam_render_field');
1189
1190 function_alias('wpjam_get_qqv_id', 'wpjam_get_qqv_vid', 'wpjam_get_qq_vid');
1191
1192 function_alias('wpjam_has_term_thumbnail', 'wpjam_has_category_thumbnail', 'wpjam_has_tag_thumbnail');
1193
1194 function_alias('wpjam_get_term_thumbnail', 'wpjam_get_category_thumbnail', 'wpjam_get_tag_thumbnail');
1195 function_alias('wpjam_term_thumbnail', 'wpjam_category_thumbnail', 'wpjam_tag_thumbnail');
1196
1197 function_alias('wpjam_get_term_thumbnail_url', 'wpjam_get_category_thumbnail_url', 'wpjam_get_tag_thumbnail_url', 'wpjam_get_term_thumbnail_src', 'wpjam_get_category_thumbnail_src', 'wpjam_get_tag_thumbnail_src', 'wpjam_get_term_thumbnail_uri', 'wpjam_get_category_thumbnail_uri', 'wpjam_get_tag_thumbnail_uri');
1198
1199 function_alias('wpjam_get_post_thumbnail_url', 'wpjam_get_post_thumbnail_src', 'wpjam_get_post_thumbnail_uri');
1200
1201 function_alias('wpjam_get_default_thumbnail_url', 'wpjam_get_default_thumbnail_src', 'wpjam_get_default_thumbnail_uri');
1202
1203 add_action('wpjam_api', fn($json)=> do_action('wpjam_api_template_redirect', $json));
1204 add_filter('rewrite_rules_array', fn($rules)=> array_merge(apply_filters('wpjam_rewrite_rules', []), $rules));
1205
1206 if(is_admin()){
1207 add_action('wpjam_plugin_page', function($object, $type){
1208 if($type == 'tab'){
1209 if(has_filter($filter = wpjam_get_filter_name($object->menu_slug, 'tabs'))){
1210 $object->tabs = apply_filters($filter, $object->get_arg('tabs', [], 'callback'));
1211 }
1212 }elseif(in_array($type, ['option', 'list_table']) && !$object->$type){
1213 if(has_filter($filter = wpjam_get_filter_name($object->{$type.'_name'} ?: $object->menu_slug, $type == 'option' ? 'setting' : $type))){
1214 $object->$type = apply_filters($filter, []);
1215 }
1216 }
1217 }, 10, 2);
1218 }
1219
1220 class WPJAM_Qrcode_Bind extends WPJAM_Bind{
1221 public function call_qrcode($method='', ...$args){
1222 $type = $this->type;
1223 $object = wpjam_get_registered('WPJAM_Qrcode', $type) ?: wpjam_register('WPJAM_Qrcode', $type, [
1224 'handler' => [$type.'_Account', 'get_handler']($this->appid),
1225 'creator' => [$this, 'create_qrcode']
1226 ]);
1227
1228 return $method ? [$object, $method](...$args) : $object;
1229 }
1230
1231 public function verify_qrcode($scene, $code, $output=''){
1232 return wpjam_then($this->call_qrcode('verify', $scene, $code), fn($qrcode)=> $output == 'openid' ? $qrcode['openid'] : $qrcode);
1233 }
1234
1235 public function scan_qrcode($openid, $scene){
1236 return $this->call_qrcode('scan', $scene, $openid);
1237 }
1238 }
1239
1240 class WPJAM_User_Qrcode_Signup extends WPJAM_User_Signup{
1241 public function __construct($name, $args=[]){
1242 parent::__construct($name, $args+['by'=>'scene']);
1243
1244 $this->qrcode = $this->call_qrcode();
1245 }
1246 }
1247
1248 class WPJAM_JSON_Module{
1249 public static function __callStatic($method, $args){
1250 return ['WPJAM_JSON', $method == 'parse' ? 'parse_module' : $method](...$args);
1251 }
1252 }
1253
1254 class WPJAM_Option_Model{
1255 protected static function get_object(){
1256 return wpjam_get_option(static::class, 'model', self::class);
1257 }
1258
1259 public static function __callStatic($method, $args){
1260 return ($object = self::get_object()) ? [$object, $method](...$args) : null;
1261 }
1262 }
1263
1264 class WPJAM_Posts_Widget extends WPJAM_Widget{
1265 public function __construct(){
1266 parent::__construct('wpjam-posts', 'WPJAM - 文章列表', WPJAM_Basic_Posts::get_widget());
1267 }
1268 }
1269
1270 class WPJAM_Crypt extends WPJAM_Args{
1271 public function __construct(...$args){
1272 $this->args = ($args && is_string($args[0]) ? ['key'=>array_shift($args)] : [])+($args[0] ?? [])+[
1273 'method' => 'aes-256-cbc',
1274 'key' => '',
1275 'iv' => '',
1276 'options' => OPENSSL_ZERO_PADDING,
1277 ];
1278 }
1279
1280 public function encrypt($text){
1281 return wpjam_encrypt($text, $this->args);
1282 }
1283
1284 public function decrypt($text){
1285 return wpjam_decrypt($text, $this->args);
1286 }
1287
1288 public static function pad($text, $type, ...$args){
1289 return wpjam_pad($text, $type, ...$args);
1290 }
1291
1292 public static function unpad($text, $type, ...$args){
1293 return wpjam_pad($text, '-'.$type, ...$args);
1294 }
1295
1296 public static function generate_signature(...$args){
1297 return wpjam_generate_signature('sha1', ...$args);
1298 }
1299 }
1300
1301 class WPJAM_Option_Items extends WPJAM_Items{
1302 public function __construct($option_name, $args=[]){
1303 parent::__construct(['option_name'=>$option_name]+(is_array($args) ? $args+['type'=>empty($args['items_field']) ? 'option' : 'setting'] : ['primary_key'=>$args, 'type'=>'option']));
1304 }
1305 }
1306
1307 class WPJAM_Meta_Items extends WPJAM_Items{
1308 public function __construct($meta_type, $object_id=0, $meta_key='', $args=[]){
1309 parent::__construct(compact('meta_type', 'object_id', 'meta_key')+['type'=>'meta']+$args);
1310 }
1311 }
1312
1313 class WPJAM_Content_Items extends WPJAM_Items{
1314 public function __construct($post_id, $args=[]){
1315 parent::__construct(['type'=>'post_content', 'post_id'=>$post_id]+$args);
1316 }
1317 }
1318
1319 class WPJAM_Cache_Items extends WPJAM_Items{
1320 public function __construct($key, $args=[]){
1321 parent::__construct(['type'=>'cache', 'cache_key'=> $key]+$args);
1322 }
1323 }
1324
1325 class WPJAM_DBTransaction{
1326 public static function beginTransaction(){
1327 return $GLOBALS['wpdb']->query("START TRANSACTION;");
1328 }
1329
1330 public static function queryException(){
1331 if($error = $GLOBALS['wpdb']->last_error){
1332 throw new Exception($error);
1333 }
1334 }
1335
1336 public static function commit(){
1337 self::queryException();
1338 return $GLOBALS['wpdb']->query("COMMIT;");
1339 }
1340
1341 public static function rollBack(){
1342 return $GLOBALS['wpdb']->query("ROLLBACK;");
1343 }
1344 }
1345
1346 class WPJAM_Post_Option{
1347 public static function get($name){
1348 return wpjam_get_post_option($name);
1349 }
1350
1351 public static function get_registereds(){
1352 return wpjam_get_post_options();
1353 }
1354 }
1355
1356 class WPJAM_Term_Option{
1357 public static function get($name){
1358 return wpjam_get_term_option($name);
1359 }
1360
1361 public static function get_registereds(){
1362 return wpjam_get_term_options();
1363 }
1364 }
1365
1366 class WPJAM_Bit{
1367 protected $value = 0;
1368
1369 public function __construct($value=0){
1370 $this->value = (int)$value;
1371 }
1372
1373 public function __get($name){
1374 return $name == 'value' ? $this->value : null;
1375 }
1376
1377 public function has($bit){
1378 $bit = (int)$bit;
1379
1380 return ($this->value & $bit) == $bit;
1381 }
1382
1383 public function add($bit){
1384 $this->value = $this->value | (int)$bit;
1385
1386 return $this;
1387 }
1388
1389 public function remove($bit){
1390 $this->value = $this->value & (~(int)$bit);
1391
1392 return $this;
1393 }
1394 }
1395
1396 class WPJAM_Platforms extends WPJAM_Args{
1397 public function parse_item($item, $suffix=''){
1398 return wpjam_parse_path_item($item, $this->names, $suffix);
1399 }
1400
1401 public function validate_item($item, $suffix=''){
1402 return wpjam_validate_path_item($item, $this->names, $suffix);
1403 }
1404
1405 public function get_fields($args=[]){
1406 return wpjam_get_path_fields($this->names, $args);
1407 }
1408
1409 public function get_current(){
1410 $platforms = WPJAM_Platform::get_by($this->names);
1411
1412 return count($platforms) > 1 ? array_find($platforms, fn($v)=> $v->verify()) : array_first($platforms);
1413 }
1414
1415 public static function get_instance($names=null){
1416 if($platforms = array_filter(WPJAM_Platform::get_by((array)($names ?? ['path'=>true])))){
1417 return wpjam_var('platforms:'.implode('-', array_keys($platforms)), fn()=> new self(['names'=>array_keys($platforms)]));
1418 }
1419 }
1420 }
1421
1422 trait WPJAM_Instance_Trait{
1423 use WPJAM_Call_Trait;
1424
1425 public static function instance_exists($name){
1426 return wpjam_get_instance(self::get_called(), $name) ?: false;
1427 }
1428
1429 public static function add_instance($name, $object){
1430 return wpjam_add_instance(self::get_called(), $name, $object);
1431 }
1432
1433 protected static function create_instance(...$args){
1434 return new static(...$args);
1435 }
1436
1437 public static function instance(...$args){
1438 return wpjam_get_instance(self::get_called(), ...(count($args) == 2 && is_callable($args[1])) ? $args : [$args ? implode(':', $args) : 'singleton', fn()=> static::create_instance(...$args)]);
1439 }
1440 }
1441
1442 trait WPJAM_Setting_Trait{
1443 private $settings = [];
1444 public $option_name = '';
1445 public $site_default = false;
1446
1447 private function init($option_name, $site_default=false){
1448 $this->option_name = $option_name;
1449 $this->site_default = $site_default;
1450
1451 $this->reset_settings();
1452 }
1453
1454 public function __get($name){
1455 if(is_null(get_option($option_name, null))){
1456 add_option($option_name, []);
1457 }
1458
1459 return $this->get_setting($name);
1460 }
1461
1462 public function __set($name, $value){
1463 return $this->update_setting($name, $value);
1464 }
1465
1466 public function __isset($name){
1467 return isset($this->settings[$name]);
1468 }
1469
1470 public function __unset($name){
1471 $this->delete_setting($name);
1472 }
1473
1474 public function get_settings(){
1475 return $this->settings;
1476 }
1477
1478 public function reset_settings(){
1479 $this->settings = wpjam_get_option($this->option_name);
1480 $this->settings += $this->site_default ? wpjam_get_site_option($this->option_name) : [];
1481 }
1482
1483 public function get_setting($name='', $default=null){
1484 return $name ? ($this->settings[$name] ?? $default) : $this->settings;
1485 }
1486
1487 public function update_setting($name, $value){
1488 $this->settings[$name] = $value;
1489
1490 return $this->save();
1491 }
1492
1493 public function delete_setting($name){
1494 $this->settings = wpjam_except($this->settings, $name);
1495
1496 return $this->save();
1497 }
1498
1499 private function save($settings=[]){
1500 $this->settings = array_merge($this->settings, $settings ?: []);
1501
1502 return update_option($this->option_name, $this->settings);
1503 }
1504
1505 private static $instances = [];
1506
1507 public static function get_instance(){
1508 $blog_id = get_current_blog_id();
1509
1510 return self::$instances[$blog_id] ??= new self();
1511 }
1512
1513 public static function register_option($args=[]){
1514 $instance = self::get_instance();
1515 $defaults = array_filter([
1516 'site_default' => $instance->site_default,
1517 'sanitize_callback' => wpjam_callback([$instance, 'sanitize_callback']),
1518 'summary' => wpjam_callback([$instance, 'get_summary']),
1519 'sections' => wpjam_callback([$instance, 'get_sections']),
1520 'fields' => wpjam_callback([$instance, 'get_fields']),
1521 ]);
1522
1523 return wpjam_register_option($instance->option_name, $args+$defaults);
1524 }
1525 }
1526
1527 trait WPJAM_Meta_Trait{
1528 public static function get_meta_type_object(){
1529 return wpjam_get_meta_type(self::$meta_type);
1530 }
1531
1532 public static function add_meta($id, $meta_key, $meta_value, $unique=false){
1533 return self::get_meta_type_object()->add_data($id, $meta_key, $meta_value, $unique);
1534 }
1535
1536 public static function delete_meta($id, $meta_key, $meta_value=''){
1537 return self::get_meta_type_object()->delete_data($id, $meta_key, $meta_value);
1538 }
1539
1540 public static function get_meta($id, $key = '', $single = false){
1541 return self::get_meta_type_object()->get_data($id, $key, $single);
1542 }
1543
1544 public static function update_meta($id, $meta_key, $meta_value, $prev_value=''){
1545 return self::get_meta_type_object()->update_data($id, $meta_key, wp_slash($meta_value), $prev_value);
1546 }
1547
1548 public static function delete_meta_by_key($meta_key){
1549 return self::get_meta_type_object()->delete_by_key($meta_key);
1550 }
1551
1552 public static function update_meta_cache($object_ids){
1553 self::get_meta_type_object()->update_cache($object_ids);
1554 }
1555
1556 public static function create_meta_table(){
1557 self::get_meta_type_object()->create_table();
1558 }
1559
1560 public static function get_meta_table(){
1561 return self::get_meta_type_object()->get_table();
1562 }
1563 }
1564
1565 class WPJAM_Posts{
1566 public static function __callStatic($method, $args){
1567 return wpjam_query($method, ...$args);
1568 }
1569 }