-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathfunctions-ur-core.php
More file actions
12743 lines (11314 loc) · 458 KB
/
Copy pathfunctions-ur-core.php
File metadata and controls
12743 lines (11314 loc) · 458 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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* UserRegistration Functions.
*
* General core functions available on both the front-end and admin.
*
* @package UserRegistration/Functions
* @version 1.0.0
*/
use WPEverest\URMembership\Admin\Repositories\MembershipRepository;
use WPEverest\URMembership\Admin\Repositories\MembersOrderRepository;
use WPEverest\URMembership\Admin\Services\MembershipService;
use WPEverest\URMembership\Admin\Services\Stripe\StripeService;
defined( 'ABSPATH' ) || exit;
// Include core functions (available in both admin and frontend).
require UR_ABSPATH . 'includes/functions-ur-page.php';
require UR_ABSPATH . 'includes/functions-ur-account.php';
require UR_ABSPATH . 'includes/functions-ur-deprecated.php';
/**
* Define a constant if it is not already defined.
*
* @param string $name Constant name.
* @param string $value Value.
*/
function ur_maybe_define_constant( $name, $value ) {
if ( ! defined( $name ) ) {
define( $name, $value );
}
}
if ( ! function_exists( 'is_ur_endpoint_url' ) ) {
/**
* Check if an endpoint is showing.
*
* @param string $endpoint User registration myaccount endpoints.
*
* @return bool
*/
function is_ur_endpoint_url( $endpoint = false ) {
global $wp;
$ur_endpoints = UR()->query->get_query_vars();
if ( false !== $endpoint ) {
if ( ! isset( $ur_endpoints[ $endpoint ] ) ) {
return false;
} else {
$endpoint_var = $ur_endpoints[ $endpoint ];
}
return isset( $wp->query_vars[ $endpoint_var ] );
} else {
foreach ( $ur_endpoints as $key => $value ) {
if ( isset( $wp->query_vars[ $key ] ) ) {
return true;
}
}
return false;
}
}
}
if ( ! function_exists( 'is_ur_account_page' ) ) {
/**
* Returns true when viewing an account page.
*
* @return bool
*/
function is_ur_account_page() {
/**
* Filter hook to modify the result of determining if the current page is an
* account page in user registration.
*
* @param bool $is_account_page The result of determining if the current page is
* a user registration account page. Default is false.
*/
return is_page( ur_get_page_id( 'myaccount' ) ) || ur_post_content_has_shortcode( 'user_registration_my_account' ) || apply_filters( 'user_registration_is_account_page', false );
}
}
if ( ! function_exists( 'is_ur_login_page' ) ) {
/**
* Returns true when viewing an login page.
*
* @return bool
*/
function is_ur_login_page() {
/**
* Filter hook to modify the result of determining if the current page is an
* login page in user registration.
*
* @param bool $is_login_page The result of determining if the current page is
* a user registration login page. Default is false.
*/
return is_page( ur_get_page_id( 'login' ) ) || ur_post_content_has_shortcode( 'user_registration_login' ) || apply_filters( 'user_registration_is_login_page', false );
}
}
if ( ! function_exists( 'is_ur_edit_account_page' ) ) {
/**
* Check for edit account page.
* Returns true when viewing the edit account page.
*
* @return bool
*/
function is_ur_edit_account_page() {
global $wp;
return ( is_ur_account_page() && isset( $wp->query_vars['edit-password'] ) );
}
}
if ( ! function_exists( 'is_ur_lost_password_page' ) ) {
/**
* Returns true when viewing the lost password page.
*
* @return bool
*/
function is_ur_lost_password_page() {
global $wp;
$lost_password_page_id = get_option( 'user_registration_lost_password_page_id', false );
return ( is_ur_account_page() && isset( $wp->query_vars['ur-lost-password'] ) ) || is_page( $lost_password_page_id );
}
}
/**
* Clean variables using sanitize_text_field. Arrays are cleaned recursively.
* Non-scalar values are ignored.
*
* @param string|array $var Variable.
*
* @return string|array
*/
function ur_clean( $var ) {
if ( is_array( $var ) ) {
return array_map( 'ur_clean', $var );
} else {
return is_scalar( $var ) ? sanitize_text_field( $var ) : $var;
}
}
/**
* Sanitize a string destined to be a tooltip.
*
* @param string $var Value to sanitize.
*
* @return string
* @since 1.0.0 Tooltips are encoded with htmlspecialchars to prevent XSS. Should not be used in conjunction with esc_attr()
*/
function ur_sanitize_tooltip( $var ) {
return htmlspecialchars(
wp_kses(
html_entity_decode( $var ),
array(
'br' => array(),
'em' => array(),
'strong' => array(),
'small' => array(),
'span' => array(),
'ul' => array(),
'li' => array(),
'ol' => array(),
'p' => array(),
)
)
);
}
/**
* Format dimensions for display.
*
* @param array $dimensions Array of dimensions.
* @param array $unit Unit, defaults to 'px'.
*
* @return string
* @since 1.7.0
*/
function ur_sanitize_dimension_unit( $dimensions = array(), $unit = 'px' ) {
return ur_array_to_string( ur_suffix_array( $dimensions, $unit ) );
}
/**
* Add a suffix into an array.
*
* @param array $array Raw array data.
* @param string $suffix Suffix to be added.
*
* @return array Modified array with suffix added.
* @since 1.7.0
*/
function ur_suffix_array( $array = array(), $suffix = '' ) {
return preg_filter( '/$/', $suffix, $array );
}
/**
* Implode an array into a string by $glue and remove empty values.
*
* @param array $array Array to convert.
* @param string $glue Glue, defaults to ' '.
*
* @return string
* @since 1.7.0
*/
function ur_array_to_string( $array = array(), $glue = ' ' ) {
return is_string( $array ) ? $array : implode( $glue, array_filter( $array ) );
}
/**
* Explode a string into an array by $delimiter and remove empty values.
*
* @param string $string String to convert.
* @param string $delimiter Delimiter, defaults to ','.
*
* @return array
* @since 1.7.0
*/
function ur_string_to_array( $string, $delimiter = ',' ) {
return is_array( $string ) ? $string : array_filter( explode( $delimiter, $string ) );
}
/**
* Converts a string (e.g. 'yes' or 'no') to a bool.
*
* @param string $string String to convert.
*
* @return bool
*/
function ur_string_to_bool( $string ) {
return is_bool( $string ) ? $string : ( ( 'yes' === $string || 'on' === $string || 1 === $string || 'true' === $string || '1' === $string || 'today' === $string || 'range' === $string ) ? true : ( null === $string ? '0' : false ) );
}
/**
* Converts a bool to a 'yes' or 'no'.
*
* @param bool $bool String to convert.
*
* @return string
*/
function ur_bool_to_string( $bool ) {
if ( ! is_bool( $bool ) ) {
$bool = ur_string_to_bool( $bool );
}
return true === $bool ? 'yes' : 'no';
}
/**
* Get other templates (e.g. my account) passing attributes and including the file.
*
* @param string $template_name Template Name.
* @param array $args Extra arguments(default: array()).
* @param string $template_path Path of template provided (default: '').
* @param string $default_path Default path of template provided(default: '').
*/
function ur_get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
// NOTE: extract() is intentionally deferred to the isolated closure below to prevent
// $args from overriding $template_name or other path-related variables.
$located = ur_locate_template( $template_name, $template_path, $default_path );
// Snapshot the trusted resolved path BEFORE the filter can modify it.
$trusted_located = realpath( $located );
/** Allow 3rd party plugin filter template file from their plugin.
*
* @param string $located Template locate.
* @param string $template_name Template Name.
* @param array $args Extra arguments(default: array()).
* @param string $template_path Path of template provided (default: '').
* @param string $default_path Default path of template provided(default: '').
*/
$located = apply_filters( 'ur_get_template', $located, $template_name, $args, $template_path, $default_path );
// Use realpath() to resolve symlinks and '..' components before validation.
$real_located = realpath( $located );
// Derive the plugin's templates/ root from the pre-filter trusted path.
// This works for every plugin/addon without needing any *_ABSPATH constant.
$templates_marker = DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR;
$templates_pos = ( false !== $trusted_located ) ? strpos( $trusted_located, $templates_marker ) : false;
$real_plugin_path = ( false !== $templates_pos )
? realpath( substr( $trusted_located, 0, $templates_pos + strlen( $templates_marker ) - 1 ) )
: false;
// Theme override directories (child + parent) — only if /user-registration/ subfolder exists.
$real_child_path = realpath( get_stylesheet_directory() . '/user-registration' );
$real_parent_path = realpath( get_template_directory() . '/user-registration' );
$path_allowed = false;
if ( false !== $real_located && false !== $real_plugin_path && 0 === strpos( $real_located, $real_plugin_path . DIRECTORY_SEPARATOR ) ) {
$path_allowed = true;
}
if ( ! $path_allowed && false !== $real_located && false !== $real_child_path && 0 === strpos( $real_located, $real_child_path . DIRECTORY_SEPARATOR ) ) {
$path_allowed = true;
}
if ( ! $path_allowed && false !== $real_located && false !== $real_parent_path && 0 === strpos( $real_located, $real_parent_path . DIRECTORY_SEPARATOR ) ) {
$path_allowed = true;
}
if ( ! $path_allowed ) {
_doing_it_wrong(
__FUNCTION__,
esc_html__( 'Template path is not allowed.', 'user-registration' ),
'1.0'
);
return;
}
if ( ! file_exists( $located ) ) {
_doing_it_wrong(
__FUNCTION__,
sprintf( '<code>%s</code> does not exist.', esc_html( $located ) ),
'1.0'
);
return;
}
ob_start();
/**
* Executes an action before including a template part.
*
* @param string $template_name Name of the template part.
* @param string $template_path Path to the template part.
* @param string $located Path to the located template file.
* @param array $args Additional arguments passed to the template part.
*/
do_action( 'user_registration_before_template_part', $template_name, $template_path, $located, $args );
// Isolated closure: extract() runs inside a separate scope so it cannot override
// $located or any other path-related variable in the outer function.
// EXTR_SKIP prevents the closure's own parameters from being overridden.
( function ( $ur_template_file, $ur_template_args ) {
if ( ! empty( $ur_template_args ) && is_array( $ur_template_args ) ) {
extract( $ur_template_args, EXTR_SKIP ); // phpcs:ignore WordPress.PHP.DontExtract.extract_extract
}
include $ur_template_file; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable
} )( $located, $args );
/**
* Executes an action after including a template part.
*
* @param string $template_name Name of the template part.
* @param string $template_path Path to the template part.
* @param string $located Path to the located template file.
* @param array $args Additional arguments passed to the template part.
*/
do_action( 'user_registration_after_template_part', $template_name, $template_path, $located, $args );
$template_content = ob_get_clean();
/**
* Filter hook to process the smart tags in the template content.
*
* @param string $template_content The template content.
*/
$template_content = apply_filters( 'user_registration_process_smart_tags', $template_content, array(), array() );
echo $template_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Locate a template and return the path for inclusion.
*
* This is the load order:
*
* yourtheme / $template_path / $template_name
* yourtheme / $template_name
* $default_path / $template_name
*
* @param string $template_name Template Name.
* @param string $template_path Path of template provided (default: '').
* @param string $default_path Default path of template provided(default: '').
*
* @return string
*/
function ur_locate_template( $template_name, $template_path = '', $default_path = '' ) {
if ( ! $template_path ) {
$template_path = UR()->template_path();
}
if ( ! $default_path ) {
$default_path = UR()->plugin_path() . '/templates/';
}
// Look within passed path within the theme - this is priority.
$template = locate_template(
array(
trailingslashit( $template_path ) . $template_name,
$template_name,
)
);
// Get default template.
if ( ! $template || UR_TEMPLATE_DEBUG_MODE ) {
$template = $default_path . $template_name;
}
/**
* Filters the located template file path before including it.
*
* @param string $template The located template file path.
* @param string $template_name The name of the template file.
* @param string $template_path The path to the template file.
*/
return apply_filters( 'user_registration_locate_template', $template, $template_name, $template_path );
}
/**
* Display a UserRegistration help tip.
*
* @param string $tip Help tip text.
* @param bool $allow_html Allow sanitized HTML if true or escape.
* @param string $classname Classname.
*
* @return string
*/
function ur_help_tip( $tip, $allow_html = false, $classname = 'user-registration-help-tip' ) {
if ( $allow_html ) {
$tip = ur_sanitize_tooltip( $tip );
} else {
$tip = esc_attr( $tip );
}
return sprintf( '<span class="%s" data-tip="%s"></span>', $classname, $tip );
}
function ur_render_premium_feature_gate_template( $args = array() ) {
$args = wp_parse_args(
$args,
array(
'template_id' => 'ur-pro-feature',
'utm_source' => 'ur-membership-create',
'plan_gated' => false,
'title' => esc_html__( 'You have run into a premium feature, please upgrade to URM Pro', 'user-registration' ),
'button_text' => esc_html__( 'Upgrade to Pro', 'user-registration' ),
)
);
// Render for free installs, or when a higher plan is required even though Pro is active.
if ( UR_PRO_ACTIVE && empty( $args['plan_gated'] ) ) {
return;
}
if ( empty( $args['upgrade_url'] ) ) {
$args['upgrade_url'] = 'https://wpuserregistration.com/upgrade/?utm_source=' . esc_attr( $args['utm_source'] ) . '&utm_medium=upgrade-link&utm-campaign=lite-version';
}
static $rendered_templates = array();
if ( in_array( $args['template_id'], $rendered_templates, true ) ) {
return;
}
$rendered_templates[] = $args['template_id'];
?>
<template id="<?php echo esc_attr( $args['template_id'] ); ?>">
<div class="ur-feature">
<div class="ur-feature__title">
<?php echo esc_html( $args['title'] ); ?>
</div>
<a class="ur-feature__btn" target="_blank" href="<?php echo esc_url( $args['upgrade_url'] ); ?>">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M11.562 3.266a.5.5 0 0 1 .876 0L15.39 8.87a1 1 0 0 0 1.516.294L21.183 5.5a.5.5 0 0 1 .798.519l-2.834 10.246a1 1 0 0 1-.956.734H5.81a1 1 0 0 1-.957-.734L2.02 6.02a.5.5 0 0 1 .798-.519l4.276 3.664a1 1 0 0 0 1.516-.294z"></path><path d="M5 21h14"></path></svg>
<?php echo esc_html( $args['button_text'] ); ?>
</a>
</div>
</template>
<?php
}
function ur_render_premium_feature_gate( $args = array() ) {
$args = wp_parse_args(
$args,
array(
'template_id' => 'ur-pro-feature',
'utm_source' => 'ur-membership-create',
'render_template' => true,
'plan_gated' => false,
'title' => esc_html__( 'You have run into a premium feature, please upgrade to URM Pro', 'user-registration' ),
'button_text' => esc_html__( 'Upgrade to Pro', 'user-registration' ),
)
);
// Render for free installs, or when a higher plan is required even though Pro is active.
if ( UR_PRO_ACTIVE && empty( $args['plan_gated'] ) ) {
return;
}
$args['upgrade_url'] = 'https://wpuserregistration.com/upgrade/?utm_source=' . esc_attr( $args['utm_source'] ) . '&utm_medium=upgrade-link&utm-campaign=lite-version';
if ( ! empty( $args['render_template'] ) ) {
ur_render_premium_feature_gate_template( $args );
}
$gate_attrs = 'data-feature-gate="tooltip" data-gate-placement="right" data-gate-interactive="true" data-gate-content="' . esc_attr( $args['template_id'] ) . '"';
?>
<span class="ur-premium-pro" <?php echo $gate_attrs; ?>><img src="<?php echo esc_url( UR()->plugin_url() . '/assets/images/icons/ur-pro-icon.png' ); ?>" alt="" /></span>
<?php
}
/**
* Checks whether the content passed contains a specific short code.
*
* @param string $tag Shortcode tag to check.
*
* @return bool
*/
function ur_post_content_has_shortcode( $tag = '' ) {
global $post;
$new_shortcode = '';
$wp_version = '5.0';
if ( version_compare( $GLOBALS['wp_version'], $wp_version, '>=' ) ) {
if ( is_object( $post ) && ! empty( $post->post_content ) ) {
$blocks = parse_blocks( $post->post_content );
foreach ( $blocks as $block ) {
if ( ( 'core/shortcode' === $block['blockName'] || 'core/paragraph' === $block['blockName'] ) && isset( $block['innerHTML'] ) ) {
$new_shortcode = ( 'core/shortcode' === $block['blockName'] ) ? $block['innerHTML'] : wp_strip_all_tags( $block['innerHTML'] );
} elseif ( 'user-registration/form-selector' === $block['blockName'] && isset( $block['attrs']['shortcode'] ) ) {
$new_shortcode = '[' . $block['attrs']['shortcode'] . ']';
}
}
}
return ( is_singular() || is_front_page() ) && is_a( $post, 'WP_Post' ) && has_shortcode( $new_shortcode, $tag );
} else {
return ( is_singular() || is_front_page() ) && is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, $tag );
}
}
if ( ! function_exists( 'ur_is_login_form_markup_rendered' ) ) {
/**
*
* Detects the actual rendered HTML (id="ur-frontend-form" and form class user-registration-form-login).
*
* @param string $content Optional. HTML to check. If empty, uses current post content.
* @return bool True if the login form markup is present, false otherwise.
*/
function ur_is_login_form_markup_rendered( $content = '' ) {
if ( '' !== $content ) {
$html = $content;
} else {
if ( ! is_singular() && ! is_front_page() ) {
return false;
}
$post = get_post();
if ( ! $post || ! $post->post_content ) {
return false;
}
$html = apply_filters( 'the_content', $post->post_content );
}
$has_container = ( false !== strpos( $html, 'id="ur-frontend-form"' ) );
$has_login_form = ( false !== strpos( $html, 'user-registration-form-login' ) );
return $has_container && $has_login_form;
}
}
/**
* Wrapper for ur_doing_it_wrong.
*
* @param string $function Callback function name.
* @param string $message Message to display.
* @param string $version Version of the plugin.
*
* @since 1.0.0
*/
function ur_doing_it_wrong( $function, $message, $version ) {
$message .= ' Backtrace: ' . wp_debug_backtrace_summary(); // PHPCS:Ignore WordPress.PHP.DevelopmentFunctions.error_log_backtrace_summary -- Backtrace is added to the message for better debugging and error tracking.
if ( defined( 'DOING_AJAX' ) ) {
/**
* The 'doing_it_wrong_run' action is triggered when the function is called incorrectly.
*
* @param string $function The function that was called incorrectly.
* @param string $message Error message providing details about the incorrect usage.
* @param string $version The version when the incorrect usage was introduced.
*/
do_action( 'doing_it_wrong_run', $function, $message, $version );
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( "{$function} was called incorrectly. {$message}. This message was added in version {$version}." );
} else {
_doing_it_wrong( esc_html( $function ), esc_html( $message ), esc_html( $version ) );
}
}
/**
* Set a cookie - wrapper for setcookie using WP constants.
*
* @param string $name Name of the cookie being set.
* @param string $value Value of the cookie.
* @param integer $expire Expiry of the cookie.
* @param string $secure Whether the cookie should be served only over https.
*/
function ur_setcookie( $name, $value, $expire = 0, $secure = null, $httponly = true ) {
if ( null === $secure ) {
$secure = is_ssl();
}
if ( ! headers_sent() ) {
setcookie( $name, $value, $expire, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, $secure, $httponly );
} elseif ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
headers_sent( $file, $line );
trigger_error( "{$name} cookie cannot be set - headers already sent by {$file} on line {$line}", E_USER_NOTICE ); //phpcs:ignore.
}
}
/**
* Read in UserRegistration headers when reading plugin headers.
*
* @param array $headers header.
*
* @return array $headers
* @since 1.1.0
*/
function ur_enable_ur_plugin_headers( $headers ) {
if ( ! class_exists( 'UR_Plugin_Updates', false ) ) {
include_once __DIR__ . '/admin/updater/class-ur-plugin-updates.php';
}
$headers['URRequires'] = UR_Plugin_Updates::VERSION_REQUIRED_HEADER;
$headers['URTested'] = UR_Plugin_Updates::VERSION_TESTED_HEADER;
return $headers;
}
add_filter( 'extra_plugin_headers', 'ur_enable_ur_plugin_headers' );
/**
* Set field type for all registrered field keys
*
* @param string $field_key field's field key.
*
* @return string $field_type
*/
function ur_get_field_type( $field_key ) {
$fields = ur_get_registered_form_fields();
if ( ur_check_module_activation( 'coupon' ) ) {
$fields[] = 'coupon';
}
$field_type = 'text';
if ( in_array( $field_key, $fields ) ) {
switch ( $field_key ) {
case 'user_email':
case 'user_confirm_email':
case 'email':
$field_type = 'email';
break;
case 'user_confirm_password':
case 'password':
case 'user_pass':
$field_type = 'password';
break;
case 'user_login':
case 'nickname':
case 'first_name':
case 'last_name':
case 'display_name':
case 'text':
$field_type = 'text';
break;
case 'user_url':
$field_type = 'url';
break;
case 'description':
case 'textarea':
$field_type = 'textarea';
break;
case 'select':
case 'country':
$field_type = 'select';
break;
case 'file':
$field_type = 'file';
break;
case 'privacy_policy':
case 'mailchimp':
case 'mailerlite':
case 'checkbox':
$field_type = 'checkbox';
break;
case 'number':
$field_type = 'number';
break;
case 'date':
$field_type = 'date';
break;
case 'radio':
$field_type = 'radio';
break;
case 'coupon':
$field_type = 'coupon';
break;
}
}
/**
* Filters the field keys before rendering or processing.
*
* @param string $field_type The type of the user registration field.
* @param string $field_key The key identifying the specific field.
*/
return apply_filters( 'user_registration_field_keys', $field_type, $field_key );
}
/**
* Get user table fields.
*
* @return array
*/
function ur_get_user_table_fields() {
/**
* Filters the user table fields before rendering or processing.
*
* @param array $user_table_fields An array of user table fields to be displayed
* or processed during user registration.
*/
return apply_filters(
'user_registration_user_table_fields',
array(
'user_email',
'user_pass',
'user_login',
'user_url',
'display_name',
)
);
}
/**
* Get required fields.
*
* @return array
*/
function ur_get_required_fields() {
/**
* Filters the list of required form fields during user registration.
*
* @param array $required_form_fields An array of user fields that are required.
*/
return apply_filters(
'user_registration_required_form_fields',
array(
'user_email',
'user_pass',
)
);
}
/**
* Get one time draggable fields fields.
*
* @return array
*/
function ur_get_one_time_draggable_fields() {
$form_fields = ur_get_user_field_only();
/**
* Filters the list of one-time draggable form fields during user registration.
*
* @param array $form_fields An array of user fields to be used as one-time draggable form fields.
*/
return apply_filters( 'user_registration_one_time_draggable_form_fields', $form_fields );
}
/**
* Check if membership is available for the form builder (at least one group or one active membership).
*
* @return bool
*/
function ur_has_membership_available() {
if ( ! ur_check_module_activation( 'membership' ) ) {
return false;
}
$has_groups = false;
if ( class_exists( 'WPEverest\URMembership\Admin\Repositories\MembershipGroupRepository' ) ) {
$repository = new \WPEverest\URMembership\Admin\Repositories\MembershipGroupRepository();
$groups = $repository->get_all_membership_groups();
$has_groups = ! empty( $groups );
}
$has_memberships = false;
if ( class_exists( 'WPEverest\URMembership\Admin\Services\MembershipService' ) ) {
$service = new \WPEverest\URMembership\Admin\Services\MembershipService();
$memberships = $service->list_active_memberships();
$has_memberships = ! empty( $memberships );
}
return $has_groups || $has_memberships;
}
/**
* Get fields excluding in profile tab
*
* @return array
*/
function ur_exclude_profile_details_fields() {
$fields_to_exclude = array(
'user_pass',
'user_confirm_password',
'user_confirm_email',
'invite_code',
'learndash_course',
);
// Check if the my account page contains [user_registration_my_account] shortcode.
if ( ur_post_content_has_shortcode( 'user_registration_my_account' ) || ur_post_content_has_shortcode( 'user_registration_edit_profile' ) ) {
// Push profile_picture field to fields_to_exclude array.
array_push( $fields_to_exclude, 'profile_picture' );
}
/**
* Filters the list of profile fields to be excluded during user registration.
*
* @param array $fields_to_exclude An array of profile fields to be excluded.
*/
return apply_filters(
'user_registration_exclude_profile_fields',
$fields_to_exclude
);
}
/**
* Get readonly fields in profile tab
*
* @return array
*/
function ur_readonly_profile_details_fields() {
/**
* Filters the list of readonly profile fields during user registration.
*
* @param array $readonly_profile_fields An associative array where keys are the
* profile fields to be marked as readonly,
* and values are arrays containing optional
* messages or values associated with each field.
*/
return apply_filters(
'user_registration_readonly_profile_fields',
array(
'user_login' => array(
'message' => __( 'Username can not be changed.', 'user-registration' ),
),
'user_pass' => array(
'value' => 'password',
'message' => __( 'Passowrd can not be changed.', 'user-registration' ),
),
'user_confirm_password' => array(
'value' => 'password',
'message' => __( 'Confirm password can not be changed.', 'user-registration' ),
),
'user_confirm_email' => array(
'message' => __( 'Confirm email can not be changed.', 'user-registration' ),
),
)
);
}
/**
* Get profile detail fields.
*
* @return void
* @deprecated 1.4.1
*/
function ur_get_account_details_fields() {
ur_deprecated_function( 'ur_get_account_details_fields', '1.4.1', 'ur_exclude_profile_details_fields' );
}
/**
* Get all fields appearing in profile tab.
*
* @return array
*/
function ur_get_user_profile_field_only() {
$user_fields = array_diff( ur_get_registered_form_fields(), ur_exclude_profile_details_fields() );
/**
* Filters the list of user profile fields during user registration.
*
* @param array $user_fields An array of user profile fields to be used during user registration.
*/
return apply_filters( 'user_registration_user_profile_field_only', $user_fields );
}
/**
* All fields to update without adding prefix.
*
* @return array
*/
function ur_get_fields_without_prefix() {
$fields = ur_get_user_field_only();
/**
* Filters the list of user registration fields without the field prefix.
*
* @param array $fields An array of user registration fields without the field prefix.
*/
return apply_filters( 'user_registration_fields_without_prefix', $fields );
}
/**
* Get all default fields by WordPress.
*
* @return array
*/
function ur_get_user_field_only() {
/**
* Filters the list of user form fields during user registration.
*
* @param array $user_form_fields An array of user form fields to be used during user registration.
*/
return apply_filters(
'user_registration_user_form_fields',
array(
'user_email',
'user_confirm_email',
'user_pass',
'user_confirm_password',
'user_login',
'nickname',
'first_name',
'last_name',
'user_url',
'display_name',
'description',
)
);
}
/**
* Get all extra form fields
*
* @return array
*/
function ur_get_other_form_fields() {
$registered = ur_get_registered_form_fields();
$user_fields = ur_get_user_field_only();
$result = array_diff( $registered, $user_fields );
/**
* Filters the list of other form fields during user registration.
*
* @param mixed $result The result of processing other form fields during user registration.
*/
return apply_filters( 'user_registration_other_form_fields', $result );
}
/**
* All default fields storing in usermeta table
*
* @return mixed|array
*/
function ur_get_registered_user_meta_fields() {
/**
* Filters the list of user meta fields for a registered user during user registration.
*
* @param array $registered_user_meta_fields An array of user meta fields associated with a registered user.
*/
return apply_filters(
'user_registration_registered_user_meta_fields',
array(
'nickname',
'first_name',
'last_name',
'description',
)
);
}
if ( ! function_exists( 'ur_get_field_name_with_prefix_usermeta' ) ) {
/**
* Returns user registration meta fields with prefix before registration.
*
* @param string $field_name Field name.
*
* @return string
*/
function ur_get_field_name_with_prefix_usermeta( $field_name ) {
$default_fields = array_merge_recursive( ur_get_user_table_fields(), ur_get_registered_user_meta_fields() );
if ( ! in_array( $field_name, $default_fields ) ) {
$field_name = 'user_registration_' . $field_name;
}
return $field_name;
}