Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/05/2016 04:08:04 AM (11 years ago)
Author:
boonebgorges
Message:

Move bp-blogs classes to their own files.

See #6870.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-blogs/bp-blogs-template.php

    r10417 r10517  
    1010// Exit if accessed directly.
    1111defined( 'ABSPATH' ) || exit;
     12
     13require dirname( __FILE__ ) . '/classes/class-bp-blogs-template.php';
    1214
    1315/**
     
    102104                return apply_filters( 'bp_get_blogs_directory_permalink', trailingslashit( bp_get_root_domain() . '/' . bp_get_blogs_root_slug() ) );
    103105        }
    104 
    105 /**
    106  * The main blog template loop class.
    107  *
    108  * Responsible for loading a group of blogs into a loop for display.
    109  */
    110 class BP_Blogs_Template {
    111 
    112         /**
    113          * The loop iterator.
    114          *
    115          * @var int
    116          */
    117         public $current_blog = -1;
    118 
    119         /**
    120          * The number of blogs returned by the paged query.
    121          *
    122          * @var int
    123          */
    124         public $blog_count = 0;
    125 
    126         /**
    127          * Array of blogs located by the query..
    128          *
    129          * @var array
    130          */
    131         public $blogs = array();
    132 
    133         /**
    134          * The blog object currently being iterated on.
    135          *
    136          * @var object
    137          */
    138         public $blog;
    139 
    140         /**
    141          * A flag for whether the loop is currently being iterated.
    142          *
    143          * @var bool
    144          */
    145         public $in_the_loop = false;
    146 
    147         /**
    148          * The page number being requested.
    149          *
    150          * @var int
    151          */
    152         public $pag_page = 1;
    153 
    154         /**
    155          * The number of items being requested per page.
    156          *
    157          * @var int
    158          */
    159         public $pag_num = 20;
    160 
    161         /**
    162          * An HTML string containing pagination links.
    163          *
    164          * @var string
    165          */
    166         public $pag_links = '';
    167 
    168         /**
    169          * The total number of blogs matching the query parameters.
    170          *
    171          * @var int
    172          */
    173         public $total_blog_count = 0;
    174 
    175         /**
    176          * Constructor method.
    177          *
    178          * @see BP_Blogs_Blog::get() for a description of parameters.
    179          *
    180          * @param string     $type              See {@link BP_Blogs_Blog::get()}.
    181          * @param string     $page              See {@link BP_Blogs_Blog::get()}.
    182          * @param string     $per_page          See {@link BP_Blogs_Blog::get()}.
    183          * @param string     $max               See {@link BP_Blogs_Blog::get()}.
    184          * @param string     $user_id           See {@link BP_Blogs_Blog::get()}.
    185          * @param string     $search_terms      See {@link BP_Blogs_Blog::get()}.
    186          * @param string     $page_arg          The string used as a query parameter in
    187          *                                      pagination links. Default: 'bpage'.
    188          * @param bool       $update_meta_cache Whether to pre-fetch metadata for
    189          *                                      queried blogs.
    190          * @param array|bool $include_blog_ids  Array of blog IDs to include.
    191          */
    192         public function __construct( $type, $page, $per_page, $max, $user_id, $search_terms, $page_arg = 'bpage', $update_meta_cache = true, $include_blog_ids = false ) {
    193 
    194                 $this->pag_arg  = sanitize_key( $page_arg );
    195                 $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $page     );
    196                 $this->pag_num  = bp_sanitize_pagination_arg( 'num',          $per_page );
    197 
    198                 // Backwards compatibility support for blogs by first letter.
    199                 if ( ! empty( $_REQUEST['letter'] ) ) {
    200                         $this->blogs = BP_Blogs_Blog::get_by_letter( $_REQUEST['letter'], $this->pag_num, $this->pag_page );
    201 
    202                 // Typical blogs query.
    203                 } else {
    204                         $this->blogs = bp_blogs_get_blogs( array(
    205                                 'type'              => $type,
    206                                 'per_page'          => $this->pag_num,
    207                                 'page'              => $this->pag_page,
    208                                 'user_id'           => $user_id,
    209                                 'search_terms'      => $search_terms,
    210                                 'update_meta_cache' => $update_meta_cache,
    211                                 'include_blog_ids'  => $include_blog_ids,
    212                         ) );
    213                 }
    214 
    215                 // Set the total blog count.
    216                 if ( empty( $max ) || ( $max >= (int) $this->blogs['total'] ) ) {
    217                         $this->total_blog_count = (int) $this->blogs['total'];
    218                 } else {
    219                         $this->total_blog_count = (int) $max;
    220                 }
    221 
    222                 // Set the blogs array (to loop through later.
    223                 $this->blogs = $this->blogs['blogs'];
    224 
    225                 // Get the current blog count to compare maximum against.
    226                 $blog_count = count( $this->blogs );
    227 
    228                 // Set the current blog count.
    229                 if ( empty( $max ) || ( $max >= (int) $blog_count ) ) {
    230                         $this->blog_count = (int) $blog_count;
    231                 } else {
    232                         $this->blog_count = (int) $max;
    233                 }
    234 
    235                 // Build pagination links based on total blogs and current page number.
    236                 if ( ! empty( $this->total_blog_count ) && ! empty( $this->pag_num ) ) {
    237                         $this->pag_links = paginate_links( array(
    238                                 'base'      => add_query_arg( $this->pag_arg, '%#%' ),
    239                                 'format'    => '',
    240                                 'total'     => ceil( (int) $this->total_blog_count / (int) $this->pag_num ),
    241                                 'current'   => (int) $this->pag_page,
    242                                 'prev_text' => _x( '←', 'Blog pagination previous text', 'buddypress' ),
    243                                 'next_text' => _x( '→', 'Blog pagination next text',     'buddypress' ),
    244                                 'mid_size'  => 1,
    245                                 'add_args'  => array(),
    246                         ) );
    247                 }
    248         }
    249 
    250         /**
    251          * Whether there are blogs available in the loop.
    252          *
    253          * @see bp_has_blogs()
    254          *
    255          * @return bool True if there are items in the loop, otherwise false.
    256          */
    257         public function has_blogs() {
    258                 return (bool) ! empty( $this->blog_count );
    259         }
    260 
    261         /**
    262          * Set up the next blog and iterate index.
    263          *
    264          * @return object The next blog to iterate over.
    265          */
    266         public function next_blog() {
    267                 $this->current_blog++;
    268                 $this->blog = $this->blogs[ $this->current_blog ];
    269 
    270                 return $this->blog;
    271         }
    272 
    273         /**
    274          * Rewind the blogs and reset blog index.
    275          */
    276         public function rewind_blogs() {
    277                 $this->current_blog = -1;
    278                 if ( $this->blog_count > 0 ) {
    279                         $this->blog = $this->blogs[0];
    280                 }
    281         }
    282 
    283         /**
    284          * Whether there are blogs left in the loop to iterate over.
    285          *
    286          * This method is used by {@link bp_blogs()} as part of the while loop
    287          * that controls iteration inside the blogs loop, eg:
    288          *     while ( bp_blogs() ) { ...
    289          *
    290          * @see bp_blogs()
    291          *
    292          * @return bool True if there are more blogs to show, otherwise false.
    293          */
    294         public function blogs() {
    295                 if ( ( $this->current_blog + 1 ) < $this->blog_count ) {
    296                         return true;
    297                 } elseif ( ( $this->current_blog + 1 ) === $this->blog_count ) {
    298 
    299                         /**
    300                          * Fires right before the rewinding of blogs listing after all are shown.
    301                          *
    302                          * @since 1.5.0
    303                          */
    304                         do_action( 'blog_loop_end' );
    305                         // Do some cleaning up after the loop.
    306                         $this->rewind_blogs();
    307                 }
    308 
    309                 $this->in_the_loop = false;
    310                 return false;
    311         }
    312 
    313         /**
    314          * Set up the current blog inside the loop.
    315          *
    316          * Used by {@link bp_the_blog()} to set up the current blog data while
    317          * looping, so that template tags used during that iteration make
    318          * reference to the current blog.
    319          *
    320          * @see bp_the_blog()
    321          */
    322         public function the_blog() {
    323 
    324                 $this->in_the_loop = true;
    325                 $this->blog        = $this->next_blog();
    326 
    327                 // Loop has just started.
    328                 if ( 0 === $this->current_blog ) {
    329 
    330                         /**
    331                          * Fires if on the first blog in the loop.
    332                          *
    333                          * @since 1.5.0
    334                          */
    335                         do_action( 'blog_loop_start' );
    336                 }
    337         }
    338 }
    339106
    340107/**
Note: See TracChangeset for help on using the changeset viewer.