forked from strangerstudios/pmpro-membership-card
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pmpro-membership-card.php
executable file
·267 lines (231 loc) · 7.87 KB
/
pmpro-membership-card.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
<?php
/*
Plugin Name: Paid Memberships Pro - Membership Card Add On
Plugin URI: http://www.paidmembershipspro.com/wp/pmpro-membership-card/
Description: Display a printable Membership Card for Paid Memberships Pro members or WP users.
Version: .4
Author: Stranger Studios
Author URI: http://www.strangerstudios.com
*/
/*
Load on the membership card page to setup vars and possibly redirect away
*/
// loads class to find post based on content (and supports WP caching).
require_once( plugin_dir_path(__FILE__) . 'class.pmpro_posts_by_content.php');
function pmpro_membership_card_wp()
{
/*
Check if we're on the membership card page.
*/
global $post;
if(is_admin() || empty($post) || ! has_shortcode($post->post_content, "pmpro_membership_card"))
return;
/*
Set the $pmpro_membership_card_user
*/
global $pmpro_membership_card_user, $current_user;
if(!empty($_REQUEST['u']))
$pmpro_membership_card_user = get_userdata(intval($_REQUEST['u']));
else
$pmpro_membership_card_user = $current_user;
/*
No user? Die
*/
if(empty($pmpro_membership_card_user))
{
wp_die("Invalid user.");
}
/*
Make sure we have level data for user.
*/
if(function_exists("pmpro_getMembershipLevelForUser"))
$pmpro_membership_card_user->membership_level = pmpro_getMembershipLevelForUser($pmpro_membership_card_user->ID);
/*
Make sure that the current user can "edit" the user being viewed.
*/
if(!current_user_can("edit_user", $pmpro_membership_card_user->ID))
{
wp_die("You do not have permission to view the membership card for this user.");
}
/*
If PMPro is activated, make sure the current user is a member or admin.
If not, make sure they are at least logged in.
*/
if(function_exists("pmpro_hasMembershipLevel"))
{
if(!pmpro_hasMembershipLevel() && !current_user_can("manage_options"))
{
wp_redirect(pmpro_url("levels"));
exit;
}
}
else
{
if(!is_user_logged_in())
{
wp_redirect(wp_login_url());
exit;
}
}
}
add_action('wp', 'pmpro_membership_card_wp');
/*
The membership card shortcode/template
*/
function pmpro_membership_card_shortcode($atts, $content=null, $code="")
{
/*
Look for a custom template.
*/
if(file_exists(get_stylesheet_directory() . "/membership-card.php"))
$template_path = get_stylesheet_directory() . "/membership-card.php";
elseif(file_exists(get_template_directory() . "/membership-card.php"))
$template_path = get_template_directory() . "/membership-card.php";
else
$template_path = plugin_dir_path(__FILE__) . "templates/membership-card.php";
extract(shortcode_atts(array(
'print_size' => 'all',
), $atts));
$print_sizes = explode(",", $print_size);
/*
Load the Template
*/
ob_start();
include($template_path);
$temp_content = ob_get_contents();
ob_end_clean();
return $temp_content;
}
add_shortcode('pmpro_membership_card', 'pmpro_membership_card_shortcode');
/*
Figure out which post/page id is the membership card page.
*/
function pmpro_membership_card_get_post_id()
{
//check cache
global $pmpro_membership_card_get_post_id;
if(isset($pmpro_membership_card_get_post_id))
return $pmpro_membership_card_get_post_id;
//default to false
$pmpro_membership_card_get_post_id = false;
//look in options
$from_options = get_option("pmpro_membership_card_post_ids", array());
// Make this an array so we can check it.
if (!is_array($from_options) && !empty($from_options))
$from_options = array($from_options);
// check status of the stored posts ID(s) for the membership card(s).
if (is_array($from_options) && !empty($from_options))
{
foreach($from_options as $k => $mc_id)
{
$p = get_post($mc_id);
// remove any entry that isn't a published post/page
if (!in_array( $p->post_status, array('publish', 'private')))
unset($from_options[$k]);
}
}
if(!empty($from_options) && is_array($from_options))
$pmpro_membership_card_get_post_id = end($from_options);
elseif(!empty($from_options))
$pmpro_membership_card_get_post_id = $from_options;
else
{
// Search for post based on content
// returns a single post or page (the first one found).
$args = array(
'posts_per_page' => 1,
'content' => '%pmpro_membership_card%',
'post_type' => array( 'post', 'page'),
'post_status' => array('publish', 'private')
);
$posts = pmpro_posts_by_content::get( $args );
$from_post_content = $posts[0]->ID;
//look for a post with the shortcode in it
if(!empty($from_post_content))
$pmpro_membership_card_get_post_id = $from_post_content;
}
//didn't find anything
return $pmpro_membership_card_get_post_id;
}
/*
Use an option to track pages with the [pmpro_membership_card] shortcode.
*/
function pmpro_membership_card_save_post($post_id)
{
global $post;
if ( !isset( $post->post_type) )
return;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( wp_is_post_revision( $post_id ) !== false )
return;
if ( 'trash' == get_post_status( $post_id ) )
return;
$args = array(
'p' => $post_id,
'posts_per_page' => 1,
'post_type' => array( 'post', 'page'),
'post_status' => array('publish', 'private')
);
$posts = pmpro_posts_by_content::get($args);
$post = isset($posts[0]) ? $posts[0] : null;
$option = get_option("pmpro_membership_card_post_ids", array());
if(empty($option))
$option = array();
if(isset($post->post_content) && has_shortcode($post->post_content, "pmpro_membership_card") && in_array($post->post_status, array('publish', 'private')) )
$option[$post_id] = $post_id;
else
unset($option[$post_id]);
update_option("pmpro_membership_card_post_ids", $option);
}
add_action('save_post', 'pmpro_membership_card_save_post');
/*
Add the link to view the card in the user profile
*/
function pmpro_membership_card_profile_fields($user)
{
global $current_user;
$membership_level_capability = apply_filters("pmpro_edit_member_capability", "manage_options");
if(!current_user_can($membership_level_capability))
return false;
if(!function_exists("pmpro_hasMembershipLevel") || (function_exists("pmpro_hasMembershipLevel") && pmpro_hasMembershipLevel(NULL, $user->ID)))
{
?>
<h3><?php _e("Membership Card", "pmpro"); ?></h3>
<table class="form-table">
<tr>
<th> </th>
<td><a href="<?php echo add_query_arg('u', $user->ID, get_permalink(pmpro_membership_card_get_post_id()));?>">View and Print Membership Card</a></td>
</tr>
</table>
<?php
}
}
add_action('edit_user_profile', 'pmpro_membership_card_profile_fields');
add_action('show_user_profile', 'pmpro_membership_card_profile_fields');
/*
Add the link to view the card in the Member Links section of the Membership Account page
*/
function pmpro_membership_card_member_links_top()
{
global $current_user;
?>
<li><a href="<?php echo add_query_arg('u', $current_user->ID, get_permalink(pmpro_membership_card_get_post_id()));?>"><?php _e("View and Print Membership Card", "pmpro"); ?></a></li>
<?php
}
add_action("pmpro_member_links_top", "pmpro_membership_card_member_links_top");
/*
Function to add links to the plugin row meta
*/
function pmpro_membership_card_plugin_row_meta($links, $file) {
if(strpos($file, 'pmpro-membership-card.php') !== false)
{
$new_links = array(
'<a href="' . esc_url('http://www.paidmembershipspro.com/add-ons/plugins-on-github/pmpro-membership-card/') . '" title="' . esc_attr( __( 'View Documentation', 'pmpro' ) ) . '">' . __( 'Docs', 'pmpro' ) . '</a>',
'<a href="' . esc_url('http://paidmembershipspro.com/support/') . '" title="' . esc_attr( __( 'Visit Customer Support Forum', 'pmpro' ) ) . '">' . __( 'Support', 'pmpro' ) . '</a>',
);
$links = array_merge($links, $new_links);
}
return $links;
}
add_filter('plugin_row_meta', 'pmpro_membership_card_plugin_row_meta', 10, 2);