rest_get_route_for_post()

最后更新于:2021-11-27 23:37:18

rest_get_route_for_post( int|GC_Post$post)

Gets the REST API route for a post.

参数

$post

(int|GC_Post) (Required) Post ID or post object.

响应

(string) The route path with a leading slash for the given post, or an empty string if there is not a route.

源文件

文件: gc-includes/rest-api.php

function rest_get_route_for_post( $post ) {
	$post = get_post( $post );

	if ( ! $post instanceof GC_Post ) {
		return '';
	}

	$post_type = get_post_type_object( $post->post_type );
	if ( ! $post_type ) {
		return '';
	}

	$controller = $post_type->get_rest_controller();
	if ( ! $controller ) {
		return '';
	}

	$route = '';

	// The only two controllers that we can detect are the Attachments and Posts controllers.
	if ( in_array( get_class( $controller ), array( 'GC_REST_Attachments_Controller', 'GC_REST_Posts_Controller' ), true ) ) {
		$namespace = 'gc/v2';
		$rest_base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
		$route     = sprintf( '/%s/%s/%d', $namespace, $rest_base, $post->ID );
	}

	/**
	 * Filters the REST API route for a post.
	 *
	 * @since 5.5.0
	 *
	 * @param string  $route The route path.
	 * @param GC_Post $post  The post object.
	 */
	return apply_filters( 'rest_route_for_post', $route, $post );
}