[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/rest-api/endpoints/ -> class-wp-rest-edit-site-export-controller.php (source)

   1  <?php
   2  /**
   3   * REST API: WP_REST_Edit_Site_Export_Controller class
   4   *
   5   * @package    WordPress
   6   * @subpackage REST_API
   7   */
   8  
   9  /**
  10   * Controller which provides REST endpoint for exporting current templates
  11   * and template parts.
  12   *
  13   * @since 5.9.0
  14   *
  15   * @see WP_REST_Controller
  16   */
  17  class WP_REST_Edit_Site_Export_Controller extends WP_REST_Controller {
  18  
  19      /**
  20       * Constructor.
  21       *
  22       * @since 5.9.0
  23       */
  24  	public function __construct() {
  25          $this->namespace = 'wp-block-editor/v1';
  26          $this->rest_base = 'export';
  27      }
  28  
  29      /**
  30       * Registers the site export route.
  31       *
  32       * @since 5.9.0
  33       */
  34  	public function register_routes() {
  35          register_rest_route(
  36              $this->namespace,
  37              '/' . $this->rest_base,
  38              array(
  39                  array(
  40                      'methods'             => WP_REST_Server::READABLE,
  41                      'callback'            => array( $this, 'export' ),
  42                      'permission_callback' => array( $this, 'permissions_check' ),
  43                  ),
  44              )
  45          );
  46      }
  47  
  48      /**
  49       * Checks whether a given request has permission to export.
  50       *
  51       * @since 5.9.0
  52       *
  53       * @return WP_Error|true True if the request has access, or WP_Error object.
  54       */
  55  	public function permissions_check() {
  56          if ( current_user_can( 'edit_theme_options' ) ) {
  57              return true;
  58          }
  59  
  60          return new WP_Error(
  61              'rest_cannot_export_templates',
  62              __( 'Sorry, you are not allowed to export templates and template parts.' ),
  63              array( 'status' => rest_authorization_required_code() )
  64          );
  65      }
  66  
  67      /**
  68       * Output a ZIP file with an export of the current templates
  69       * and template parts from the site editor, and close the connection.
  70       *
  71       * @since 5.9.0
  72       *
  73       * @return WP_Error|void
  74       */
  75  	public function export() {
  76          // Generate the export file.
  77          $filename = wp_generate_block_templates_export_file();
  78  
  79          if ( is_wp_error( $filename ) ) {
  80              $filename->add_data( array( 'status' => 500 ) );
  81  
  82              return $filename;
  83          }
  84  
  85          $theme_name = wp_get_theme()->get( 'TextDomain' );
  86          header( 'Content-Type: application/zip' );
  87          header( 'Content-Disposition: attachment; filename=' . $theme_name . '.zip' );
  88          header( 'Content-Length: ' . filesize( $filename ) );
  89          flush();
  90          readfile( $filename );
  91          unlink( $filename );
  92          exit;
  93      }
  94  }


Generated: Mon Nov 11 01:00:02 2024 Cross-referenced by PHPXref 0.7.1