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

  1. <?php 
  2. /** 
  3. * BuddyPress Blogs Theme Compatibility. 
  4. * 
  5. * @package BuddyPress 
  6. * @subpackage BlogsScreens 
  7. * @since 1.5.0 
  8. */ 
  9.  
  10. // Exit if accessed directly. 
  11. defined( 'ABSPATH' ) || exit; 
  12.  
  13. /** 
  14. * The main theme compat class for BuddyPress Blogs. 
  15. * 
  16. * This class sets up the necessary theme compatibility actions to safely output 
  17. * group template parts to the_title and the_content areas of a theme. 
  18. * 
  19. * @since 1.7.0 
  20. */ 
  21. class BP_Blogs_Theme_Compat { 
  22.  
  23. /** 
  24. * Set up theme compatibility for the Blogs component. 
  25. * 
  26. * @since 1.7.0 
  27. */ 
  28. public function __construct() { 
  29. add_action( 'bp_setup_theme_compat', array( $this, 'is_blogs' ) ); 
  30.  
  31. /** 
  32. * Are we looking at something that needs Blogs theme compatibility? 
  33. * 
  34. * @since 1.7.0 
  35. */ 
  36. public function is_blogs() { 
  37.  
  38. // Bail if not looking at a group. 
  39. if ( ! bp_is_blogs_component() ) 
  40. return; 
  41.  
  42. // Bail if looking at a users sites. 
  43. if ( bp_is_user() ) 
  44. return; 
  45.  
  46. // Blog Directory. 
  47. if ( is_multisite() && ! bp_current_action() ) { 
  48. bp_update_is_directory( true, 'blogs' ); 
  49.  
  50. /** 
  51. * Fires if in the blog directory and BuddyPress needs Blog theme compatibility,  
  52. * before the actions and filters are added. 
  53. * 
  54. * @since 1.5.0 
  55. */ 
  56. do_action( 'bp_blogs_screen_index' ); 
  57.  
  58. add_filter( 'bp_get_buddypress_template', array( $this, 'directory_template_hierarchy' ) ); 
  59. add_action( 'bp_template_include_reset_dummy_post_data', array( $this, 'directory_dummy_post' ) ); 
  60. add_filter( 'bp_replace_the_content', array( $this, 'directory_content' ) ); 
  61.  
  62. // Create blog. 
  63. } elseif ( is_user_logged_in() && bp_blog_signup_enabled() ) { 
  64. add_filter( 'bp_get_buddypress_template', array( $this, 'create_template_hierarchy' ) ); 
  65. add_action( 'bp_template_include_reset_dummy_post_data', array( $this, 'create_dummy_post' ) ); 
  66. add_filter( 'bp_replace_the_content', array( $this, 'create_content' ) ); 
  67.  
  68. /** Directory *************************************************************/ 
  69.  
  70. /** 
  71. * Add template hierarchy to theme compat for the blog directory page. 
  72. * 
  73. * This is to mirror how WordPress has 
  74. * {@link https://codex.wordpress.org/Template_Hierarchy template hierarchy}. 
  75. * 
  76. * @since 1.8.0 
  77. * 
  78. * @param string $templates The templates from bp_get_theme_compat_templates(). 
  79. * @return array $templates Array of custom templates to look for. 
  80. */ 
  81. public function directory_template_hierarchy( $templates ) { 
  82.  
  83. /** 
  84. * Filters the custom templates used for theme compat with the blog directory page. 
  85. * 
  86. * @since 1.8.0 
  87. * 
  88. * @param array $value Array of template paths to add to template list to look for. 
  89. */ 
  90. $new_templates = apply_filters( 'bp_template_hierarchy_blogs_create', array( 
  91. 'blogs/index-directory.php' 
  92. ) ); 
  93.  
  94. // Merge new templates with existing stack 
  95. // @see bp_get_theme_compat_templates(). 
  96. $templates = array_merge( (array) $new_templates, $templates ); 
  97.  
  98. return $templates; 
  99.  
  100. /** 
  101. * Update the global $post with directory data. 
  102. * 
  103. * @since 1.7.0 
  104. */ 
  105. public function directory_dummy_post() { 
  106.  
  107. bp_theme_compat_reset_post( array( 
  108. 'ID' => 0,  
  109. 'post_title' => __( 'Sites', 'buddypress' ),  
  110. 'post_author' => 0,  
  111. 'post_date' => 0,  
  112. 'post_content' => '',  
  113. 'post_type' => 'page',  
  114. 'post_status' => 'publish',  
  115. 'is_page' => true,  
  116. 'comment_status' => 'closed' 
  117. ) ); 
  118.  
  119. /** 
  120. * Filter the_content with the groups index template part. 
  121. * 
  122. * @since 1.7.0 
  123. */ 
  124. public function directory_content() { 
  125. return bp_buffer_template_part( 'blogs/index', null, false ); 
  126.  
  127. /** Create ****************************************************************/ 
  128.  
  129. /** 
  130. * Add custom template hierarchy to theme compat for the blog create page. 
  131. * 
  132. * This is to mirror how WordPress has 
  133. * {@link https://codex.wordpress.org/Template_Hierarchy template hierarchy}. 
  134. * 
  135. * @since 1.8.0 
  136. * 
  137. * @param string $templates The templates from bp_get_theme_compat_templates(). 
  138. * @return array $templates Array of custom templates to look for. 
  139. */ 
  140. public function create_template_hierarchy( $templates ) { 
  141.  
  142. /** 
  143. * Filters the custom templates used for theme compat with the blog create page. 
  144. * 
  145. * @since 1.8.0 
  146. * 
  147. * @param array $value Array of template paths to add to template list to look for. 
  148. */ 
  149. $new_templates = apply_filters( 'bp_template_hierarchy_blogs_create', array( 
  150. 'blogs/index-create.php' 
  151. ) ); 
  152.  
  153. // Merge new templates with existing stack 
  154. // @see bp_get_theme_compat_templates(). 
  155. $templates = array_merge( (array) $new_templates, $templates ); 
  156.  
  157. return $templates; 
  158.  
  159. /** 
  160. * Update the global $post with create screen data. 
  161. * 
  162. * @since 1.7.0 
  163. */ 
  164. public function create_dummy_post() { 
  165.  
  166. // Title based on ability to create blogs. 
  167. if ( is_user_logged_in() && bp_blog_signup_enabled() ) { 
  168. $title = __( 'Create a Site', 'buddypress' ); 
  169. } else { 
  170. $title = __( 'Sites', 'buddypress' ); 
  171.  
  172. bp_theme_compat_reset_post( array( 
  173. 'ID' => 0,  
  174. 'post_title' => $title,  
  175. 'post_author' => 0,  
  176. 'post_date' => 0,  
  177. 'post_content' => '',  
  178. 'post_type' => 'page',  
  179. 'post_status' => 'publish',  
  180. 'is_page' => true,  
  181. 'comment_status' => 'closed' 
  182. ) ); 
  183.  
  184. /** 
  185. * Filter the_content with the create screen template part. 
  186. * 
  187. * @since 1.7.0 
  188. */ 
  189. public function create_content() { 
  190. return bp_buffer_template_part( 'blogs/create', null, false ); 
.