BP_REST_Attachments_Blog_Avatar_Endpoint::get_item( WP_REST_Request $request )

Fetch an existing blog avatar.


Parameters Parameters

$request

(WP_REST_Request) (Required) Full details about the request.


Top ↑

Return Return

(WP_REST_Response|WP_Error)


Source Source

File: bp-blogs/classes/class-bp-rest-attachments-blog-avatar-endpoint.php

	public function get_item( $request ) {
		$no_user_grav = (bool) $request->get_param( 'no_user_gravatar' );

		if ( empty( $this->blog->admin_user_id ) && ! $no_user_grav ) {
			return new WP_Error(
				'bp_rest_blog_avatar_get_item_user_failed',
				__( 'There was a problem confirming the blog\'s user admin is valid.', 'buddypress' ),
				array(
					'status' => 500,
				)
			);
		}

		// Set the requested args.
		$requested_args = array(
			'blog_id' => $request['id'],
			'no_grav' => $no_user_grav,
			'html'    => (bool) $request['html'],
		);

		if ( $request['alt'] ) {
			$requested_args['alt'] = $request['alt'];
		}

		if ( ! $no_user_grav ) {
			$requested_args['admin_user_id'] = (int) $this->blog->admin_user_id;

			if ( ! isset( $requested_args['alt'] ) ) {
				$requested_args['alt'] = '';
			}
		}

		$args = array();
		foreach ( array( 'full', 'thumb' ) as $type ) {
			$requested_args['type'] = $type;
			$args[ $type ]          = bp_get_blog_avatar( $requested_args );
		}

		// Get the avatar object.
		$avatar = $this->get_avatar_object( $args );

		if ( ! $avatar->full && ! $avatar->thumb ) {
			return new WP_Error(
				'bp_rest_attachments_blog_avatar_no_image',
				__( 'Sorry, there was a problem fetching the blog avatar.', 'buddypress' ),
				array(
					'status' => 500,
				)
			);
		}

		$retval = array(
			$this->prepare_response_for_collection(
				$this->prepare_item_for_response( $avatar, $request )
			),
		);

		$response = rest_ensure_response( $retval );

		/**
		 * Fires after a blog avatar is fetched via the REST API.
		 *
		 * @since 6.0.0
		 *
		 * @param stdClass          $avatar   The avatar object.
		 * @param WP_REST_Response  $response The response data.
		 * @param WP_REST_Request   $request  The request sent to the API.
		 */
		do_action( 'bp_rest_attachments_blog_avatar_get_item', $avatar, $response, $request );

		return $response;
	}


Top ↑

Changelog Changelog

Changelog
Version Description
6.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.