类文档

WP_REST_Edit_Site_Export_Controller

💡 云策文档标注

概述

WP_REST_Edit_Site_Export_Controller 是一个 WordPress REST API 控制器,用于提供导出当前模板和模板部分的端点。它继承自 WP_REST_Controller,主要功能包括注册路由、权限检查和生成 ZIP 导出文件。

关键要点

  • 控制器继承自 WP_REST_Controller,命名空间为 'wp-block-editor/v1',基础路径为 'export'。
  • 提供 export 方法,输出包含当前模板和模板部分的 ZIP 文件,并关闭连接。
  • 通过 permissions_check 方法检查用户是否具有 'export' 权限,否则返回 WP_Error。
  • 使用 register_routes 方法注册 REST 路由,支持 WP_REST_Server::READABLE 方法。
  • 自 WordPress 5.9.0 版本引入。

代码示例

public function export() {
    // Generate the export file.
    $filename = wp_generate_block_templates_export_file();

    if ( is_wp_error( $filename ) ) {
        $filename->add_data( array( 'status' => 500 ) );

        return $filename;
    }

    $theme_name = basename( get_stylesheet() );
    header( 'Content-Type: application/zip' );
    header( 'Content-Disposition: attachment; filename=' . $theme_name . '.zip' );
    header( 'Content-Length: ' . filesize( $filename ) );
    flush();
    readfile( $filename );
    unlink( $filename );
    exit;
}

注意事项

  • 导出功能依赖于 wp_generate_block_templates_export_file 函数生成文件,需确保其正常工作。
  • 权限检查基于 current_user_can( 'export' ),用户需具备相应能力才能访问端点。
  • 导出后文件会被删除(通过 unlink),避免服务器存储残留。

📄 原文内容

Controller which provides REST endpoint for exporting current templates and template parts.

Description

See also

Methods

Name Description
WP_REST_Edit_Site_Export_Controller::__construct Constructor.
WP_REST_Edit_Site_Export_Controller::export Output a ZIP file with an export of the current templates and template parts from the site editor, and close the connection.
WP_REST_Edit_Site_Export_Controller::permissions_check Checks whether a given request has permission to export.
WP_REST_Edit_Site_Export_Controller::register_routes Registers the site export route.

Source

class WP_REST_Edit_Site_Export_Controller extends WP_REST_Controller {

	/**
	 * Constructor.
	 *
	 * @since 5.9.0
	 */
	public function __construct() {
		$this->namespace = 'wp-block-editor/v1';
		$this->rest_base = 'export';
	}

	/**
	 * Registers the site export route.
	 *
	 * @since 5.9.0
	 */
	public function register_routes() {
		register_rest_route(
			$this->namespace,
			'/' . $this->rest_base,
			array(
				array(
					'methods'             => WP_REST_Server::READABLE,
					'callback'            => array( $this, 'export' ),
					'permission_callback' => array( $this, 'permissions_check' ),
				),
			)
		);
	}

	/**
	 * Checks whether a given request has permission to export.
	 *
	 * @since 5.9.0
	 *
	 * @return true|WP_Error True if the request has access, or WP_Error object.
	 */
	public function permissions_check() {
		if ( current_user_can( 'export' ) ) {
			return true;
		}

		return new WP_Error(
			'rest_cannot_export_templates',
			__( 'Sorry, you are not allowed to export templates and template parts.' ),
			array( 'status' => rest_authorization_required_code() )
		);
	}

	/**
	 * Output a ZIP file with an export of the current templates
	 * and template parts from the site editor, and close the connection.
	 *
	 * @since 5.9.0
	 *
	 * @return void|WP_Error
	 */
	public function export() {
		// Generate the export file.
		$filename = wp_generate_block_templates_export_file();

		if ( is_wp_error( $filename ) ) {
			$filename->add_data( array( 'status' => 500 ) );

			return $filename;
		}

		$theme_name = basename( get_stylesheet() );
		header( 'Content-Type: application/zip' );
		header( 'Content-Disposition: attachment; filename=' . $theme_name . '.zip' );
		header( 'Content-Length: ' . filesize( $filename ) );
		flush();
		readfile( $filename );
		unlink( $filename );
		exit;
	}
}

Changelog

Version Description
5.9.0 Introduced.