-
Notifications
You must be signed in to change notification settings - Fork 29
/
pmpro-approvals.php
1891 lines (1561 loc) · 63.3 KB
/
pmpro-approvals.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
/*
Plugin Name: Paid Memberships Pro - Approvals Add On
Plugin URI: https://www.paidmembershipspro.com/add-ons/approval-process-membership/
Description: Grants administrators the ability to approve/deny memberships after signup.
Version: 1.6.1
Author: Stranger Studios
Author URI: https://www.paidmembershipspro.com
Text Domain: pmpro-approvals
Domain Path: /languages
*/
define( 'PMPRO_APP_DIR', dirname( __FILE__ ) );
/**
* Only load approvals after plugins have been loaded. Otherwise it may be loaded too early (e.g., before PMPro).
*/
function pmpro_approvals_plugins_loaded() {
require PMPRO_APP_DIR . '/classes/class.approvalemails.php';
}
add_action( 'plugins_loaded', 'pmpro_approvals_plugins_loaded' );
class PMPro_Approvals {
/*
Attributes
*/
private static $instance = null; // Refers to a single instance of this class.
/**
* Constructor
* Initializes the plugin by setting localization, filters, and administration functions.
*/
private function __construct() {
//activation/deactivation
register_activation_hook( __FILE__, array( 'PMPro_Approvals', 'activation' ) );
register_deactivation_hook( __FILE__, array( 'PMPro_Approvals', 'deactivation' ) );
//initialize the plugin
add_action( 'init', array( 'PMPro_Approvals', 'init' ) );
add_action( 'plugins_loaded', array( 'PMPro_Approvals', 'text_domain' ) );
//add support for PMPro Email Templates Add-on
add_filter( 'pmproet_templates', array( 'PMPro_Approvals', 'pmproet_templates' ) );
add_filter( 'pmpro_email_filter', array( 'PMPro_Approvals', 'pmpro_email_filter' ) );
//add support for PMPro BuddyPress Add On
add_filter( 'pmpro_bp_directory_sql_parts', array( 'PMPro_Approvals', 'buddypress_sql' ), 10, 2 );
}
/**
* Creates or returns an instance of this class.
*
* @return PMPro_Approvals A single instance of this class.
*/
public static function get_instance() {
if ( null == self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Run code on init.
*/
public static function init() {
//check that PMPro is active
if ( ! defined( 'PMPRO_VERSION' ) ) {
return;
}
//add admin menu items to 'Memberships' in WP dashboard and admin bar
add_action( 'admin_menu', array( 'PMPro_Approvals', 'admin_menu' ) );
add_action( 'admin_bar_menu', array( 'PMPro_Approvals', 'admin_bar_menu' ), 1000 );
add_action( 'admin_init', array( 'PMPro_Approvals', 'admin_init' ) );
//add user actions to the approvals page
add_filter( 'pmpro_approvals_user_row_actions', array( 'PMPro_Approvals', 'pmpro_approvals_user_row_actions' ), 10, 3 );
//add approval section to edit user page
$membership_level_capability = apply_filters( 'pmpro_edit_member_capability', 'manage_options' );
if ( current_user_can( $membership_level_capability ) ) {
//current user can change membership levels
add_action( 'pmpro_after_membership_level_profile_fields', array( 'PMPro_Approvals', 'show_user_profile_status' ), 5 );
} else {
//current user can't change membership level; use different hooks
add_action( 'edit_user_profile', array( 'PMPro_Approvals', 'show_user_profile_status' ) );
add_action( 'show_user_profile', array( 'PMPro_Approvals', 'show_user_profile_status' ) );
}
//check approval status at checkout
add_action( 'pmpro_checkout_preheader', array( 'PMPro_Approvals', 'pmpro_checkout_preheader' ) );
//add approval status to members list
add_action( 'pmpro_members_list_user', array( 'PMPro_Approvals', 'pmpro_members_list_user' ) );
//filter to add the approval membership level template
add_filter( 'pmpro_membershiplevels_template_level', array( 'PMPro_Approvals', 'pmpro_membershiplevels_template_level' ), 10, 2 );
//filter membership and content access
add_filter( 'pmpro_has_membership_level', array( 'PMPro_Approvals', 'pmpro_has_membership_level' ), 10, 3 );
add_filter( 'pmpro_has_membership_access_filter', array( 'PMPro_Approvals', 'pmpro_has_membership_access_filter' ), 10, 4 );
add_filter( 'pmpro_member_shortcode_access', array( 'PMPro_Approvals', 'pmpro_member_shortcode_access' ), 10, 4 );
//load checkbox in membership level edit page for users to select.
if ( defined( 'PMPRO_VERSION' ) && PMPRO_VERSION >= '2.9' ) {
add_action( 'pmpro_membership_level_before_billing_information', array( 'PMPro_Approvals', 'pmpro_membership_level_settings' ) );
} else {
add_action( 'pmpro_membership_level_after_other_settings', array( 'PMPro_Approvals', 'pmpro_membership_level_settings' ) );
}
add_action( 'pmpro_save_membership_level', array( 'PMPro_Approvals', 'pmpro_save_membership_level' ) );
//Add code for filtering checkouts, confirmation, and content filters
add_filter( 'pmpro_no_access_message_header', array( 'PMPro_Approvals', 'pmpro_no_access_message_header' ) ); // PMPro v3.1+.
add_filter( 'pmpro_no_access_message_body', array( 'PMPro_Approvals', 'pmpro_non_member_text_filter' ) ); // PMPro v3.1+.
add_filter( 'pmpro_non_member_text_filter', array( 'PMPro_Approvals', 'pmpro_non_member_text_filter' ) ); // Pre-PMPro 3.1
add_action( 'pmpro_account_bullets_top', array( 'PMPro_Approvals', 'pmpro_account_bullets_top' ) );
add_filter( 'pmpro_confirmation_message', array( 'PMPro_Approvals', 'pmpro_confirmation_message' ), 10, 2 );
add_action( 'pmpro_before_change_membership_level', array( 'PMPro_Approvals', 'pmpro_before_change_membership_level' ), 10, 4 );
add_action( 'pmpro_after_change_membership_level', array( 'PMPro_Approvals', 'pmpro_after_change_membership_level' ), 10, 2 );
//Integrate with Member Directory.
add_filter( 'pmpro_member_directory_sql_parts', array( 'PMPro_Approvals', 'pmpro_member_directory_sql_parts'), 10, 9 );
//Integrate with Pay By Check Add On
add_action( 'pmpro_approvals_after_approve_member', array( 'PMPro_Approvals', 'pmpro_pay_by_check_approve' ), 10, 2 );
//plugin row meta
add_filter( 'plugin_row_meta', array( 'PMPro_Approvals', 'plugin_row_meta' ), 10, 2 );
}
/**
* Run code on admin init
*/
public static function admin_init() {
//get role of administrator
$role = get_role( 'administrator' );
//add custom capability to administrator
$role->add_cap( 'pmpro_approvals' );
//make sure the current user has the updated cap
global $current_user;
setup_userdata( $current_user->ID );
}
/**
* Run code on activation
*/
public static function activation() {
//add Membership Approver role
remove_role( 'pmpro_approver' ); //in case we updated the caps below
add_role(
'pmpro_approver', 'Membership Approver', array(
'read' => true,
'pmpro_memberships_menu' => true,
'pmpro_memberslist' => true,
'pmpro_approvals' => true,
)
);
}
/**
* Run code on deactivation
*/
public static function deactivation() {
//remove Membership Approver role
remove_role( 'pmpro_approver' );
}
/**
* Create the submenu item 'Approvals' under the 'Memberships' link in WP dashboard.
* Fires during the "admin_menu" action.
*/
public static function admin_menu() {
global $menu, $submenu;
if ( ! defined( 'PMPRO_VERSION' ) ) {
return;
}
$approval_menu_text = __( 'Approvals', 'pmpro-approvals' );
$user_count = self::getApprovalCount();
if ( $user_count > 0 ) {
$approval_menu_text .= ' <span class="wp-core-ui wp-ui-notification pmpro-issue-counter" style="display: inline; padding: 1px 7px 1px 6px!important; border-radius: 50%; color: #fff; ">' . $user_count . '</span>';
foreach ( $menu as $key => $value ) {
if ( $menu[ $key ][1] === 'pmpro_memberships_menu' ) {
$menu[ $key ][0] .= ' <span class="update-plugins"><span class="update-count"> ' . $user_count . '</span></span>';
}
}
}
if ( ! defined( 'PMPRO_VERSION' ) ) {
return;
}
if ( version_compare( PMPRO_VERSION, '2.0' ) >= 0 ) {
add_submenu_page( 'pmpro-dashboard', __( 'Approvals', 'pmpro-approvals' ), $approval_menu_text, 'pmpro_approvals', 'pmpro-approvals', array( 'PMPro_Approvals', 'admin_page_approvals' ) );
} else {
add_submenu_page( 'pmpro-membershiplevels', __( 'Approvals', 'pmpro-approvals' ), $approval_menu_text, 'pmpro_approvals', 'pmpro-approvals', array( 'PMPro_Approvals', 'admin_page_approvals' ) );
}
}
/**
* Create 'Approvals' link under the admin bar link 'Memberships'.
* Fires during the "admin_bar_menu" action.
*/
public static function admin_bar_menu() {
global $wp_admin_bar;
//check capabilities (TODO: Define a new capability (pmpro_approvals) for managing approvals.)
if ( ! is_super_admin() && ! current_user_can( 'pmpro_approvals' ) || ! is_admin_bar_showing() ) {
return;
}
//default title for admin bar menu
$title = __( 'Approvals', 'pmpro-approvals' );
$user_count = self::getApprovalCount();
//if returned data contains pending users, adjust the title of the admin bar menu.
if ( $user_count > 0 ) {
$title .= ' <span class="wp-core-ui wp-ui-notification pmpro-issue-counter" style="display: inline; padding: 1px 7px 1px 6px!important; border-radius: 50%; color: #fff; background:red; background:#CA4A1E;">' . $user_count . '</span>';
}
//add the admin link
$wp_admin_bar->add_menu(
array(
'id' => 'pmpro-approvals',
'title' => $title,
'href' => get_admin_url( null, '/admin.php?page=pmpro-approvals' ),
'parent' => 'paid-memberships-pro',
)
);
}
/**
* Load the Approvals admin page.
*/
public static function admin_page_approvals() {
if ( ! empty( $_REQUEST['user_id'] ) ) {
require_once dirname( __FILE__ ) . '/adminpages/userinfo.php';
} else {
require_once dirname( __FILE__ ) . '/adminpages/approvals.php';
}
}
/**
* Get options for level.
*/
public static function getOptions( $level_id = null ) {
$options = get_option( 'pmproapp_options', array() );
if ( ! empty( $level_id ) ) {
if ( ! empty( $options[ $level_id ] ) ) {
$r = $options[ $level_id ];
} else {
$r = array(
'requires_approval' => 0,
'restrict_checkout' => 0,
);
}
} else {
$r = $options;
//clean up extra values that were accidentally stored in here in old versions
if ( isset( $r['requires_approval'] ) ) {
unset( $r['requires_approval'] );
}
if ( isset( $r['restrict_checkout'] ) ) {
unset( $r['restrict_checkout'] );
}
}
return $r;
}
/**
* Save options for level.
*/
public static function saveOptions( $options ) {
update_option( 'pmproapp_options', $options, 'no' );
}
/**
* Check if a level requires approval
*/
public static function requiresApproval( $level_id = null ) {
//no level?
if ( empty( $level_id ) ) {
return false;
}
$options = self::getOptions( $level_id );
$requires_approval = apply_filters( 'pmpro_approvals_level_requires_approval', $options['requires_approval'], $level_id);
return $requires_approval;
}
/**
* Check if level has a restriction level at checkout
*/
public static function restrictCheckout( $level_id = null ) {
//no level?
if ( empty( $level_id ) ) {
return false;
}
$options = self::getOptions( $level_id );
$restrict_checkout = apply_filters( 'pmpro_approvals_level_restrict_checkout', $options['restrict_checkout'], $level_id);
return $restrict_checkout;
}
/**
* Load check box to make level require membership.
*/
public static function pmpro_membership_level_settings() {
$level_id = $_REQUEST['edit'];
// Get the template if passed in the URL.
if ( isset( $_REQUEST['template'] ) ) {
$template = sanitize_text_field( $_REQUEST['template'] );
} else {
$template = false;
}
// Get approval settings or set defaults if this is a new approvals level.
if ( $level_id > 0 ) {
$options = self::getOptions( $level_id );
} elseif ( $template === 'approvals' ) {
$options = array(
'requires_approval' => true,
'restrict_checkout' => true,
);
} else {
$options = array(
'requires_approval' => false,
'restrict_checkout' => false,
);
}
//figure out approval_setting from the actual options
if ( ! $options['requires_approval'] && ! $options['restrict_checkout'] ) {
$approval_setting = 0;
} elseif ( $options['requires_approval'] && ! $options['restrict_checkout'] ) {
$approval_setting = 1;
} elseif ( ! $options['requires_approval'] && $options['restrict_checkout'] ) {
$approval_setting = 2;
} else {
$approval_setting = 3;
}
//get all levels for which level option
$levels = pmpro_getAllLevels( true, true );
if ( isset( $levels[ $level_id ] ) ) {
unset( $levels[ $level_id ] ); //remove this level
}
// Hide or show this section based on settings
if ( $template === 'approvals' || $approval_setting > 0 ) {
$section_visibility = 'shown';
$section_activated = 'true';
} else {
$section_visibility = 'hidden';
$section_activated = 'false';
}
?>
<div id="approval-settings" class="pmpro_section" data-visibility="<?php echo esc_attr( $section_visibility ); ?>" data-activated="<?php echo esc_attr( $section_activated ); ?>">
<div class="pmpro_section_toggle">
<button class="pmpro_section-toggle-button" type="button" aria-expanded="<?php echo $section_visibility === 'hidden' ? 'false' : 'true'; ?>">
<span class="dashicons dashicons-arrow-<?php echo $section_visibility === 'hidden' ? 'down' : 'up'; ?>-alt2"></span>
<?php esc_html_e( 'Approval Settings', 'pmpro-approvals' ); ?>
</button>
</div>
<div class="pmpro_section_inside" <?php echo $section_visibility === 'hidden' ? 'style="display: none"' : ''; ?>>
<table class="form-table">
<tbody>
<tr>
<th scope="row" valign="top"><label for="approval_setting"><?php esc_html_e( 'Requires Approval?', 'pmpro-approvals' ); ?></label></th>
<td>
<select id="approval_setting" name="approval_setting">
<option value="0" <?php selected( $approval_setting, 0 ); ?>><?php esc_html_e( 'No.', 'pmpro-approvals' ); ?></option>
<option value="1" <?php selected( $approval_setting, 1 ); ?>><?php esc_html_e( 'Yes. Admin must approve new members for this level.', 'pmpro-approvals' ); ?></option>
<?php if ( ! empty( $levels ) ) { ?>
<option value="2" <?php selected( $approval_setting, 2 ); ?>><?php esc_html_e( 'Yes. User must have an approved membership for a different level.', 'pmpro-approvals' ); ?></option>
<option value="3" <?php selected( $approval_setting, 3 ); ?>><?php esc_html_e( 'Yes. User must have an approved membership for a different level AND admin must approve new members for this level.', 'pmpro-approvals' ); ?></option>
<?php } ?>
</select>
</td>
</tr>
<?php if ( ! empty( $levels ) ) { ?>
<tr
<?php
if ( $approval_setting < 2 ) {
?>
style="display: none;"<?php } ?>>
<th scope="row" valign="top"><label for="approval_restrict_level"><?php esc_html_e( 'Which Level?', 'pmpro-approvals' ); ?></label></th>
<td>
<select id="approval_restrict_level" name="approval_restrict_level">
<?php
foreach ( $levels as $level ) {
?>
<option value="<?php echo $level->id; ?>" <?php selected( $options['restrict_checkout'], $level->id ); ?>><?php echo $level->name; ?></option>
<?php
}
?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php if ( ! empty( $levels ) ) { ?>
<script>
jQuery(document).ready(function() {
function pmproap_toggleWhichLevel() {
if(jQuery('#approval_setting').val() > 1)
jQuery('#approval_restrict_level').closest('tr').show();
else
jQuery('#approval_restrict_level').closest('tr').hide();
}
//bind to approval setting change
jQuery('#approval_setting').change(function() { pmproap_toggleWhichLevel(); });
//run on load
pmproap_toggleWhichLevel();
});
</script>
<?php } ?>
</div> <!-- end pmpro_section_inside -->
</div> <!-- end pmpro_section -->
<?php
}
/**
* Save settings when editing the membership level
* Fires on pmpro_save_membership_level
*/
public static function pmpro_save_membership_level( $level_id ) {
global $msg, $msgt, $saveid, $edit;
//get value
if ( ! empty( $_REQUEST['approval_setting'] ) ) {
$approval_setting = intval( $_REQUEST['approval_setting'] );
} else {
$approval_setting = 0;
}
if ( ! empty( $_REQUEST['approval_restrict_level'] ) ) {
$restrict_checkout = intval( $_REQUEST['approval_restrict_level'] );
} else {
$restrict_checkout = 0;
}
//figure out requires_approval and restrict_checkout value from setting
if ( $approval_setting == 1 ) {
$requires_approval = 1;
$restrict_checkout = 0;
} elseif ( $approval_setting == 2 ) {
$requires_approval = 0;
//restrict_checkout set correctly above from input, but check that a level was chosen
} elseif ( $approval_setting == 3 ) {
$requires_approval = 1;
//restrict_checkout set correctly above from input, but check that a level was chosen
} else {
//assume 0, all off
$requires_approval = 0;
$restrict_checkout = 0;
}
//get options
$options = self::getOptions();
//create array if we don't have options for this level already
if ( empty( $options[ $level_id ] ) ) {
$options[ $level_id ] = array();
}
//update options
$options[ $level_id ]['requires_approval'] = $requires_approval;
$options[ $level_id ]['restrict_checkout'] = $restrict_checkout;
//save it
self::saveOptions( $options );
}
/**
* Deny access to member content if user is not approved
* Fires on pmpro_has_membership_access_filter
*/
public static function pmpro_has_membership_access_filter( $access, $post, $user, $levels ) {
//if we don't have access now, we still won't
if ( ! $access ) {
return $access;
}
//no user, this must be open to everyone
if ( empty( $user ) || empty( $user->ID ) ) {
return $access;
}
//no levels, must be open
if ( empty( $levels ) ) {
return $access;
}
// If the current user doesn't have a level, bail.
if ( ! pmpro_hasMembershipLevel() ) {
return $access;
}
//now we need to check if the user is approved for ANY of the $levels
$access = false; //assume no access
foreach ( $levels as $level ) {
if ( self::isApproved( $user->ID, $level->id ) ) {
$access = true;
break;
}
}
return $access;
}
/**
* Deny access for shortcode specific content to pending members.
* @since 1.4
*/
public static function pmpro_member_shortcode_access( $access, $content, $levels, $delay ) {
global $current_user;
// Bail if they are not logged-in, default behavior.
if ( ! is_user_logged_in() ) {
return $access;
}
// Bail if the user is logged in but doesn't have a membership level.
if ( ! pmpro_hasMembershipLevel() ) {
return $access;
}
// If no levels are defined but they aren't approved. Let's set this to false.
if ( empty( $levels ) && ! self::isApproved( $current_user->ID ) ) {
return false;
}
if ( self::isApproved( $current_user->ID ) ) {
return $access;
}
return $access;
}
/**
* Filter hasMembershipLevel to return false
* if a user is not approved.
* Fires on pmpro_has_membership_level filter
*/
public static function pmpro_has_membership_level( $haslevel, $user_id, $levels ) {
global $pmpro_pages;
// Let members access PMPro pages, PMPro can handle the cases here.
if ( is_page( $pmpro_pages ) ) {
return $haslevel;
}
//if already false, skip
if ( ! $haslevel ) {
return $haslevel;
}
// Show the real levels in the admin.
if ( is_admin() ) {
return $haslevel;
}
//no user, skip
if ( empty( $user_id ) ) {
return $haslevel;
}
//no levels, skip
if ( empty( $levels ) ) {
return $haslevel;
}
// If the current user doesn't have a level, bail.
if ( ! pmpro_hasMembershipLevel( null, $user_id ) ) {
return $haslevel;
}
//now we need to check if the user is approved for ANY of the $levels
$haslevel = false;
foreach ( $levels as $level ) {
if ( self::isApproved( $user_id, $level ) ) {
$haslevel = true;
break;
}
}
return $haslevel;
}
/**
* Show potential errors on the checkout page.
* Note that the precense of these errors will halt checkout as well.
*/
public static function pmpro_checkout_preheader() {
global $pmpro_level, $current_user;
//are they denied for this level?
if ( self::isDenied( null, $pmpro_level->id ) ) {
pmpro_setMessage( __( 'Your previous application for this level has been denied. You will not be allowed to check out.', 'pmpro-approvals' ), 'pmpro_error' );
}
//does this level require approval of another level?
$restrict_checkout = self::restrictCheckout( $pmpro_level->id );
if ( $restrict_checkout ) {
$other_level = pmpro_getLevel( $restrict_checkout );
//check that they are approved and not denied for that other level
if ( self::isDenied( null, $restrict_checkout ) ) {
pmpro_setMessage( sprintf( __( 'Since your application to the %s level has been denied, you may not check out for this level.', 'pmpro-approvals' ), $other_level->name ), 'pmpro_error' );
} elseif ( self::isPending( null, $restrict_checkout ) ) {
//note we use pmpro_getMembershipLevelForUser instead of pmpro_hasMembershipLevel because the latter is filtered
$user_level = pmpro_getMembershipLevelForUser( $current_user->ID );
if ( ! empty( $user_level ) && $user_level->id == $other_level->id ) {
//already applied but still pending
pmpro_setMessage( sprintf( __( 'Your application to %s is still pending.', 'pmpro-approvals' ), $other_level->name ), 'pmpro_error' );
} else {
//haven't applied yet, check if the level is hidden
if ( isset( $other_level->hidden ) && true == $other_level->hidden ) {
pmpro_setMessage( sprintf( __( 'You must be approved for %s before checking out here.', 'pmpro-approvals' ), $other_level->name ), 'pmpro_error' );
} else {
pmpro_setMessage( sprintf( __( 'You must register and be approved for <a href="%1$s">%2$s</a> before checking out here.', 'pmpro-approvals' ), pmpro_url( 'checkout', '?level=' . $other_level->id ), $other_level->name ), 'pmpro_error' );
}
}
}
}
}
/**
* Get User Approval Meta
*/
public static function getUserApproval( $user_id = null, $level_id = null ) {
//default to false
$user_approval = false; //false will function as a kind of N/A at times
//default to the current user
if ( empty( $user_id ) ) {
global $current_user;
$user_id = $current_user->ID;
}
//get approval status for this level from user meta
if ( ! empty( $user_id ) ) {
//default to the user's current level
if ( empty( $level_id ) ) {
_doing_it_wrong( __FUNCTION__, __( 'You should pass a level ID to getUserApproval.', 'pmpro-approvals' ), '1.5' );
$level = pmpro_getMembershipLevelForUser( $user_id );
if ( ! empty( $level ) ) {
$level_id = $level->id;
}
}
//if we have a level, check if it requires approval and if so check user meta
if ( ! empty( $level_id ) && self::hasMembershipLevelSansApproval( $level_id, $user_id ) ) {
//if the level doesn't require approval, then the user is approved
if ( ! self::requiresApproval( $level_id ) ) {
//approval not required, so return status approved
$user_approval = array( 'status' => 'approved' );
} else {
//approval required, check user meta
$user_approval = get_user_meta( $user_id, 'pmpro_approval_' . $level_id, true );
}
}
}
return $user_approval;
}
/**
* Returns status of a given or current user. Returns 'approved', 'denied' or 'pending'.
* If the users level does not require approval it will not return anything.
*/
public static function getUserApprovalStatus( $user_id = null, $level_id = null, $short = true ) {
global $current_user;
//check if user ID is blank, set to current user ID.
if ( empty( $user_id ) ) {
$user_id = $current_user->ID;
}
//get the PMPro level for the user
if ( empty( $level_id ) ) {
_doing_it_wrong( __FUNCTION__, __( 'You should pass a level ID to getUserApprovalStatus.', 'pmpro-approvals' ), '1.5' );
$level = pmpro_getMembershipLevelForUser( $user_id );
if ( ! empty( $level ) ) {
$level_id = $level->ID;
}
} else {
$level = pmpro_getLevel( $level_id );
}
//make sure we have a user and level by this point
if ( empty( $user_id ) || empty( $level_id ) ) {
return false;
}
//check if level requires approval.
if ( ! self::requiresApproval( $level_id ) ) {
return;
}
//Get the user approval status. If it's not Approved/Denied it's set to Pending.
if ( ! self::isPending( $user_id, $level_id ) ) {
$approval_data = self::getUserApproval( $user_id, $level_id );
if ( $short ) {
if ( ! empty( $approval_data ) ) {
$status = $approval_data['status'];
} else {
$status = __( 'approved', 'pmpro-approvals' );
}
} else {
if ( ! empty( $approval_data ) ) {
$approver = get_userdata( $approval_data['who'] );
if ( current_user_can( 'edit_users' ) ) {
$approver_text = '<a href="' . get_edit_user_link( $approver->ID ) . '">' . esc_attr( $approver->display_name ) . '</a>';
} elseif ( current_user_can( 'pmpro_approvals' ) ) {
$approver_text = $approver->display_name;
} else {
$approver_text = '';
}
if ( $approver_text ) {
$status = sprintf( __( '%1$s on %2$s by %3$s', 'pmpro-approvals' ), ucwords( $approval_data['status'] ), date_i18n( get_option( 'date_format' ), $approval_data['timestamp'] ), $approver_text );
} else {
$status = sprintf( __( '%1$s on %2$s', 'pmpro-approvals' ), ucwords( $approval_data['status'] ), date_i18n( get_option( 'date_format' ), $approval_data['timestamp'] ) );
}
} else {
$status = __( 'Approved', 'pmpro-approvals' );
}
}
} else {
if ( $short ) {
$status = __( 'pending', 'pmpro-approvals' );
} else {
$status = sprintf( __( 'Pending Approval for %s', 'pmpro-approvals' ), $level->name );
}
}
$status = apply_filters( 'pmpro_approvals_status_filter', $status, $user_id, $level_id );
return $status;
}
/**
* Get user approval statuses for all levels that require approval
* Level IDs are used for the index the array
*/
public static function getUserApprovalStatuses( $user_id = null, $short = false ) {
//default to current user
if ( empty( $user_id ) ) {
global $current_user;
$user_id = $current_user->ID;
}
$approval_levels = self::getApprovalLevels();
$r = array();
foreach ( $approval_levels as $level_id ) {
$r[ $level_id ] = self::getUserApprovalStatus( $user_id, $level_id, $short );
}
return $r;
}
/**
* Check if a user is approved.
*/
public static function isApproved( $user_id = null, $level_id = null ) {
//default to the current user
if ( empty( $user_id ) ) {
global $current_user;
$user_id = $current_user->ID;
}
//get approval for this user/level
$user_approval = self::getUserApproval( $user_id, $level_id );
if ( empty( $user_approval ) || ! is_array( $user_approval ) ) {
// Check if the user had this level before it was set to require approval.
if ( ! empty( $level_id ) && self::hasMembershipLevelSansApproval( $level_id, $user_id ) ) {
$user_approval = array( 'status' => 'approved' );
} else {
$user_approval = array( 'status' => 'pending' );
}
}
/**
* @filter pmproap_user_is_approved - Filter to override whether the user ID is approved for access to for the level ID
*
* @param bool $is_approved - Whether the $user_id is approved for the specified $level_id
* @param int $user_id - The ID of the User being tested for approval
* @param int $level_id - The ID of the Membership Level the $user_id is being thested for approval
* @param array $user_approval - The approval status information for the user_id/level_id
*
* @return bool
*/
return apply_filters( 'pmproap_user_is_approved', ( 'approved' == $user_approval['status'] ? true : false ), $user_id, $level_id, $user_approval );
}
/**
* Check if a user is approved.
*/
public static function isDenied( $user_id = null, $level_id = null ) {
//get approval for this user/level
$user_approval = self::getUserApproval( $user_id, $level_id );
//if no array, return false
if ( empty( $user_approval ) || ! is_array( $user_approval ) ) {
return false;
}
/**
* @filter pmproap_user_is_denied - Filter to override whether the user ID is denied for access to the level ID
*
* @param bool $is_denied - Whether the $user_id is denied for the specified $level_id
* @param int $user_id - The ID of the User being tested for approval
* @param int $level_id - The ID of the Membership Level the $user_id is being thested for approval
* @param array $user_approval - The approval status information for the user_id/level_id
*
* @return bool
*/
return apply_filters( 'pmproap_user_is_denied', ( 'denied' == $user_approval['status'] ? true : false ), $user_id, $level_id, $user_approval );
}
/**
* Check if a user is pending
*/
public static function isPending( $user_id = null, $level_id = null ) {
//default to the current user
if ( empty( $user_id ) ) {
global $current_user;
$user_id = $current_user->ID;
}
//get approval for this user/level
$user_approval = self::getUserApproval( $user_id, $level_id );
//if no array, check if they already had the level
if ( empty( $user_approval ) || ! is_array( $user_approval ) ) {
// Check if the user had this level before it was set to require approval.
if ( ! empty( $level_id ) && self::hasMembershipLevelSansApproval( $level_id, $user_id ) ) {
return false;
} else {
return true;
}
}
/**
* @filter pmproap_user_is_pending - Filter to override whether the user ID is pending access to the level ID
*
* @param bool $is_pending - Whether the $user_id is pending for the specified $level_id
* @param int $user_id - The ID of the User being tested for approval
* @param int $level_id - The ID of the Membership Level the $user_id is being thested for approval
* @param array $user_approval - The approval status information for the user_id/level_id
*
* @return bool
*/
return apply_filters( 'pmproap_user_is_pending', ( 'pending' == $user_approval['status'] ? true : false ), $user_id, $level_id, $user_approval );
}
/**
* Get levels that require approval
*/
public static function getApprovalLevels() {
$options = self::getOptions();
$r = array();
foreach ( $options as $level_id => $level_options ) {
if ( $level_options['requires_approval'] ) {
$r[] = $level_id;
}
}
return $r;
}
/**
* Get list of approvals
*/
public static function getApprovals( $l = false, $s = '', $status = 'pending', $sortby = 'user_registered', $sortorder = 'ASC', $pn = 1, $limit = 15 ) {
global $wpdb;
$end = $pn * $limit;
$start = $end - $limit;
$sql_parts = array();
$sql_parts['SELECT'] = "SELECT SQL_CALC_FOUND_ROWS u.ID, u.user_login, u.user_email, UNIX_TIMESTAMP(u.user_registered) as joindate, mu.membership_id, mu.initial_payment, mu.billing_amount, mu.cycle_period, mu.cycle_number, mu.billing_limit, mu.trial_amount, mu.trial_limit, UNIX_TIMESTAMP(mu.startdate) as startdate, UNIX_TIMESTAMP(mu.enddate) as enddate, m.name as membership FROM $wpdb->users u ";
$sql_parts['JOIN'] = "LEFT JOIN $wpdb->pmpro_memberships_users mu ON u.ID = mu.user_id LEFT JOIN $wpdb->pmpro_membership_levels m ON mu.membership_id = m.id ";
$sql_parts['WHERE'] = "WHERE mu.status = 'active' AND mu.membership_id > 0 ";
$sql_parts['GROUP'] = "";
$sql_parts['LIMIT'] = "LIMIT $start, $limit";
if ( ! empty( $status ) && $status != 'all' ) {
$sql_parts['JOIN'] .= "LEFT JOIN $wpdb->usermeta um ON um.user_id = u.ID AND um.meta_key LIKE CONCAT('pmpro_approval_', mu.membership_id) ";
}
if ( ! empty( $s ) ) {
$sql_parts['WHERE'] .= "AND (u.user_login LIKE '%" . esc_sql( $s ) . "%' OR u.user_email LIKE '%" . esc_sql( $s ) . "%' OR u.display_name LIKE '%" . esc_sql( $s ) . "%') ";
}
if ( $l ) {
$sql_parts['WHERE'] .= "AND mu.membership_id = '" . esc_sql( $l ) . "' ";
} else {
$sql_parts['WHERE'] .= "AND mu.membership_id IN(" . implode( ',', self::getApprovalLevels() ) . ") ";
}
if ( ! empty( $status ) && $status != 'all' ) {
$sql_parts['WHERE'] .= "AND um.meta_value LIKE '%\"" . esc_sql( $status ) . "\"%' ";
}
if ( $sortby == 'pmpro_approval' ) {
$sql_parts['ORDER'] = "ORDER BY (um2.meta_value IS NULL) $sortorder ";
} else {
$sql_parts['ORDER'] = "ORDER BY $sortby $sortorder ";
}
/**
* Filters SQL parts for the query to fetch all users pending approval.
*
* @since
*
* @param array $sql_parts The current SQL query parts
* @param int $l Level ID
* @param string $s Search string
* @param string $status Approval status
* @param string $sortby Sort by
* @param string $sortby Sort order
* @param int $pn Results page number
* @param int $limit Number of results per page limit
*
*/
$sql_parts = apply_filters(
'pmpro_approvals_pending_approvals_sql_parts',
$sql_parts,
$l,
$s,
$status,
$sortby,
$sortorder ,
$pn,
$limit
);
$sqlQuery = $sql_parts['SELECT'] . $sql_parts['JOIN'] . $sql_parts['WHERE'] . $sql_parts['GROUP'] . $sql_parts['ORDER'] . $sql_parts['LIMIT'];
/**
* Filters final SQL string for the query to fetch all users pending approval.
*
* @since
*
* @param array $sqlQuery The current SQL query
* @param int $l Level ID
* @param string $s Search string
* @param string $status Approval status
* @param string $sortby Sort by
* @param string $sortby Sort order
* @param int $pn Results page number
* @param int $limit Number of results per page limit
*
*/
$sqlQuery = apply_filters(
'pmpro_approvals_pending_approvals_sql',
$sqlQuery,
$l,
$s,
$status,
$sortby,
$sortorder ,
$pn,
$limit
);
$theusers = $wpdb->get_results( $sqlQuery );
return $theusers;
}