BP_Blogs_Template

The main blog template loop class.

Defined (1)

The class is defined in the following location(s).

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