-
Notifications
You must be signed in to change notification settings - Fork 3
/
pmpro-certification-levels.php
163 lines (144 loc) · 5.18 KB
/
pmpro-certification-levels.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
<?php
/*
Plugin Name: Paid Memberships Pro - Certification Levels Addon
Plugin URI: http://www.paidmembershipspro.com/wp/pmpro-certification-levels/
Description: Adds a "certification level" field to the user profile which determines which levels are available to a user.
Version: .1
Author: Stranger Studios
Author URI: http://www.strangerstudios.com
*/
/*
Add certification level value to edit user profile
*/
//show the shipping address in the profile
function pmprocl_show_extra_profile_fields($user)
{
if(!current_user_can('edit_users'))
return;
$certification_level = $user->certification_level;
?>
<h3><?php _e('Certification Level', 'pmpro');?></h3>
<table class="form-table">
<tr>
<th><?php _e('Level', 'pmpro');?></th>
<td>
<input id="certification_level" name="certification_level" type="text" size="5" value="<?php echo esc_attr($certification_level);?>" /> <small>Whole numbers only.</small>
</td>
</tr>
</table>
<?php
}
add_action( 'show_user_profile', 'pmprocl_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'pmprocl_show_extra_profile_fields' );
function pmprocl_save_extra_profile_fields( $user_id )
{
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
if(isset($_POST['certification_level']) && current_user_can('edit_users'))
update_usermeta( $user_id, 'certification_level', intval($_POST['certification_level'] ));
}
add_action( 'personal_options_update', 'pmprocl_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'pmprocl_save_extra_profile_fields' );
/*
add column to export
*/
//columns
function pmprocl_pmpro_members_list_csv_extra_columns($columns)
{
$new_columns = array(
"certification_level" => "pmprocl_extra_column_certification_level",
);
$columns = array_merge($columns, $new_columns);
return $columns;
}
add_filter("pmpro_members_list_csv_extra_columns", "pmprocl_pmpro_members_list_csv_extra_columns");
//callback
function pmprocl_extra_column_certification_level($user)
{
return $user->certification_level;
}
/*
Add certification level value to edit levels page
*/
//show the checkbox on the edit level page
function pmprocl_pmpro_membership_level_after_other_settings()
{
$level_id = intval($_REQUEST['edit']);
if($level_id > 0)
$certification_level = get_option('pmpro_certification_level_' . $level_id);
else
$certification_level = 0;
?>
<h3 class="topborder">Require Certification Level</h3>
<p>Only users with a certification level equal to or greater than the value above will be able to checkout for this level. Users and levels default to certification level 0.</p>
<table>
<tbody class="form-table">
<tr>
<th scope="row" valign="top"><label for="certification_level"><?php _e('Certification Level Required:', 'pmpro');?></label></th>
<td>
<input id="certification_level" name="certification_level" type="text" size="5" value="<?php echo esc_attr($certification_level);?>" /> <small>Whole numbers only.</small>
</td>
</tr>
</tbody>
</table>
<?php
}
add_action('pmpro_membership_level_after_other_settings', 'pmprocl_pmpro_membership_level_after_other_settings');
//save hide shipping setting when the level is saved/added
function pmprocl_pmpro_save_membership_level($level_id)
{
if(isset($_REQUEST['certification_level']))
$certification_level = intval($_REQUEST['certification_level']);
else
$certification_level = 0;
delete_option('pmpro_certification_level_' . $level_id);
add_option('pmpro_certification_level_' . $level_id, $certification_level, "", "no");
}
add_action("pmpro_save_membership_level", "pmprocl_pmpro_save_membership_level");
/*
Check certification level at checkout.
*/
function pmprocl_template_redirect()
{
//make sure pmpro is activated
if(!function_exists('pmpro_url'))
return;
//check if we're on front end and looking for a level
if(!is_admin() && !empty($_REQUEST['level']))
{
//make sure this is a checkout page
global $post;
if(!empty($post) && strpos($post->post_content, "pmpro_checkout") !== false)
{
//see if level requires certification
$level_certification_level = get_option('pmpro_certification_level_' . intval($_REQUEST['level']));
if(!empty($level_certification_level))
{
//must be logged in and have a certification level !=
if(is_user_logged_in())
{
global $current_user;
$user_certification_level = $current_user->certification_level;
if($user_certification_level >= $level_certification_level);
return; //we're good, don't redirect away
}
//if we get here, need to redirect back to levels page
$url = add_query_arg('notcertified', '1', pmpro_url('levels'));
wp_redirect($url);
exit;
}
}
}
}
add_action('template_redirect', 'pmprocl_template_redirect');
/*
Show message on levels page if not certified
*/
function pmprocl_set_level_page_message()
{
if(!empty($_REQUEST['notcertified']) && function_exists('pmpro_setMessage'))
{
pmpro_setMessage('That level requires a higher level of certification to checkout.', 'pmpro_error');
}
}
add_action('wp','pmprocl_set_level_page_message', 99);