Skip to:
Content

BuddyPress.org

Changeset 8758


Ignore:
Timestamp:
08/05/2014 04:10:55 PM (5 years ago)
Author:
johnjamesjacoby
Message:

Clean up BP_Blogs_Template() methods:

  • Add inline doc
  • Use public modifiers
  • Normalize comparisons
  • Add default variable values
  • Add brackets and bring code formatting up to date
  • Use empty() checks where appropriate
File:
1 edited

Legend:

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

    r8757 r8758  
    9191     * @var int
    9292     */
    93     var $current_blog = -1;
     93    public $current_blog = -1;
    9494
    9595    /**
     
    9999     * @var int
    100100     */
    101     var $blog_count;
     101    public $blog_count = 0;
    102102
    103103    /**
     
    107107     * @var array
    108108     */
    109     var $blogs;
     109    public $blogs = array();
    110110
    111111    /**
     
    115115     * @var object
    116116     */
    117     var $blog;
     117    public $blog;
    118118
    119119    /**
     
    123123     * @var bool
    124124     */
    125     var $in_the_loop;
     125    public $in_the_loop = false;
    126126
    127127    /**
     
    131131     * @var public
    132132     */
    133     var $pag_page;
     133    public $pag_page = 1;
    134134
    135135    /**
     
    139139     * @var public
    140140     */
    141     var $pag_num;
     141    public $pag_num = 20;
    142142
    143143    /**
     
    147147     * @var string
    148148     */
    149     var $pag_links;
     149    public $pag_links = '';
    150150
    151151    /**
     
    155155     * @var int
    156156     */
    157     var $total_blog_count;
     157    public $total_blog_count = 0;
    158158
    159159    /**
     
    174174     * @param array $include_blog_ids Array of blog IDs to include.
    175175     */
    176     function __construct( $type, $page, $per_page, $max, $user_id, $search_terms, $page_arg = 'bpage', $update_meta_cache = true, $include_blog_ids = false ) {
    177 
    178         $this->pag_page = isset( $_REQUEST[$page_arg] ) ? intval( $_REQUEST[$page_arg] ) : $page;
    179         $this->pag_num = isset( $_REQUEST['num'] ) ? intval( $_REQUEST['num'] ) : $per_page;
    180 
    181         if ( isset( $_REQUEST['letter'] ) && '' != $_REQUEST['letter'] ) {
     176    public function __construct( $type, $page, $per_page, $max, $user_id, $search_terms, $page_arg = 'bpage', $update_meta_cache = true, $include_blog_ids = false ) {
     177
     178        $this->pag_page = isset( $_REQUEST[ $page_arg ] ) ? intval( $_REQUEST[ $page_arg ] ) : $page;
     179        $this->pag_num  = isset( $_REQUEST['num']       ) ? intval( $_REQUEST['num']       ) : $per_page;
     180
     181        // Backwards compatibility support for blogs by first letter
     182        if ( ! empty( $_REQUEST['letter'] ) ) {
    182183            $this->blogs = BP_Blogs_Blog::get_by_letter( $_REQUEST['letter'], $this->pag_num, $this->pag_page );
     184
     185        // Typical blogs query
    183186        } else {
    184187            $this->blogs = bp_blogs_get_blogs( array(
     
    193196        }
    194197
    195         if ( !$max || $max >= (int) $this->blogs['total'] )
     198        // Set the total blog count
     199        if ( empty( $max ) || ( $max >= (int) $this->blogs['total'] ) ) {
    196200            $this->total_blog_count = (int) $this->blogs['total'];
    197         else
     201        } else {
    198202            $this->total_blog_count = (int) $max;
    199 
     203        }
     204
     205        // Set the blogs array (to loop through later
    200206        $this->blogs = $this->blogs['blogs'];
    201207
    202         if ( $max ) {
    203             if ( $max >= count($this->blogs) ) {
    204                 $this->blog_count = count( $this->blogs );
    205             } else {
    206                 $this->blog_count = (int) $max;
    207             }
     208        // Get the current blog count to compare maximum against
     209        $blog_count = count( $this->blogs );
     210
     211        // Set the current blog count
     212        if ( empty( $max ) || ( $max >= (int) $blog_count ) ) {
     213            $this->blog_count = (int) $blog_count;
    208214        } else {
    209             $this->blog_count = count( $this->blogs );
    210         }
    211 
    212         if ( (int) $this->total_blog_count && (int) $this->pag_num ) {
     215            $this->blog_count = (int) $max;
     216        }
     217
     218        // Build pagination links based on total blogs and current page number
     219        if ( ! empty( $this->total_blog_count ) && ! empty( $this->pag_num ) ) {
    213220            $this->pag_links = paginate_links( array(
    214221                'base'      => add_query_arg( $page_arg, '%#%' ),
     
    217224                'current'   => (int) $this->pag_page,
    218225                'prev_text' => _x( '←', 'Blog pagination previous text', 'buddypress' ),
    219                 'next_text' => _x( '→', 'Blog pagination next text', 'buddypress' ),
     226                'next_text' => _x( '→', 'Blog pagination next text',     'buddypress' ),
    220227                'mid_size'  => 1
    221228            ) );
     
    230237     * @return bool True if there are items in the loop, otherwise false.
    231238     */
    232     function has_blogs() {
    233         if ( $this->blog_count )
    234             return true;
    235 
    236         return false;
     239    public function has_blogs() {
     240        return (bool) ! empty( $this->blog_count );
    237241    }
    238242
     
    242246     * @return object The next blog to iterate over.
    243247     */
    244     function next_blog() {
     248    public function next_blog() {
    245249        $this->current_blog++;
    246         $this->blog = $this->blogs[$this->current_blog];
     250        $this->blog = $this->blogs[ $this->current_blog ];
    247251
    248252        return $this->blog;
     
    252256     * Rewind the blogs and reset blog index.
    253257     */
    254     function rewind_blogs() {
     258    public function rewind_blogs() {
    255259        $this->current_blog = -1;
    256260        if ( $this->blog_count > 0 ) {
     
    270274     * @return bool True if there are more blogs to show, otherwise false.
    271275     */
    272     function blogs() {
    273         if ( $this->current_blog + 1 < $this->blog_count ) {
     276    public function blogs() {
     277        if ( ( $this->current_blog + 1 ) < $this->blog_count ) {
    274278            return true;
    275         } elseif ( $this->current_blog + 1 == $this->blog_count ) {
    276             do_action('blog_loop_end');
     279        } elseif ( ( $this->current_blog + 1 ) === $this->blog_count ) {
     280            do_action( 'blog_loop_end' );
    277281            // Do some cleaning up after the loop
    278282            $this->rewind_blogs();
     
    292296     * @see bp_the_blog()
    293297     */
    294     function the_blog() {
     298    public function the_blog() {
    295299
    296300        $this->in_the_loop = true;
    297301        $this->blog        = $this->next_blog();
    298302
    299         if ( 0 == $this->current_blog ) // loop has just started
    300             do_action('blog_loop_start');
     303        // loop has just started
     304        if ( 0 === $this->current_blog ) {
     305            do_action( 'blog_loop_start' );
     306        }
    301307    }
    302308}
Note: See TracChangeset for help on using the changeset viewer.