Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/05/2016 03:54:56 AM (10 years ago)
Author:
boonebgorges
Message:

Move bp-activity classes to their own files.

See #6870.

File:
1 edited

Legend:

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

    r10487 r10516  
    1010// Exit if accessed directly.
    1111defined( 'ABSPATH' ) || exit;
     12
     13require dirname( __FILE__ ) . '/classes/class-bp-activity-template.php';
    1214
    1315/**
     
    106108        return apply_filters( 'bp_get_activity_directory_permalink', trailingslashit( bp_get_root_domain() . '/' . bp_get_activity_root_slug() ) );
    107109    }
    108 
    109 /**
    110  * The main activity template loop class.
    111  *
    112  * This is responsible for loading a group of activity items and displaying them.
    113  *
    114  * @since 1.0.0
    115  */
    116 class BP_Activity_Template {
    117 
    118     /**
    119      * The loop iterator.
    120      *
    121      * @since 1.0.0
    122      * @var int
    123      */
    124     public $current_activity = -1;
    125 
    126     /**
    127      * The activity count.
    128      *
    129      * @since 1.0.0
    130      * @var int
    131      */
    132     public $activity_count;
    133 
    134     /**
    135      * The total activity count.
    136      *
    137      * @since 1.0.0
    138      * @var int
    139      */
    140     public $total_activity_count;
    141 
    142     /**
    143      * Array of activities located by the query.
    144      *
    145      * @since 1.0.0
    146      * @var array
    147      */
    148     public $activities;
    149 
    150     /**
    151      * The activity object currently being iterated on.
    152      *
    153      * @since 1.0.0
    154      * @var object
    155      */
    156     public $activity;
    157 
    158     /**
    159      * A flag for whether the loop is currently being iterated.
    160      *
    161      * @since 1.0.0
    162      * @var bool
    163      */
    164     public $in_the_loop;
    165 
    166     /**
    167      * URL parameter key for activity pagination. Default: 'acpage'.
    168      *
    169      * @since 2.1.0
    170      * @var string
    171      */
    172     public $pag_arg;
    173 
    174     /**
    175      * The page number being requested.
    176      *
    177      * @since 1.0.0
    178      * @var int
    179      */
    180     public $pag_page;
    181 
    182     /**
    183      * The number of items being requested per page.
    184      *
    185      * @since 1.0.0
    186      * @var int
    187      */
    188     public $pag_num;
    189 
    190     /**
    191      * An HTML string containing pagination links.
    192      *
    193      * @since 1.0.0
    194      * @var string
    195      */
    196     public $pag_links;
    197 
    198     /**
    199      * The displayed user's full name.
    200      *
    201      * @since 1.0.0
    202      * @var string
    203      */
    204     public $full_name;
    205 
    206     /**
    207      * Constructor method.
    208      *
    209      * The arguments passed to this class constructor are of the same
    210      * format as {@link BP_Activity_Activity::get()}.
    211      *
    212      * @since 1.5.0
    213      *
    214      * @see BP_Activity_Activity::get() for a description of the argument
    215      *      structure, as well as default values.
    216      *
    217      * @param array $args {
    218      *     Array of arguments. Supports all arguments from
    219      *     BP_Activity_Activity::get(), as well as 'page_arg' and
    220      *     'include'. Default values for 'per_page' and 'display_comments'
    221      *     differ from the originating function, and are described below.
    222      *     @type string      $page_arg         The string used as a query parameter in
    223      *                                         pagination links. Default: 'acpage'.
    224      *     @type array|bool  $include          Pass an array of activity IDs to
    225      *                                         retrieve only those items, or false to noop the 'include'
    226      *                                         parameter. 'include' differs from 'in' in that 'in' forms
    227      *                                         an IN clause that works in conjunction with other filters
    228      *                                         passed to the function, while 'include' is interpreted as
    229      *                                         an exact list of items to retrieve, which skips all other
    230      *                                         filter-related parameters. Default: false.
    231      *     @type int|bool    $per_page         Default: 20.
    232      *     @type string|bool $display_comments Default: 'threaded'.
    233      * }
    234      */
    235     public function __construct( $args ) {
    236         $bp = buddypress();
    237 
    238         // Backward compatibility with old method of passing arguments.
    239         if ( !is_array( $args ) || func_num_args() > 1 ) {
    240             _deprecated_argument( __METHOD__, '1.6', 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__ ) );
    241 
    242             $old_args_keys = array(
    243                 0 => 'page',
    244                 1 => 'per_page',
    245                 2 => 'max',
    246                 3 => 'include',
    247                 4 => 'sort',
    248                 5 => 'filter',
    249                 6 => 'search_terms',
    250                 7 => 'display_comments',
    251                 8 => 'show_hidden',
    252                 9 => 'exclude',
    253                 10 => 'in',
    254                 11 => 'spam',
    255                 12 => 'page_arg'
    256             );
    257 
    258             $func_args = func_get_args();
    259             $args = bp_core_parse_args_array( $old_args_keys, $func_args );
    260         }
    261 
    262         $defaults = array(
    263             'page'              => 1,
    264             'per_page'          => 20,
    265             'page_arg'          => 'acpage',
    266             'max'               => false,
    267             'fields'            => 'all',
    268             'count_total'       => false,
    269             'sort'              => false,
    270             'include'           => false,
    271             'exclude'           => false,
    272             'in'                => false,
    273             'filter'            => false,
    274             'scope'             => false,
    275             'search_terms'      => false,
    276             'meta_query'        => false,
    277             'date_query'        => false,
    278             'filter_query'      => false,
    279             'display_comments'  => 'threaded',
    280             'show_hidden'       => false,
    281             'spam'              => 'ham_only',
    282             'update_meta_cache' => true,
    283         );
    284         $r = wp_parse_args( $args, $defaults );
    285         extract( $r );
    286 
    287         $this->pag_arg  = sanitize_key( $r['page_arg'] );
    288         $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $r['page']     );
    289         $this->pag_num  = bp_sanitize_pagination_arg( 'num',          $r['per_page'] );
    290 
    291         // Check if blog/forum replies are disabled.
    292         $this->disable_blogforum_replies = (bool) bp_core_get_root_option( 'bp-disable-blogforum-comments' );
    293 
    294         // Get an array of the logged in user's favorite activities.
    295         $this->my_favs = maybe_unserialize( bp_get_user_meta( bp_loggedin_user_id(), 'bp_favorite_activities', true ) );
    296 
    297         // Fetch specific activity items based on ID's.
    298         if ( !empty( $include ) ) {
    299             $this->activities = bp_activity_get_specific( array(
    300                 'activity_ids'      => explode( ',', $include ),
    301                 'max'               => $max,
    302                 'count_total'       => $count_total,
    303                 'page'              => $this->pag_page,
    304                 'per_page'          => $this->pag_num,
    305                 'sort'              => $sort,
    306                 'display_comments'  => $display_comments,
    307                 'show_hidden'       => $show_hidden,
    308                 'spam'              => $spam,
    309                 'update_meta_cache' => $update_meta_cache,
    310             ) );
    311 
    312         // Fetch all activity items.
    313         } else {
    314             $this->activities = bp_activity_get( array(
    315                 'display_comments'  => $display_comments,
    316                 'max'               => $max,
    317                 'count_total'       => $count_total,
    318                 'per_page'          => $this->pag_num,
    319                 'page'              => $this->pag_page,
    320                 'sort'              => $sort,
    321                 'search_terms'      => $search_terms,
    322                 'meta_query'        => $meta_query,
    323                 'date_query'        => $date_query,
    324                 'filter_query'      => $filter_query,
    325                 'filter'            => $filter,
    326                 'scope'             => $scope,
    327                 'show_hidden'       => $show_hidden,
    328                 'exclude'           => $exclude,
    329                 'in'                => $in,
    330                 'spam'              => $spam,
    331                 'update_meta_cache' => $update_meta_cache,
    332             ) );
    333         }
    334 
    335         // The total_activity_count property will be set only if a
    336         // 'count_total' query has taken place.
    337         if ( ! is_null( $this->activities['total'] ) ) {
    338             if ( ! $max || $max >= (int) $this->activities['total'] ) {
    339                 $this->total_activity_count = (int) $this->activities['total'];
    340             } else {
    341                 $this->total_activity_count = (int) $max;
    342             }
    343         }
    344 
    345         $this->has_more_items = $this->activities['has_more_items'];
    346 
    347         $this->activities = $this->activities['activities'];
    348 
    349         if ( $max ) {
    350             if ( $max >= count($this->activities) ) {
    351                 $this->activity_count = count( $this->activities );
    352             } else {
    353                 $this->activity_count = (int) $max;
    354             }
    355         } else {
    356             $this->activity_count = count( $this->activities );
    357         }
    358 
    359         $this->full_name = bp_get_displayed_user_fullname();
    360 
    361         // Fetch parent content for activity comments so we do not have to query in the loop.
    362         foreach ( (array) $this->activities as $activity ) {
    363             if ( 'activity_comment' != $activity->type ) {
    364                 continue;
    365             }
    366 
    367             $parent_ids[] = $activity->item_id;
    368         }
    369 
    370         if ( !empty( $parent_ids ) ) {
    371             $activity_parents = bp_activity_get_specific( array( 'activity_ids' => $parent_ids ) );
    372         }
    373 
    374         if ( !empty( $activity_parents['activities'] ) ) {
    375             foreach( $activity_parents['activities'] as $parent ) {
    376                 $this->activity_parents[$parent->id] = $parent;
    377             }
    378 
    379             unset( $activity_parents );
    380         }
    381 
    382         if ( (int) $this->total_activity_count && (int) $this->pag_num ) {
    383             $this->pag_links = paginate_links( array(
    384                 'base'      => add_query_arg( $this->pag_arg, '%#%' ),
    385                 'format'    => '',
    386                 'total'     => ceil( (int) $this->total_activity_count / (int) $this->pag_num ),
    387                 'current'   => (int) $this->pag_page,
    388                 'prev_text' => _x( '←', 'Activity pagination previous text', 'buddypress' ),
    389                 'next_text' => _x( '→', 'Activity pagination next text', 'buddypress' ),
    390                 'mid_size'  => 1,
    391                 'add_args'  => array(),
    392             ) );
    393         }
    394     }
    395 
    396     /**
    397      * Whether there are activity items available in the loop.
    398      *
    399      * @since 1.0.0
    400      *
    401      * @see bp_has_activities()
    402      *
    403      * @return bool True if there are items in the loop, otherwise false.
    404      */
    405     function has_activities() {
    406         if ( $this->activity_count ) {
    407             return true;
    408         }
    409 
    410         return false;
    411     }
    412 
    413     /**
    414      * Set up the next activity item and iterate index.
    415      *
    416      * @since 1.0.0
    417      *
    418      * @return object The next activity item to iterate over.
    419      */
    420     public function next_activity() {
    421         $this->current_activity++;
    422         $this->activity = $this->activities[ $this->current_activity ];
    423 
    424         return $this->activity;
    425     }
    426 
    427     /**
    428      * Rewind the posts and reset post index.
    429      *
    430      * @since 1.0.0
    431      */
    432     public function rewind_activities() {
    433         $this->current_activity = -1;
    434         if ( $this->activity_count > 0 ) {
    435             $this->activity = $this->activities[0];
    436         }
    437     }
    438 
    439     /**
    440      * Whether there are activity items left in the loop to iterate over.
    441      *
    442      * This method is used by {@link bp_activities()} as part of the while loop
    443      * that controls iteration inside the activities loop, eg:
    444      *     while ( bp_activities() ) { ...
    445      *
    446      * @since 1.0.0
    447      *
    448      * @see bp_activities()
    449      *
    450      * @return bool True if there are more activity items to show,
    451      *              otherwise false.
    452      */
    453     public function user_activities() {
    454         if ( ( $this->current_activity + 1 ) < $this->activity_count ) {
    455             return true;
    456         } elseif ( ( $this->current_activity + 1 ) == $this->activity_count ) {
    457 
    458             /**
    459              * Fires right before the rewinding of activity posts.
    460              *
    461              * @since 1.1.0
    462              */
    463             do_action( 'activity_loop_end' );
    464 
    465             // Do some cleaning up after the loop.
    466             $this->rewind_activities();
    467         }
    468 
    469         $this->in_the_loop = false;
    470 
    471         return false;
    472     }
    473 
    474     /**
    475      * Set up the current activity item inside the loop.
    476      *
    477      * Used by {@link bp_the_activity()} to set up the current activity item
    478      * data while looping, so that template tags used during that iteration
    479      * make reference to the current activity item.
    480      *
    481      * @since 1.0.0
    482      *
    483      * @see bp_the_activity()
    484      */
    485     public function the_activity() {
    486 
    487         $this->in_the_loop = true;
    488         $this->activity    = $this->next_activity();
    489 
    490         if ( is_array( $this->activity ) ) {
    491             $this->activity = (object) $this->activity;
    492         }
    493 
    494         // Loop has just started.
    495         if ( $this->current_activity == 0 ) {
    496 
    497             /**
    498              * Fires if the current activity item is the first in the activity loop.
    499              *
    500              * @since 1.1.0
    501              */
    502             do_action('activity_loop_start');
    503         }
    504     }
    505 }
    506110
    507111/**
Note: See TracChangeset for help on using the changeset viewer.