BP_REST_Topics_Actions_Endpoint::rest_update_favorite( integer $topic_id, boolean $value, integer $user_id )

Update favourites for the topic.

Description

Parameters

$topic_id

(Required) Topic ID.

$value

(Required) Action value.

$user_id

(Required) Current users ID.

Return

(bool|WP_Error)

Source

File: bp-forums/classes/class-bp-rest-topics-actions-endpoint.php

1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
protected function rest_update_favorite( $topic_id, $value, $user_id ) {
    if ( ! bbp_is_favorites_active() ) {
        return new WP_Error(
            'bp_rest_bbp_topic_action_disabled',
            __( 'Favorites are no longer active.', 'buddyboss' ),
            array(
                'status' => 404,
            )
        );
    }
 
    // Bail if user cannot add favorites for this user.
    if ( ! current_user_can( 'edit_user', $user_id ) ) {
        return new WP_Error(
            'bp_rest_authorization_required',
            __( 'You do not have permission to do this.', 'buddyboss' ),
            array(
                'status' => 404,
            )
        );
    }
 
    $favorited = bbp_is_user_favorite( $user_id, $topic_id );
 
    $status = true;
 
    if ( true === $favorited && empty( $value ) ) {
        $status = bbp_remove_user_favorite( $user_id, $topic_id );
 
    } elseif ( false === $favorited && ! empty( $value ) ) {
        $status = bbp_add_user_favorite( $user_id, $topic_id );
    }
 
    return $status;
}

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.