Skip to:
Content

BuddyPress.org

Changeset 9536


Ignore:
Timestamp:
02/23/2015 05:36:57 PM (11 years ago)
Author:
johnjamesjacoby
Message:

Activity: Clean up bp_has_activities():

  • Remove $bp global usage. See #5138.
  • Remove extract() usage. See #5698.
  • Remove one-time-use variables.
  • Consolidate arrays for improved readability.
File:
1 edited

Legend:

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

    r9530 r9536  
    602602 */
    603603function bp_has_activities( $args = '' ) {
    604         global $activities_template, $bp;
    605 
    606         /***
    607          * Set the defaults based on the current page. Any of these will be overridden
    608          * if arguments are directly passed into the loop. Custom plugins should always
    609          * pass their parameters directly to the loop.
    610          */
    611         $user_id     = false;
    612         $include     = false;
    613         $exclude     = false;
    614         $in          = false;
    615         $show_hidden = false;
    616         $object      = false;
    617         $primary_id  = false;
     604        global $activities_template;
     605
     606        // Get BuddyPress
     607        $bp = buddypress();
     608
     609        /** Smart Defaults ********************************************************/
    618610
    619611        // User filtering
    620         if ( bp_displayed_user_id() )
    621                 $user_id = bp_displayed_user_id();
     612        $user_id = bp_displayed_user_id()
     613                ? bp_displayed_user_id()
     614                : false;
    622615
    623616        // Group filtering
    624         if ( !empty( $bp->groups->current_group ) ) {
    625                 $object = $bp->groups->id;
    626                 $primary_id = $bp->groups->current_group->id;
    627 
    628                 if ( groups_is_user_member( bp_loggedin_user_id(), $bp->groups->current_group->id ) || bp_current_user_can( 'bp_moderate' ) ) {
    629                         $show_hidden = true;
    630                 }
     617        if ( groups_get_current_group() ) {
     618                $object      = $bp->groups->id;
     619                $primary_id  = bp_current_group_id();
     620                $show_hidden = (bool) ( groups_is_user_member( bp_loggedin_user_id(), $primary_id ) || bp_current_user_can( 'bp_moderate' ) );
     621        } else {
     622                $object      = false;
     623                $primary_id  = false;
     624                $show_hidden = false;
    631625        }
    632626
    633627        // The default scope should recognize custom slugs
    634         if ( array_key_exists( bp_current_action(), (array) $bp->loaded_components ) ) {
    635                 $scope = $bp->loaded_components[bp_current_action()];
    636         }
    637         else
    638                 $scope = bp_current_action();
     628        $scope = array_key_exists( bp_current_action(), (array) $bp->loaded_components )
     629                ? $bp->loaded_components[ bp_current_action() ]
     630                : bp_current_action();
    639631
    640632        // Support for permalinks on single item pages: /groups/my-group/activity/124/
    641         if ( bp_is_current_action( bp_get_activity_slug() ) )
    642                 $include = bp_action_variable( 0 );
    643 
    644         // Note: any params used for filtering can be a single value, or multiple values comma separated.
    645         $defaults = array(
     633        $include = bp_is_current_action( bp_get_activity_slug() )
     634                ? bp_action_variable( 0 )
     635                : false;
     636
     637        /** Parse Args ************************************************************/
     638
     639        // Note: any params used for filtering can be a single value, or multiple
     640        // values comma separated.
     641        $r = bp_parse_args( $args, array(
    646642                'display_comments'  => 'threaded',   // false for none, stream/threaded - show comments in the stream or threaded under items
    647643                'include'           => $include,     // pass an activity_id or string of IDs comma-separated
    648                 'exclude'           => $exclude,     // pass an activity_id or string of IDs comma-separated
    649                 'in'                => $in,          // comma-separated list or array of activity IDs among which to search
     644                'exclude'           => false,        // pass an activity_id or string of IDs comma-separated
     645                'in'                => false,        // comma-separated list or array of activity IDs among which to search
    650646                'sort'              => 'DESC',       // sort DESC or ASC
    651647                'page'              => 1,            // which page to load
    652648                'per_page'          => 20,           // number of items per page
     649                'page_arg'          => 'acpage',     // See https://buddypress.trac.wordpress.org/ticket/3679
    653650                'max'               => false,        // max number to return
    654651                'count_total'       => false,
    655652                'show_hidden'       => $show_hidden, // Show activity items that are hidden site-wide?
    656653                'spam'              => 'ham_only',   // Hide spammed items
    657 
    658                 'page_arg'          => 'acpage',     // See https://buddypress.trac.wordpress.org/ticket/3679
    659654
    660655                // Scope - pre-built activity filters for a user (friends/groups/favorites/mentions)
     
    677672                'search_terms'      => false,        // specify terms to search on
    678673                'update_meta_cache' => true,
    679         );
    680 
    681         $r = bp_parse_args( $args, $defaults, 'has_activities' );
    682         extract( $r );
     674        ), 'has_activities' );
     675
     676        /** Smart Overrides *******************************************************/
    683677
    684678        // Translate various values for 'display_comments'
    685679        // This allows disabling comments via ?display_comments=0
    686680        // or =none or =false. Final true is a strict type check. See #5029
    687         if ( in_array( $display_comments, array( 0, '0', 'none', 'false' ), true ) ) {
    688                 $display_comments = false;
     681        if ( in_array( $r['display_comments'], array( 0, '0', 'none', 'false' ), true ) ) {
     682                $r['display_comments'] = false;
    689683        }
    690684
    691685        // Ignore pagination if an offset is passed
    692         if ( ! empty( $offset ) ) {
    693                 $page = 0;
     686        if ( ! empty( $r['offset'] ) ) {
     687                $r['page'] = 0;
    694688        }
    695689
    696690        // Search terms
    697         if ( empty( $search_terms ) && ! empty( $_REQUEST['s'] ) )
    698                 $search_terms = $_REQUEST['s'];
     691        if ( ! empty( $_REQUEST['s'] ) && empty( $r['search_terms'] ) ) {
     692                $r['search_terms'] = $_REQUEST['s'];
     693        }
    699694
    700695        // Do not exceed the maximum per page
    701         if ( !empty( $max ) && ( (int) $per_page > (int) $max ) )
    702                 $per_page = $max;
     696        if ( ! empty( $r['max'] ) && ( (int) $r['per_page'] > (int) $r['max'] ) ) {
     697                $r['per_page'] = $r['max'];
     698        }
    703699
    704700        /**
     
    713709         * @param bool $value True if BuddyPress should enable afilter support.
    714710         */
    715         if ( isset( $_GET['afilter'] ) && apply_filters( 'bp_activity_enable_afilter_support', false ) )
    716                 $filter = array( 'object' => $_GET['afilter'] );
    717         elseif ( ! empty( $user_id ) || ! empty( $object ) || ! empty( $action ) || ! empty( $primary_id ) || ! empty( $secondary_id ) || ! empty( $offset ) || ! empty( $since ) )
    718                 $filter = array( 'user_id' => $user_id, 'object' => $object, 'action' => $action, 'primary_id' => $primary_id, 'secondary_id' => $secondary_id, 'offset' => $offset, 'since' => $since );
    719         else
    720                 $filter = false;
    721 
    722         // If specific activity items have been requested, override the $hide_spam argument. This prevents backpat errors with AJAX.
    723         if ( !empty( $include ) && ( 'ham_only' == $spam ) )
    724                 $spam = 'all';
    725 
    726         $template_args = array(
    727                 'page'              => $page,
    728                 'per_page'          => $per_page,
    729                 'page_arg'          => $page_arg,
    730                 'max'               => $max,
    731                 'count_total'       => $count_total,
    732                 'sort'              => $sort,
    733                 'include'           => $include,
    734                 'exclude'           => $exclude,
    735                 'in'                => $in,
    736                 'filter'            => $filter,
    737                 'scope'             => $scope,
    738                 'search_terms'      => $search_terms,
    739                 'meta_query'        => $meta_query,
    740                 'date_query'        => $date_query,
    741                 'filter_query'      => $filter_query,
    742                 'display_comments'  => $display_comments,
    743                 'show_hidden'       => $show_hidden,
    744                 'spam'              => $spam,
    745                 'update_meta_cache' => $update_meta_cache,
    746         );
    747 
    748         $activities_template = new BP_Activity_Template( $template_args );
     711        if ( isset( $_GET['afilter'] ) && apply_filters( 'bp_activity_enable_afilter_support', false ) ) {
     712                $r['filter'] = array(
     713                        'object' => $_GET['afilter']
     714                );
     715        } elseif ( ! empty( $r['user_id'] ) || ! empty( $r['object'] ) || ! empty( $r['action'] ) || ! empty( $r['primary_id'] ) || ! empty( $r['secondary_id'] ) || ! empty( $r['offset'] ) || ! empty( $r['since'] ) ) {
     716                $r['filter'] = array(
     717                        'user_id'      => $r['user_id'],
     718                        'object'       => $r['object'],
     719                        'action'       => $r['action'],
     720                        'primary_id'   => $r['primary_id'],
     721                        'secondary_id' => $r['secondary_id'],
     722                        'offset'       => $r['offset'],
     723                        'since'        => $r['since']
     724                );
     725        } else {
     726                $r['filter'] = false;
     727        }
     728
     729        // If specific activity items have been requested, override the $hide_spam
     730        // argument. This prevents backpat errors with AJAX.
     731        if ( ! empty( $r['include'] ) && ( 'ham_only' === $r['spam'] ) ) {
     732                $r['spam'] = 'all';
     733        }
     734
     735        /** Query *****************************************************************/
     736
     737        $activities_template = new BP_Activity_Template( $r );
    749738
    750739        /**
     
    753742         * @since BuddyPress (1.1.0)
    754743         *
    755          * @param bool   $has_activities Whether or not there are activity items to display.
     744         * @param bool   $has_activities      Whether or not there are activity items to display.
    756745         * @param string $activities_template Current activities template being used.
    757          * @param array  $template_args Array of arguments passed into the BP_Activity_Template class.
    758          */
    759         return apply_filters( 'bp_has_activities', $activities_template->has_activities(), $activities_template, $template_args );
     746         * @param array  $template_args       Array of arguments passed into the BP_Activity_Template class.
     747         */
     748        return apply_filters( 'bp_has_activities', $activities_template->has_activities(), $activities_template, $r );
    760749}
    761750
Note: See TracChangeset for help on using the changeset viewer.