-
Notifications
You must be signed in to change notification settings - Fork 18
/
pmpro-bbpress.php
594 lines (513 loc) · 18.7 KB
/
pmpro-bbpress.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
<?php
/**
* Plugin Name: Paid Memberships Pro - bbPress Add On
* Plugin URI: https://www.paidmembershipspro.com/add-ons/pmpro-bbpress/
* Description: Restrict access to bbPress for free or premium members by integrating bbPress with Paid Memberships Pro.
* Version: 1.8
* Author: Paid Memberships Pro
* Author URI: https://www.paidmembershipspro.com
* Text Domain: pmpro-bbpress
* Domain Path: /languages
*/
//includes
define('PMPROBB_DIR', dirname(__FILE__));
require_once(PMPROBB_DIR . '/includes/functions.php');
require_once(PMPROBB_DIR . '/includes/options.php');
require_once(PMPROBB_DIR . '/includes/options-membership-levels.php');
require_once(PMPROBB_DIR . '/includes/shortcodes.php');
/**
* Load text domain
*/
function pmprobb_load_plugin_text_domain() {
load_plugin_textdomain( 'pmpro-bbpress', false, basename( dirname( __FILE__ ) ) . '/languages' );
}
add_action( 'plugins_loaded', 'pmprobb_load_plugin_text_domain' );
/**
* Load search filters if set.
*/
function pmprobb_init() {
// Make sure PMPro and bbPress is installed.
if ( !defined('PMPRO_VERSION') || !class_exists('bbPress') ) {
return;
}
// bail if we're in the admin.
if ( is_admin() ) {
return;
}
$filterqueries = get_option( "pmpro_filterqueries" );
if ( apply_filters( 'pmprobb_filter_forum_queries', true ) && ! empty( $filterqueries ) ) {
add_filter( 'pre_get_posts', 'pmprobb_pre_get_posts' );
}
// Try to filter the search results too, just in case.
$options = pmprobb_getOptions();
if(!empty($options['hide_member_forums']) && function_exists('bbp_is_forum_archive') && bbp_is_forum_archive() ) {
add_filter( 'pre_get_posts', 'pmpro_search_filter' );
add_filter( 'pmpro_search_filter_post_types', 'pmprobb_pmpro_search_filter_post_types' );
}
}
add_action('init', 'pmprobb_init', 50);
/**
* Filter the search results for forums/topics.
* @since 1.7
*/
function pmprobb_filter_forum_search_results( $query ) {
// Make sure that the query we want to filter has posts.
if ( ! $query->have_posts() ) {
return $query;
}
if ( ! is_admin() && apply_filters( 'pmprobb_filter_topic_queries', true ) && function_exists('bbp_is_search_results') && bbp_is_search_results() ) {
add_filter( 'pre_get_posts', 'pmprobb_pre_get_posts' );
}
return $query;
}
add_filter( 'pre_get_posts', 'pmprobb_filter_forum_search_results' );
/**
* Admin init
*/
function pmprobb_admin_init() {
//if on the edit level page, enqueue color picker
if(!empty($_REQUEST['page']) && $_REQUEST['page'] == 'pmpro-membershiplevels' && isset($_REQUEST['edit'])) {
wp_enqueue_script( 'wp-color-picker' );
wp_enqueue_style( 'wp-color-picker' );
}
}
add_action('admin_init', 'pmprobb_admin_init');
/**
* These next two functions work together to lock down bbPress forums based on PMPro membership level.
*/
/**
* Check that the current user has access to this forum.
*/
add_action( 'template_redirect', 'pmprobbp_check_forum' );
function pmprobbp_check_forum() {
// Make sure pmpro and bbpress are active.
if ( ! defined( 'PMPRO_VERSION' ) || ! class_exists( 'bbPress' ) ) {
return;
}
global $current_user;
$forum_id = bbp_get_forum_id();
// Is this even a forum page at all?
if( ! bbp_is_forum_archive() && ! empty( $forum_id ) && pmpro_bbp_is_forum() ) {
// The current user does not have access to this forum, re-direct them away
if( ! pmpro_has_membership_access( $forum_id ) ) {
// save to session in case we want to redirect later on
$_SESSION['pmpro_bbp_redirected_from'] = $_SERVER['REQUEST_URI'];
$redirect_to = add_query_arg( 'noaccess', 1, get_post_type_archive_link( 'forum' ) );
$redirect_to = apply_filters( 'pmprobbp_check_forum_redirect_url', $redirect_to, $forum_id );
wp_redirect( $redirect_to );
exit;
}
}
}
/**
* Function to tell if the current forum, topic, or reply is a subpost of the forum_id passed.
* If no forum_id is passed, it will return true if it is any forum, topic, or reply.
*/
function pmpro_bbp_is_forum( $forum_id = NULL ) {
global $post;
//avoid notices on 404 pages
if(!$post)
return false;
//if bbPress is not active, no forum
if(!function_exists('bbp_is_forum'))
return false;
//check bbpress tests
if(bbp_is_forum($post->ID))
{
if(!empty($forum_id) && $post->ID == $forum_id)
return true;
elseif(empty($forum_id))
return true;
else
return false;
}
elseif(bbp_is_topic($post->ID))
{
if(!empty($forum_id) && $post->post_parent == $forum_id)
return true;
elseif(empty($forum_id))
return true;
else
return false;
}
elseif(bbp_is_reply($post->ID))
{
if(!empty($forum_id) && in_array($forum_id, $post->ancestors))
return true;
elseif(empty($forum_id))
return true;
else
return false;
}
else
return false;
}
/* Add membership level required message if user does not have access */
function pmpro_bbp_membership_msg() {
// Make sure bbpress is active.
if ( ! class_exists( 'bbPress' ) ) {
return;
}
if (bbp_is_forum_archive() && !empty($_REQUEST['noaccess'])) {
$pmpro_bbp_error_msg = apply_filters('pmpro_bbp_error_msg', __( 'You do not have the required membership level to access that forum.', 'pmpro-bbpress' ) );
echo '<p class="pmpro_bbp_membership_msg">' . esc_html( $pmpro_bbp_error_msg ) . '</p>';
}
}
add_action('bbp_template_before_forums_index','pmpro_bbp_membership_msg');
/*
* Add topics and forums to pmpro_search_query
*/
function pmprobb_pre_get_posts($query) {
global $wpdb;
// Make sure pmpro and bbpress are active.
if ( ! defined( 'PMPRO_VERSION' ) || ! class_exists( 'bbPress' ) ) {
return $query;
}
// Make sure that the query we want to filter has posts.
if ( ! $query->have_posts() ) {
return $query;
}
// Get option to see if we need to hide Membership forum from front-end.
$options = pmprobb_getOptions();
if(empty($options['hide_member_forums'])) {
return $query;
}
//only filter front end queries for forums/topics.
if( is_admin() || ! isset( $query->query_vars['post_type'] ) || ! pmprobb_check_values( $query->query_vars['post_type'], array( 'forum', 'topic', 'reply' ) ) ) {
return $query;
}
//get all member forums
$sqlQuery = "SELECT ID FROM $wpdb->posts WHERE post_type LIKE 'forum'";
$all_forums = $wpdb->get_col($sqlQuery);
//no forums?
if(empty($all_forums))
return $query;
//add restricted forums to array
$restricted_forum_ids = array();
foreach($all_forums as $forum_id) {
if(!pmpro_has_membership_access($forum_id))
$restricted_forum_ids[] = $forum_id;
}
//if there are restricted forums, find topics and exclude them all from searches
if(!empty($restricted_forum_ids))
{
//get topics belonging to restricted forums
$sqlQuery = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key LIKE '_bbp_forum_id' AND meta_value IN(" . implode(',', $restricted_forum_ids) . ")";
$restricted_topic_ids = $wpdb->get_col($sqlQuery);
//exclude restricted topics and posts
$query->set('post__not_in', array_merge($query->get('post__not_in'), $restricted_topic_ids, $restricted_forum_ids));
}
return $query;
}
/*
Adds "pmpro-level-ID" to the forum topic replies post class where ID
is the membership level of the reply author. Useful for styling
forum replies based on membership level.
Style the replies using this post class in the following format:
#bbpress-forums li.bbp-body div.pmpro-level-1 { }
#bbpress-forums li.bbp-body div.pmpro-level-2 { }
Add this code to your active theme's functions.php or a custom plugin.
*/
function pmprobb_pmpro_reply_post_class($classes) {
// Make sure pmpro and bbpress are active.
if ( ! defined( 'PMPRO_VERSION' ) || ! class_exists( 'bbPress' ) ) {
return $classes;
}
$reply_author_id = bbp_get_reply_author_id();
$reply_author_membership_levels = pmpro_getMembershipLevelsForUser( $reply_author_id );
foreach ($reply_author_membership_levels as $level) {
$classes[] = 'pmpro-level-' . $level->id;
}
return $classes;
}
add_filter( 'bbp_get_reply_class', 'pmprobb_pmpro_reply_post_class');
/*
Generates CSS to color member posts.
*/
function pmprobb_forum_color_css() {
//only on forum pages
if(!pmpro_bbp_is_forum())
return;
//get color options and build rules
$options = pmprobb_getOptions();
$rules = array();
if(!empty($options['levels'])) {
foreach($options['levels'] as $level_id => $level) {
if(!empty($level['color']))
$rules[] = ".topic.pmpro-level-" . $level_id . ", .reply.pmpro-level-" . $level_id . " {background-color: " . $level['color'] . " !important; }";
}
}
//no rules?
if(empty($rules))
return false;
//show rules
?>
<style type="text/css" media="screen">
<?php echo implode("\n", $rules) . "\n";?>
</style>
<?php
}
add_action('wp_head', 'pmprobb_forum_color_css');
/*
Add links to the top of the member links
*/
function pmprobb_pmpro_member_links_top() {
// Make sure pmpro and bbpress are active.
if ( ! defined( 'PMPRO_VERSION' ) || ! class_exists( 'bbPress' ) ) {
return;
}
$options = pmprobb_getOptions();
if(empty($options['member_links']))
return;
$forums = get_posts(array('post_type'=>'forum', 'post_status'=>'publish'));
foreach($forums as $forum) {
//show in member links?
if(pmpro_has_membership_access($forum->ID)) {
?>
<li><a href="<?php echo get_permalink($forum->ID);?>"><?php echo $forum->post_title;?></a></li>
<?php
}
}
}
add_filter('pmpro_member_links_top','pmprobb_pmpro_member_links_top');
/*
Hide forums from list and search results
*/
function pmprobb_pmpro_search_filter_post_types($post_types)
{
$options = pmprobb_getOptions();
if(!empty($options['hide_member_forums'])) {
$post_types[] = 'forum';
$post_types[] = 'topic';
$post_types[] = 'reply';
array_unique($post_types);
}
return $post_types;
}
/**
* Change error message for PMPro bbPress
*/
function pmprobb_pmpro_bbp_error_msg()
{
$options = pmprobb_getOptions();
return $options['error_message'];
}
add_filter('pmpro_bbp_error_msg', 'pmprobb_pmpro_bbp_error_msg');
/*
Hide the forum role from the bbPress forums replies author link.
*/
function pmprobb_pmpro_hide_role($args) {
$options = pmprobb_getOptions();
if(!empty($options['hide_forum_roles']))
$args['show_role'] = false;
return $args;
}
add_filter ('bbp_before_get_reply_author_link_parse_args', 'pmprobb_pmpro_hide_role' );
/*
Change user's forum role when they change levels.
TODO: For MMPU compatibility, we need to get all of the user's Levels
and use the highest role found.
@deprecated TBD
*/
function pmprobb_pmpro_after_change_membership_level( $level_id, $user_id, $cancel_level_id ) {
// Mark as deprecated.
_deprecated_function( __FUNCTION__, 'TBD', 'pmprobb_pmpro_after_change_membership_level' );
// Make sure bbPress is active.
if ( ! function_exists( 'bbp_set_user_role' ) ) {
return;
}
// Ignore admins.
if ( user_can( $user_id, 'manage_options' ) ) {
return;
}
if ( $level_id > 0 ) {
// Give them the role for their level.
$bbp_new_role = pmprobb_get_role_for_level( $level_id );
bbp_set_user_role( $user_id, $bbp_new_role );
} else {
// Cancelling. Give them the default role back
// if their old level was a non-default role.
$bbp_old_role = pmprobb_get_role_for_level( $cancel_level_id );
$bbp_default_role = get_option( '_bbp_default_role', 'bbp_participant' );
if ( $bbp_old_role != $bbp_default_role ) {
bbp_set_user_role( $user_id, $bbp_default_role );
}
}
}
/**
* Change user's forum role when they change levels.
*
* @since TBD
*
* @param array $pmpro_old_user_levels Array of user_id => old_levels_for_user.
*/
function pmprobb_after_all_membership_level_changes( $pmpro_old_user_levels ) {
// Make sure that bbPress is active.
if ( ! function_exists( 'bbp_set_user_role' ) ) {
return;
}
// Loop through all users.
foreach ( $pmpro_old_user_levels as $user_id => $old_levels ) {
// Get the user who changed levels.
$user = get_userdata( $user_id );
// Ignore admins and users who don't exist.
if ( empty( $user ) || user_can( $user_id, 'manage_options' ) ) {
continue;
}
// bbPress only supports one role per user, so let's get all of the roles
// assigned to this user's current levels and find the one with the
// most capabilities. This will be the BP role we assign to the user.
$new_levels = pmpro_getMembershipLevelsForUser( $user_id );
$bp_role_options = array();
foreach ( $new_levels as $new_level ) {
$bp_role_options[] = pmprobb_get_role_for_level( $new_level->id );
}
// Remove duplicates in the array of role options.
$bp_role_options = array_unique( $bp_role_options );
// Find the role with the most capabilities.
$bp_new_role = '';
$new_role_caps = 0;
foreach ( $bp_role_options as $bp_role_option ) {
// Get the capabilities for this role.
$role = get_role( $bp_role_option );
if ( empty( $role ) ) {
continue;
}
// If this role has more capabilities than the current role, use it.
$role_caps = count( $role->capabilities );
if ( $role_caps > $new_role_caps ) {
$bp_new_role = $bp_role_option;
$new_role_caps = $role_caps;
}
}
// If the user has a new role, set it.
if ( ! empty( $bp_new_role ) ) {
bbp_set_user_role( $user_id, $bp_new_role );
} else {
// The user no longer has a bbPress role given by PMPro. Set them to the default.
$bbp_default_role = get_option( '_bbp_default_role', 'bbp_participant' );
bbp_set_user_role( $user_id, $bbp_default_role );
}
}
}
add_action( 'pmpro_after_all_membership_level_changes', 'pmprobb_after_all_membership_level_changes' );
/*
Adds a Section "Membership Level" and displays the user's levels
on the bbPress User Profile page.
*/
function pmprobb_pmpro_bbp_template_before_user_profile()
{
// Make sure pmpro and bbpress are active.
if ( ! defined( 'PMPRO_VERSION' ) || ! class_exists( 'bbPress' ) ) {
return;
}
$options = pmprobb_getOptions();
if(empty($options['show_membership_levels']))
return;
$membership_levels = pmpro_getMembershipLevelsForUser( bbp_get_user_id( 0, true, false ) );
$level_names = wp_list_pluck( $membership_levels, 'name' );
if ( ! empty( $level_names ) ) {
?>
<div id="bbp-user-profile" class="bbp-user-profile">
<h2 class="entry-title"><?php echo esc_html( _n( 'Membership Level', 'Membership Levels', count( $level_names ), 'pmpro-bbpress' ) );?></h2>
<div class="bbp-user-section">
<?php echo esc_html( implode( ', ', $level_names ) ); ?>
</div>
</div>
<?php
}
};
add_action( 'bbp_template_before_user_profile', 'pmprobb_pmpro_bbp_template_before_user_profile', 10, 0 );
/*
Display the Membership Levels of the reply author
in your bbPress forum replies.
*/
function pmprobb_pmpro_bbp_theme_after_reply_author_details()
{
// Make sure pmpro and bbpress are active.
if ( ! defined( 'PMPRO_VERSION' ) || ! class_exists( 'bbPress' ) ) {
return;
}
$options = pmprobb_getOptions();
if(empty($options['show_membership_levels']))
return;
$displayed_user = bbp_get_reply_author_id(bbp_get_reply_id());
$membership_levels = pmpro_getMembershipLevelsForUser( $displayed_user );
$level_names = wp_list_pluck( $membership_levels, 'name' );
if ( ! empty( $level_names ) ) {
echo '<br /><div class="bbp-author-role">' . esc_html( implode( ', ', $level_names ) ) . '</div>';
}
}
add_action('bbp_theme_after_reply_author_details','pmprobb_pmpro_bbp_theme_after_reply_author_details', 10, 0);
/*
Block the reply content if non-members try to access it directly
*/
function pmprobb_auth_reply_view($content, $reply_id)
{
//make sure PMPro is active
if(!function_exists('pmpro_has_membership_access'))
return $content;
$has_access = pmpro_has_membership_access(bbp_get_reply_forum_id($reply_id), NULL, true);
if(!$has_access[0] || (!empty($has_access[1]) && !is_user_logged_in())) {
$content = __( 'Replies viewable by members only', 'pmpro-bbpress' );
}
return $content;
}
add_filter( 'bbp_get_reply_content', 'pmprobb_auth_reply_view', 10, 2 );
/**
* This will check if values are inside the haystack to check. Wrapper function for pre_get_posts sometimes passing array sometimes passes string.
* @param $needle string/array. Checks whether values are in array via array to array comparison or string to array.
* @param $haystack array. Array of values to compare $needle to.
* @param $r boolean. Returns true or false if arrays match.
*/
function pmprobb_check_values( $needle, $haystack ) {
if ( is_array( $needle ) ) {
$r = array_intersect( $needle, $haystack );
} else {
$r = in_array( $needle, $haystack );
}
return $r;
}
/**
* Function to stop non-members from receiving member only emails/notifications to subscribed topics
* @since 1.7
*/
function pmprobb_non_member_reply_notifications( $user_ids, $reply_id, $topic_id ) {
// If no user ID's are subscribed to a topic, just bail.
if ( empty( $user_ids ) ) {
return $user_ids;
}
// Make sure PMPro is active.
if ( ! function_exists( 'pmpro_has_membership_access' ) ) {
return $user_ids;
}
// Check if topic id belongs to restricted forum.
$forum_id = bbp_get_topic_forum_id( $topic_id );
if ( is_array( $user_ids ) ) {
$allowed_users = array();
foreach( $user_ids as $user_id ) {
if ( pmpro_has_membership_access( $forum_id, $user_id, false ) ) {
$allowed_users[] = $user_id;
}
}
$user_ids = $allowed_users;
}
// Check if users have access to that forum or has an active membership level, if not remove them.
return $user_ids;
}
add_filter( 'bbp_topic_subscription_user_ids', 'pmprobb_non_member_reply_notifications', 10, 3 );
/*
Function to add links to the plugin row meta
*/
function pmprobb_plugin_row_meta($links, $file) {
if(strpos($file, 'pmpro-bbpress.php') !== false)
{
$new_links = array(
'<a href="' . esc_url('https://www.paidmembershipspro.com/add-ons/pmpro-bbpress/') . '" title="' . esc_attr( __( 'View Documentation', 'pmpro-bbpress' ) ) . '">' . esc_html__( 'Docs', 'pmpro-bbpress' ) . '</a>',
'<a href="' . esc_url('https://paidmembershipspro.com/support/') . '" title="' . esc_attr( __( 'Visit Customer Support Forum', 'pmpro-bbpress' ) ) . '">' . esc_html__( 'Support', 'pmpro-bbpress' ) . '</a>',
);
$links = array_merge($links, $new_links);
}
return $links;
}
add_filter('plugin_row_meta', 'pmprobb_plugin_row_meta', 10, 2);