/bp-members/classes/class-bp-members-theme-compat.php

  1. <?php 
  2. /** 
  3. * BuddyPress Member Theme Compat. 
  4. * 
  5. * @package BuddyPress 
  6. * @subpackage MembersScreens 
  7. * @since 1.7.0 
  8. */ 
  9.  
  10. // Exit if accessed directly. 
  11. defined( 'ABSPATH' ) || exit; 
  12.  
  13. /** 
  14. * The main theme compat class for BuddyPress Members. 
  15. * 
  16. * This class sets up the necessary theme compatibility actions to safely output 
  17. * member template parts to the_title and the_content areas of a theme. 
  18. * 
  19. * @since 1.7.0 
  20. */ 
  21. class BP_Members_Theme_Compat { 
  22.  
  23. /** 
  24. * Set up the members component theme compatibility. 
  25. * 
  26. * @since 1.7.0 
  27. */ 
  28. public function __construct() { 
  29. add_action( 'bp_setup_theme_compat', array( $this, 'is_members' ) ); 
  30.  
  31. /** 
  32. * Are we looking at something that needs members theme compatibility? 
  33. * 
  34. * @since 1.7.0 
  35. */ 
  36. public function is_members() { 
  37.  
  38. // Bail if not looking at the members component or a user's page. 
  39. if ( ! bp_is_members_component() && ! bp_is_user() ) { 
  40. return; 
  41.  
  42. // Members Directory. 
  43. if ( ! bp_current_action() && ! bp_current_item() ) { 
  44. bp_update_is_directory( true, 'members' ); 
  45.  
  46. /** 
  47. * Fires if looking at Members directory when needing theme compat. 
  48. * 
  49. * @since 1.5.0 
  50. */ 
  51. do_action( 'bp_members_screen_index' ); 
  52.  
  53. add_filter( 'bp_get_buddypress_template', array( $this, 'directory_template_hierarchy' ) ); 
  54. add_action( 'bp_template_include_reset_dummy_post_data', array( $this, 'directory_dummy_post' ) ); 
  55. add_filter( 'bp_replace_the_content', array( $this, 'directory_content' ) ); 
  56.  
  57. // User page. 
  58. } elseif ( bp_is_user() ) { 
  59.  
  60. // If we're on a single activity permalink page, we shouldn't use the members 
  61. // template, so stop here! 
  62. if ( bp_is_active( 'activity' ) && bp_is_single_activity() ) { 
  63. return; 
  64.  
  65. /** 
  66. * Fires if looking at Members user page when needing theme compat. 
  67. * 
  68. * @since 1.5.0 
  69. */ 
  70. do_action( 'bp_members_screen_display_profile' ); 
  71.  
  72. add_filter( 'bp_get_buddypress_template', array( $this, 'single_template_hierarchy' ) ); 
  73. add_action( 'bp_template_include_reset_dummy_post_data', array( $this, 'single_dummy_post' ) ); 
  74. add_filter( 'bp_replace_the_content', array( $this, 'single_dummy_content' ) ); 
  75.  
  76.  
  77. /** Directory *************************************************************/ 
  78.  
  79. /** 
  80. * Add template hierarchy to theme compat for the members directory page. 
  81. * 
  82. * This is to mirror how WordPress has 
  83. * {@link https://codex.wordpress.org/Template_Hierarchy template hierarchy}. 
  84. * 
  85. * @since 1.8.0 
  86. * 
  87. * @param array $templates The templates from bp_get_theme_compat_templates(). 
  88. * @return array $templates Array of custom templates to look for. 
  89. */ 
  90. public function directory_template_hierarchy( $templates = array() ) { 
  91.  
  92. // Set up the template hierarchy. 
  93. $new_templates = array(); 
  94. if ( '' !== bp_get_current_member_type() ) { 
  95. $new_templates[] = 'members/index-directory-type-' . sanitize_file_name( bp_get_current_member_type() ) . '.php'; 
  96. $new_templates[] = 'members/index-directory.php'; 
  97.  
  98. /** 
  99. * Filters the template hierarchy for theme compat and members directory page. 
  100. * 
  101. * @since 1.8.0 
  102. * 
  103. * @param array $value Array of template paths to add to hierarchy. 
  104. */ 
  105. $new_templates = apply_filters( 'bp_template_hierarchy_members_directory', $new_templates ); 
  106.  
  107. // Merge new templates with existing stack 
  108. // @see bp_get_theme_compat_templates(). 
  109. $templates = array_merge( (array) $new_templates, $templates ); 
  110.  
  111. return $templates; 
  112.  
  113. /** 
  114. * Update the global $post with directory data. 
  115. * 
  116. * @since 1.7.0 
  117. */ 
  118. public function directory_dummy_post() { 
  119. bp_theme_compat_reset_post( array( 
  120. 'ID' => 0,  
  121. 'post_title' => bp_get_directory_title( 'members' ),  
  122. 'post_author' => 0,  
  123. 'post_date' => 0,  
  124. 'post_content' => '',  
  125. 'post_type' => 'page',  
  126. 'post_status' => 'publish',  
  127. 'is_page' => true,  
  128. 'comment_status' => 'closed' 
  129. ) ); 
  130.  
  131. /** 
  132. * Filter the_content with the members index template part. 
  133. * 
  134. * @since 1.7.0 
  135. */ 
  136. public function directory_content() { 
  137. return bp_buffer_template_part( 'members/index', null, false ); 
  138.  
  139. /** Single ****************************************************************/ 
  140.  
  141. /** 
  142. * Add custom template hierarchy to theme compat for member pages. 
  143. * 
  144. * This is to mirror how WordPress has 
  145. * {@link https://codex.wordpress.org/Template_Hierarchy template hierarchy}. 
  146. * 
  147. * @since 1.8.0 
  148. * 
  149. * @param string $templates The templates from 
  150. * bp_get_theme_compat_templates(). 
  151. * @return array $templates Array of custom templates to look for. 
  152. */ 
  153. public function single_template_hierarchy( $templates ) { 
  154. // Setup some variables we're going to reference in our custom templates. 
  155. $user_nicename = buddypress()->displayed_user->userdata->user_nicename; 
  156.  
  157. /** 
  158. * Filters the template hierarchy for theme compat and member pages. 
  159. * 
  160. * @since 1.8.0 
  161. * 
  162. * @param array $value Array of template paths to add to hierarchy. 
  163. */ 
  164. $new_templates = apply_filters( 'bp_template_hierarchy_members_single_item', array( 
  165. 'members/single/index-id-' . sanitize_file_name( bp_displayed_user_id() ) . '.php',  
  166. 'members/single/index-nicename-' . sanitize_file_name( $user_nicename ) . '.php',  
  167. 'members/single/index-action-' . sanitize_file_name( bp_current_action() ) . '.php',  
  168. 'members/single/index-component-' . sanitize_file_name( bp_current_component() ) . '.php',  
  169. 'members/single/index.php' 
  170. ) ); 
  171.  
  172. // Merge new templates with existing stack 
  173. // @see bp_get_theme_compat_templates(). 
  174. $templates = array_merge( (array) $new_templates, $templates ); 
  175.  
  176. return $templates; 
  177.  
  178. /** 
  179. * Update the global $post with the displayed user's data. 
  180. * 
  181. * @since 1.7.0 
  182. */ 
  183. public function single_dummy_post() { 
  184. bp_theme_compat_reset_post( array( 
  185. 'ID' => 0,  
  186. 'post_title' => bp_get_displayed_user_fullname(),  
  187. 'post_author' => 0,  
  188. 'post_date' => 0,  
  189. 'post_content' => '',  
  190. 'post_type' => 'page',  
  191. 'post_status' => 'publish',  
  192. 'is_page' => true,  
  193. 'comment_status' => 'closed' 
  194. ) ); 
  195.  
  196. /** 
  197. * Filter the_content with the members' single home template part. 
  198. * 
  199. * @since 1.7.0 
  200. */ 
  201. public function single_dummy_content() { 
  202. return bp_buffer_template_part( 'members/single/home', null, false ); 
.