AnsPress_API()

Description #

AnsPress REST endpoint class.

Changelog #

VersionDescription
unknownIntroduced.

Source #

File: includes/api.php

class AnsPress_API {
	/**
	 * Register REST route.
	 */
	public static function register() {
		register_rest_route(
			'anspress',
			'/user/avatar',
			array(
				'methods'  => 'GET',
				'callback' => array( 'AnsPress_API', 'avatar' ),
			)
		);
	}

	/**
	 * Callback for route `/anspress/user/avatar/`.
	 *
	 * @param WP_REST_Request $request Request data.
	 * @return WP_REST_Response|WP_Error
	 */
	public static function avatar( $request ) {
		$args = $request->get_query_params();
		if ( isset( $args['id'] ) ) {
			$size   = isset( $args['size'] ) ? (int) $args['size'] : 90;
			$avatar = get_avatar_url( (int) $args['id'], $size );
			return new WP_REST_Response( $avatar, 200 );
		}
		return new WP_Error( 'wrongData', __( 'Wrong data supplied', 'anspress-question-answer' ) );
	}
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment