Skip to:
Content

BuddyPress.org

Changeset 13337


Ignore:
Timestamp:
10/06/2022 10:31:49 PM (19 months ago)
Author:
imath
Message:

Activity introduce 2 new parameters to BP_Activity_Activity::get()`

  • The user_id__in parameter makes it possible to retrieve activities for

a list of user IDs.

  • The user_id__not_in parameter makes it possible to exclude activity

posted by a list of user IDs from retrieved activities.

Props username_, slaFFik, boonebgorges

Fixes #4184

Location:
trunk
Files:
3 edited

Legend:

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

    r13309 r13337  
    18481848            'date_query'        => false,        // Filter by date. See first parameter of WP_Date_Query for format.
    18491849            'filter_query'      => false,
     1850            'user_id__in'       => array(),      // Array of user ids to include.
     1851            'user_id__not_in'   => array(),      // Array of user ids to excluce.
    18501852            'show_hidden'       => false,        // Show activity items that are hidden site-wide?
    18511853            'exclude'           => false,        // Comma-separated list of activity IDs to exclude.
     
    18831885            'filter_query'      => $r['filter_query'],
    18841886            'filter'            => $r['filter'],
     1887            'user_id__in'       => $r['user_id__in'],
     1888            'user_id__not_in'   => $r['user_id__not_in'],
    18851889            'scope'             => $r['scope'],
    18861890            'display_comments'  => $r['display_comments'],
  • trunk/src/bp-activity/classes/class-bp-activity-activity.php

    r13312 r13337  
    343343     * @since 2.9.0 Introduced the `$order_by` parameter.
    344344     * @since 10.0.0 Introduced the `$count_total_only` parameter.
     345     * @since 11.0.0 Introduced the `$user_id__in` and `$user_id__not_in` parameters.
    345346     *
    346347     * @see BP_Activity_Activity::get_filter_sql() for a description of the
     
    367368     *     @type string|array $scope             Pre-determined set of activity arguments.
    368369     *     @type array        $filter            See BP_Activity_Activity::get_filter_sql().
     370     *     @type array        $user_id__in       An array of user ids to include. Activity posted by users matching one of these
     371     *                                           user ids will be included in results. Default empty array.
     372     *     @type array        $user_id__not_in   An array of user ids to exclude. Activity posted by users matching one of these
     373     *                                           user ids will not be included in results. Default empty array.
    369374     *     @type string       $search_terms      Limit results by a search term. Default: false.
    370375     *     @type bool         $display_comments  Whether to include activity comments. Default: false.
     
    431436                'date_query'        => false,           // Filter by date.
    432437                'filter_query'      => false,           // Advanced filtering - see BP_Activity_Query.
     438                'user_id__in'       => array(),         // Array of user ids to include.
     439                'user_id__not_in'   => array(),         // Array of user ids to excluce.
    433440                'filter'            => false,           // See self::get_filter_sql().
    434441                'scope'             => false,           // Preset activity arguments.
     
    482489        if ( $r['filter'] && $filter_sql = BP_Activity_Activity::get_filter_sql( $r['filter'] ) ) {
    483490            $where_conditions['filter_sql'] = $filter_sql;
     491        }
     492
     493        // User IDs filtering.
     494        $user_ids_clause  = array();
     495        $user_ids_filters = array_filter(
     496            array_intersect_key(
     497                $r,
     498                array(
     499                    'user_id__in'     => true,
     500                    'user_id__not_in' => true,
     501                )
     502            )
     503        );
     504
     505        foreach ( $user_ids_filters as $user_ids_filter_key => $user_ids_filter ) {
     506            $user_ids_operator = 'IN';
     507            if ( 'user_id__not_in' === $user_ids_filter_key ) {
     508                $user_ids_operator = 'NOT IN';
     509            }
     510
     511            if ( $user_ids_clause ) {
     512                $user_ids_clause[] = array(
     513                    'column'  => 'user_id',
     514                    'compare' => $user_ids_operator,
     515                    'value'   => (array) $user_ids_filter,
     516                );
     517            } else {
     518                $user_ids_clause = array(
     519                    'relation' => 'AND',
     520                    array(
     521                        'column'  => 'user_id',
     522                        'compare' => $user_ids_operator,
     523                        'value'   => (array) $user_ids_filter,
     524                    ),
     525                );
     526            }
     527        }
     528
     529        if ( $user_ids_clause ) {
     530            $user_ids_query = new BP_Activity_Query( $user_ids_clause );
     531            $user_ids_sql   = $user_ids_query->get_sql();
     532            if ( ! empty( $user_ids_sql ) ) {
     533                $where_conditions['user_ids_query_sql'] = $user_ids_sql;
     534            }
    484535        }
    485536
  • trunk/tests/phpunit/testcases/activity/class.BP_Activity_Activity.php

    r13150 r13337  
    413413    /**
    414414     * @group get
     415     */
     416    public function test_get_with_user_id__in() {
     417        $u1 = self::factory()->user->create();
     418        $u2 = self::factory()->user->create();
     419        $u3 = self::factory()->user->create();
     420        $a1 = self::factory()->activity->create( array( 'user_id' => $u1 ) );
     421        $a2 = self::factory()->activity->create( array( 'user_id' => $u2 ) );
     422        $a3 = self::factory()->activity->create( array( 'user_id' => $u3 ) );
     423
     424        $activities = BP_Activity_Activity::get( array(
     425            'user_id__in' => array( $u1, $u2 ),
     426        ) );
     427
     428        $ids = wp_list_pluck( $activities['activities'], 'id' );
     429        sort( $ids );
     430        $this->assertEquals( array( $a1, $a2 ), $ids );
     431    }
     432
     433    /**
     434     * @group get
     435     */
     436    public function test_get_with_user_id__not_in() {
     437        $u1 = self::factory()->user->create();
     438        $u2 = self::factory()->user->create();
     439        $u3 = self::factory()->user->create();
     440        $a1 = self::factory()->activity->create( array( 'user_id' => $u1 ) );
     441        $a2 = self::factory()->activity->create( array( 'user_id' => $u2 ) );
     442        $a3 = self::factory()->activity->create( array( 'user_id' => $u3 ) );
     443
     444        $activities = BP_Activity_Activity::get( array(
     445            'user_id__not_in' => array( $u2, $u3 ),
     446        ) );
     447
     448        $ids = wp_list_pluck( $activities['activities'], 'id' );
     449        $this->assertEquals( array( $a1 ), $ids );
     450    }
     451
     452    /**
     453     * @group get
     454     */
     455    public function test_get_with_user_id__in_andnot_in() {
     456        $u1 = self::factory()->user->create();
     457        $u2 = self::factory()->user->create();
     458        $u3 = self::factory()->user->create();
     459        $u4 = self::factory()->user->create();
     460        $u5 = self::factory()->user->create();
     461        $a1 = self::factory()->activity->create( array( 'user_id' => $u1 ) );
     462        $a2 = self::factory()->activity->create( array( 'user_id' => $u2 ) );
     463        $a3 = self::factory()->activity->create( array( 'user_id' => $u3 ) );
     464        $a4 = self::factory()->activity->create( array( 'user_id' => $u4 ) );
     465        $a5 = self::factory()->activity->create( array( 'user_id' => $u5 ) );
     466
     467        $activities = BP_Activity_Activity::get( array(
     468            'user_id__in'     => array( $u1, $u2, $u4, $u5 ),
     469            'user_id__not_in' => array( $u4, $u5 ),
     470        ) );
     471
     472        $ids = wp_list_pluck( $activities['activities'], 'id' );
     473        sort( $ids );
     474        $this->assertEquals( array( $a1, $a2 ), $ids );
     475    }
     476
     477    /**
     478     * @group get
    415479     * @group count_total
    416480     */
Note: See TracChangeset for help on using the changeset viewer.