| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * REST API: WP_REST_Template_Autosaves_Controller class. 4 * 5 * @package WordPress 6 * @subpackage REST_API 7 * @since 6.4.0 8 */ 9 10 /** 11 * Core class used to access template autosaves via the REST API. 12 * 13 * @since 6.4.0 14 * 15 * @see WP_REST_Autosaves_Controller 16 */ 17 class WP_REST_Template_Autosaves_Controller extends WP_REST_Autosaves_Controller { 18 19 /** 20 * Parent post controller. 21 * 22 * @since 6.4.0 23 * @var WP_REST_Controller 24 */ 25 private $parent_controller; 26 27 /** 28 * Revision controller. 29 * 30 * @since 6.4.0 31 * @var WP_REST_Revisions_Controller 32 */ 33 private $revisions_controller; 34 35 /** 36 * The base of the parent controller's route. 37 * 38 * @since 6.4.0 39 * @var string 40 */ 41 private $parent_base; 42 43 /** 44 * Constructor. 45 * 46 * @since 6.4.0 47 * 48 * @param string $parent_post_type Post type of the parent. 49 */ 50 public function __construct( $parent_post_type ) { 51 parent::__construct( $parent_post_type ); 52 $post_type_object = get_post_type_object( $parent_post_type ); 53 $parent_controller = $post_type_object->get_rest_controller(); 54 55 if ( ! $parent_controller ) { 56 $parent_controller = new WP_REST_Templates_Controller( $parent_post_type ); 57 } 58 59 $this->parent_controller = $parent_controller; 60 61 $revisions_controller = $post_type_object->get_revisions_rest_controller(); 62 if ( ! $revisions_controller ) { 63 $revisions_controller = new WP_REST_Revisions_Controller( $parent_post_type ); 64 } 65 $this->revisions_controller = $revisions_controller; 66 $this->rest_base = 'autosaves'; 67 $this->parent_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name; 68 $this->namespace = ! empty( $post_type_object->rest_namespace ) ? $post_type_object->rest_namespace : 'wp/v2'; 69 } 70 71 /** 72 * Registers the routes for autosaves. 73 * 74 * @since 6.4.0 75 * 76 * @see register_rest_route() 77 */ 78 public function register_routes() { 79 register_rest_route( 80 $this->namespace, 81 sprintf( 82 '/%s/(?P<id>%s%s)/%s', 83 $this->parent_base, 84 /* 85 * Matches theme's directory: `/themes/<subdirectory>/<theme>/` or `/themes/<theme>/`. 86 * Excludes invalid directory name characters: `/:<>*?"|`. 87 */ 88 '([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)', 89 // Matches the template name. 90 '[\/\w%-]+', 91 $this->rest_base 92 ), 93 array( 94 'args' => array( 95 'id' => array( 96 'description' => __( 'The id of a template' ), 97 'type' => 'string', 98 'sanitize_callback' => array( $this->parent_controller, '_sanitize_template_id' ), 99 ), 100 ), 101 array( 102 'methods' => WP_REST_Server::READABLE, 103 'callback' => array( $this, 'get_items' ), 104 'permission_callback' => array( $this, 'get_items_permissions_check' ), 105 'args' => $this->get_collection_params(), 106 ), 107 array( 108 'methods' => WP_REST_Server::CREATABLE, 109 'callback' => array( $this, 'create_item' ), 110 'permission_callback' => array( $this, 'create_item_permissions_check' ), 111 'args' => $this->parent_controller->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), 112 ), 113 'schema' => array( $this, 'get_public_item_schema' ), 114 ) 115 ); 116 117 register_rest_route( 118 $this->namespace, 119 sprintf( 120 '/%s/(?P<parent>%s%s)/%s/%s', 121 $this->parent_base, 122 /* 123 * Matches theme's directory: `/themes/<subdirectory>/<theme>/` or `/themes/<theme>/`. 124 * Excludes invalid directory name characters: `/:<>*?"|`. 125 */ 126 '([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)', 127 // Matches the template name. 128 '[\/\w%-]+', 129 $this->rest_base, 130 '(?P<id>[\d]+)' 131 ), 132 array( 133 'args' => array( 134 'parent' => array( 135 'description' => __( 'The id of a template' ), 136 'type' => 'string', 137 'sanitize_callback' => array( $this->parent_controller, '_sanitize_template_id' ), 138 ), 139 'id' => array( 140 'description' => __( 'The ID for the autosave.' ), 141 'type' => 'integer', 142 ), 143 ), 144 array( 145 'methods' => WP_REST_Server::READABLE, 146 'callback' => array( $this, 'get_item' ), 147 'permission_callback' => array( $this->revisions_controller, 'get_item_permissions_check' ), 148 'args' => array( 149 'context' => $this->get_context_param( array( 'default' => 'view' ) ), 150 ), 151 ), 152 'schema' => array( $this, 'get_public_item_schema' ), 153 ) 154 ); 155 } 156 157 /** 158 * Prepares the item for the REST response. 159 * 160 * @since 6.4.0 161 * 162 * @param WP_Post $item Post revision object. 163 * @param WP_REST_Request $request Request object. 164 * @return WP_REST_Response Response object. 165 */ 166 public function prepare_item_for_response( $item, $request ) { 167 $template = _build_block_template_result_from_post( $item ); 168 $response = $this->parent_controller->prepare_item_for_response( $template, $request ); 169 170 // Don't prepare the response body for HEAD requests. 171 if ( $request->is_method( 'HEAD' ) ) { 172 return $response; 173 } 174 175 $fields = $this->get_fields_for_response( $request ); 176 $data = $response->get_data(); 177 178 if ( in_array( 'parent', $fields, true ) ) { 179 $data['parent'] = (int) $item->post_parent; 180 } 181 182 $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 183 $data = $this->filter_response_by_context( $data, $context ); 184 185 // Wrap the data in a response object. 186 $response = new WP_REST_Response( $data ); 187 188 if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) { 189 $links = $this->prepare_links( $template ); 190 $response->add_links( $links ); 191 } 192 193 return $response; 194 } 195 196 /** 197 * Gets the autosave, if the ID is valid. 198 * 199 * @since 6.4.0 200 * 201 * @param WP_REST_Request $request Full details about the request. 202 * @return WP_Post|WP_Error Autosave post object if ID is valid, WP_Error otherwise. 203 */ 204 public function get_item( $request ) { 205 $parent = $this->get_parent( $request['parent'] ); 206 if ( is_wp_error( $parent ) ) { 207 return $parent; 208 } 209 210 $autosave = wp_get_post_autosave( $parent->ID ); 211 212 if ( ! $autosave ) { 213 return new WP_Error( 214 'rest_post_no_autosave', 215 __( 'There is no autosave revision for this template.' ), 216 array( 'status' => 404 ) 217 ); 218 } 219 220 $response = $this->prepare_item_for_response( $autosave, $request ); 221 return $response; 222 } 223 224 /** 225 * Get the parent post. 226 * 227 * @since 6.4.0 228 * 229 * @param int $parent_id Supplied ID. 230 * @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise. 231 */ 232 protected function get_parent( $parent_id ) { 233 return $this->revisions_controller->get_parent( $parent_id ); 234 } 235 236 /** 237 * Prepares links for the request. 238 * 239 * @since 6.4.0 240 * 241 * @param WP_Block_Template $template Template. 242 * @return array Links for the given post. 243 */ 244 protected function prepare_links( $template ) { 245 $links = array( 246 'self' => array( 247 'href' => rest_url( sprintf( '/%s/%s/%s/%s/%d', $this->namespace, $this->parent_base, $template->id, $this->rest_base, $template->wp_id ) ), 248 ), 249 'parent' => array( 250 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->parent_base, $template->id ) ), 251 ), 252 ); 253 254 return $links; 255 } 256 257 /** 258 * Retrieves the autosave's schema, conforming to JSON Schema. 259 * 260 * @since 6.4.0 261 * 262 * @return array Item schema data. 263 */ 264 public function get_item_schema() { 265 if ( $this->schema ) { 266 return $this->add_additional_fields_schema( $this->schema ); 267 } 268 269 $this->schema = $this->revisions_controller->get_item_schema(); 270 271 return $this->add_additional_fields_schema( $this->schema ); 272 } 273 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Sat Sep 5 08:20:28 2026 | Cross-referenced by PHPXref |