[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/rest-api/endpoints/ -> class-wp-rest-registered-templates-controller.php (source)

   1  <?php
   2  
   3  class WP_REST_Registered_Templates_Controller extends WP_REST_Templates_Controller {
   4  	public function register_routes() {
   5          // Lists all templates.
   6          register_rest_route(
   7              $this->namespace,
   8              '/' . $this->rest_base,
   9              array(
  10                  array(
  11                      'methods'             => WP_REST_Server::READABLE,
  12                      'callback'            => array( $this, 'get_items' ),
  13                      'permission_callback' => array( $this, 'get_items_permissions_check' ),
  14                      'args'                => $this->get_collection_params(),
  15                  ),
  16                  'schema' => array( $this, 'get_public_item_schema' ),
  17              )
  18          );
  19  
  20          // Lists/updates a single template based on the given id.
  21          register_rest_route(
  22              $this->namespace,
  23              // The route.
  24              sprintf(
  25                  '/%s/(?P<id>%s%s)',
  26                  $this->rest_base,
  27                  /*
  28                   * Matches theme's directory: `/themes/<subdirectory>/<theme>/` or `/themes/<theme>/`.
  29                   * Excludes invalid directory name characters: `/:<>*?"|`.
  30                   */
  31                  '([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)',
  32                  // Matches the template name.
  33                  '[\/\w%-]+'
  34              ),
  35              array(
  36                  'args'   => array(
  37                      'id' => array(
  38                          'description'       => __( 'The id of a template' ),
  39                          'type'              => 'string',
  40                          'sanitize_callback' => array( $this, '_sanitize_template_id' ),
  41                      ),
  42                  ),
  43                  array(
  44                      'methods'             => WP_REST_Server::READABLE,
  45                      'callback'            => array( $this, 'get_item' ),
  46                      'permission_callback' => array( $this, 'get_item_permissions_check' ),
  47                      'args'                => array(
  48                          'context' => $this->get_context_param( array( 'default' => 'view' ) ),
  49                      ),
  50                  ),
  51                  'schema' => array( $this, 'get_public_item_schema' ),
  52              )
  53          );
  54      }
  55  
  56  	public function get_item_schema() {
  57          $schema                            = parent::get_item_schema();
  58          $schema['properties']['is_custom'] = array(
  59              'description' => __( 'Whether a template is a custom template.' ),
  60              'type'        => 'bool',
  61              'context'     => array( 'embed', 'view', 'edit' ),
  62              'readonly'    => true,
  63          );
  64          $schema['properties']['plugin']    = array(
  65              'type'        => 'string',
  66              'description' => __( 'Plugin that registered the template.' ),
  67              'readonly'    => true,
  68              'context'     => array( 'view', 'edit', 'embed' ),
  69          );
  70          return $schema;
  71      }
  72  
  73  	public function get_items( $request ) {
  74          $query = array();
  75          if ( isset( $request['area'] ) ) {
  76              $query['area'] = $request['area'];
  77          }
  78          if ( isset( $request['post_type'] ) ) {
  79              $query['post_type'] = $request['post_type'];
  80          }
  81          $query_result = get_registered_block_templates( $query );
  82          $templates    = array();
  83          foreach ( $query_result as $template ) {
  84              $item               = $this->prepare_item_for_response( $template, $request );
  85              $item->data['type'] = 'wp_registered_template';
  86              $templates[]        = $this->prepare_response_for_collection( $item );
  87          }
  88  
  89          return rest_ensure_response( $templates );
  90      }
  91  
  92  	public function get_item( $request ) {
  93          $template = get_block_file_template( $request['id'], 'wp_template' );
  94  
  95          if ( ! $template ) {
  96              return new WP_Error( 'rest_template_not_found', __( 'No templates exist with that id.' ), array( 'status' => 404 ) );
  97          }
  98  
  99          $item = $this->prepare_item_for_response( $template, $request );
 100          // adjust the template type here instead
 101          $item->data['type'] = 'wp_registered_template';
 102          return rest_ensure_response( $item );
 103      }
 104  }


Generated : Thu Oct 23 08:20:05 2025 Cross-referenced by PHPXref