BP_REST_Media_Endpoint::bp_media_ids_update_rest_field_callback( object $object, object $value, string $attribute )

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

Description

Parameters

$object

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

$value

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

$attribute

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

Return

(object)

Source

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

	protected function bp_media_ids_update_rest_field_callback( $object, $value, $attribute ) {

		if ( 'bp_media_ids' !== $attribute || empty( $object ) ) {
			$value->bp_media_ids = null;
			return $value;
		}

		$privacy = $value->privacy;
		$medias  = wp_parse_id_list( $object );
		if ( empty( $medias ) ) {
			$value->bp_media_ids = null;
			return $value;
		}

		$args = array(
			'upload_ids'  => $medias,
			'privacy'     => $privacy,
			'activity_id' => $value->id,
		);

		if ( ! empty( $value->component ) && 'groups' === $value->component ) {
			$args['group_id'] = $value->item_id;
			$args['privacy']  = 'grouponly';
		}

		$medias_ids = $this->bp_rest_create_media( $args );

		if ( is_wp_error( $medias_ids ) ) {
			$value->bp_media_ids = $medias_ids;
			return $value;
		}

		$medias = $this->assemble_response_data( array( 'media_ids' => $medias_ids ) );

		if ( empty( $medias['medias'] ) ) {
			return;
		}

		$retval = array();
		foreach ( $medias['medias'] as $media ) {
			$retval[] = array(
				'id'    => $media->id,
				'full'  => wp_get_attachment_image_url( $media->attachment_id, 'full' ),
				'thumb' => wp_get_attachment_image_url( $media->attachment_id, 'bp-media-thumbnail' ),
			);
		}

		$value->bp_media_ids = $retval;
		return $value;
	}

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.