Skip to:
Content

BuddyPress.org


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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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;
Note: See TracChangeset for help on using the changeset viewer.