class-functions.php
18.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
<?php if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! class_exists( 'UM_Functions' ) ) {
/**
* Class UM_Functions
*/
class UM_Functions {
/**
* Store URL
*
* @var string
*/
var $store_url = 'https://ultimatemember.com/';
/**
* WP remote Post timeout
* @var int
*/
var $request_timeout = 60;
/**
* UM_Functions constructor.
*/
function __construct() {
}
/**
* Check if AJAX now
*
* @return bool
*/
function is_ajax() {
return function_exists( 'wp_doing_ajax' ) ? wp_doing_ajax() : defined( 'DOING_AJAX' );
}
/**
* Check frontend nonce
*
* @param bool $action
*/
function check_ajax_nonce( $action = false ) {
$nonce = isset( $_REQUEST['nonce'] ) ? sanitize_text_field( $_REQUEST['nonce'] ) : '';
$action = empty( $action ) ? 'um-frontend-nonce' : $action;
if ( ! wp_verify_nonce( $nonce, $action ) ) {
wp_send_json_error( esc_js( __( 'Wrong Nonce', 'ultimate-member' ) ) );
}
}
/**
* What type of request is this?
*
* @param string $type String containing name of request type (ajax, frontend, cron or admin)
*
* @return bool
*/
public function is_request( $type ) {
switch ( $type ) {
case 'admin' :
return is_admin();
case 'ajax' :
return defined( 'DOING_AJAX' );
case 'cron' :
return defined( 'DOING_CRON' );
case 'frontend' :
return ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' );
}
return false;
}
/**
* Help Tip displaying
*
* Function for render/displaying UltimateMember help tip
*
* @since 2.0.0
*
* @param string $tip Help tip text
* @param bool $allow_html Allow sanitized HTML if true or escape
* @param bool $echo Return HTML or echo
* @return string
*/
function tooltip( $tip, $allow_html = false, $echo = true ) {
if ( $allow_html ) {
$tip = htmlspecialchars( wp_kses( html_entity_decode( $tip ), array(
'br' => array(),
'em' => array(),
'strong' => array(),
'small' => array(),
'span' => array(),
'ul' => array(),
'li' => array(),
'ol' => array(),
'p' => array(),
) ) );
} else {
$tip = esc_attr( $tip );
}
ob_start(); ?>
<span class="um_tooltip dashicons dashicons-editor-help" title="<?php echo $tip ?>"></span>
<?php if ( $echo ) {
ob_get_flush();
return '';
} else {
return ob_get_clean();
}
}
/**
* @return mixed|void
*/
function excluded_taxonomies() {
$taxes = array(
'nav_menu',
'link_category',
'post_format',
);
/**
* UM hook
*
* @type filter
* @title um_excluded_taxonomies
* @description Exclude taxonomies for UM
* @input_vars
* [{"var":"$taxes","type":"array","desc":"Taxonomies keys"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_excluded_taxonomies', 'function_name', 10, 1 ); ?>
* @example
* <?php
* add_filter( 'um_excluded_taxonomies', 'my_excluded_taxonomies', 10, 1 );
* function my_excluded_taxonomies( $taxes ) {
* // your code here
* return $taxes;
* }
* ?>
*/
return apply_filters( 'um_excluded_taxonomies', $taxes );
}
/**
* Output templates
*
* @access public
* @param string $template_name
* @param string $basename (default: '')
* @param array $t_args (default: array())
* @param bool $echo
*
* @return string|void
*/
public function get_template( $template_name, $basename = '', $t_args = array(), $echo = false ) {
if ( ! empty( $t_args ) && is_array( $t_args ) ) {
/*
* This use of extract() cannot be removed. There are many possible ways that
* templates could depend on variables that it creates existing, and no way to
* detect and deprecate it.
*
* Passing the EXTR_SKIP flag is the safest option, ensuring globals and
* function variables cannot be overwritten.
*/
// phpcs:ignore WordPress.PHP.DontExtract.extract_extract
extract( $t_args, EXTR_SKIP );
}
$path = '';
if ( $basename ) {
// use '/' instead of "DIRECTORY_SEPARATOR", because wp_normalize_path makes the correct replacement
$array = explode( '/', wp_normalize_path( trim( $basename ) ) );
$path = $array[0];
}
$located = $this->locate_template( $template_name, $path );
if ( ! file_exists( $located ) ) {
_doing_it_wrong( __FUNCTION__, sprintf( '<code>%s</code> does not exist.', $located ), '2.1' );
return;
}
/**
* UM hook
*
* @type filter
* @title um_get_template
* @description Change template location
* @input_vars
* [{"var":"$located","type":"string","desc":"template Located"},
* {"var":"$template_name","type":"string","desc":"Template Name"},
* {"var":"$path","type":"string","desc":"Template Path at server"},
* {"var":"$t_args","type":"array","desc":"Template Arguments"}]
* @change_log
* ["Since: 2.0"]
* @usage add_filter( 'um_get_template', 'function_name', 10, 4 );
* @example
* <?php
* add_filter( 'um_get_template', 'my_get_template', 10, 4 );
* function my_get_template( $located, $template_name, $path, $t_args ) {
* // your code here
* return $located;
* }
* ?>
*/
$located = apply_filters( 'um_get_template', $located, $template_name, $path, $t_args );
ob_start();
/**
* UM hook
*
* @type action
* @title um_before_template_part
* @description Make some action before include template file
* @input_vars
* [{"var":"$template_name","type":"string","desc":"Template Name"},
* {"var":"$path","type":"string","desc":"Template Path at server"},
* {"var":"$located","type":"string","desc":"template Located"},
* {"var":"$t_args","type":"array","desc":"Template Arguments"}]
* @change_log
* ["Since: 2.0"]
* @usage add_action( 'um_before_template_part', 'function_name', 10, 4 );
* @example
* <?php
* add_action( 'um_before_template_part', 'my_before_template_part', 10, 4 );
* function my_before_template_part( $template_name, $path, $located, $t_args ) {
* // your code here
* }
* ?>
*/
do_action( 'um_before_template_part', $template_name, $path, $located, $t_args );
include $located;
/**
* UM hook
*
* @type action
* @title um_after_template_part
* @description Make some action after include template file
* @input_vars
* [{"var":"$template_name","type":"string","desc":"Template Name"},
* {"var":"$path","type":"string","desc":"Template Path at server"},
* {"var":"$located","type":"string","desc":"template Located"},
* {"var":"$t_args","type":"array","desc":"Template Arguments"}]
* @change_log
* ["Since: 2.0"]
* @usage add_action( 'um_after_template_part', 'function_name', 10, 4 );
* @example
* <?php
* add_action( 'um_after_template_part', 'my_after_template_part', 10, 4 );
* function my_after_template_part( $template_name, $path, $located, $t_args ) {
* // your code here
* }
* ?>
*/
do_action( 'um_after_template_part', $template_name, $path, $located, $t_args );
$html = ob_get_clean();
if ( ! $echo ) {
return $html;
} else {
echo $html;
return;
}
}
/**
* Locate a template and return the path for inclusion.
*
* @access public
* @param string $template_name
* @param string $path (default: '')
* @return string
*/
function locate_template( $template_name, $path = '' ) {
// check if there is template at theme folder
$template = locate_template( array(
trailingslashit( 'ultimate-member' . DIRECTORY_SEPARATOR . $path ) . $template_name
) );
if ( ! $template ) {
if ( $path ) {
$template = trailingslashit( trailingslashit( WP_PLUGIN_DIR ) . $path );
} else {
$template = trailingslashit( UM_PATH );
}
$template .= 'templates' . DIRECTORY_SEPARATOR . $template_name;
}
/**
* UM hook
*
* @type filter
* @title um_locate_template
* @description Change template locate
* @input_vars
* [{"var":"$template","type":"string","desc":"Template locate"},
* {"var":"$template_name","type":"string","desc":"Template Name"},
* {"var":"$path","type":"string","desc":"Template Path at server"}]
* @change_log
* ["Since: 2.0"]
* @usage add_filter( 'um_locate_template', 'function_name', 10, 3 );
* @example
* <?php
* add_filter( 'um_locate_template', 'my_locate_template', 10, 3 );
* function my_locate_template( $template, $template_name, $path ) {
* // your code here
* return $template;
* }
* ?>
*/
return apply_filters( 'um_locate_template', $template, $template_name, $path );
}
/**
* @deprecated 2.8.0
*
* @return array
*/
public function cpt_list() {
_deprecated_function( __METHOD__, '2.8.0', 'UM()->common()->cpt()->get_list()' );
return UM()->common()->cpt()->get_list();
}
/**
* @param array $array
* @param string $key
* @param array $insert_array
*
* @return array
*/
function array_insert_before( $array, $key, $insert_array ) {
$index = array_search( $key, array_keys( $array ) );
if ( $index === false ) {
return $array;
}
$array = array_slice( $array, 0, $index, true ) +
$insert_array +
array_slice( $array, $index, count( $array ) - 1, true );
return $array;
}
/**
* @since 2.1.0
*
* @param $var
* @return array|string
*/
function clean_array( $var ) {
if ( is_array( $var ) ) {
return array_map( array( $this, 'clean_array' ), $var );
} else {
return ! is_scalar( $var ) ? sanitize_text_field( $var ) : $var;
}
}
/**
* Replace the first match in the string, alternative for the `str_replace()` function
*
* @param string $search
* @param string $replace
* @param string $subject
*
* @return string
*/
function str_replace_first( $search, $replace, $subject ) {
$search = '/' . preg_quote( $search, '/' ) . '/';
return preg_replace( $search, $replace, $subject, 1 );
}
/**
* @param string $context
*
* @return array
*/
public function get_allowed_html( $context = '' ) {
switch ( $context ) {
case 'wp-admin':
$allowed_html = array(
'img' => array(
'alt' => true,
'align' => true,
'border' => true,
'height' => true,
'hspace' => true,
'loading' => true,
'longdesc' => true,
'vspace' => true,
'src' => true,
'srcset' => true,
'usemap' => true,
'width' => true,
),
'ol' => array(),
'ul' => array(),
'li' => array(),
'h1' => array(
'align' => true,
),
'h2' => array(
'align' => true,
),
'h3' => array(
'align' => true,
),
'p' => array(
'align' => true,
'dir' => true,
'lang' => true,
),
'form' => array(
'action' => true,
'accept' => true,
'accept-charset' => true,
'enctype' => true,
'method' => true,
'name' => true,
'target' => true,
),
'label' => array(
'for' => true,
),
'select' => array(
'name' => true,
'multiple' => true,
'disabled' => true,
'readonly' => true,
'required' => true,
'autocomplete' => true,
),
'option' => array(
'value' => true,
'selected' => true,
'disabled' => true,
),
'input' => array(
'type' => true,
'name' => true,
'value' => true,
'placeholder' => true,
'readonly' => true,
'disabled' => true,
'checked' => true,
'selected' => true,
'required' => true,
'autocomplete' => true,
),
'textarea' => array(
'cols' => true,
'rows' => true,
'disabled' => true,
'name' => true,
'readonly' => true,
'required' => true,
'autocomplete' => true,
),
'table' => array(
'align' => true,
'bgcolor' => true,
'border' => true,
'cellpadding' => true,
'cellspacing' => true,
'dir' => true,
'rules' => true,
'summary' => true,
'width' => true,
),
'tbody' => array(
'align' => true,
'char' => true,
'charoff' => true,
'valign' => true,
),
'td' => array(
'abbr' => true,
'align' => true,
'axis' => true,
'bgcolor' => true,
'char' => true,
'charoff' => true,
'colspan' => true,
'dir' => true,
'headers' => true,
'height' => true,
'nowrap' => true,
'rowspan' => true,
'scope' => true,
'valign' => true,
'width' => true,
),
'tfoot' => array(
'align' => true,
'char' => true,
'charoff' => true,
'valign' => true,
),
'th' => array(
'abbr' => true,
'align' => true,
'axis' => true,
'bgcolor' => true,
'char' => true,
'charoff' => true,
'colspan' => true,
'headers' => true,
'height' => true,
'nowrap' => true,
'rowspan' => true,
'scope' => true,
'valign' => true,
'width' => true,
),
'thead' => array(
'align' => true,
'char' => true,
'charoff' => true,
'valign' => true,
),
'tr' => array(
'align' => true,
'bgcolor' => true,
'char' => true,
'charoff' => true,
'valign' => true,
),
'a' => array(
'onclick' => array(),
),
);
break;
case 'templates':
$allowed_html = array(
'style' => array(),
'link' => array(
'rel' => true,
'href' => true,
'media' => true,
),
'form' => array(
'action' => true,
'accept' => true,
'accept-charset' => true,
'enctype' => true,
'method' => true,
'name' => true,
'target' => true,
),
'label' => array(
'for' => true,
),
'select' => array(
'name' => true,
'multiple' => true,
'disabled' => true,
'readonly' => true,
'required' => true,
'autocomplete' => true,
),
'option' => array(
'value' => true,
'selected' => true,
'disabled' => true,
),
'input' => array(
'type' => true,
'name' => true,
'value' => true,
'placeholder' => true,
'readonly' => true,
'disabled' => true,
'checked' => true,
'selected' => true,
'required' => true,
'autocomplete' => true,
'size' => true,
'step' => true,
'min' => true,
'max' => true,
'minlength' => true,
'maxlength' => true,
'pattern' => true,
),
'textarea' => array(
'cols' => true,
'rows' => true,
'disabled' => true,
'name' => true,
'readonly' => true,
'required' => true,
'autocomplete' => true,
),
'img' => array(
'alt' => true,
'align' => true,
'border' => true,
'height' => true,
'hspace' => true,
'loading' => true,
'longdesc' => true,
'vspace' => true,
'src' => true,
'srcset' => true,
'usemap' => true,
'width' => true,
),
'h1' => array(
'align' => true,
),
'h2' => array(
'align' => true,
),
'h3' => array(
'align' => true,
),
'p' => array(
'align' => true,
'dir' => true,
'lang' => true,
),
'ol' => array(),
'ul' => array(),
'li' => array(),
'time' => array(
'datetime' => true,
),
);
break;
case 'admin_notice':
$allowed_html = array(
'p' => array(
'align' => true,
'dir' => true,
'lang' => true,
),
'label' => array(
'for' => true,
),
'strong' => array(
'style' => true,
),
);
break;
default:
$allowed_html = array();
break;
}
$global_allowed = array(
'a' => array(
'href' => array(),
'rel' => true,
'rev' => true,
'name' => true,
'target' => true,
'download' => array(
'valueless' => 'y',
),
),
'em' => array(),
'i' => array(),
'b' => array(),
'q' => array(
'cite' => true,
),
's' => array(),
'strike' => array(),
'strong' => array(),
'br' => array(),
'div' => array(
'align' => true,
'dir' => true,
'lang' => true,
),
'span' => array(
'dir' => true,
'align' => true,
'lang' => true,
),
'code' => array(),
'hr' => array(
'style' => true,
),
);
$allowed_html = array_merge( $global_allowed, $allowed_html );
$allowed_html = array_map( '_wp_add_global_attributes', $allowed_html );
/**
* Filters the allowed HTML tags and their attributes in the late escaping before echo.
*
* Note: Please use the `wp_kses()` allowed tags structure.
*
* @since 2.5.4
* @hook um_late_escaping_allowed_tags
*
* @param {array} $allowed_html Allowed HTML tags with attributes.
* @param {string} $context Function context 'wp-admin' for Admin Dashboard echo, 'templates' for the frontend.
*
* @return {array} Allowed HTML tags with attributes.
*
* @example <caption>It adds iframe HTML tag and 'onclick' attribute for strong tag.</caption>
* function add_extra_kses_allowed_tags( $allowed_html, $context ) {
* if ( 'templates' === $context ) {
* $allowed_html['iframe'] = array(
* 'src' => true,
* );
* $allowed_html['strong']['onclick'] = true;
* }
* return $allowed_html;
* }
* add_filter( 'um_late_escaping_allowed_tags', 'add_extra_kses_allowed_tags', 10, 2 );
*/
$allowed_html = apply_filters( 'um_late_escaping_allowed_tags', $allowed_html, $context );
return $allowed_html;
}
}
}