[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-blogs/classes/ -> class-bp-blogs-template.php (source)

   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  
 123          // Set the total blog count.
 124          if ( empty( $max ) || ( $max >= (int) $this->blogs['total'] ) ) {
 125              $this->total_blog_count = (int) $this->blogs['total'];
 126          } else {
 127              $this->total_blog_count = (int) $max;
 128          }
 129  
 130          // Set the blogs array (to loop through later.
 131          $this->blogs = $this->blogs['blogs'];
 132  
 133          // Get the current blog count to compare maximum against.
 134          $blog_count = count( $this->blogs );
 135  
 136          // Set the current blog count.
 137          if ( empty( $max ) || ( $max >= (int) $blog_count ) ) {
 138              $this->blog_count = (int) $blog_count;
 139          } else {
 140              $this->blog_count = (int) $max;
 141          }
 142  
 143          // Build pagination links based on total blogs and current page number.
 144          if ( ! empty( $this->total_blog_count ) && ! empty( $this->pag_num ) ) {
 145              $this->pag_links = paginate_links( array(
 146                  'base'      => add_query_arg( $this->pag_arg, '%#%' ),
 147                  'format'    => '',
 148                  'total'     => ceil( (int) $this->total_blog_count / (int) $this->pag_num ),
 149                  'current'   => (int) $this->pag_page,
 150                  'prev_text' => _x( '&larr;', 'Blog pagination previous text', 'buddypress' ),
 151                  'next_text' => _x( '&rarr;', 'Blog pagination next text',     'buddypress' ),
 152                  'mid_size'  => 1,
 153                  'add_args'  => array(),
 154              ) );
 155          }
 156      }
 157  
 158      /**
 159       * Whether there are blogs available in the loop.
 160       *
 161       * @see bp_has_blogs()
 162       *
 163       * @return bool True if there are items in the loop, otherwise false.
 164       */
 165  	public function has_blogs() {
 166          return (bool) ! empty( $this->blog_count );
 167      }
 168  
 169      /**
 170       * Set up the next blog and iterate index.
 171       *
 172       * @return object The next blog to iterate over.
 173       */
 174  	public function next_blog() {
 175          $this->current_blog++;
 176          $this->blog = $this->blogs[ $this->current_blog ];
 177  
 178          return $this->blog;
 179      }
 180  
 181      /**
 182       * Rewind the blogs and reset blog index.
 183       */
 184  	public function rewind_blogs() {
 185          $this->current_blog = -1;
 186          if ( $this->blog_count > 0 ) {
 187              $this->blog = $this->blogs[0];
 188          }
 189      }
 190  
 191      /**
 192       * Whether there are blogs left in the loop to iterate over.
 193       *
 194       * This method is used by {@link bp_blogs()} as part of the while loop
 195       * that controls iteration inside the blogs loop, eg:
 196       *     while ( bp_blogs() ) { ...
 197       *
 198       * @see bp_blogs()
 199       *
 200       * @return bool True if there are more blogs to show, otherwise false.
 201       */
 202  	public function blogs() {
 203          if ( ( $this->current_blog + 1 ) < $this->blog_count ) {
 204              return true;
 205          } elseif ( ( $this->current_blog + 1 ) === $this->blog_count ) {
 206  
 207              /**
 208               * Fires right before the rewinding of blogs listing after all are shown.
 209               *
 210               * @since 1.5.0
 211               */
 212              do_action( 'blog_loop_end' );
 213              // Do some cleaning up after the loop.
 214              $this->rewind_blogs();
 215          }
 216  
 217          $this->in_the_loop = false;
 218          return false;
 219      }
 220  
 221      /**
 222       * Set up the current blog inside the loop.
 223       *
 224       * Used by {@link bp_the_blog()} to set up the current blog data while
 225       * looping, so that template tags used during that iteration make
 226       * reference to the current blog.
 227       *
 228       * @see bp_the_blog()
 229       */
 230  	public function the_blog() {
 231  
 232          $this->in_the_loop = true;
 233          $this->blog        = $this->next_blog();
 234  
 235          // Loop has just started.
 236          if ( 0 === $this->current_blog ) {
 237  
 238              /**
 239               * Fires if on the first blog in the loop.
 240               *
 241               * @since 1.5.0
 242               */
 243              do_action( 'blog_loop_start' );
 244          }
 245      }
 246  }


Generated: Thu Oct 17 01:01:39 2019 Cross-referenced by PHPXref 0.7.1