Skip to:
Content

BuddyPress.org

Changeset 13636


Ignore:
Timestamp:
11/07/2023 10:41:34 PM (20 months ago)
Author:
imath
Message:

Improve the favorites management using the WP Meta API

  • Introduce bp_activity_register_user_favorites_meta() to register the user meta and define sanitization callback.
  • Introduce bp_activity_sanitize_user_favorites_meta() to do the sanitization job.
  • Improve Ajax callbacks about favoriting/unfavoriting activities in template packs.

Props emaralive, dcavins

Fixes #9021
Closes https://github.com/buddypress/buddypress/pull/189

Location:
trunk
Files:
5 edited

Legend:

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

    r13491 r13636  
    104104add_filter( 'bp_get_activity_content',      'bp_activity_truncate_entry', 5 );
    105105
    106 add_filter( 'bp_get_total_favorite_count_for_user', 'bp_core_number_format' );
    107 add_filter( 'bp_get_total_mention_count_for_user',  'bp_core_number_format' );
     106add_filter( 'bp_activity_get_user_favorites',       'bp_activity_sanitize_user_favorites_meta' );
     107add_filter( 'bp_get_total_favorite_count_for_user', 'bp_core_number_format'                    );
     108add_filter( 'bp_get_total_mention_count_for_user',  'bp_core_number_format'                    );
    108109
    109110add_filter( 'bp_activity_get_embed_excerpt', 'bp_activity_embed_excerpt_onclick_location_filter', 9 );
  • trunk/src/bp-activity/bp-activity-functions.php

    r13539 r13636  
    10151015
    10161016/**
     1017 * Sanitize callback for the User's favorites meta.
     1018 *
     1019 * @since 12.0.0
     1020 *
     1021 * @param array $value The list of favorited activity IDs.
     1022 * @return array The sanitized list of favorited activity IDs.
     1023 */
     1024function bp_activity_sanitize_user_favorites_meta( $value = array() ) {
     1025    return array_filter( wp_parse_id_list( $value ) );
     1026}
     1027
     1028/**
     1029 * Use WordPress Meta API to deal with favorites meta properties and sanitization.
     1030 *
     1031 * @since 12.0.0
     1032 */
     1033function bp_activity_register_user_favorites_meta() {
     1034    register_meta(
     1035        'user',
     1036        'bp_favorite_activities',
     1037        array(
     1038            'single'            => true,
     1039            'type'              => 'array',
     1040            'description'       => __( 'The list of Activity IDs a user favorited.', 'buddypress' ),
     1041            'show_in_rest'      => false, // We're not showing this meta into the WP users REST endpoint.
     1042            'sanitize_callback' => 'bp_activity_sanitize_user_favorites_meta',
     1043            'default'           => array(),
     1044        )
     1045    );
     1046}
     1047add_action( 'bp_init', 'bp_activity_register_user_favorites_meta' );
     1048
     1049/**
    10171050 * Get a users favorite activity stream items.
    10181051 *
     
    10521085 */
    10531086function bp_activity_add_user_favorite( $activity_id, $user_id = 0 ) {
     1087    // Cast as an integer to make sure we're only saving integers into the user's meta.
     1088    if ( ! empty( $activity_id ) ) {
     1089        $activity_id = (int) $activity_id;
     1090    } else {
     1091        $activity_id = 0;
     1092    }
     1093
     1094    if ( ! $activity_id ) {
     1095        return false;
     1096    }
    10541097
    10551098    // Fallback to logged in user if no user_id is passed.
     
    10581101    }
    10591102
    1060     $my_favs = bp_get_user_meta( $user_id, 'bp_favorite_activities', true );
    1061     if ( empty( $my_favs ) || ! is_array( $my_favs ) ) {
    1062         $my_favs = array();
    1063     }
     1103    // Get user's existing favorites.
     1104    $my_favs = bp_activity_get_user_favorites( $user_id );
    10641105
    10651106    // Bail if the user has already favorited this activity item.
    1066     if ( in_array( $activity_id, $my_favs ) ) {
     1107    if ( in_array( $activity_id, $my_favs, true ) ) {
    10671108        return false;
    10681109    }
     
    10721113
    10731114    // Update the total number of users who have favorited this activity.
    1074     $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' );
    1075     $fav_count = !empty( $fav_count ) ? (int) $fav_count + 1 : 1;
     1115    $fav_count = (int) bp_activity_get_meta( $activity_id, 'favorite_count' );
     1116    if ( ! empty( $fav_count ) ) {
     1117        $fav_count += 1;
     1118    } else {
     1119        $fav_count = 1;
     1120    }
    10761121
    10771122    // Update user meta.
     
    11271172    }
    11281173
    1129     $my_favs = bp_get_user_meta( $user_id, 'bp_favorite_activities', true );
    1130     $my_favs = array_flip( (array) $my_favs );
     1174    $my_favs = bp_activity_get_user_favorites( $user_id );
     1175    $my_favs = array_flip( $my_favs );
    11311176
    11321177    // Bail if the user has not previously favorited the item.
     
    11361181
    11371182    // Remove the fav from the user's favs.
    1138     unset( $my_favs[$activity_id] );
     1183    unset( $my_favs[ $activity_id ] );
    11391184    $my_favs = array_unique( array_flip( $my_favs ) );
    11401185
  • trunk/src/bp-templates/bp-legacy/buddypress-functions.php

    r13526 r13636  
    12781278    $activity_id   = (int) $_POST['id'];
    12791279    $activity_item = new BP_Activity_Activity( $activity_id );
    1280     if ( ! bp_activity_user_can_read( $activity_item, bp_loggedin_user_id() ) ) {
    1281         return;
    1282     }
    1283 
    1284     if ( bp_activity_add_user_favorite( $_POST['id'] ) )
    1285         _e( 'Remove Favorite', 'buddypress' );
    1286     else
    1287         _e( 'Favorite', 'buddypress' );
     1280    if ( empty( $activity_item->id ) || ! bp_activity_user_can_read( $activity_item, bp_loggedin_user_id() ) ) {
     1281        return;
     1282    }
     1283
     1284    if ( bp_activity_add_user_favorite( $activity_id ) ) {
     1285        esc_html_e( 'Remove Favorite', 'buddypress' );
     1286    } else {
     1287        esc_html_e( 'Favorite', 'buddypress' );
     1288    }
    12881289
    12891290    exit;
     
    13121313    }
    13131314
    1314     if ( bp_activity_remove_user_favorite( $_POST['id'] ) )
    1315         _e( 'Favorite', 'buddypress' );
    1316     else
    1317         _e( 'Remove Favorite', 'buddypress' );
     1315    $activity_id = (int) $_POST['id'];
     1316
     1317    if ( bp_activity_remove_user_favorite( $activity_id ) ) {
     1318        esc_html_e( 'Favorite', 'buddypress' );
     1319    } else {
     1320        esc_html_e( 'Remove Favorite', 'buddypress' );
     1321    }
    13181322
    13191323    exit;
  • trunk/src/bp-templates/bp-nouveau/includes/activity/ajax.php

    r13503 r13636  
    104104    $activity_id   = (int) $_POST['id'];
    105105    $activity_item = new BP_Activity_Activity( $activity_id );
    106     if ( ! bp_activity_user_can_read( $activity_item, bp_loggedin_user_id() ) ) {
    107         wp_send_json_error();
    108     }
    109 
    110     if ( bp_activity_add_user_favorite( $_POST['id'] ) ) {
     106    if ( empty( $activity_item->id ) || ! bp_activity_user_can_read( $activity_item, bp_loggedin_user_id() ) ) {
     107        wp_send_json_error();
     108    }
     109
     110    if ( bp_activity_add_user_favorite( $activity_id ) ) {
    111111        $response = array( 'content' => __( 'Remove Favorite', 'buddypress' ) );
    112112
     
    150150    }
    151151
    152     if ( bp_activity_remove_user_favorite( $_POST['id'] ) ) {
     152    $activity_id = (int) $_POST['id'];
     153
     154    if ( bp_activity_remove_user_favorite( $activity_id ) ) {
    153155        $response = array( 'content' => __( 'Mark as Favorite', 'buddypress' ) );
    154156
  • trunk/tests/phpunit/testcases/activity/functions.php

    r13414 r13636  
    14671467
    14681468    /**
     1469     * @group favorites
     1470     * @group bp_activity_add_user_favorite
     1471     */
     1472    public function test_add_user_favorite_not_an_activity_id() {
     1473        $u1 = self::factory()->user->create();
     1474        $a = self::factory()->activity->create();
     1475
     1476        // bp_activity_add_user_favorite() requires a logged-in user.
     1477        $current_user = bp_loggedin_user_id();
     1478        $this->set_current_user( $u1 );
     1479
     1480        // Only favorite for user 1
     1481        bp_activity_add_user_favorite( $a, $u1 );
     1482        $user_favorites = array( $a );
     1483        $this->assertEquals( $user_favorites, bp_activity_get_user_favorites( $u1 ) );
     1484
     1485        // Adds something that is not an activity id.
     1486        bp_activity_add_user_favorite( 'not_an_activity_id', $u1 );
     1487
     1488        // The above shouldn't be added.
     1489        $this->assertEquals( $user_favorites, bp_activity_get_user_favorites( $u1 ) );
     1490        $this->assertEquals( 1, bp_activity_get_meta( $a, 'favorite_count' ) );
     1491
     1492        $this->set_current_user( $current_user );
     1493    }
     1494
     1495    /**
    14691496     * @group bp_activity_post_update
    14701497     */
Note: See TracChangeset for help on using the changeset viewer.