wppaste
WordPress

WP_REST_Edit_Site_Export_Controller

Since
5.9.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-edit-site-export-controller.php:17
Controller which provides REST endpoint for exporting current templates and template parts.

Compatibility

WordPress
since 5.9.0
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0).

Methods · 4

Source code

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

Introduced in 5.9.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/rest-api/endpoints/class-wp-rest-edit-site-export-controller.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.