/
functions.php
4748 lines (4121 loc) · 153 KB
/
functions.php
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
/****************************************************************
IMPORTANT. PLEASE READ.
DO NOT EDIT THIS FILE or any other file in the /wp-content/plugins/paid-memberships-pro/ directory.
Doing so could break the PMPro plugin and/or keep you from upgrading this plugin in the future.
We regularly release updates to the plugin, including important security fixes and new features.
You want to be able to upgrade.
If you were asked to insert code into "your functions.php file", it was meant that you edit the functions.php
in the root folder of your active theme. e.g. /wp-content/themes/twentytwelve/functions.php
You can also create a custom plugin to place customization code into. Instructions are here:
http://www.paidmembershipspro.com/2012/08/create-a-plugin-for-pmpro-customizations/
Further documentation for customizing Paid Memberships Pro can be found here:
http://www.paidmembershipspro.com/documentation/
****************************************************************/
if ( ! function_exists( 'sornot' ) ) {
function sornot( $t, $n ) {
if ( $n == 1 ) {
return $t;
} else {
return $t . 's';
}
}
}
// set up wpdb for the tables we need
function pmpro_setDBTables() {
global $wpdb;
$wpdb->hide_errors();
$wpdb->pmpro_membership_levels = $wpdb->prefix . 'pmpro_membership_levels';
$wpdb->pmpro_memberships_users = $wpdb->prefix . 'pmpro_memberships_users';
$wpdb->pmpro_memberships_categories = $wpdb->prefix . 'pmpro_memberships_categories';
$wpdb->pmpro_memberships_pages = $wpdb->prefix . 'pmpro_memberships_pages';
$wpdb->pmpro_membership_orders = $wpdb->prefix . 'pmpro_membership_orders';
$wpdb->pmpro_discount_codes = $wpdb->prefix . 'pmpro_discount_codes';
$wpdb->pmpro_discount_codes_levels = $wpdb->prefix . 'pmpro_discount_codes_levels';
$wpdb->pmpro_discount_codes_uses = $wpdb->prefix . 'pmpro_discount_codes_uses';
$wpdb->pmpro_membership_levelmeta = $wpdb->prefix . 'pmpro_membership_levelmeta';
$wpdb->pmpro_subscriptions = $wpdb->prefix . 'pmpro_subscriptions';
$wpdb->pmpro_membership_ordermeta = $wpdb->prefix . 'pmpro_membership_ordermeta';
$wpdb->pmpro_subscriptionmeta = $wpdb->prefix . 'pmpro_subscriptionmeta';
$wpdb->pmpro_groups = $wpdb->prefix . 'pmpro_groups';
$wpdb->pmpro_membership_levels_groups = $wpdb->prefix . 'pmpro_membership_levels_groups';
}
pmpro_setDBTables();
// thanks: http://wordpress.org/support/topic/is_plugin_active
function pmpro_is_plugin_active( $plugin ) {
return in_array( $plugin, (array) get_option( 'active_plugins', array() ) );
}
/**
* @param int $n Override if you have more than 1 group of matches and don't want the first group.
*/
function pmpro_getMatches( $p, $s, $firstvalue = false, $n = 1 ) {
$ok = preg_match_all( $p, $s, $matches );
if ( ! $ok ) {
return false;
} else {
if ( $firstvalue ) {
return $matches[ $n ][0];
} else {
return $matches[ $n ];
}
}
}
function pmpro_br2nl( $text, $tags = 'br' ) {
if ( ! is_array( $tags ) ) {
$tags = explode( ' ', $tags );
}
foreach ( $tags as $tag ) {
$text = preg_replace( "/<{$tag}[^>]*>/", "\n", $text );
$text = preg_replace( "/<\/{$tag}[^>]*>/", "\n", $text );
}
return( $text );
}
/**
* get_option() should be used directly instead.
*
* Will be deprecated in a future release.
*/
function pmpro_getOption( $s, $force = false ) {
return get_option( 'pmpro_' . $s, '' );
}
function pmpro_setOption( $s, $v = null, $sanitize_function = 'sanitize_text_field', $autoload = false ) {
if ( $v === null && isset( $_POST[ $s ] ) ) {
// phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( is_array( $_POST[ $s ] ) ) {
$v = array_map( $sanitize_function, $_POST[ $s ] );
} else {
$v = call_user_func( $sanitize_function, $_POST[ $s ] );
}
// phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
}
if ( is_array( $v ) ) {
$v = implode( ',', $v );
} elseif ( is_string( $v ) ) {
$v = trim( $v );
}
return update_option( 'pmpro_' . $s, $v, $autoload );
}
function pmpro_get_slug( $post_id ) {
global $pmpro_slugs, $wpdb;
// make sure post id is int for security
$post_id = intval( $post_id );
if ( ! $pmpro_slugs[ $post_id ] ) {
$pmpro_slugs[ $post_id ] = $wpdb->get_var( "SELECT post_name FROM $wpdb->posts WHERE ID = '" . esc_sql( $post_id ) . "' LIMIT 1" );
}
return $pmpro_slugs[ $post_id ];
}
function pmpro_url( $page = null, $querystring = '', $scheme = null ) {
global $besecure;
$besecure = apply_filters( 'besecure', $besecure );
if ( ! $scheme && $besecure ) {
$scheme = 'https';
} elseif ( ! $scheme ) {
$scheme = 'http';
}
if ( ! $page ) {
$page = 'levels';
}
global $pmpro_pages;
if ( ! empty( $pmpro_pages[ $page ] ) ) {
// start with the permalink
$url = get_permalink( $pmpro_pages[ $page ] );
// WPML/etc support
if ( function_exists( 'icl_object_id' ) && defined( 'ICL_LANGUAGE_CODE' ) ) {
$trans_id = icl_object_id( $pmpro_pages[ $page ], 'page', false, ICL_LANGUAGE_CODE );
if ( ! empty( $trans_id ) ) {
$url = get_permalink( $trans_id );
}
}
} else {
$url = '';
}
// figure out querystring
$querystring = str_replace( '?', '', $querystring );
parse_str( $querystring, $query_args );
if ( ! empty( $url ) ) {
$url = esc_url_raw( add_query_arg( $query_args, $url ) );
// figure out scheme
if ( is_ssl() ) {
$url = str_replace( 'http:', 'https:', $url );
}
}
/**
* Filter the URL before returning.
*/
$url = apply_filters( 'pmpro_url', $url, $page, $querystring, $scheme );
return $url;
}
function pmpro_isLevelFree( &$level ) {
if ( ! empty( $level ) && $level->initial_payment <= 0 && $level->billing_amount <= 0 && $level->trial_amount <= 0 ) {
$r = true;
} else {
$r = false;
}
$r = apply_filters( 'pmpro_is_level_free', $r, $level );
return $r;
}
/**
* Given an array of levels, will return true if all of them are free.
*/
function pmpro_areLevelsFree( $levelarr ) {
if ( ! is_array( $levelarr ) ) {
return false; }
foreach ( $levelarr as $curlevel ) {
if ( ! empty( $curlevel ) && ( $curlevel->initial_payment > 0 || $curlevel->billing_amount > 0 || $curlevel->trial_amount > 0 ) ) {
return false;
}
}
return true;
}
/**
* Check to see if only free levels are available.
* @return boolean This will return true if only free levels are available for signup.
* @internal Creates a filter 'pmpro_only_free_levels'.
* @since 2.1
*/
function pmpro_onlyFreeLevels() {
// Get levels that are available for checkout only.
$levels = pmpro_getAllLevels( false, true );
return apply_filters( 'pmpro_only_free_levels', pmpro_areLevelsFree( $levels ) );
}
function pmpro_isLevelRecurring( &$level ) {
if ( ! empty( $level ) && ( $level->billing_amount > 0 || $level->trial_amount > 0 ) ) {
$r = true;
} else {
$r = false;
}
$r = apply_filters( 'pmpro_is_level_recurring', $r, $level );
return $r;
}
/**
* Check if a user has any recurring levels.
* Supports Multiple Memberships Per User scenarios.
* @param $user_id int User to check for. Defaults to current user.
* @since 2.2.6
*/
function pmpro_has_recurring_level( $user_id = null ) {
global $current_user;
if ( empty( $user_id ) ) {
$user_id = $current_user->ID;
}
if ( empty( $user_id ) ) {
return false;
}
$levels = pmpro_getMembershipLevelsForUser( $user_id );
if ( empty( $levels ) ) {
return false;
}
foreach( $levels as $level ) {
if ( pmpro_isLevelRecurring( $level ) ) {
return true;
}
}
return false;
}
function pmpro_isLevelTrial( &$level ) {
if ( ! empty( $level ) && ! empty( $level->trial_limit ) && $level->trial_limit > 0 ) {
$r = true;
} else {
$r = false;
}
$r = apply_filters( 'pmpro_is_level_trial', $r, $level );
return $r;
}
function pmpro_isLevelExpiring( &$level ) {
if ( ! empty( $level ) && ( ! empty( $level->expiration_number ) && $level->expiration_number > 0 ) || ! empty( $level->enddate ) ) {
$r = true;
} else {
$r = false;
}
$r = apply_filters( 'pmpro_is_level_expiring', $r, $level );
return $r;
}
/**
* Is this level expiring within one pay period
*
* @since 1.8.6.3
*
* @param object $level PMPro Level Object to test
*/
function pmpro_isLevelExpiringSoon( &$level ) {
if ( ! pmpro_isLevelExpiring( $level ) || empty( $level->enddate ) ) {
$r = false;
} else {
// days til expiration for the standard level
$standard = pmpro_getLevel( $level->id );
if ( ! empty( $standard->expiration_number ) ) {
if ( $standard->expiration_period == 'Hour' ) {
$days = $level->expiration_number;
} else if ( $standard->expiration_period == 'Day' ) {
$days = $level->expiration_number;
} elseif ( $standard->expiration_period == 'Week' ) {
$days = $level->expiration_number * 7;
} elseif ( $standard->expiration_period == 'Month' ) {
$days = $level->expiration_number * 30;
} elseif ( $standard->expiration_period == 'Year' ) {
$days = $level->expiration_number * 365;
}
} else {
$days = 30;
}
// are we within the days til expiration?
$now = current_time( 'timestamp' );
if( $standard->expiration_period == 'Hour' ){
if( $now + ( $days * 60 ) >= $level->enddate ){
$r = true;
} else {
$r = false;
}
} else if ( $now + ( $days * 3600 * 24 ) >= $level->enddate ) {
$r = true;
} else {
$r = false;
}
}
// filter
$r = apply_filters( 'pmpro_is_level_expiring_soon', $r, $level );
return $r;
}
function pmpro_getLevelCost( &$level, $tags = true, $short = false ) {
// initial payment
if ( ! $short ) {
$r = sprintf( __( 'The price for membership is <strong>%s</strong> now', 'paid-memberships-pro' ), pmpro_formatPrice( $level->initial_payment ) );
} else {
if ( pmpro_isLevelFree( $level ) ) {
$r = '<strong>' . __('Free', 'paid-memberships-pro' ) . '</strong>';
} else {
$r = sprintf( __( '<strong>%s</strong> now', 'paid-memberships-pro' ), pmpro_formatPrice( $level->initial_payment ) );
}
}
// recurring part
if ( (float)$level->billing_amount > 0 ) {
if ( $level->billing_limit > 1 ) {
if ( $level->cycle_number == '1' ) {
$r .= sprintf( __( ' and then <strong>%1$s per %2$s for %3$d more %4$s</strong>.', 'paid-memberships-pro' ), pmpro_formatPrice( $level->billing_amount ), pmpro_translate_billing_period( $level->cycle_period ), $level->billing_limit, pmpro_translate_billing_period( $level->cycle_period, $level->billing_limit ) );
} else {
$r .= sprintf( __( ' and then <strong>%1$s every %2$d %3$s for %4$d more payments</strong>.', 'paid-memberships-pro' ), pmpro_formatPrice( $level->billing_amount ), $level->cycle_number, pmpro_translate_billing_period( $level->cycle_period, $level->cycle_number ), $level->billing_limit );
}
} elseif ( $level->billing_limit == 1 ) {
$r .= sprintf( __( ' and then <strong>%1$s after %2$d %3$s</strong>.', 'paid-memberships-pro' ), pmpro_formatPrice( $level->billing_amount ), $level->cycle_number, pmpro_translate_billing_period( $level->cycle_period, $level->cycle_number ) );
} else {
if ( $level->billing_amount === $level->initial_payment ) {
if ( $level->cycle_number == '1' ) {
if ( ! $short ) {
$r = sprintf( __( 'The price for membership is <strong>%1$s per %2$s</strong>.', 'paid-memberships-pro' ), pmpro_formatPrice( $level->initial_payment ), pmpro_translate_billing_period( $level->cycle_period ) );
} else {
$r = sprintf( __( '<strong>%1$s per %2$s</strong>.', 'paid-memberships-pro' ), pmpro_formatPrice( $level->initial_payment ), pmpro_translate_billing_period( $level->cycle_period ) );
}
} else {
if ( ! $short ) {
$r = sprintf( __( 'The price for membership is <strong>%1$s every %2$d %3$s</strong>.', 'paid-memberships-pro' ), pmpro_formatPrice( $level->initial_payment ), $level->cycle_number, pmpro_translate_billing_period( $level->cycle_period, $level->cycle_number ) );
} else {
$r = sprintf( __( '<strong>%1$s every %2$d %3$s</strong>.', 'paid-memberships-pro' ), pmpro_formatPrice( $level->initial_payment ), $level->cycle_number, pmpro_translate_billing_period( $level->cycle_period, $level->cycle_number ) );
}
}
} else {
if ( $level->cycle_number == '1' ) {
$r .= sprintf( __( ' and then <strong>%1$s per %2$s</strong>.', 'paid-memberships-pro' ), pmpro_formatPrice( $level->billing_amount ), pmpro_translate_billing_period( $level->cycle_period ) );
} else {
$r .= sprintf( __( ' and then <strong>%1$s every %2$d %3$s</strong>.', 'paid-memberships-pro' ), pmpro_formatPrice( $level->billing_amount ), $level->cycle_number, pmpro_translate_billing_period( $level->cycle_period, $level->cycle_number ) );
}
}
}
} else {
$r .= '.';
}
// add a space
$r .= ' ';
// trial part
if ( $level->trial_limit ) {
if ( (float)$level->trial_amount == 0 ) {
if ( $level->trial_limit == '1' ) {
$r .= ' ' . __( 'After your initial payment, your first payment is Free.', 'paid-memberships-pro' );
} else {
$r .= ' ' . sprintf( __( 'After your initial payment, your first %d payments are Free.', 'paid-memberships-pro' ), $level->trial_limit );
}
} else {
if ( $level->trial_limit == '1' ) {
$r .= ' ' . sprintf( __( 'After your initial payment, your first payment will cost %s.', 'paid-memberships-pro' ), pmpro_formatPrice( $level->trial_amount ) );
} else {
$r .= ' ' . sprintf( __( 'After your initial payment, your first %1$d payments will cost %2$s.', 'paid-memberships-pro' ), $level->trial_limit, pmpro_formatPrice( $level->trial_amount ) );
}
}
}
// taxes part
$tax_state = get_option( 'pmpro_tax_state' );
$tax_rate = get_option( 'pmpro_tax_rate' );
if ( $tax_state && $tax_rate && ! pmpro_isLevelFree( $level ) ) {
$r .= sprintf( __( 'Customers in %1$s will be charged %2$s%% tax.', 'paid-memberships-pro' ), $tax_state, round( $tax_rate * 100, 2 ) );
}
if ( ! $tags ) {
$r = strip_tags( $r );
}
$r = apply_filters( 'pmpro_level_cost_text', $r, $level, $tags, $short ); // passing $tags and $short since v1.8
return $r;
}
/**
* Similar to pmpro_getLevelCost, but loops through all levels in the incoming array and puts it all together.
*/
function pmpro_getLevelsCost( &$levels, $tags = true, $short = false ) {
// let's build the array to work from to consolidate recurring info.
// recurpmts[cycle_period][cycle_number][billing_limit] = total_amount
$initpmt = 0;
$recurpmts = array();
$trialperiods = 0;
foreach ( $levels as $curlevel ) {
$initpmt += $curlevel->initial_payment;
if ( (float)$curlevel->billing_amount > 0 ) {
if ( array_key_exists( $curlevel->cycle_period, $recurpmts ) ) {
if ( array_key_exists( $curlevel->cycle_number, $recurpmts[ $curlevel->cycle_period ] ) ) {
if ( array_key_exists( $curlevel->billing_limit, $recurpmts[ $curlevel->cycle_period ][ $curlevel->cycle_number ] ) ) {
$recurpmts[ $curlevel->cycle_period ][ $curlevel->cycle_number ][ $curlevel->billing_limit ] += $curlevel->billing_amount;
} else {
$recurpmts[ $curlevel->cycle_period ][ $curlevel->cycle_number ][ $curlevel->billing_limit ] = $curlevel->billing_amount;
}
} else {
$recurpmts[ $curlevel->cycle_period ][ $curlevel->cycle_number ] = array();
$recurpmts[ $curlevel->cycle_period ][ $curlevel->cycle_number ][ $curlevel->billing_limit ] = $curlevel->billing_amount;
}
} else {
$recurpmts[ $curlevel->cycle_period ] = array();
$recurpmts[ $curlevel->cycle_period ][ $curlevel->cycle_number ] = array();
$recurpmts[ $curlevel->cycle_period ][ $curlevel->cycle_number ][ $curlevel->billing_limit ] = $curlevel->billing_amount;
}
}
if ( $curlevel->trial_limit && intval( $curlevel->trial_limit ) > $trialperiods ) {
$trialperiods = intval( $curlevel->trial_limit );
}
}
// initial payment
if ( ! $short ) {
$r = sprintf( __( 'The price for membership is <strong>%s</strong> now', 'paid-memberships-pro' ), pmpro_formatPrice( $initpmt ) );
} else {
$r = sprintf( __( '<strong>%s</strong> now', 'paid-memberships-pro' ), pmpro_formatPrice( $initpmt ) );
}
// recurring part
$billtextparts = array();
if ( count( $recurpmts ) > 0 ) {
foreach ( $recurpmts as $curperiod => $curpddata ) {
foreach ( $curpddata as $curcyclenum => $curcycledata ) {
foreach ( $curcycledata as $curbilllimit => $curtotal ) {
if ( $curbilllimit > 1 ) {
if ( $curcyclenum == '1' ) {
$billtextparts[] = sprintf( __( '<strong>%1$s per %2$s for %3$d more %4$s</strong>', 'paid-memberships-pro' ), pmpro_formatPrice( $curtotal ), pmpro_translate_billing_period( $curperiod ), $curbilllimit, pmpro_translate_billing_period( $curperiod, $curbilllimit ) );
} else {
$billtextparts[] = sprintf( __( '<strong>%1$s every %2$d %3$s for %4$d more payments</strong>', 'paid-memberships-pro' ), pmpro_formatPrice( $curtotal ), $curcyclenum, pmpro_translate_billing_period( $curperiod, $curcyclenum ), $curbilllimit );
}
} elseif ( $curbilllimit == 1 ) {
$billtextparts[] = sprintf( __( '<strong>%1$s after %2$d %3$s</strong>', 'paid-memberships-pro' ), pmpro_formatPrice( $curtotal ), $curcyclenum, pmpro_translate_billing_period( $curperiod, $curcyclenum ) );
} else {
if ( $curcyclenum == '1' ) {
$billtextparts[] = sprintf( __( '<strong>%1$s every %2$s</strong>', 'paid-memberships-pro' ), pmpro_formatPrice( $curtotal ), pmpro_translate_billing_period( $curperiod ) );
} else {
$billtextparts[] = sprintf( __( '<strong>%1$s every %2$d %3$s</strong>', 'paid-memberships-pro' ), pmpro_formatPrice( $curtotal ), $curcyclenum, pmpro_translate_billing_period( $curperiod, $curcyclenum ) );
}
}
}
}
}
$laststanza = array_pop( $billtextparts );
if ( count( $billtextparts ) > 0 ) {
$r .= ', ';
$r .= implode( ', ', $billtextparts );
}
$r .= ', and ' . $laststanza . '.';
} else {
$r .= '.';
}
// add a space
$r .= ' ';
// trial part - not as detailed as the single-level counterpart. Could be improved in the future.
if ( $trialperiods > 0 ) {
if ( $trialperiods == 1 ) {
$r .= __( 'Trial pricing has been applied to the first payment.', 'paid-memberships-pro' );
} else {
$r .= sprintf( __( 'Trial pricing has been applied to the first %d payments.', 'paid-memberships-pro' ), $trialperiods );
}
}
// taxes part
$tax_state = get_option( 'pmpro_tax_state' );
$tax_rate = get_option( 'pmpro_tax_rate' );
if ( $tax_state && $tax_rate && ! pmpro_areLevelsFree( $levels ) ) {
$r .= sprintf( __( 'Customers in %1$s will be charged %2$s%% tax.', 'paid-memberships-pro' ), $tax_state, round( $tax_rate * 100, 2 ) );
}
if ( ! $tags ) {
$r = strip_tags( $r );
}
/**
* Filter the levels cost text. Note the s in levels. Similar to pmpro_levels_cost_text
*/
$r = apply_filters( 'pmpro_levels_cost_text', $r, $levels, $tags, $short );
return $r;
}
function pmpro_getLevelExpiration( &$level ) {
if ( $level->expiration_number ) {
$expiration_text = sprintf( __( 'Membership expires after %1$d %2$s.', 'paid-memberships-pro' ), $level->expiration_number, pmpro_translate_billing_period( $level->expiration_period, $level->expiration_number ) );
} else {
$expiration_text = '';
}
$expiration_text = apply_filters( 'pmpro_levels_expiration_text', $expiration_text, $level );
$expiration_text = apply_filters( 'pmpro_level_expiration_text', $expiration_text, $level ); // Backwards compatible
return $expiration_text;
}
function pmpro_getLevelsExpiration( &$levels ) {
$expirystrings = array();
$ongoinglevelnum = 0;
if ( ! empty( $levels ) && ! is_array( $levels ) ) {
$levels = array( $levels );
} elseif ( empty( $levels ) ) {
$levels = array(); }
foreach ( $levels as $curlevel ) {
if ( $curlevel->expiration_number ) {
$expirystrings[] = sprintf( __( '%1$s membership expires after %2$d %3$s', 'paid-memberships-pro' ), $curlevel->name, $curlevel->expiration_number, pmpro_translate_billing_period( $curlevel->expiration_period, $curlevel->expiration_number ) );
} else {
$ongoinglevelnum++;
}
}
$expiration_text = '';
if ( count( $expirystrings ) > 0 ) {
$laststanza = array_pop( $expirystrings );
$expiration_text = implode( ', ', $expirystrings );
if ( count( $expirystrings ) > 0 ) {
$expiration_text .= ', and '; }
$expiration_text .= $laststanza;
$expiration_text .= '. ';
if ( $ongoinglevelnum > 0 ) {
$expiration_text .= 'The remaining membership';
if ( $ongoinglevelnum > 1 ) {
$expiration_text .= 's are';
} else {
$expiration_text .= ' is'; }
$expiration_text .= ' ongoing.';
}
}
/**
* Filter the levels expiration text. Note the s in levels. Similar to pmpro_levels_expiration_text
*/
$expiration_text = apply_filters( 'pmpro_levels_expiration_text', $expiration_text, $levels );
// Backwards compatible
if ( ! empty( $levels ) ) {
$first_level = reset($levels);
} else {
$first_level = false;
}
$expiration_text = apply_filters( 'pmpro_level_expiration_text', $expiration_text, $first_level );
return $expiration_text;
}
/**
* Get the text to display a membership's expiration date.
*
* @since 3.0
*
* @param object|int $level The level object or ID to get the expiration date for.
* @param WP_User|int $user The user object or ID to get the expiration date for.
*
* @return string The expiration date text.
*/
function pmpro_get_membership_expiration_text( $level, $user ) {
// If a user ID was passed, get the user object.
if ( is_numeric( $user ) ) {
$user = get_userdata( $user );
}
// Make sure that we have a user object.
if ( empty( $user ) || ! is_a( $user, 'WP_User' ) ) {
return '';
}
// If a level ID was passed, get the level object.
if ( is_numeric( $level ) ) {
$level = pmpro_getSpecificMembershipLevelForUser( $user->ID, (int)$level );
}
// Make sure that we have a level object.
if ( empty( $level ) || ! is_object( $level ) ) {
return '';
}
/**
* Filter to include the expiration time with expiration date.
* Used in adminpages/member-edit/pmpro-class-member-edit-panel-memberships.php.
*
* @param bool $show_time Whether to show the expiration time with expiration date.
*
* @return bool Whether to show the expiration time with expiration date.
*/
$show_time = apply_filters( 'pmpro_show_time_on_expiration_date', false );
// Generate the expiration date text.
if ( empty( $level->enddate ) ) {
// If the level does not have an enddate, show a dash (—).
$text = esc_html_x( '—', 'A dash is shown when there is no expiration date.', 'paid-memberships-pro' );
} elseif ( $show_time ) {
// Show the enddate with the time.
$text = sprintf(
// translators: %1$s is the date and %2$s is the time.
esc_html__( '%1$s at %2$s', 'paid-memberships-pro' ),
date_i18n( get_option( 'date_format'), $level->enddate ),
date_i18n( get_option( 'time_format'), $level->enddate )
);
} else {
// Show the enddate without the time.
$text = date_i18n( get_option( 'date_format' ), $level->enddate );
}
// Apply legacy filter pmpro_memberslist_expires_column.
if ( is_admin() && has_filter( 'pmpro_memberslist_expires_column' ) ) {
/**
* Legacy filter for showing the expiration date in the WP Dashboard.
*
* Note: Since level data is not passed, this filter is not MMPU-compatible.
*
* @deprecated 3.0 Use the pmpro_membership_expiration_text filter instead.
*
* @param string $text The expiration date text to show for this level.
* @param WP_User $user The user that the expiration date is for.
*
* @return string $text The expiration date text to show for this level.
*/
$text = apply_filters_deprecated( 'pmpro_memberslist_expires_column', array( $text, $user ), '3.0', 'pmpro_membership_expiration_text' );
}
// Apply legacy filter pmpro_account_membership_expiration_text.
if ( ! is_admin() && has_filter( 'pmpro_account_membership_expiration_text' ) ) {
/**
* Legacy filter for showing the expiration date on the frontend.
*
* @deprecated 3.0 Use the pmpro_membership_expiration_text filter instead.
*
* @param string $text The expiration date text to show for this level.
* @param object $level The level that the expiration date is for.
*
* @return string $text The expiration date text to show for this level.
*/
$text = apply_filters_deprecated( 'pmpro_account_membership_expiration_text', array( $text, $level ), '3.0', 'pmpro_membership_expiration_text' );
}
/**
* Filter the expiration date text to show for this level.
*
* @since 3.0
*
* @param string $text The expiration date text to show for this level.
* @param object $level The level that the expiration date is for.
* @param WP_User $user The user that the expiration date is for.
* @param bool $show_time Whether to show the expiration time with expiration date.
*
* @return string $text The expiration date text to show for this level.
*/
$text = apply_filters( 'pmpro_membership_expiration_text', $text, $level, $user, $show_time );
return $text;
}
/**
* pmpro_membership_level Meta Functions
*
* @ssince 1.8.6.5
*/
function add_pmpro_membership_level_meta( $level_id, $meta_key, $meta_value, $unique = false ) {
return add_metadata( 'pmpro_membership_level', $level_id, $meta_key, $meta_value, $unique );
}
function get_pmpro_membership_level_meta( $level_id, $key = '', $single = false ) {
return get_metadata( 'pmpro_membership_level', $level_id, $key, $single );
}
function update_pmpro_membership_level_meta( $level_id, $meta_key, $meta_value, $prev_value = '' ) {
return update_metadata( 'pmpro_membership_level', $level_id, $meta_key, $meta_value, $prev_value );
}
function delete_pmpro_membership_level_meta( $level_id, $meta_key, $meta_value = '' ) {
return delete_metadata( 'pmpro_membership_level', $level_id, $meta_key, $meta_value );
}
/**
* pmpro_membership_order Meta Functions
*/
function add_pmpro_membership_order_meta( $order_id, $meta_key, $meta_value, $unique = false ) {
return add_metadata( 'pmpro_membership_order', $order_id, $meta_key, $meta_value, $unique );
}
function get_pmpro_membership_order_meta( $order_id, $key = '', $single = false ) {
return get_metadata( 'pmpro_membership_order', $order_id, $key, $single );
}
function update_pmpro_membership_order_meta( $order_id, $meta_key, $meta_value, $prev_value = '' ) {
return update_metadata( 'pmpro_membership_order', $order_id, $meta_key, $meta_value, $prev_value );
}
function delete_pmpro_membership_order_meta( $order_id, $meta_key, $meta_value = '' ) {
return delete_metadata( 'pmpro_membership_order', $order_id, $meta_key, $meta_value );
}
/**
* pmpro_subscription Meta Functions
*/
function add_pmpro_subscription_meta( $subscription_id, $meta_key, $meta_value, $unique = false ) {
return add_metadata( 'pmpro_subscription', $subscription_id, $meta_key, $meta_value, $unique );
}
function get_pmpro_subscription_meta( $subscription_id, $key = '', $single = false ) {
return get_metadata( 'pmpro_subscription', $subscription_id, $key, $single );
}
function update_pmpro_subscription_meta( $subscription_id, $meta_key, $meta_value, $prev_value = '' ) {
return update_metadata( 'pmpro_subscription', $subscription_id, $meta_key, $meta_value, $prev_value );
}
function delete_pmpro_subscription_meta( $subscription_id, $meta_key, $meta_value = '' ) {
return delete_metadata( 'pmpro_subscription', $subscription_id, $meta_key, $meta_value );
}
function pmpro_hideAds() {
global $pmpro_display_ads;
return ! $pmpro_display_ads;
}
function pmpro_displayAds() {
global $pmpro_display_ads;
return $pmpro_display_ads;
}
/**
* Get the next payment date for a user.
*
* This function will only return the next payment date for the subscription with
* the most recent start date. For this reason, it is not MMPU-compatible.
*
* @since unknown
*
* @param int|null $user_id User ID. Defaults to the current user.
* @param string $order_status If value passed in is not "success", will get next payment date for a cancelled subscription.
* @param string $format Date format to return.
*
* @return string|bool Date of next payment, or false if no subscription is found.
*/
function pmpro_next_payment( $user_id = null, $order_status = 'success', $format = 'timestamp' ) {
global $wpdb, $current_user;
if ( ! $user_id ) {
$user_id = $current_user->ID;
}
if ( $user_id ) {
// Convert passed order status to a subscription status.
$subscription_status = ( $order_status === 'success' || ( is_array( $order_status ) && in_array( 'success', $order_status ) ) ) ? 'active' : 'cancelled';
$subscriptions = PMPro_Subscription::get_subscriptions_for_user( $user_id, null, $subscription_status );
if ( ! empty( $subscriptions ) ) {
return $subscriptions[0]->get_next_payment_date( $format );
}
}
return false;
}
if ( ! function_exists( 'last4' ) ) {
function last4( $t ) {
return substr( $t, strlen( $t ) - 4, 4 );
}
}
if ( ! function_exists( 'hideCardNumber' ) ) {
function hideCardNumber( $c, $dashes = true ) {
if ( $c ) {
if ( $dashes ) {
return 'XXXX-XXXX-XXXX-' . substr( $c, strlen( $c ) - 4, 4 );
} else {
return 'XXXXXXXXXXXX' . substr( $c, strlen( $c ) - 4, 4 );
}
} else {
return '';
}
}
}
// check for existing functions since we didn't use a prefix for this function
if ( ! function_exists( 'cleanPhone' ) ) {
/**
* Function to remove special characters from a phone number.
* NOTE: Could probably replace with preg_replace("[^0-9]", "", $phone)
*
* @since 1.0
*
* @param string $phone The phone number to clean.
*/
function cleanPhone( $phone ) {
// if a + is passed, just pass it along
if ( strpos( $phone, '+' ) !== false ) {
return $phone;
}
// clean the phone
$phone = str_replace( '-', '', $phone );
$phone = str_replace( '.', '', $phone );
$phone = str_replace( '(', '', $phone );
$phone = str_replace( ')', '', $phone );
$phone = str_replace( ' ', '', $phone );
return $phone;
}
}
// check for existing functions since we didn't use a prefix for this function
if ( ! function_exists( 'formatPhone' ) ) {
/**
* Function to format a phone number.
*
* @since 1.0
*
* @param string $phone The phone number to format.
*/
function formatPhone( $phone ) {
$r = cleanPhone( $phone );
if ( strlen( $r ) == 11 ) {
$r = substr( $r, 0, 1 ) . ' (' . substr( $r, 1, 3 ) . ') ' . substr( $r, 4, 3 ) . '-' . substr( $r, 7, 4 );
} elseif ( strlen( $r ) == 10 ) {
$r = '(' . substr( $r, 0, 3 ) . ') ' . substr( $r, 3, 3 ) . '-' . substr( $r, 6, 4 );
} elseif ( strlen( $r ) == 7 ) {
$r = substr( $r, 0, 3 ) . '-' . substr( $r, 3, 4 );
}
/**
* Filter to do more or less cleaning of phone numbers.
*
* @since 1.8.4.4
*
* @param string $r The formatted phone number.
* @param string $phone The original phone number.
*/
return apply_filters( 'pmpro_format_phone', $r, $phone );
}
}
function pmpro_showRequiresMembershipMessage() {
global $current_user, $post_membership_levels_names;
// get the correct message
if ( is_feed() ) {
$content = get_option( 'pmpro_rsstext' );
$content = str_replace( '!!levels!!', implode( ', ', $post_membership_levels_names ), $content );
} elseif ( $current_user->ID ) {
// not a member
$content = get_option( 'pmpro_nonmembertext' );
$content = str_replace( '!!levels!!', implode( ', ', $post_membership_levels_names ), $content );
} else {
// not logged in!
$content = get_option( 'pmpro_notloggedintext' );
$content = str_replace( '!!levels!!', implode( ', ', $post_membership_levels_names ), $content );
}
}
/**
* Function to check if a user has specified membership levels.
*
* pmpro_hasMembershipLevel() checks if the passed user is a member of the passed level
* $level may either be the ID or name of the desired membership_level. (or an array of such or a comma separated string)
* If $user_id is omitted, the value will be retrieved from $current_user.
*
* Return values:
* * Success returns boolean true.
* * Failure returns a string containing the error message.
*
* @since 2.12.3 Added support to pass comma separated value to $levels
* @since 1.8.5 Added 'e' option for expired members.
* @since 1.0.0
*
* @param mixed $levels The levels to check.
* @param int $user_id The user ID to check.
*
* @return bool Result of membership query.
*/
function pmpro_hasMembershipLevel( $levels = null, $user_id = null ) {
global $current_user, $wpdb;
$return = false;
if ( empty( $user_id ) ) {
$user_id = $current_user->ID;
}
if ( ! empty( $user_id ) && is_numeric( $user_id ) ) { // get membership levels for given user
$membership_levels = pmpro_getMembershipLevelsForUser( $user_id );
} else {
$membership_levels = null; // non-users don't have levels
}
if ( $levels === '0' || $levels === 0 ) {
$return = empty( $membership_levels );
} elseif ( empty( $levels ) ) {
$return = ! empty( $membership_levels );
} else {
if ( ! is_array( $levels ) ) {
// Check for a comma.
$levels_str = (string)$levels;
if ( strpos( $levels_str, ',' ) !== false ) {
// We have a string with at least 1 comma in it, turn it into an array.
$level_ids = explode( ',', $levels_str );
// Trim whitespace from the levels ids or names.
$levels = array_map( 'trim', $level_ids );
} else {
// No comma, but we want an array of levels.
$levels = array( $levels );
}
}
if ( empty( $membership_levels ) ) {
// check for negative level
$negative_level = false;
foreach ( $levels as $level ) {
if ( intval( $level ) < 0 ) {
$negative_level = true;
break;
}
}
// are we looking for non-members or not?
if ( $negative_level ) {
return true; // -1/etc, negative level
} elseif ( in_array( 0, $levels, true ) || in_array( '0', $levels ) ) {
$return = true; // 0 level
} elseif ( in_array( 'L', $levels ) || in_array( 'l', $levels ) ) {
$return = ( ! empty( $user_id ) && $user_id == $current_user->ID ); // L, logged in users
} elseif ( in_array( '-L', $levels ) || in_array( '-l', $levels ) ) {
$return = ( empty( $user_id ) || $user_id != $current_user->ID ); // -L, not logged in users
} elseif ( in_array( 'E', $levels ) || in_array( 'e', $levels ) ) {
$sql = "SELECT id FROM $wpdb->pmpro_memberships_users WHERE user_id = " . (int) $user_id . " AND status ='expired' LIMIT 1";
$expired = $wpdb->get_var( $sql ); // E, expired members
$return = ! empty( $expired );
}
} else {
foreach ( $levels as $level ) {
if ( strtoupper( $level ) == 'L' ) {
// checking if user is logged in
if ( ! empty( $user_id ) && $user_id == $current_user->ID ) {
$return = true;
}
} elseif ( strtoupper( $level ) == '-L' ) {
// checking if user is logged out
if ( empty( $user_id ) || $user_id != $current_user->ID ) {
$return = true;
}
} elseif ( $level === '0' || $level === 0 || strtoupper( $level ) === 'E' ) {
continue; // user with levels so not a "non-member" or expired
} else {
// checking a level id
$level_obj = pmpro_getLevel( is_numeric( $level ) ? abs( intval( $level ) ) : $level ); // make sure our level is in a proper format
if ( empty( $level_obj ) ) {
continue;} //invalid level
$found_level = false;
foreach ( $membership_levels as $membership_level ) {
if ( $membership_level->id == $level_obj->id || $membership_level->name == $level_obj->name) {
$found_level = true;
}
}
if ( is_numeric( $level ) && intval( $level ) < 0 && ! $found_level ) {
$return = true;
} elseif ( is_numeric( $level ) && intval( $level ) > 0 && $found_level ) {