WP_REST_Posts_Controller::check_template()publicWP 4.9.0

Checks whether the template is valid for the given post.

Метод класса: WP_REST_Posts_Controller{}

Хуков нет.

Возвращает

true|WP_Error. True if template is still valid or if the same as existing value, or a WP_Error if template not supported.

Использование

$WP_REST_Posts_Controller = new WP_REST_Posts_Controller();
$WP_REST_Posts_Controller->check_template( $template, $request );
$template(строка) (обязательный)
Page template filename.
$request(WP_REST_Request) (обязательный)
Request.

Список изменений

С версии 4.9.0 Введена.

Код WP_REST_Posts_Controller::check_template() WP 6.5.2

public function check_template( $template, $request ) {

	if ( ! $template ) {
		return true;
	}

	if ( $request['id'] ) {
		$post             = get_post( $request['id'] );
		$current_template = get_page_template_slug( $request['id'] );
	} else {
		$post             = null;
		$current_template = '';
	}

	// Always allow for updating a post to the same template, even if that template is no longer supported.
	if ( $template === $current_template ) {
		return true;
	}

	// If this is a create request, get_post() will return null and wp theme will fallback to the passed post type.
	$allowed_templates = wp_get_theme()->get_page_templates( $post, $this->post_type );

	if ( isset( $allowed_templates[ $template ] ) ) {
		return true;
	}

	return new WP_Error(
		'rest_invalid_param',
		/* translators: 1: Parameter, 2: List of valid values. */
		sprintf( __( '%1$s is not one of %2$s.' ), 'template', implode( ', ', array_keys( $allowed_templates ) ) )
	);
}