forked from strangerstudios/pmpro-addon-packages
/
pmpro-addon-packages.php
executable file
·795 lines (677 loc) · 28.4 KB
/
pmpro-addon-packages.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
<?php
/*
Plugin Name: Paid Memberships Pro - Addon Packages
Plugin URI: http://www.paidmembershipspro.com/pmpro-addon-packages/
Description: Allow PMPro members to purchase access to specific pages. This plugin is meant to be a temporary solution until support for multiple membership levels is added to PMPro.
Version: .7.2
Author: Stranger Studios
Author URI: http://www.strangerstudios.com
*/
/*
Story
* Admin designates a post as an "addon package".
* Sets a price for access to the post. (pmproap_price)
* Selects which membership levels can purchase the package. (pmproap_levels)
* For users without access, the page will show a link to purchase at the bottom of the page.
* Purchase goes to either a new checkout page or the PMPro checkout page with some parameters passed in.
* After checking out, they are taken to a new confirmation page or the PMPro confirmation page with extra info.
* After purchasing, the user ID is added to post meta (pmproap_users). The post ID is also added to user meta (pmproap_posts).
Limitations
* Only one time charges.
* No tax.
* No discount codes.
*/
$custom_dir = get_stylesheet_directory() . "/paid-memberships-pro/pmpro-addon-packages/";
$custom_file = $custom_dir . "pmpro-addon-packages-shortcode.php";
if (file_exists($custom_file)) {
require_once($custom_file);
} else
require_once(dirname(__FILE__) . "/shortcodes/pmpro-addon-packages-shortcode.php");
/* Globals */
//add this to your wp-config.php to set an expiration on addon packages
//define('PMPROAP_EXP_DAYS', 3); //expires in X days from purchase, 0 never expires
/*
add meta box to posts to set price and levels
*/
function pmproap_post_meta()
{
global $membership_levels, $post, $wpdb, $pmpro_currency_symbol, $pmpro_page_levels;
if(empty($pmpro_page_levels[$post->ID]))
$pmpro_page_levels[$post->ID] = $wpdb->get_col("SELECT membership_id FROM {$wpdb->pmpro_memberships_pages} WHERE page_id = '{$post->ID}'");
$pmproap_price = get_post_meta($post->ID, "_pmproap_price", true);
?>
<input type="hidden" name="pmproap_noncename" id="pmproap_noncename" value="<?php echo wp_create_nonce( plugin_basename(__FILE__) )?>" />
<?php if($pmproap_price && empty($pmpro_page_levels[$post->ID])) { ?>
<p><strong class="pmpro_red"><?php _e('Warning: This page is not locked down yet.', 'pmproap');?></strong> <?php _e('You must select at least one membership level in the sidebar to the right to restrict access to this page. You can create a free membership level for this purpose if you need to.', 'pmproap');?></p>
<?php } elseif($pmproap_price) { ?>
<p><strong class="pmpro_green"><?php _e('This page is restricted.', 'pmproap');?></strong> <?php printf(__('Members will have to pay %s to gain access to this page. To open access to all members, delete the price below then Save/Update this post.', 'pmproap'), pmpro_formatPrice($pmproap_price));?></p>
<?php } else { ?>
<p><?php _e('To charge for access to this post and any subpages, set a price below then Save/Update this post. Only members of the levels set in the "Require Membership" sidebar will be able to purchase access to this post.', 'pmproap');?></p>
<?php } ?>
<div>
<label><strong><?php _e('Price', 'pmproap');?></strong></label>
<?php echo $pmpro_currency_symbol; ?><input type="text" id="pmproap_price" name="pmproap_price" value="<?php echo esc_attr($pmproap_price); ?>" />
</div>
<?php
}
function pmproap_post_save($post_id)
{
global $wpdb;
if (empty($post_id))
return false;
if (!empty($_POST['pmproap_noncename']) && !wp_verify_nonce($_POST['pmproap_noncename'], plugin_basename(__FILE__))) {
return $post_id;
}
// verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
// to do anything
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
// Check permissions
if (!current_user_can('manage_options'))
return $post_id;
// OK, we're authenticated: we need to find and save the data
if (!empty($_POST['pmproap_price']))
$mydata = preg_replace("[^0-9\.]", "", $_POST['pmproap_price']);
else
$mydata = "";
update_post_meta($post_id, "_pmproap_price", $mydata);
return $mydata;
}
function pmproap_post_meta_wrapper()
{
/**
* @since v0.7 - Supports Custom Post Type (CPT) posts as add-on package/posts
*/
$post_types = apply_filters('pmproap_supported_post_types', array( 'page', 'post') );
// get extra post types from PMPro CPT, if available
if(function_exists('pmprocpt_getCPTs')) {
$post_types = array_merge($post_types, pmprocpt_getCPTs());
}
foreach( $post_types as $type ) {
add_meta_box('pmproap_post_meta', __('PMPro Addon Package Settings', 'pmproap'), 'pmproap_post_meta', $type, 'normal');
}
}
if (is_admin()) {
add_action('admin_menu', 'pmproap_post_meta_wrapper');
add_action('save_post', 'pmproap_post_save');
if (!defined('WP_CONTENT_DIR')) {
define('WP_CONTENT_DIR', ABSPATH . 'wp-content');
}
require_once(WP_CONTENT_DIR . '/plugins/paid-memberships-pro/adminpages/dashboard.php');
}
/*
These functions are used to add a member to a post or check if a member has been added to a post.
*/
function pmproap_addMemberToPost($user_id, $post_id)
{
$user_posts = get_user_meta($user_id, "_pmproap_posts", true);
$post_users = get_post_meta($post_id, "_pmproap_users", true);
//add the post to the user
if (is_array($user_posts)) {
if (!in_array($post_id, $user_posts)) {
$user_posts[] = $post_id;
}
} else {
$user_posts = array($post_id);
}
//add the user to the post
if (is_array($post_users)) {
if (!in_array($user_id, $post_users)) {
$post_users[] = $user_id;
}
} else {
$post_users = array($user_id);
}
//save the meta
update_user_meta($user_id, "_pmproap_posts", $user_posts);
update_post_meta($post_id, "_pmproap_users", $post_users);
//Trigger that user has been added.
do_action('pmproap_action_add_to_package', $user_id, $post_id);
}
//returns true if this post is locked to at least one membership level and has a pmproap_price
function pmproap_isPostLocked($post_id)
{
global $wpdb, $pmpro_page_levels;
$pmproap_price = get_post_meta($post_id, "_pmproap_price", true);
//has a price?
if (empty($pmproap_price))
return false;
//has a membership level
if (empty($pmpro_page_levels[$post_id]))
$pmpro_page_levels[$post_id] = $wpdb->get_col("SELECT membership_id FROM {$wpdb->pmpro_memberships_pages} WHERE page_id = '{$post_id}'");
if (empty($pmpro_page_levels[$post_id]))
return false;
//must be locked down then
return true;
}
//returns true if a user has access to a page
function pmproap_hasAccess($user_id, $post_id)
{
//does this user have a level giving them access to everything?
$all_access_levels = apply_filters("pmproap_all_access_levels", array(), $user_id, $post_id);
if (!empty($all_access_levels) && pmpro_hasMembershipLevel($all_access_levels, $user_id))
return true; //user has one of the all access levels
//check for expiration date
if (defined('PMPROAP_EXP_DAYS') && PMPROAP_EXP_DAYS) {
if (strtotime('now') >= get_user_meta($user_id, 'pmproap_post_id_' . $post_id . '_exp_date', true)) {
pmproap_removeMemberFromPost($user_id, $post_id);
delete_user_meta($user_id, 'pmproap_post_id_' . $post_id . '_exp_date');
return false;
}
}
//check if the user has access to the post
$post_users = get_post_meta($post_id, "_pmproap_users", true);
if (is_array($post_users) && in_array($user_id, $post_users))
return true;
else
return false; //unless everyone has access
}
function pmproap_removeMemberFromPost($user_id, $post_id)
{
$user_posts = get_user_meta($user_id, "_pmproap_posts", true);
$post_users = get_post_meta($post_id, "_pmproap_users", true);
//remove the post from the user
if (is_array($user_posts)) {
if (($key = array_search($post_id, $user_posts)) !== false) {
unset($user_posts[$key]);
}
}
//remove the user from the post
if (is_array($post_users)) {
if (($key = array_search($user_id, $post_users)) !== false) {
unset($post_users[$key]);
}
}
//save the meta
update_user_meta($user_id, "_pmproap_posts", $user_posts);
update_post_meta($post_id, "_pmproap_users", $post_users);
// Trigger that user has been removed.
do_action('pmproap_action_remove_from_package', $user_id, $post_id);
}
/*
Filter pmpro_has_membership_access based on addon access.
*/
function pmproap_pmpro_has_membership_access_filter($hasaccess, $mypost, $myuser, $post_membership_levels)
{
//If the user doesn't have access already, we won't change that. So only check if they already have access.
if ($hasaccess) {
//is this post locked down via an addon?
if (pmproap_isPostLocked($mypost->ID)) {
//okay check if the user has access
if (pmproap_hasAccess($myuser->ID, $mypost->ID))
$hasaccess = true;
else
$hasaccess = false;
}
}
return $hasaccess;
}
add_filter("pmpro_has_membership_access_filter", "pmproap_pmpro_has_membership_access_filter", 10, 4);
/*
Filter the message for users without access.
*/
function pmproap_pmpro_text_filter($text)
{
global $wpdb, $current_user, $post, $pmpro_currency_symbol;
if (!empty($post)) {
if (pmproap_isPostLocked($post->ID) && !pmproap_hasAccess($current_user->ID, $post->ID)) {
//which level to use for checkout link?
$text_level_id = pmproap_getLevelIDForCheckoutLink($post->ID, $current_user->ID);
if(empty($text_level_id)) {
$text = "<p>" . __("You must first purchase a membership level before purchasing this content. ", "pmproap") . "</p>";
$text .= "<p><a href=\"" . pmpro_url("levels") . "\">" . __("Click here to choose a membership level.", "pmproap") . "</a></p>";
} else {
//what's the price
$pmproap_price = get_post_meta($post->ID, "_pmproap_price", true);
//check for all access levels
$all_access_levels = apply_filters("pmproap_all_access_levels", array(), $current_user->ID, $post->ID);
//update text
if(!empty($all_access_levels))
{
$level_names = array();
foreach ($all_access_levels as $level_id)
{
$level = pmpro_getLevel($level_id);
$level_names[] = $level->name;
}
$text = "<p>" . __("This content requires that you purchase additional access. The price is %s or free for our %s members.", pmpro_formatPrice($pmproap_price), pmpro_implodeToEnglish($level_names)) . "</p>";
$text .= "<p><a href=\"" . pmpro_url("checkout", "?level=" . $text_level_id . "&ap=" . $post->ID) . "\">" . sprintf(__("Purchase this Content (%s)", 'pmproap'), pmpro_formatPrice($pmproap_price)) . "</a> <a href=\"" . pmpro_url("levels") . "\">" . __("Choose a Membership Level", "pmproap") . "</a></p>";
}
else
{
$text = "<p>" . sprintf(__("This content requires that you purchase additional access. The price is %s.", "pmproap"), pmpro_formatPrice($pmproap_price)) . "</p>";
$text .= "<p><a href=\"" . pmpro_url("checkout", "?level=" . $text_level_id . "&ap=" . $post->ID) . "\">" . __("Click here to checkout", "pmproap") . "</a></p>";
}
}
}
}
return $text;
}
add_filter("pmpro_non_member_text_filter", "pmproap_pmpro_text_filter");
add_filter("pmpro_not_logged_in_text_filter", "pmproap_pmpro_text_filter");
/*
Figure out which PMPro level ID to use for the checkout link for an addon page.
*/
function pmproap_getLevelIDForCheckoutLink($post_id = NULL, $user_id = NULL)
{
global $current_user, $post;
//default to current user
if (empty($user_id) && !empty($current_user) && !empty($current_user->ID)) {
$user_id = $current_user->ID;
}
//default to current post
if (empty($post_id) && !empty($post) && !empty($post->ID)) {
$post_id = $post->ID;
}
//no post, we bail
if (empty($post_id)) {
return false;
}
//no user id? make sure it's null
if (empty($user_id)) {
$user_id = NULL;
}
//use current level or offer a free level checkout
$has_access = pmpro_has_membership_access($post_id, $user_id, true);
$post_levels = $has_access[1];
//make sure membership_level obj is populated
if(is_user_logged_in())
$current_user->membership_level = pmpro_getMembershipLevelForUser($current_user->ID);
if(!empty($current_user->membership_level) && in_array($current_user->membership_level->ID, $post_levels))
{
$text_level_id = $current_user->membership_level->id;
}
elseif(!empty($post_levels))
{
//find a free level to checkout with
foreach($post_levels as $post_level_id)
{
$post_level = pmpro_getLevel($post_level_id);
if(pmpro_isLevelFree($post_level))
{
$text_level_id = $post_level->id;
break;
}
}
}
//didn't find a level id to use yet? return false and user's will be linked to the levels page.
if(empty($text_level_id))
$text_level_id = false;
return $text_level_id;
}
/*
Add ap to PayPal Express return url parameters
*/
function pmproap_pmpro_paypal_express_return_url_parameters($params)
{
if (!empty($_REQUEST['ap'])) {
$params["ap"] = isset($_REQUEST['ap']) ? intval($_REQUEST['ap']) : null;
}
return $params;
}
add_filter("pmpro_paypal_express_return_url_parameters", "pmproap_pmpro_paypal_express_return_url_parameters");
/*
Tweak the checkout page when ap is passed in.
*/
//update the level cost
function pmproap_pmpro_checkout_level($level)
{
global $current_user;
if (!isset($level->id)) {
return $level;
}
//are we purchasing a post?
if (isset($_REQUEST['ap']) && !empty($_REQUEST['ap'])) {
$ap = intval($_REQUEST['ap']);
$ap_post = get_post($ap);
$pmproap_price = get_post_meta($ap, "_pmproap_price", true);
if (!empty($pmproap_price)) {
if (pmpro_hasMembershipLevel($level->id)) {
//already have the membership, so price is just the ap price
$level->initial_payment = $pmproap_price;
//zero the rest out
$level->billing_amount = 0;
$level->cycle_number = 0;
$level->trial_amount = 0;
$level->trial_limit = 0;
//unset expiration period and number
$level->expiration_period = NULL;
$level->expiration_number = NULL;
//don't unsubscribe to the old level after checkout
if (!function_exists("pmproap_pmpro_cancel_previous_subscriptions")) {
function pmproap_pmpro_cancel_previous_subscriptions($cancel)
{
return false;
}
}
add_filter("pmpro_cancel_previous_subscriptions", "pmproap_pmpro_cancel_previous_subscriptions");
//keep current enddate
if (!function_exists("pmproap_pmpro_checkout_end_date")) {
function pmproap_pmpro_checkout_end_date($enddate, $user_id, $pmpro_level, $startdate)
{
$user_level = pmpro_getMembershipLevelForUser($user_id);
if(!empty($user_level) && !empty($user_level->enddate) && $user->enddate != '0000-00-00 00:00:00')
return date_i18n('Y-m-d H:i:s', $user_level->enddate);
else
return $enddate;
}
}
add_filter("pmpro_checkout_end_date", "pmproap_pmpro_checkout_end_date", 10, 4);
} else {
//add the ap price to the membership
$level->initial_payment = $level->initial_payment + $pmproap_price;
}
//update the name
if(pmpro_hasMembershipLevel($level->id))
$level->name = $ap_post->post_title;
else
$level->name .= sprintf(__(" + access to %s", "pmproap"), $ap_post->post_title);
//don't show the discount code field
if (!function_exists("pmproap_pmpro_show_discount_code")) {
function pmproap_pmpro_show_discount_code($show)
{
return false;
}
}
add_filter("pmpro_show_discount_code", "pmproap_pmpro_show_discount_code");
//add hidden input to carry ap value
if (!function_exists("pmproap_pmpro_checkout_boxes")) {
function pmproap_pmpro_checkout_boxes()
{
if (!empty($_REQUEST['ap'])) {
?>
<input type="hidden" name="ap" value="<?php echo esc_attr($_REQUEST['ap']); ?>"/>
<?php
}
}
}
add_action("pmpro_checkout_boxes", "pmproap_pmpro_checkout_boxes");
//give the user access to the page after checkout
if (!function_exists("pmproap_pmpro_after_checkout")) {
function pmproap_pmpro_after_checkout($user_id)
{
global $pmproap_ap;
if (!empty($_SESSION['ap'])) {
$pmproap_ap = intval($_SESSION['ap']);
unsset($_SESSION['ap']);
} elseif (!empty($_REQUEST['ap'])) {
$pmproap_ap = intval($_REQUEST['ap']);
}
if (!empty($pmproap_ap)) {
pmproap_addMemberToPost($user_id, $pmproap_ap);
//update the confirmation url
if (!function_exists("pmproap_pmpro_confirmation_url")) {
function pmproap_pmpro_confirmation_url($url, $user_id, $level)
{
global $pmproap_ap;
$url = add_query_arg("ap", $pmproap_ap, $url);
return $url;
}
}
add_filter("pmpro_confirmation_url", "pmproap_pmpro_confirmation_url", 10, 3);
}
}
}
add_action("pmpro_after_checkout", "pmproap_pmpro_after_checkout");
} else {
//woah, they passed a post id that isn't locked down
}
}
return $level;
}
add_filter("pmpro_checkout_level", "pmproap_pmpro_checkout_level");
//remove level description if checking out for level you already have
function pmproap_pmpro_checkout_level_have_it($level)
{
global $pmpro_pages;
//only checkout page, with ap passed in, and have the level checking out for
if (is_page($pmpro_pages['checkout']) &&
!empty($_REQUEST['ap']) &&
pmpro_hasMembershipLevel($level->id)
) {
$level->description = "";
}
return $level;
}
add_filter("pmpro_checkout_level", "pmproap_pmpro_checkout_level_have_it");
//remove "membership level" from name if the user already has a level
function pmproap_gettext_you_have_selected($translated_text, $text, $domain)
{
global $pmpro_pages;
//only checkout page, with ap passed in, and "you have selected..." string, and have the level checking out for
if(!empty($pmpro_pages) && is_page($pmpro_pages['checkout']) &&
!empty($_REQUEST['ap']) &&
$domain == "pmpro" &&
strpos($text, "have selected") !== false &&
pmpro_hasMembershipLevel(intval($_REQUEST['level'])))
{
$translated_text = str_replace(__(" membership level", "pmproap"), "", $translated_text);
$translated_text = str_replace(__("You have selected the", "pmproap"), __("You are purchasing additional access to:", "pmproap"), $translated_text);
}
return $translated_text;
}
add_filter("gettext", "pmproap_gettext_you_have_selected", 10, 3);
//remove "for membership" from cost text
function pmproap_pmpro_level_cost_text($text, $level)
{
global $pmpro_pages;
//only checkout page, with ap passed in, and have the level checking out for
if(is_page($pmpro_pages['checkout']) &&
!empty($_REQUEST['ap']) &&
pmpro_hasMembershipLevel($level->id))
{
$text = str_replace(__("The price for membership", "pmproap"), __("The price is", "pmproap"), $text);
$text = str_replace(__(" now", "pmproap"), "", $text);
}
return $text;
}
add_filter("pmpro_level_cost_text", "pmproap_pmpro_level_cost_text", 10, 2);
/*
Add info on addon to notes section of order.
*/
function pmproap_pmpro_added_order($order)
{
global $pmpro_pages;
if (is_page($pmpro_pages['checkout']) && !empty($_REQUEST['ap'])) {
global $wpdb;
$post = get_post(intval($_REQUEST['ap']));
$order->notes .= "Addon Package: " . $post->post_title . " (#" . $post->ID . ")\n";
$sqlQuery = $wpdb->prepare(
"UPDATE {$wpdb->pmpro_membership_orders} SET notes = %s WHERE id = %d LIMIT 1",
$order->notes,
$order->id
);
$wpdb->query($sqlQuery);
}
return $order;
}
add_filter('pmpro_added_order', 'pmproap_pmpro_added_order');
/**
* Insert Addon Package column to Orders CSV export.
*
* @since 0.6
*/
function pmproap_pmpro_orders_csv_extra_columns($columns) {
$columns['addon_package'] = 'pmproap_csv_addon_package';
return $columns;
}
add_filter('pmpro_orders_csv_extra_columns', 'pmproap_pmpro_orders_csv_extra_columns');
/*
Update the confirmation page to have a link to the purchased page.
*/
function pmproap_pmpro_confirmation_message($message)
{
if (!empty($_REQUEST['ap'])) {
$ap = $_REQUEST['ap'];
$ap_post = get_post($ap);
$message .= "<p class=\"pmproap_confirmation\">" . sprintf(__("Continue on to %s.", "pmproap"), "<a href=\"" . get_permalink($ap_post->ID) . "\">" . $ap_post->post_title . "</a>") . "</p>";
}
return $message;
}
add_filter("pmpro_confirmation_message", "pmproap_pmpro_confirmation_message");
/*
Show purchased posts on the account page
*/
function pmproap_pmpro_member_links_top()
{
global $current_user;
$post_ids = get_user_meta($current_user->ID, "_pmproap_posts", true);
if (is_array($post_ids)) {
foreach ($post_ids as $post_id) {
$apost = get_post($post_id);
?>
<li><a href="<?php echo get_permalink($post_id); ?>"><?php echo $apost->post_title; ?></a></li>
<?php
}
}
}
add_action("pmpro_member_links_top", "pmproap_pmpro_member_links_top");
/*
Show the purchased pages for each user on the edit user/profile page of the admin
*/
function pmproap_profile_fields($user_id)
{
if (is_object($user_id))
$user_id = $user_id->ID;
if(!current_user_can("administrator"))
return false;
?>
<h3><?php _e("Purchased Addon Packages", "pmproap"); ?></h3>
<table class="form-table">
<?php
$user_posts = get_user_meta($user_id, "_pmproap_posts", true);
if(!empty($user_posts))
{
foreach($user_posts as $upost_id)
{
?>
<tr>
<th></th>
<td>
<?php
$upost = get_post($upost_id);
?>
<span id="pmproap_remove_span_<?php echo $upost->ID;?>">
<a target="_blank" href="<?php echo esc_attr(get_permalink($upost->ID));?>"><?php echo $upost->post_title;?></a>
<a style="color: red;" id="pmproap_remove_<?php echo $upost->ID;?>" class="pmproap_remove" href="javascript:void(0);"><?php _e('remove', 'pmproap');?></a>
</span>
<?php
?>
</td>
</tr>
<?php
}
}
?>
<tr>
<th><?php _e('Give this User a Package', 'pmproap');?></th>
<td>
<input type="text" id="new_pmproap_posts_1" name="new_pmproap_posts[]" size="10" value="" /> <small><?php _e('Enter a post/page ID', 'pmproap');?></small>
</td>
</tr>
<tr id="pmproap_add_tr">
<th></th>
<td>
<a id="pmproap_add" href="javascript:void(0);"><?php _e('+ Add Another', 'pmproap');?></a>
</td>
</tr>
</table>
<input type="hidden" id="remove_pmproap_posts" name="remove_pmproap_posts" value="" />
<script>
var npmproap_adds = 1;
jQuery(function() {
//too add another text input for a new package
jQuery('#pmproap_add').click(function() {
npmproap_adds++;
jQuery('#pmproap_add_tr').before('<tr><th></th><td><input type="text" id="new_pmproap_posts_' + npmproap_adds + '" name="new_pmproap_posts[]" size="10" value="" /> <small><?php _e('Enter a post/page ID', 'pmproap');?></small></td></tr>');
});
//removing a package
jQuery('.pmproap_remove').click(function () {
var thispost = jQuery(this);
var thisid = thispost.attr('id').replace('pmproap_remove_', '');
//strike through the post
jQuery('#pmproap_remove_span_' + thisid).css('text-decoration', 'line-through');
//add id to remove list
jQuery('#remove_pmproap_posts').val(jQuery('#remove_pmproap_posts').val() + thisid + ',');
});
});
</script>
<?php
}
function pmproap_profile_fields_update()
{
if (isset($_REQUEST['new_pmproap_posts']) || isset($_REQUEST['remove_pmproap_posts'])) {
//get the user id
global $wpdb, $current_user, $user_ID;
get_currentuserinfo();
if (!empty($_REQUEST['user_id']))
$user_ID = intval($_REQUEST['user_id']);
if (!current_user_can('edit_user', $user_ID))
return false;
//adding
if (is_array($_REQUEST['new_pmproap_posts'])) {
foreach ($_REQUEST['new_pmproap_posts'] as $post_id) {
$post_id = intval($post_id);
if (!empty($post_id))
pmproap_addMemberToPost($user_ID, $post_id);
}
}
//remove
if (!empty($_REQUEST['remove_pmproap_posts'])) {
//convert to array
$post_ids = explode(",", $_REQUEST['remove_pmproap_posts']);
foreach ($post_ids as $post_id) {
$post_id = intval($post_id);
if (!empty($post_id))
pmproap_removeMemberFromPost($user_ID, $post_id);
}
}
}
}
add_action('show_user_profile', 'pmproap_profile_fields');
add_action('edit_user_profile', 'pmproap_profile_fields');
add_action('profile_update', 'pmproap_profile_fields_update');
/* Add Expiration Date to User Meta if set above */
function pmproap_add_exp_date($user_id, $post_id)
{
if (defined('PMPROAP_EXP_DAYS') && PMPROAP_EXP_DAYS > 0) {
$expdate = strtotime('+' . PMPROAP_EXP_DAYS . ' days');
update_user_meta($user_id, 'pmproap_post_id_' . $post_id . '_exp_date', $expdate);
}
}
add_action('pmproap_action_add_to_package', 'pmproap_add_exp_date', 10, 2);
function pmproap_remove_exp_date($user_id, $post_id)
{
if (get_user_meta($user_id, 'pmproap_post_id_' . $post_id . '_exp_date'))
delete_user_meta($user_id, 'pmproap_post_id_' . $post_id . '_exp_date');
}
add_action('pmproap_action_remove_from_package', 'pmproap_remove_exp_date', 10, 2);
/**
* Function to print Addon Package to Orders CSV export.
*
* @param $order MemberOrder.
*
* @since 0.5.2
*/
function pmproap_csv_addon_package($order) {
$ap = preg_match('/Addon Package: (.*\))/', $order->notes, $matches);
if(!empty($ap))
return $matches[1];
else
return "";
}
/*
Function to add links to the plugin row meta
*/
function pmproap_plugin_row_meta($links, $file) {
if(strpos($file, 'pmpro-addon-packages.php') !== false)
{
$new_links = array(
'<a href="' . esc_url('http://www.paidmembershipspro.com/add-ons/plus-add-ons/pmpro-purchase-access-to-a-single-page/') . '" title="' . esc_attr( __( 'View Documentation', 'pmproap' ) ) . '">' . __( 'Docs', 'pmproap' ) . '</a>',
'<a href="' . esc_url('http://paidmembershipspro.com/support/') . '" title="' . esc_attr( __( 'Visit Customer Support Forum', 'pmproap' ) ) . '">' . __( 'Support', 'pmproap' ) . '</a>',
);
$links = array_merge($links, $new_links);
}
return $links;
}
add_filter('plugin_row_meta', 'pmproap_plugin_row_meta', 10, 2);