[ 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       * @since 1.2.0
  87       * @since 10.0.0 Converted to array as main function argument. Added $date_query parameter.
  88       *
  89       * @see BP_Blogs_Blog::get() for a description of parameters.
  90       *
  91       * @param array $args {
  92       *     Array of arguments. See {@link BP_Blogs_Blog::get()}.
  93       * }
  94       */
  95  	public function __construct( ...$args ) {
  96          // Backward compatibility with old method of passing arguments.
  97          if ( ! is_array( $args[0] ) || count( $args ) > 1 ) {
  98              _deprecated_argument( __METHOD__, '10.0.0', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) );
  99  
 100              $old_args_keys = [
 101                  0  => 'type',
 102                  1  => 'page',
 103                  2  => 'per_page',
 104                  3  => 'max',
 105                  4  => 'user_id',
 106                  5  => 'search_terms',
 107                  6  => 'page_arg',
 108                  7  => 'update_meta_cache',
 109                  8  => 'include_blog_ids',
 110              ];
 111  
 112              $args = bp_core_parse_args_array( $old_args_keys, $args );
 113          } else {
 114              $args = reset( $args );
 115          }
 116  
 117          $r = bp_parse_args(
 118              $args,
 119              array(
 120                  'type'              => '',
 121                  'page'              => false,
 122                  'per_page'          => false,
 123                  'max'               => false,
 124                  'user_id'           => false,
 125                  'search_terms'      => false,
 126                  'page_arg'          => 'bpage',
 127                  'update_meta_cache' => true,
 128                  'include_blog_ids'  => false,
 129                  'date_query'        => false,
 130              ),
 131              'blogs_template'
 132          );
 133  
 134          $this->pag_arg  = sanitize_key( $r['page_arg'] );
 135          $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $r['page'] );
 136          $this->pag_num  = bp_sanitize_pagination_arg( 'num', $r['per_page'] );
 137  
 138          // Backwards compatibility support for blogs by first letter.
 139          if ( ! empty( $_REQUEST['letter'] ) ) {
 140              $this->blogs = BP_Blogs_Blog::get_by_letter( $_REQUEST['letter'], $this->pag_num, $this->pag_page );
 141  
 142          // Typical blogs query.
 143          } else {
 144              $this->blogs = bp_blogs_get_blogs( array(
 145                  'type'              => $r['type'],
 146                  'per_page'          => $this->pag_num,
 147                  'page'              => $this->pag_page,
 148                  'user_id'           => $r['user_id'],
 149                  'search_terms'      => $r['search_terms'],
 150                  'update_meta_cache' => $r['update_meta_cache'],
 151                  'include_blog_ids'  => $r['include_blog_ids'],
 152                  'date_query'        => $r['date_query']
 153              ) );
 154          }
 155  
 156          // Set the total blog count.
 157          if ( empty( $r['max'] ) || ( $r['max'] >= (int) $this->blogs['total'] ) ) {
 158              $this->total_blog_count = (int) $this->blogs['total'];
 159          } else {
 160              $this->total_blog_count = (int) $max;
 161          }
 162  
 163          // Set the blogs array (to loop through later.
 164          $this->blogs = $this->blogs['blogs'];
 165  
 166          // Get the current blog count to compare maximum against.
 167          $blog_count = count( $this->blogs );
 168  
 169          // Set the current blog count.
 170          if ( empty( $r['max'] ) || ( $r['max'] >= (int) $blog_count ) ) {
 171              $this->blog_count = (int) $blog_count;
 172          } else {
 173              $this->blog_count = (int) $max;
 174          }
 175  
 176          // Build pagination links based on total blogs and current page number.
 177          if ( ! empty( $this->total_blog_count ) && ! empty( $this->pag_num ) ) {
 178              $this->pag_links = paginate_links( array(
 179                  'base'      => add_query_arg( $this->pag_arg, '%#%' ),
 180                  'format'    => '',
 181                  'total'     => ceil( (int) $this->total_blog_count / (int) $this->pag_num ),
 182                  'current'   => (int) $this->pag_page,
 183                  'prev_text' => _x( '&larr;', 'Blog pagination previous text', 'buddypress' ),
 184                  'next_text' => _x( '&rarr;', 'Blog pagination next text',     'buddypress' ),
 185                  'mid_size'  => 1,
 186                  'add_args'  => array(),
 187              ) );
 188          }
 189      }
 190  
 191      /**
 192       * Whether there are blogs available in the loop.
 193       *
 194       * @see bp_has_blogs()
 195       *
 196       * @return bool True if there are items in the loop, otherwise false.
 197       */
 198  	public function has_blogs() {
 199          return (bool) ! empty( $this->blog_count );
 200      }
 201  
 202      /**
 203       * Set up the next blog and iterate index.
 204       *
 205       * @return object The next blog to iterate over.
 206       */
 207  	public function next_blog() {
 208          $this->current_blog++;
 209          $this->blog = $this->blogs[ $this->current_blog ];
 210  
 211          return $this->blog;
 212      }
 213  
 214      /**
 215       * Rewind the blogs and reset blog index.
 216       */
 217  	public function rewind_blogs() {
 218          $this->current_blog = -1;
 219          if ( $this->blog_count > 0 ) {
 220              $this->blog = $this->blogs[0];
 221          }
 222      }
 223  
 224      /**
 225       * Whether there are blogs left in the loop to iterate over.
 226       *
 227       * This method is used by {@link bp_blogs()} as part of the while loop
 228       * that controls iteration inside the blogs loop, eg:
 229       *     while ( bp_blogs() ) { ...
 230       *
 231       * @see bp_blogs()
 232       *
 233       * @return bool True if there are more blogs to show, otherwise false.
 234       */
 235  	public function blogs() {
 236          if ( ( $this->current_blog + 1 ) < $this->blog_count ) {
 237              return true;
 238          } elseif ( ( $this->current_blog + 1 ) === $this->blog_count ) {
 239  
 240              /**
 241               * Fires right before the rewinding of blogs listing after all are shown.
 242               *
 243               * @since 1.5.0
 244               */
 245              do_action( 'blog_loop_end' );
 246              // Do some cleaning up after the loop.
 247              $this->rewind_blogs();
 248          }
 249  
 250          $this->in_the_loop = false;
 251          return false;
 252      }
 253  
 254      /**
 255       * Set up the current blog inside the loop.
 256       *
 257       * Used by {@link bp_the_blog()} to set up the current blog data while
 258       * looping, so that template tags used during that iteration make
 259       * reference to the current blog.
 260       *
 261       * @see bp_the_blog()
 262       */
 263  	public function the_blog() {
 264  
 265          $this->in_the_loop = true;
 266          $this->blog        = $this->next_blog();
 267  
 268          // Loop has just started.
 269          if ( 0 === $this->current_blog ) {
 270  
 271              /**
 272               * Fires if on the first blog in the loop.
 273               *
 274               * @since 1.5.0
 275               */
 276              do_action( 'blog_loop_start' );
 277          }
 278      }
 279  }


Generated: Fri Oct 4 01:00:58 2024 Cross-referenced by PHPXref 0.7.1