BP_REST_Document_Endpoint::bp_documents_update_rest_field_callback( object $object, object $value, string $attribute )

The function to use to update the document’s value of the activity REST Field.

Description

Parameters

$object

(Required) The BuddyPress component's object that was just created/updated during the request. (in this case the BP_Activity_Activity object).

$value

(Required) The value of the REST Field to save.

$attribute

(Required) The REST Field key used into the REST response.

Return

(object)

Source

File: bp-document/classes/class-bp-rest-document-endpoint.php

2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
protected function bp_documents_update_rest_field_callback( $object, $value, $attribute ) {
 
    global $bp_activity_edit, $bp_activity_post_update_id, $bp_activity_post_update;
 
    if ( 'bp_documents' !== $attribute ) {
        $value->bp_documents = null;
 
        return $value;
    }
 
    $bp_activity_edit = ( isset( $value->edit ) ? true : false );
    // phpcs:ignore
    $_POST['edit'] = $bp_activity_edit;
 
    if ( false === $bp_activity_edit && empty( $object ) ) {
        return $value;
    }
 
    $activity_id = $value->id;
    $privacy     = $value->privacy;
    $group_id    = 0;
 
    $documents = wp_parse_id_list( $object );
 
    $old_document_ids      = bp_activity_get_meta( $activity_id, 'bp_document_ids', true );
    $old_document_ids      = ( ! empty( $old_document_ids ) ? explode( ',', $old_document_ids ) : array() );
    $new_documents         = array();
    $old_documents         = array();
    $old_documents_objects = array();
 
    if ( ! empty( $old_document_ids ) ) {
        foreach ( $old_document_ids as $id ) {
            $document_object = new BP_Document( $id );
            $old_documents_objects[ $document_object->attachment_id ] = $document_object;
            $old_documents[ $id ]                                     = $document_object->attachment_id;
        }
    }
 
    $bp_activity_post_update    = true;
    $bp_activity_post_update_id = $activity_id;
 
    if ( ! empty( $value->component ) && 'groups' === $value->component ) {
        $group_id = $value->item_id;
        $privacy  = 'grouponly';
    }
 
    if ( ! isset( $documents ) || empty( $documents ) ) {
 
        // delete document ids and meta for activity if empty document in request.
        // delete media ids and meta for activity if empty media in request.
        if ( ! empty( $activity_id ) && ! empty( $old_document_ids ) ) {
            foreach ( $old_document_ids as $document_id ) {
                bp_document_delete( array( 'id' => $document_id ), 'activity' );
            }
            bp_activity_delete_meta( $activity_id, 'bp_document_ids' );
        }
 
        return $value;
    } else {
 
        $order_count = 0;
        foreach ( $documents as $id ) {
 
            $wp_attachment_url = wp_get_attachment_url( $id );
 
            // when the file found to be empty it's means it's not a valid attachment.
            if ( empty( $wp_attachment_url ) ) {
                continue;
            }
 
            $order_count ++;
 
            if ( in_array( $id, $old_documents, true ) ) {
                $new_documents[] = array(
                    'document_id' => $old_documents_objects[ $id ]->id,
                );
            } else {
                $new_documents[] = array(
                    'id'         => $id,
                    'name'       => get_the_title( $id ),
                    'folder_id'  => 0,
                    'group_id'   => $group_id,
                    'menu_order' => $order_count,
                    'privacy'    => $privacy,
                    'error_type' => 'wp_error',
                );
            }
        }
    }
 
    remove_action( 'bp_activity_posted_update', 'bp_document_update_activity_document_meta', 10, 3 );
    remove_action( 'bp_groups_posted_update', 'bp_document_groups_activity_update_document_meta', 10, 4 );
    remove_action( 'bp_activity_comment_posted', 'bp_document_activity_comments_update_document_meta', 10, 3 );
    remove_action( 'bp_activity_comment_posted_notification_skipped', 'bp_document_activity_comments_update_document_meta', 10, 3 );
 
    $document_ids = bp_document_add_handler( $new_documents, $privacy, '', $group_id );
 
    add_action( 'bp_activity_posted_update', 'bp_document_update_activity_document_meta', 10, 3 );
    add_action( 'bp_groups_posted_update', 'bp_document_groups_activity_update_document_meta', 10, 4 );
    add_action( 'bp_activity_comment_posted', 'bp_document_activity_comments_update_document_meta', 10, 3 );
    add_action( 'bp_activity_comment_posted_notification_skipped', 'bp_document_activity_comments_update_document_meta', 10, 3 );
 
    // save document meta for activity.
    if ( ! empty( $activity_id ) ) {
        // Delete document if not exists in current document ids.
 
        if ( true === $bp_activity_edit ) {
            if ( ! empty( $old_document_ids ) ) {
                foreach ( $old_document_ids as $document_id ) {
                    if ( ! in_array( (int) $document_id, $document_ids, true ) ) {
                        bp_document_delete( array( 'id' => $document_id ) );
                    }
                }
            }
        }
        bp_activity_update_meta( $activity_id, 'bp_document_ids', implode( ',', $document_ids ) );
    }
}

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.