/bp-blogs/classes/class-bp-blogs-template.php

  1. <?php 
  2. /** 
  3. * BuddyPress Blogs Template Class. 
  4. * 
  5. * @package BuddyPress 
  6. * @subpackage BlogsTemplate 
  7. * @since 1.5.0 
  8. */ 
  9.  
  10. // Exit if accessed directly. 
  11. defined( 'ABSPATH' ) || exit; 
  12.  
  13. /** 
  14. * The main blog template loop class. 
  15. * 
  16. * Responsible for loading a group of blogs into a loop for display. 
  17. */ 
  18. class BP_Blogs_Template { 
  19.  
  20. /** 
  21. * The loop iterator. 
  22. * 
  23. * @var int 
  24. */ 
  25. public $current_blog = -1; 
  26.  
  27. /** 
  28. * The number of blogs returned by the paged query. 
  29. * 
  30. * @var int 
  31. */ 
  32. public $blog_count = 0; 
  33.  
  34. /** 
  35. * Array of blogs located by the query.. 
  36. * 
  37. * @var array 
  38. */ 
  39. public $blogs = array(); 
  40.  
  41. /** 
  42. * The blog object currently being iterated on. 
  43. * 
  44. * @var object 
  45. */ 
  46. public $blog; 
  47.  
  48. /** 
  49. * A flag for whether the loop is currently being iterated. 
  50. * 
  51. * @var bool 
  52. */ 
  53. public $in_the_loop = false; 
  54.  
  55. /** 
  56. * The page number being requested. 
  57. * 
  58. * @var int 
  59. */ 
  60. public $pag_page = 1; 
  61.  
  62. /** 
  63. * The number of items being requested per page. 
  64. * 
  65. * @var int 
  66. */ 
  67. public $pag_num = 20; 
  68.  
  69. /** 
  70. * An HTML string containing pagination links. 
  71. * 
  72. * @var string 
  73. */ 
  74. public $pag_links = ''; 
  75.  
  76. /** 
  77. * The total number of blogs matching the query parameters. 
  78. * 
  79. * @var int 
  80. */ 
  81. public $total_blog_count = 0; 
  82.  
  83. /** 
  84. * Constructor method. 
  85. * 
  86. * @see BP_Blogs_Blog::get() for a description of parameters. 
  87. * 
  88. * @param string $type See {@link BP_Blogs_Blog::get()}. 
  89. * @param string $page See {@link BP_Blogs_Blog::get()}. 
  90. * @param string $per_page See {@link BP_Blogs_Blog::get()}. 
  91. * @param string $max See {@link BP_Blogs_Blog::get()}. 
  92. * @param string $user_id See {@link BP_Blogs_Blog::get()}. 
  93. * @param string $search_terms See {@link BP_Blogs_Blog::get()}. 
  94. * @param string $page_arg The string used as a query parameter in 
  95. * pagination links. Default: 'bpage'. 
  96. * @param bool $update_meta_cache Whether to pre-fetch metadata for 
  97. * queried blogs. 
  98. * @param array|bool $include_blog_ids Array of blog IDs to include. 
  99. */ 
  100. public function __construct( $type, $page, $per_page, $max, $user_id, $search_terms, $page_arg = 'bpage', $update_meta_cache = true, $include_blog_ids = false ) { 
  101.  
  102. $this->pag_arg = sanitize_key( $page_arg ); 
  103. $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $page ); 
  104. $this->pag_num = bp_sanitize_pagination_arg( 'num', $per_page ); 
  105.  
  106. // Backwards compatibility support for blogs by first letter. 
  107. if ( ! empty( $_REQUEST['letter'] ) ) { 
  108. $this->blogs = BP_Blogs_Blog::get_by_letter( $_REQUEST['letter'], $this->pag_num, $this->pag_page ); 
  109.  
  110. // Typical blogs query. 
  111. } else { 
  112. $this->blogs = bp_blogs_get_blogs( array( 
  113. 'type' => $type,  
  114. 'per_page' => $this->pag_num,  
  115. 'page' => $this->pag_page,  
  116. 'user_id' => $user_id,  
  117. 'search_terms' => $search_terms,  
  118. 'update_meta_cache' => $update_meta_cache,  
  119. 'include_blog_ids' => $include_blog_ids,  
  120. ) ); 
  121.  
  122. // Set the total blog count. 
  123. if ( empty( $max ) || ( $max >= (int) $this->blogs['total'] ) ) { 
  124. $this->total_blog_count = (int) $this->blogs['total']; 
  125. } else { 
  126. $this->total_blog_count = (int) $max; 
  127.  
  128. // Set the blogs array (to loop through later. 
  129. $this->blogs = $this->blogs['blogs']; 
  130.  
  131. // Get the current blog count to compare maximum against. 
  132. $blog_count = count( $this->blogs ); 
  133.  
  134. // Set the current blog count. 
  135. if ( empty( $max ) || ( $max >= (int) $blog_count ) ) { 
  136. $this->blog_count = (int) $blog_count; 
  137. } else { 
  138. $this->blog_count = (int) $max; 
  139.  
  140. // Build pagination links based on total blogs and current page number. 
  141. if ( ! empty( $this->total_blog_count ) && ! empty( $this->pag_num ) ) { 
  142. $this->pag_links = paginate_links( array( 
  143. 'base' => add_query_arg( $this->pag_arg, '%#%' ),  
  144. 'format' => '',  
  145. 'total' => ceil( (int) $this->total_blog_count / (int) $this->pag_num ),  
  146. 'current' => (int) $this->pag_page,  
  147. 'prev_text' => _x( '←', 'Blog pagination previous text', 'buddypress' ),  
  148. 'next_text' => _x( '→', 'Blog pagination next text', 'buddypress' ),  
  149. 'mid_size' => 1,  
  150. 'add_args' => array(),  
  151. ) ); 
  152.  
  153. /** 
  154. * Whether there are blogs available in the loop. 
  155. * 
  156. * @see bp_has_blogs() 
  157. * 
  158. * @return bool True if there are items in the loop, otherwise false. 
  159. */ 
  160. public function has_blogs() { 
  161. return (bool) ! empty( $this->blog_count ); 
  162.  
  163. /** 
  164. * Set up the next blog and iterate index. 
  165. * 
  166. * @return object The next blog to iterate over. 
  167. */ 
  168. public function next_blog() { 
  169. $this->current_blog++; 
  170. $this->blog = $this->blogs[ $this->current_blog ]; 
  171.  
  172. return $this->blog; 
  173.  
  174. /** 
  175. * Rewind the blogs and reset blog index. 
  176. */ 
  177. public function rewind_blogs() { 
  178. $this->current_blog = -1; 
  179. if ( $this->blog_count > 0 ) { 
  180. $this->blog = $this->blogs[0]; 
  181.  
  182. /** 
  183. * Whether there are blogs left in the loop to iterate over. 
  184. * 
  185. * This method is used by {@link bp_blogs()} as part of the while loop 
  186. * that controls iteration inside the blogs loop, eg: 
  187. * while ( bp_blogs() ) { ... 
  188. * 
  189. * @see bp_blogs() 
  190. * 
  191. * @return bool True if there are more blogs to show, otherwise false. 
  192. */ 
  193. public function blogs() { 
  194. if ( ( $this->current_blog + 1 ) < $this->blog_count ) { 
  195. return true; 
  196. } elseif ( ( $this->current_blog + 1 ) === $this->blog_count ) { 
  197.  
  198. /** 
  199. * Fires right before the rewinding of blogs listing after all are shown. 
  200. * 
  201. * @since 1.5.0 
  202. */ 
  203. do_action( 'blog_loop_end' ); 
  204. // Do some cleaning up after the loop. 
  205. $this->rewind_blogs(); 
  206.  
  207. $this->in_the_loop = false; 
  208. return false; 
  209.  
  210. /** 
  211. * Set up the current blog inside the loop. 
  212. * 
  213. * Used by {@link bp_the_blog()} to set up the current blog data while 
  214. * looping, so that template tags used during that iteration make 
  215. * reference to the current blog. 
  216. * 
  217. * @see bp_the_blog() 
  218. */ 
  219. public function the_blog() { 
  220.  
  221. $this->in_the_loop = true; 
  222. $this->blog = $this->next_blog(); 
  223.  
  224. // Loop has just started. 
  225. if ( 0 === $this->current_blog ) { 
  226.  
  227. /** 
  228. * Fires if on the first blog in the loop. 
  229. * 
  230. * @since 1.5.0 
  231. */ 
  232. do_action( 'blog_loop_start' ); 
.