BP_REST_Notifications_Endpoint::prepare_item_for_response( BP_Notifications_Notification $notification, WP_REST_Request $request )

Prepares notification data for return as an object.

Description

Parameters

$notification

(Required) Notification data.

$request

(Required) Full details about the request.

Return

(WP_REST_Response)

Source

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

	public function prepare_item_for_response( $notification, $request ) {
		$data = array(
			'id'                => $notification->id,
			'user_id'           => $notification->user_id,
			'item_id'           => $notification->item_id,
			'secondary_item_id' => $notification->secondary_item_id,
			'component'         => $notification->component_name,
			'action'            => $notification->component_action,
			'date'              => bp_rest_prepare_date_response( $notification->date_notified ),
			'is_new'            => $notification->is_new,
			'description'       => array(
				'rendered' => '',
			),
			'link_url'          => '',
			'rest_actions'      => '',
		);

		$component = $notification->component_name;
		$object    = $notification->component_name;
		$item_id   = $notification->item_id;
		$object_id = $notification->item_id;

		switch ( $component ) {
			case 'groups':
				if ( ! empty( $notification->item_id ) ) {
					$object = 'group';
				}
				break;
			case 'follow':
			case 'friends':
				if ( ! empty( $notification->item_id ) ) {
					$object = 'user';
				}
				break;
			default:
				if ( ! empty( $notification->secondary_item_id ) ) {
					$object    = 'user';
					$object_id = $notification->secondary_item_id;
					$item_id   = $notification->secondary_item_id;
				} else {
					$object = 'user';
				}
				break;
		}

		// Avatars.
		$data['avatar_urls'] = array(
			'full'  => bp_core_fetch_avatar(
				array(
					'item_id' => $item_id,
					'html'    => false,
					'type'    => 'full',
					'object'  => $object,
				)
			),
			'thumb' => bp_core_fetch_avatar(
				array(
					'item_id' => $item_id,
					'html'    => false,
					'object'  => $object,
				)
			),
		);

		// Notification object.
		$data['object']    = $object;
		$data['object_id'] = $object_id;

		global $bp;

		if ( ! isset( $bp->notifications ) ) {
			$bp->notifications = new \stdClass();
		}

		if ( ! isset( $bp->notifications->query_loop ) ) {
			$bp->notifications->query_loop = new \stdClass();
		}

		$bp->notifications->query_loop->notification = $notification;
		$data['description']['rendered']             = bp_get_the_notification_description();

		if ( ! empty( $data['description']['rendered'] ) ) {
			// Extract the first URL from Description.
			preg_match( '/\bhttps?:\/\/[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|\/))/', $data['description']['rendered'], $matches_url );

			if ( isset( $matches_url[0] ) && wp_http_validate_url( $matches_url[0] ) ) {
				$data['link_url'] = $matches_url[0];
			}
		}

		$data['link_url']     = $this->bp_rest_link_url_update( $data['link_url'], $notification );
		$data['rest_actions'] = $this->bp_rest_get_notification_actions( $notification );

		$context  = ! empty( $request['context'] ) ? $request['context'] : 'view';
		$data     = $this->add_additional_fields_to_object( $data, $request );
		$data     = $this->filter_response_by_context( $data, $context );
		$response = rest_ensure_response( $data );

		$response->add_links( $this->prepare_links( $notification ) );

		/**
		 * Filter a notification value returned from the API.
		 *
		 * @param WP_REST_Response              $response     The response data.
		 * @param WP_REST_Request               $request      Request used to generate the response.
		 * @param BP_Notifications_Notification $notification Notification object.
		 *
		 * @since 0.1.0
		 */
		return apply_filters( 'bp_rest_notifications_prepare_value', $response, $request, $notification );
	}

Changelog

Changelog
Version Description
0.1.0 Introduced.

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.