/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.3 
  7. * Author: Stranger Studios, Scott Sousa 
  8. * Author URI: http://www.strangerstudios.com 
  9. */ 
  10.  
  11. /** 
  12. * Include the TGM_Plugin_Activation class. 
  13. */ 
  14. require_once dirname( __FILE__ ) . '/class-tgm-plugin-activation.php'; 
  15.  
  16. /** 
  17. * Register the required plugins for this theme. 
  18. */ 
  19. add_action( 'tgmpa_register', 'pmprobbp_tgmpa_register' ); 
  20. function pmprobbp_tgmpa_register() { 
  21. /** 
  22. * Array of plugin arrays. Required keys are name and slug. 
  23. * If the source is NOT from the .org repo, then source is also required. 
  24. */ 
  25. $plugins = array( 
  26. // Paid Memberships Pro 
  27. array( 
  28. 'name' => 'Paid Memberships Pro',  
  29. 'slug' => 'paid-memberships-pro',  
  30. 'required' => true 
  31. ),  
  32. // bbPress 
  33. array( 
  34. 'name' => 'bbPress',  
  35. 'slug' => 'bbpress',  
  36. 'required' => true 
  37. ); 
  38.  
  39. // Change this to your theme text domain, used for internationalising strings 
  40. $theme_text_domain = 'pmpro'; 
  41.  
  42. $config = array( 
  43. 'domain' => $theme_text_domain, // Text domain - likely want to be the same as your theme. 
  44. 'default_path' => '', // Default absolute path to pre-packaged plugins  
  45. 'menu' => 'install-required-plugins', // Menu slug 
  46. 'has_notices' => true, // Show admin notices or not 
  47. 'is_automatic' => false, // Automatically activate plugins after installation or not 
  48. 'message' => '', // Message to output right before the plugins table 
  49. 'strings' => array( 
  50. 'page_title' => __( 'Install Required Plugins', $theme_text_domain ),  
  51. 'menu_title' => __( 'Install Plugins', $theme_text_domain ),  
  52. 'installing' => __( 'Installing Plugin: %s', $theme_text_domain ), // %1$s = plugin name 
  53. 'oops' => __( 'Something went wrong with the plugin API.', $theme_text_domain ),  
  54. 'notice_can_install_required' => _n_noop( 'PMPro bbPress requires the following plugin: %1$s.', 'PMPro bbPress requires the following plugins: %1$s.' ), // %1$s = plugin name(s) 
  55. 'notice_can_install_recommended' => _n_noop( 'PMPro bbPress recommends the following plugin: %1$s.', 'PMPro bbPress recommends the following plugins: %1$s.' ), // %1$s = plugin name(s) 
  56. 'notice_cannot_install' => _n_noop( 'PMPro bbPress: Sorry, but you do not have the correct permissions to install the %s plugin. Contact the administrator of this site for help on getting the plugin installed.', 'PMPro bbPress: Sorry, but you do not have the correct permissions to install the %s plugins. Contact the administrator of this site for help on getting the plugins installed.' ), // %1$s = plugin name(s) 
  57. 'notice_can_activate_required' => _n_noop( 'PMPro bbPress: The following required plugin is currently inactive: %1$s.', 'PMPro bbPress: The following required plugins are currently inactive: %1$s.' ), // %1$s = plugin name(s) 
  58. 'notice_can_activate_recommended' => _n_noop( 'PMPro bbPress: The following recommended plugin is currently inactive: %1$s.', 'PMPro bbPress: The following recommended plugins are currently inactive: %1$s.' ), // %1$s = plugin name(s) 
  59. 'notice_cannot_activate' => _n_noop( 'PMPro bbPress: Sorry, but you do not have the correct permissions to activate the %s plugin. Contact the administrator of this site for help on getting the plugin activated.', 'PMPro bbPress: Sorry, but you do not have the correct permissions to activate the %s plugins. Contact the administrator of this site for help on getting the plugins activated.' ), // %1$s = plugin name(s) 
  60. 'notice_ask_to_update' => _n_noop( 'PMPro bbPress: The following plugin needs to be updated to its latest version to ensure maximum compatibility with this theme: %1$s.', 'PMPro bbPress: The following plugins need to be updated to their latest version to ensure maximum compatibility with this theme: %1$s.' ), // %1$s = plugin name(s) 
  61. 'notice_cannot_update' => _n_noop( 'PMPro bbPress: Sorry, but you do not have the correct permissions to update the %s plugin. Contact the administrator of this site for help on getting the plugin updated.', 'PMPro bbPress: Sorry, but you do not have the correct permissions to update the %s plugins. Contact the administrator of this site for help on getting the plugins updated.' ), // %1$s = plugin name(s) 
  62. 'install_link' => _n_noop( 'Begin installing plugin', 'Begin installing plugins' ),  
  63. 'activate_link' => _n_noop( 'Activate installed plugin', 'Activate installed plugins' ),  
  64. 'return' => __( 'Return to Required Plugins Installer', $theme_text_domain ),  
  65. 'plugin_activated' => __( 'Plugin activated successfully.', $theme_text_domain ),  
  66. 'complete' => __( 'All plugins installed and activated successfully. %s', $theme_text_domain ), // %1$s = dashboard link 
  67. 'nag_type' => 'updated' // Determines admin notice type - can only be 'updated' or 'error' 
  68. ); 
  69.  
  70. tgmpa( $plugins, $config ); 
  71.  
  72.  
  73.  
  74.  
  75. /** 
  76. * These functions add the PMPro Require Membership metabox to bbPress Forums. 
  77. */ 
  78. add_action( 'init', 'pmprobbpress_init', 20 ); 
  79. function pmprobbp_add_meta_box() { 
  80. add_meta_box( 'pmpro_page_meta', 'Require Membership', 'pmpro_page_meta', 'forum', 'side' );  
  81. function pmprobbpress_init() { 
  82.  
  83. //make sure PMPro is activated 
  84. if(!function_exists("pmpro_getOption")) 
  85. return; 
  86.  
  87. if ( is_admin() ) 
  88. add_action( 'admin_menu', 'pmprobbp_add_meta_box' ); 
  89.  
  90. //apply search filter to bbpress searches 
  91. $filterqueries = pmpro_getOption("filterqueries"); 
  92. if(!empty($filterqueries)) 
  93. add_filter( 'pre_get_posts', 'pmprobb_pre_get_posts' ); 
  94.  
  95. /** 
  96. * These next two functions work together to lock down bbPress forums based on PMPro membership level. 
  97. */  
  98.  
  99. /** 
  100. * Check that the current user has access to this forum. 
  101. */ 
  102. add_action( 'wp', 'pmprobbp_check_forum' ); 
  103. function pmprobbp_check_forum() { 
  104. global $current_user; 
  105.  
  106. $forum_id = bbp_get_forum_id(); 
  107. $restricted_forums[bbp_get_forum_id()] = array(1, 2); 
  108.  
  109. // Is this even a forum page at all? 
  110. if( ! bbp_is_forum_archive() && ! empty( $forum_id ) && pmpro_bbp_is_forum() ) { 
  111. // The current user does not have access to this forum, re-direct them away 
  112. if( ! pmpro_has_membership_access( $forum_id ) ) { 
  113. wp_redirect(add_query_arg('noaccess', 1, get_post_type_archive_link( 'forum' ))); 
  114. exit; 
  115.  
  116. return true; 
  117.  
  118. /** 
  119. * Function to tell if the current forum, topic, or reply is a subpost of the forum_id passed. 
  120. * If no forum_id is passed, it will return true if it is any forum, topic, or reply. 
  121. */ 
  122. function pmpro_bbp_is_forum( $forum_id = NULL ) { 
  123. global $post; 
  124.  
  125. if(bbp_is_forum($post->ID)) 
  126. {  
  127. if(!empty($forum_id) && $post->ID == $forum_id) 
  128. return true; 
  129. elseif(empty($forum_id)) 
  130. return true; 
  131. else 
  132. return false; 
  133. elseif(bbp_is_topic($post->ID)) 
  134. {  
  135. if(!empty($forum_id) && $post->post_parent == $forum_id) 
  136. return true; 
  137. elseif(empty($forum_id)) 
  138. return true; 
  139. else 
  140. return false; 
  141. elseif(bbp_is_reply($post->ID)) 
  142. {  
  143. if(!empty($forum_id) && in_array($forum_id, $post->ancestors)) 
  144. return true; 
  145. elseif(empty($forum_id)) 
  146. return true; 
  147. else 
  148. return false; 
  149. else 
  150. return false; 
  151.  
  152. /** Add membership level required message if user does not have access */ 
  153. function pmpro_bbp_membership_msg()  
  154. $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 
  155.  
  156. if (bbp_is_forum_archive() && !empty($_REQUEST['noaccess']))  
  157. echo '<p class="pmpro_bbp_membership_msg">' . $pmpro_bbp_error_msg . '</p>'; 
  158. add_action('bbp_template_before_forums_index', 'pmpro_bbp_membership_msg'); 
  159.  
  160. /** 
  161. * Add topics and forums to pmpro_search_query 
  162. */ 
  163. function pmprobb_pre_get_posts($query) { 
  164.  
  165. global $wpdb; 
  166.  
  167. //only filter front end searches 
  168. if(is_admin() || !$query->is_search || bbp_is_single_topic()) 
  169. return $query; 
  170.  
  171. //get all member forums 
  172. $sqlQuery = "SELECT ID FROM $wpdb->posts WHERE post_type LIKE 'forum'"; 
  173. $all_forums = $wpdb->get_col($sqlQuery); 
  174.  
  175. //add restricted forums to array 
  176. $restricted_forum_ids = array(); 
  177. foreach($all_forums as $forum_id) { 
  178. if(!pmpro_has_membership_access($forum_id)) 
  179. $restricted_forum_ids[] = $forum_id; 
  180.  
  181. //if there are restricted forums, find topics and exclude them all from searches 
  182. if(!empty($restricted_forum_ids)) 
  183. {  
  184. //get topics belonging to restricted forums 
  185. $sqlQuery = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key LIKE '_bbp_forum_id' AND meta_value IN(" . implode(', ', $restricted_forum_ids) . ")"; 
  186. $restricted_topic_ids = $wpdb->get_col($sqlQuery); 
  187.  
  188. //exclude restricted topics and posts 
  189. $query->set('post__not_in', array_merge($query->get('post__not_in'), $restricted_topic_ids, $restricted_forum_ids));  
  190.  
  191. return $query; 
  192.  
  193. /** 
  194. Function to add links to the plugin row meta 
  195. */ 
  196. function pmprobb_plugin_row_meta($links, $file) { 
  197. if(strpos($file, 'pmpro-bbpress.php') !== false) 
  198. $new_links = array( 
  199. '<a href="' . esc_url('http://www.paidmembershipspro.com/add-ons/plugins-wordpress-repository/pmpro-bbpress/') . '" title="' . esc_attr( __( 'View Documentation', 'pmpro' ) ) . '">' . __( 'Docs', 'pmpro' ) . '</a>',  
  200. '<a href="' . esc_url('http://paidmembershipspro.com/support/') . '" title="' . esc_attr( __( 'Visit Customer Support Forum', 'pmpro' ) ) . '">' . __( 'Support', 'pmpro' ) . '</a>',  
  201. ); 
  202. $links = array_merge($links, $new_links); 
  203. return $links; 
  204. add_filter('plugin_row_meta', 'pmprobb_plugin_row_meta', 10, 2); 
.