Skip to:
Content

BuddyPress.org

Changeset 10217


Ignore:
Timestamp:
10/08/2015 04:59:40 PM (9 years ago)
Author:
boonebgorges
Message:

Introduce fields parameter to bp_has_activities() stack.

fields=ids will fetch only the IDs of matched activities. fields=all
(or any other value of fields, for the moment) will return full activity
objects.

The structure of the array returned from BP_Activity_Activity::get() is the
same regardless of the value of fields: matched items are stored in the
'activities' member of the returned array.

Props r-a-y.
Fixes #6426.

Location:
trunk
Files:
4 edited

Legend:

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

    r10184 r10217  
    14351435 *
    14361436 * @since 1.2.0
     1437 * @since 2.4.0 Introduced the `$fields` parameter.
    14371438 *
    14381439 * @see BP_Activity_Activity::get() For more information on accepted arguments
     
    14521453    $r = bp_parse_args( $args, array(
    14531454        'max'               => false,        // Maximum number of results to return
     1455        'fields'            => 'all',
    14541456        'page'              => 1,            // page 1 without a per_page will result in no pagination.
    14551457        'per_page'          => false,        // results per page
     
    14831485
    14841486    // Attempt to return a cached copy of the first page of sitewide activity.
    1485     if ( ( 1 === (int) $r['page'] ) && empty( $r['max'] ) && empty( $r['search_terms'] ) && empty( $r['meta_query'] ) && empty( $r['date_query'] ) && empty( $r['filter_query'] ) && empty( $r['filter'] ) && empty( $r['scope'] )&& empty( $r['exclude'] ) && empty( $r['in'] ) && ( 'DESC' === $r['sort'] ) && empty( $r['exclude'] ) && ( 'ham_only' === $r['spam'] ) ) {
     1487    if ( ( 1 === (int) $r['page'] ) && empty( $r['max'] ) && ( 'all' === $r['fields'] ) && empty( $r['search_terms'] ) && empty( $r['meta_query'] ) && empty( $r['date_query'] ) && empty( $r['filter_query'] ) && empty( $r['filter'] ) && empty( $r['scope'] )&& empty( $r['exclude'] ) && empty( $r['in'] ) && ( 'DESC' === $r['sort'] ) && empty( $r['exclude'] ) && ( 'ham_only' === $r['spam'] ) ) {
    14861488
    14871489        $activity = wp_cache_get( 'bp_activity_sitewide_front', 'bp' );
     
    14921494                'per_page'          => $r['per_page'],
    14931495                'max'               => $r['max'],
     1496                'fields'            => $r['fields'],
    14941497                'sort'              => $r['sort'],
    14951498                'search_terms'      => $r['search_terms'],
  • trunk/src/bp-activity/bp-activity-template.php

    r10160 r10217  
    271271            'page_arg'          => 'acpage',
    272272            'max'               => false,
     273            'fields'            => 'all',
    273274            'count_total'       => false,
    274275            'sort'              => false,
     
    508509 *
    509510 * @since 1.0.0
     511 * @since 2.4.0 Introduced the `$fields` parameter.
    510512 *
    511513 * @global object $activities_template {@link BP_Activity_Template}
     
    537539 *     @type string            $page_arg         String used as a query parameter in pagination links. Default: 'acpage'.
    538540 *     @type int|bool          $max              Maximum number of results to return. Default: false (unlimited).
     541 *     @type string            $fields           Activity fields to retrieve. 'all' to fetch entire activity objects,
     542 *                                               'ids' to get only the activity IDs. Default 'all'.
    539543 *     @type string|bool       $count_total      If true, an additional DB query is run to count the total activity items
    540544 *                                               for the query. Default: false.
     
    662666        'page_arg'          => 'acpage',     // See https://buddypress.trac.wordpress.org/ticket/3679
    663667        'max'               => false,        // max number to return
     668        'fields'            => 'all',
    664669        'count_total'       => false,
    665670        'show_hidden'       => $show_hidden, // Show activity items that are hidden site-wide?
  • trunk/src/bp-activity/classes/class-bp-activity-activity.php

    r10210 r10217  
    261261     * Get activity items, as specified by parameters.
    262262     *
     263     * @since 1.0.0
     264     * @since 2.4.0 Introduced the `$fields` parameter.
     265     *
    263266     * @see BP_Activity_Activity::get_filter_sql() for a description of the
    264267     *      'filter' parameter.
     
    273276     *     @type int|bool     $per_page          Number of results per page. Default: 25.
    274277     *     @type int|bool     $max               Maximum number of results to return. Default: false (unlimited).
     278     *     @type string       $fields            Activity fields to return. Pass 'ids' to get only the activity IDs.
     279     *                                           'all' returns full activity objects.
    275280     *     @type string       $sort              ASC or DESC. Default: 'DESC'.
    276281     *     @type array        $exclude           Array of activity IDs to exclude. Default: false.
     
    324329            'per_page'          => 25,         // Activity items per page
    325330            'max'               => false,      // Max number of items to return
     331            'fields'            => 'all',
    326332            'sort'              => 'DESC',     // ASC or DESC
    327333            'exclude'           => false,      // Array of ids to exclude
     
    571577            }
    572578
    573             $activities = self::get_activity_data( $activity_ids );
    574         }
    575 
    576         // Get the fullnames of users so we don't have to query in the loop
    577         $activities = self::append_user_fullnames( $activities );
    578 
    579         // Get activity meta
    580         $activity_ids = array();
    581         foreach ( (array) $activities as $activity ) {
    582             $activity_ids[] = $activity->id;
    583         }
    584 
    585         if ( ! empty( $activity_ids ) && $r['update_meta_cache'] ) {
    586             bp_activity_update_meta_cache( $activity_ids );
    587         }
    588 
    589         if ( $activities && $r['display_comments'] ) {
    590             $activities = BP_Activity_Activity::append_comments( $activities, $r['spam'] );
    591         }
    592 
    593         // Pre-fetch data associated with activity users and other objects
    594         BP_Activity_Activity::prefetch_object_data( $activities );
    595 
    596         // Generate action strings
    597         $activities = BP_Activity_Activity::generate_action_strings( $activities );
     579            if ( 'ids' === $r['fields'] ) {
     580                $activities = array_map( 'intval', $activity_ids );
     581            } else {
     582                $activities = self::get_activity_data( $activity_ids );
     583            }
     584        }
     585
     586        if ( 'ids' !== $r['fields'] ) {
     587            // Get the fullnames of users so we don't have to query in the loop
     588            $activities = self::append_user_fullnames( $activities );
     589
     590            // Get activity meta
     591            $activity_ids = array();
     592            foreach ( (array) $activities as $activity ) {
     593                $activity_ids[] = $activity->id;
     594            }
     595
     596            if ( ! empty( $activity_ids ) && $r['update_meta_cache'] ) {
     597                bp_activity_update_meta_cache( $activity_ids );
     598            }
     599
     600            if ( $activities && $r['display_comments'] ) {
     601                $activities = BP_Activity_Activity::append_comments( $activities, $r['spam'] );
     602            }
     603
     604            // Pre-fetch data associated with activity users and other objects
     605            BP_Activity_Activity::prefetch_object_data( $activities );
     606
     607            // Generate action strings
     608            $activities = BP_Activity_Activity::generate_action_strings( $activities );
     609        }
    598610
    599611        $retval['activities'] = $activities;
  • trunk/tests/phpunit/testcases/activity/class.BP_Activity_Activity.php

    r9819 r10217  
    5353        $meta = bp_activity_get_meta( $activity, 'Paul' );
    5454        $this->assertSame( '', $meta );
     55    }
     56
     57    /**
     58     * @group get
     59     * @group fields
     60     * @ticket BP6426
     61     */
     62    public function test_get_with_fields_parameter_by_id() {
     63        $a = $this->factory->activity->create_many( 3, array(
     64            'type' => 'activity_update',
     65        ) );
     66
     67        $result = BP_Activity_Activity::get( array(
     68            'fields' => 'ids',
     69        ) );
     70        $this->assertEqualSets( $a, $result['activities'] );
    5571    }
    5672
Note: See TracChangeset for help on using the changeset viewer.