Skip to:
Content

BuddyPress.org

Changeset 13150


Ignore:
Timestamp:
11/14/2021 03:51:15 PM (3 years ago)
Author:
imath
Message:

Group the BP_Activity_Activity::get_id() method params into an array

Deprecate old arguments and add backward compatibility for users still using these.

Props oztaser

Fixes #8596

Location:
trunk
Files:
3 edited

Legend:

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

    r13149 r13150  
    28692869     * @param array                $args  Arguments passed to the function.
    28702870     */
    2871     return apply_filters( 'bp_activity_get_activity_id', BP_Activity_Activity::get_id(
    2872         $r['user_id'],
    2873         $r['component'],
    2874         $r['type'],
    2875         $r['item_id'],
    2876         $r['secondary_item_id'],
    2877         $r['action'],
    2878         $r['content'],
    2879         $r['date_recorded']
    2880     ), $r, $args );
     2871    return apply_filters( 'bp_activity_get_activity_id', BP_Activity_Activity::get_id( $r ), $r, $args );
    28812872}
    28822873
  • trunk/src/bp-activity/classes/class-bp-activity-activity.php

    r13147 r13150  
    11911191     *
    11921192     * @since 1.2.0
    1193      *
    1194      * @todo Should parameters be optional?
    1195      *
    1196      * @param int    $user_id           User ID to filter by.
    1197      * @param string $component         Component to filter by.
    1198      * @param string $type              Activity type to filter by.
    1199      * @param int    $item_id           Associated item to filter by.
    1200      * @param int    $secondary_item_id Secondary associated item to filter by.
    1201      * @param string $action            Action to filter by.
    1202      * @param string $content           Content to filter by.
    1203      * @param string $date_recorded     Date to filter by.
     1193     * @since 10.0.0 Parameters were made optional.
     1194     *
     1195     * @param array $args {
     1196     *     An array of arguments. All items are optional.
     1197     *     @type int    $user_id           User ID to filter by.
     1198     *     @type string $component         Component to filter by.
     1199     *     @type string $type              Activity type to filter by.
     1200     *     @type int    $item_id           Associated item to filter by.
     1201     *     @type int    $secondary_item_id Secondary associated item to filter by.
     1202     *     @type string $action            Action to filter by.
     1203     *     @type string $content           Content to filter by.
     1204     *     @type string $date_recorded     Date to filter by.
     1205     * }
    12041206     * @return int|false Activity ID on success, false if none is found.
    12051207     */
    1206     public static function get_id( $user_id, $component, $type, $item_id, $secondary_item_id, $action, $content, $date_recorded ) {
     1208    public static function get_id( $args = array() ) {
    12071209        global $wpdb;
    12081210
    1209         $bp = buddypress();
     1211        $function_args = func_get_args();
     1212
     1213        // Backward compatibility with old method of passing arguments.
     1214        if ( ! is_array( $args ) || count( $function_args ) > 1 ) {
     1215            _deprecated_argument(
     1216                __METHOD__,
     1217                '10.0.0',
     1218                sprintf(
     1219                    /* translators: 1: the name of the method. 2: the name of the file. */
     1220                    esc_html__( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ),
     1221                    __METHOD__,
     1222                    __FILE__
     1223                )
     1224            );
     1225
     1226            $old_args_keys = array(
     1227                0 => 'user_id',
     1228                1 => 'component',
     1229                2 => 'type',
     1230                3 => 'item_id',
     1231                4 => 'secondary_item_id',
     1232                5 => 'action',
     1233                6 => 'content',
     1234                7 => 'date_recorded',
     1235            );
     1236
     1237            $args = bp_core_parse_args_array( $old_args_keys, $function_args );
     1238        }
     1239
     1240        $r = bp_parse_args(
     1241            $args,
     1242            array(
     1243                'user_id'           => false,
     1244                'component'         => false,
     1245                'type'              => false,
     1246                'item_id'           => false,
     1247                'secondary_item_id' => false,
     1248                'action'            => false,
     1249                'content'           => false,
     1250                'date_recorded'     => false,
     1251            )
     1252        );
    12101253
    12111254        $where_args = false;
    12121255
    1213         if ( ! empty( $user_id ) ) {
    1214             $where_args[] = $wpdb->prepare( "user_id = %d", $user_id );
    1215         }
    1216 
    1217         if ( ! empty( $component ) ) {
    1218             $where_args[] = $wpdb->prepare( "component = %s", $component );
    1219         }
    1220 
    1221         if ( ! empty( $type ) ) {
    1222             $where_args[] = $wpdb->prepare( "type = %s", $type );
    1223         }
    1224 
    1225         if ( ! empty( $item_id ) ) {
    1226             $where_args[] = $wpdb->prepare( "item_id = %d", $item_id );
    1227         }
    1228 
    1229         if ( ! empty( $secondary_item_id ) ) {
    1230             $where_args[] = $wpdb->prepare( "secondary_item_id = %d", $secondary_item_id );
    1231         }
    1232 
    1233         if ( ! empty( $action ) ) {
    1234             $where_args[] = $wpdb->prepare( "action = %s", $action );
    1235         }
    1236 
    1237         if ( ! empty( $content ) ) {
    1238             $where_args[] = $wpdb->prepare( "content = %s", $content );
    1239         }
    1240 
    1241         if ( ! empty( $date_recorded ) ) {
    1242             $where_args[] = $wpdb->prepare( "date_recorded = %s", $date_recorded );
     1256        if ( ! empty( $r['user_id'] ) ) {
     1257            $where_args[] = $wpdb->prepare( 'user_id = %d', $r['user_id'] );
     1258        }
     1259
     1260        if ( ! empty( $r['component'] ) ) {
     1261            $where_args[] = $wpdb->prepare( 'component = %s', $r['component'] );
     1262        }
     1263
     1264        if ( ! empty( $r['type'] ) ) {
     1265            $where_args[] = $wpdb->prepare( 'type = %s', $r['type'] );
     1266        }
     1267
     1268        if ( ! empty( $r['item_id'] ) ) {
     1269            $where_args[] = $wpdb->prepare( 'item_id = %d', $r['item_id'] );
     1270        }
     1271
     1272        if ( ! empty( $r['secondary_item_id'] ) ) {
     1273            $where_args[] = $wpdb->prepare( 'secondary_item_id = %d', $r['secondary_item_id'] );
     1274        }
     1275
     1276        if ( ! empty( $r['action'] ) ) {
     1277            $where_args[] = $wpdb->prepare( 'action = %s', $r['action'] );
     1278        }
     1279
     1280        if ( ! empty( $r['content'] ) ) {
     1281            $where_args[] = $wpdb->prepare( 'content = %s', $r['content'] );
     1282        }
     1283
     1284        if ( ! empty( $r['date_recorded'] ) ) {
     1285            $where_args[] = $wpdb->prepare( 'date_recorded = %s', $r['date_recorded'] );
    12431286        }
    12441287
    12451288        if ( ! empty( $where_args ) ) {
     1289            $bp        = buddypress();
    12461290            $where_sql = 'WHERE ' . join( ' AND ', $where_args );
    1247             $result = $wpdb->get_var( "SELECT id FROM {$bp->activity->table_name} {$where_sql}" );
     1291            $result    = $wpdb->get_var( "SELECT id FROM {$bp->activity->table_name} {$where_sql}" );
    12481292
    12491293            return is_numeric( $result ) ? (int) $result : false;
  • trunk/tests/phpunit/testcases/activity/class.BP_Activity_Activity.php

    r12604 r13150  
    465465        ) );
    466466
    467         $activity = BP_Activity_Activity::get_id( false, false, false, 523, false, false, false, false );
     467        $args = array(
     468            'item_id' => 523,
     469        );
     470
     471        $activity = BP_Activity_Activity::get_id( $args );
    468472        $this->assertEquals( $a1, $activity );
    469473    }
     
    471475    /**
    472476     * @group get_id
     477     * @group BP8596
     478     * @expectedDeprecated BP_Activity_Activity::get_id
     479     */
     480    public function test_get_id_with_deprecated_args() {
     481        $a1 = self::factory()->activity->create( array(
     482            'item_id' => 573,
     483        ) );
     484        $a2 = self::factory()->activity->create( array(
     485            'item_id' => 1098,
     486        ) );
     487
     488        $activity = BP_Activity_Activity::get_id( false, false, false, 1098, false, false, false, false );
     489        $this->assertEquals( $a2, $activity );
     490    }
     491
     492    /**
     493     * @group get_id
    473494     */
    474495    public function test_get_id_with_secondary_item_id() {
     
    480501        ) );
    481502
    482         $activity = BP_Activity_Activity::get_id( false, false, false, false, 523, false, false, false );
     503        $args = array(
     504            'secondary_item_id' => 523,
     505        );
     506
     507        $activity = BP_Activity_Activity::get_id( $args );
    483508        $this->assertEquals( $a1, $activity );
    484509    }
Note: See TracChangeset for help on using the changeset viewer.