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
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.