/pmpro-bbpress.php

  1. <?php 
  2. /** 
  3. * Plugin Name: Paid Memberships Pro - bbPress Add On 
  4. * Plugin URI: http://www.paidmembershipspro.com/pmpro-bbpress/ 
  5. * Description: Allow individual forums to be locked down for members. 
  6. * Version: 1.5.3 
  7. * Author: Stranger Studios, Scott Sousa 
  8. * Author URI: http://www.strangerstudios.com 
  9. */ 
  10.  
  11. //includes 
  12. define('PMPROBB_DIR', dirname(__FILE__)); 
  13. require_once(PMPROBB_DIR . '/includes/functions.php'); 
  14. require_once(PMPROBB_DIR . '/includes/options.php');  
  15. require_once(PMPROBB_DIR . '/includes/options-membership-levels.php'); 
  16. require_once(PMPROBB_DIR . '/includes/shortcodes.php');  
  17.  
  18. /** 
  19. * Admin init 
  20. */ 
  21. function pmprobb_admin_init() { 
  22. //if on the edit level page, enqueue color picker 
  23. if(!empty($_REQUEST['page']) && $_REQUEST['page'] == 'pmpro-membershiplevels' && isset($_REQUEST['edit'])) { 
  24. wp_enqueue_script( 'wp-color-picker' ); 
  25. wp_enqueue_style( 'wp-color-picker' ); 
  26. }  
  27. add_action('admin_init', 'pmprobb_admin_init'); 
  28.  
  29. /** 
  30. * These next two functions work together to lock down bbPress forums based on PMPro membership level. 
  31. */  
  32.  
  33. /** 
  34. * Check that the current user has access to this forum. 
  35. */ 
  36. add_action( 'template_redirect', 'pmprobbp_check_forum' ); 
  37. function pmprobbp_check_forum() { 
  38. //make sure pmpro and bbpress are active 
  39. if ( !defined('PMPRO_VERSION') || !class_exists('bbPress') ) 
  40. return; 
  41.  
  42. global $current_user; 
  43.  
  44. $forum_id = bbp_get_forum_id(); 
  45.  
  46. // Is this even a forum page at all? 
  47. if( ! bbp_is_forum_archive() && ! empty( $forum_id ) && pmpro_bbp_is_forum() ) { 
  48. // The current user does not have access to this forum, re-direct them away 
  49. if( ! pmpro_has_membership_access( $forum_id ) ) { 
  50.  
  51. // save to session in case we want to redirect later on 
  52. $_SESSION['pmpro_bbp_redirected_from'] = $_SERVER['REQUEST_URI']; 
  53. wp_redirect( add_query_arg( 'noaccess', 1, get_post_type_archive_link( 'forum' ) ) ); 
  54. exit; 
  55.  
  56. /** 
  57. * Function to tell if the current forum, topic, or reply is a subpost of the forum_id passed. 
  58. * If no forum_id is passed, it will return true if it is any forum, topic, or reply. 
  59. */ 
  60. function pmpro_bbp_is_forum( $forum_id = NULL ) { 
  61. global $post; 
  62.  
  63. //avoid notices on 404 pages 
  64. if(!$post) 
  65. return false; 
  66.  
  67. //if bbPress is not active, no forum 
  68. if(!function_exists('bbp_is_forum')) 
  69. return false; 
  70.  
  71. //check bbpress tests 
  72. if(bbp_is_forum($post->ID)) 
  73. {  
  74. if(!empty($forum_id) && $post->ID == $forum_id) 
  75. return true; 
  76. elseif(empty($forum_id)) 
  77. return true; 
  78. else 
  79. return false; 
  80. elseif(bbp_is_topic($post->ID)) 
  81. {  
  82. if(!empty($forum_id) && $post->post_parent == $forum_id) 
  83. return true; 
  84. elseif(empty($forum_id)) 
  85. return true; 
  86. else 
  87. return false; 
  88. elseif(bbp_is_reply($post->ID)) 
  89. {  
  90. if(!empty($forum_id) && in_array($forum_id, $post->ancestors)) 
  91. return true; 
  92. elseif(empty($forum_id)) 
  93. return true; 
  94. else 
  95. return false; 
  96. else 
  97. return false; 
  98.  
  99. /** Add membership level required message if user does not have access */ 
  100. function pmpro_bbp_membership_msg()  
  101. $pmpro_bbp_error_msg = apply_filters('pmpro_bbp_error_msg', 'You do not have the required membership level to access that forum.'); // error message to display 
  102.  
  103. if (bbp_is_forum_archive() && !empty($_REQUEST['noaccess']))  
  104. echo '<p class="pmpro_bbp_membership_msg">' . $pmpro_bbp_error_msg . '</p>'; 
  105. add_action('bbp_template_before_forums_index', 'pmpro_bbp_membership_msg'); 
  106.  
  107. /** 
  108. * Add topics and forums to pmpro_search_query 
  109. */ 
  110. function pmprobb_pre_get_posts($query) { 
  111.  
  112. global $wpdb; 
  113.  
  114. //only filter front end searches 
  115. if(is_admin() || !$query->is_search || bbp_is_single_topic()) 
  116. return $query; 
  117.  
  118. //get all member forums 
  119. $sqlQuery = "SELECT ID FROM $wpdb->posts WHERE post_type LIKE 'forum'"; 
  120. $all_forums = $wpdb->get_col($sqlQuery); 
  121.  
  122. //no forums? 
  123. if(empty($all_forums)) 
  124. return $query; 
  125.  
  126. //add restricted forums to array 
  127. $restricted_forum_ids = array(); 
  128. foreach($all_forums as $forum_id) { 
  129. if(!pmpro_has_membership_access($forum_id)) 
  130. $restricted_forum_ids[] = $forum_id; 
  131.  
  132. //if there are restricted forums, find topics and exclude them all from searches 
  133. if(!empty($restricted_forum_ids)) 
  134. {  
  135. //get topics belonging to restricted forums 
  136. $sqlQuery = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key LIKE '_bbp_forum_id' AND meta_value IN(" . implode(', ', $restricted_forum_ids) . ")"; 
  137. $restricted_topic_ids = $wpdb->get_col($sqlQuery); 
  138.  
  139. //exclude restricted topics and posts 
  140. $query->set('post__not_in', array_merge($query->get('post__not_in'), $restricted_topic_ids, $restricted_forum_ids));  
  141.  
  142. return $query; 
  143.  
  144. /** 
  145. Function to add links to the plugin row meta 
  146. */ 
  147. function pmprobb_plugin_row_meta($links, $file) { 
  148. if(strpos($file, 'pmpro-bbpress.php') !== false) 
  149. $new_links = array( 
  150. '<a href="' . esc_url('http://www.paidmembershipspro.com/add-ons/plugins-wordpress-repository/pmpro-bbpress/') . '" title="' . esc_attr( __( 'View Documentation', 'pmpro' ) ) . '">' . __( 'Docs', 'pmpro' ) . '</a>',  
  151. '<a href="' . esc_url('http://paidmembershipspro.com/support/') . '" title="' . esc_attr( __( 'Visit Customer Support Forum', 'pmpro' ) ) . '">' . __( 'Support', 'pmpro' ) . '</a>',  
  152. ); 
  153. $links = array_merge($links, $new_links); 
  154. return $links; 
  155. add_filter('plugin_row_meta', 'pmprobb_plugin_row_meta', 10, 2); 
  156.  
  157. /** 
  158. Adds "pmpro-level-ID" to the forum topic replies post class where ID 
  159. is the membership level of the reply author. Useful for styling 
  160. forum replies based on membership level. 
  161.  
  162. Style the replies using this post class in the following format: 
  163. #bbpress-forums li.bbp-body div.pmpro-level-1 { }  
  164. #bbpress-forums li.bbp-body div.pmpro-level-2 { } 
  165. Add this code to your active theme's functions.php or a custom plugin. 
  166. */ 
  167. function pmprobb_pmpro_reply_post_class($classes) { 
  168. global $reply_id; 
  169. $reply_id = bbp_get_reply_id( $reply_id ); 
  170. $reply_author_id = bbp_get_reply_author_id( $reply_id ); 
  171. $reply_author_membership_level = pmpro_getMembershipLevelForUser($reply_author_id); 
  172. if(!empty($reply_author_membership_level)) { 
  173. $classes[] = 'pmpro-level-' . $reply_author_membership_level->id; 
  174. return $classes; 
  175. add_filter( 'bbp_get_reply_class', 'pmprobb_pmpro_reply_post_class'); 
  176.  
  177. /** 
  178. Generates CSS to color member posts. 
  179. */ 
  180. function pmprobb_forum_color_css() { 
  181. //only on forum pages 
  182. if(!pmpro_bbp_is_forum()) 
  183. return; 
  184.  
  185. //get color options and build rules 
  186. $options = pmprobb_getOptions();  
  187. $rule = array(); 
  188. if(!empty($options['levels'])) { 
  189. foreach($options['levels'] as $level) { 
  190. if(!empty($level['color'])) 
  191. $rules[] = ".topic.pmpro-level-1, .reply.pmpro-level-1 {background-color: " . $level['color'] . " !important; }"; 
  192.  
  193. //no rules? 
  194. if(empty($rules)) 
  195. return false; 
  196.  
  197. //show rules 
  198. ?> 
  199. <style type="text/css" media="screen"> 
  200. <?php echo implode("\n", $rules) . "\n";?> 
  201. </style> 
  202. <?php 
  203. add_action('wp_head', 'pmprobb_forum_color_css'); 
  204.  
  205. /** 
  206. Add links to the top of the member links 
  207. */ 
  208. function pmprobb_pmpro_member_links_top() { 
  209. $options = pmprobb_getOptions(); 
  210. if(empty($options['member_links'])) 
  211. return; 
  212.  
  213. $forums = get_posts(array('post_type'=>'forum', 'post_status'=>'publish'));  
  214. foreach($forums as $forum) { 
  215. //show in member links?  
  216. if(pmpro_has_membership_access($forum->ID)) { 
  217. ?> 
  218. <li><a href="<?php echo get_permalink($forum->ID);?>"><?php echo $forum->post_title;?></a></li> 
  219. <?php 
  220. add_filter('pmpro_member_links_top', 'pmprobb_pmpro_member_links_top'); 
  221.  
  222. /** 
  223. Hide forums from list and search results 
  224. */ 
  225. function pmprobb_pmpro_search_filter_post_types($post_types) 
  226. $options = pmprobb_getOptions(); 
  227. if(!empty($options['hide_member_forums'])) { 
  228. $post_types[] = 'forum'; 
  229. array_unique($post_types);  
  230. return $post_types; 
  231. $options = pmprobb_getOptions(); 
  232. if(!empty($options['hide_member_forums'])) { 
  233. add_filter( 'pre_get_posts', 'pmpro_search_filter' );  
  234. add_filter( 'pmpro_search_filter_post_types', 'pmprobb_pmpro_search_filter_post_types' ); 
  235.  
  236. /** 
  237. * Change error message for PMPro bbPress  
  238. */ 
  239. function pmprobb_pmpro_bbp_error_msg() 
  240. $options = pmprobb_getOptions(); 
  241. return $options['error_message']; 
  242. add_filter('pmpro_bbp_error_msg', 'pmprobb_pmpro_bbp_error_msg'); 
  243.  
  244. /** 
  245. Hide the forum role from the bbPress forums replies author link.  
  246. */  
  247. function pmprobb_pmpro_hide_role($args) { 
  248. $options = pmprobb_getOptions(); 
  249. if(!empty($options['hide_forum_roles'])) 
  250. $args['show_role'] = false; 
  251. return $args; 
  252. add_filter ('bbp_before_get_reply_author_link_parse_args', 'pmprobb_pmpro_hide_role' ); 
  253.  
  254. /** 
  255. Adds a Section "Membership Level" and displays the user's level 
  256. on the bbPress User Profile page.  
  257. */ 
  258. function pmprobb_pmpro_bbp_template_before_user_profile()  
  259. $options = pmprobb_getOptions(); 
  260. if(empty($options['show_membership_levels'])) 
  261. return; 
  262.  
  263. $profile_user = new stdClass(); 
  264. $profile_user->membership_level = pmpro_getMembershipLevelForUser(bbp_get_user_id( 0, true, false )); 
  265. if(!empty($profile_user->membership_level)) 
  266. ?> 
  267. <div id="bbp-user-profile" class="bbp-user-profile"> 
  268. <h2 class="entry-title"><?php _e('Membership Level', 'pmpro');?></h2> 
  269. <div class="bbp-user-section"> 
  270. <?php echo $profile_user->membership_level->name; ?> 
  271. </div> 
  272. </div> 
  273. <?php 
  274. }; 
  275. add_action( 'bbp_template_before_user_profile', 'pmprobb_pmpro_bbp_template_before_user_profile', 10, 0 ); 
  276.  
  277. /** 
  278. Display the Membership Level of the reply author  
  279. in your bbPress forum replies.  
  280. */ 
  281. function pmprobb_pmpro_bbp_theme_after_reply_author_details()  
  282. $options = pmprobb_getOptions(); 
  283. if(empty($options['show_membership_levels'])) 
  284. return; 
  285.  
  286. $displayed_user = bbp_get_reply_author_id(bbp_get_reply_id()); 
  287. $membership_level = pmpro_getMembershipLevelForUser($displayed_user); 
  288. if(!empty($membership_level)) 
  289. echo '<br /><div class="bbp-author-role">' . $membership_level->name . '</div>'; 
  290. add_action('bbp_theme_after_reply_author_details', 'pmprobb_pmpro_bbp_theme_after_reply_author_details', 10, 0); 
.