-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Backport: Allow template duplication + concept of active templates #8063
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
e9c3308
064b000
7635f18
bee1e4a
3b09dae
ec3dfd7
949cf12
b1d0cce
2ee34ad
ce98ab9
0f85749
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -263,6 +263,15 @@ function rest_api_default_filters() { | |
| * @since 4.7.0 | ||
| */ | ||
| function create_initial_rest_routes() { | ||
| global $wp_post_types; | ||
|
|
||
| // Register the registered templates endpoint. For that we need to copy the | ||
| // wp_template post type so that it's available as an entity in core-data. | ||
| $wp_post_types['wp_registered_template'] = clone $wp_post_types['wp_template']; | ||
| $wp_post_types['wp_registered_template']->name = 'wp_registered_template'; | ||
| $wp_post_types['wp_registered_template']->rest_base = 'wp_registered_template'; | ||
| $wp_post_types['wp_registered_template']->rest_controller_class = 'WP_REST_Registered_Templates_Controller'; | ||
|
Comment on lines
+268
to
+273
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ellatrix We need to pick a shorter post type slug which is 20 characters or less, for example I discovered that this causes a very esoteric error in unit tests. It was difficult to debug, but it comes down to this:
This causes the unit tests in the WordPress/performance repo to start to fail. For example: The unit test doesn't fail when run in isolation. But it runs when running with all the other unit tests. This is because the $other_post_ids = array_merge(
$other_post_ids,
self::factory()->post->create_many( 10, compact( 'post_type' ) )
);
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, it seems this is being removed in #10425 |
||
|
|
||
| foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) { | ||
| $controller = $post_type->get_rest_controller(); | ||
|
|
||
|
|
@@ -290,7 +299,6 @@ function create_initial_rest_routes() { | |
| } | ||
|
|
||
| // Register the old templates endpoint. | ||
| global $wp_post_types; | ||
| $wp_post_types['wp_template']->rest_base = 'templates'; | ||
| $controller = new WP_REST_Templates_Controller( 'wp_template' ); | ||
| $wp_post_types['wp_template']->rest_base = 'wp_template'; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| <?php | ||
|
|
||
| class WP_REST_Registered_Templates_Controller extends WP_REST_Templates_Controller { | ||
| public function register_routes() { | ||
| // Lists all templates. | ||
| register_rest_route( | ||
| $this->namespace, | ||
| '/' . $this->rest_base, | ||
| array( | ||
| array( | ||
| 'methods' => WP_REST_Server::READABLE, | ||
| 'callback' => array( $this, 'get_items' ), | ||
| 'permission_callback' => array( $this, 'get_items_permissions_check' ), | ||
| 'args' => $this->get_collection_params(), | ||
| ), | ||
| 'schema' => array( $this, 'get_public_item_schema' ), | ||
| ) | ||
| ); | ||
|
|
||
| // Lists/updates a single template based on the given id. | ||
| register_rest_route( | ||
| $this->namespace, | ||
| // The route. | ||
| sprintf( | ||
| '/%s/(?P<id>%s%s)', | ||
| $this->rest_base, | ||
| /* | ||
| * Matches theme's directory: `/themes/<subdirectory>/<theme>/` or `/themes/<theme>/`. | ||
| * Excludes invalid directory name characters: `/:<>*?"|`. | ||
| */ | ||
| '([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)', | ||
| // Matches the template name. | ||
| '[\/\w%-]+' | ||
| ), | ||
| array( | ||
| 'args' => array( | ||
| 'id' => array( | ||
| 'description' => __( 'The id of a template' ), | ||
| 'type' => 'string', | ||
| 'sanitize_callback' => array( $this, '_sanitize_template_id' ), | ||
| ), | ||
| ), | ||
| array( | ||
| 'methods' => WP_REST_Server::READABLE, | ||
| 'callback' => array( $this, 'get_item' ), | ||
| 'permission_callback' => array( $this, 'get_item_permissions_check' ), | ||
| 'args' => array( | ||
| 'context' => $this->get_context_param( array( 'default' => 'view' ) ), | ||
| ), | ||
| ), | ||
| 'schema' => array( $this, 'get_public_item_schema' ), | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| public function get_item_schema() { | ||
| $schema = parent::get_item_schema(); | ||
| $schema['properties']['is_custom'] = array( | ||
| 'description' => __( 'Whether a template is a custom template.' ), | ||
| 'type' => 'bool', | ||
| 'context' => array( 'embed', 'view', 'edit' ), | ||
| 'readonly' => true, | ||
| ); | ||
| $schema['properties']['plugin'] = array( | ||
| 'type' => 'string', | ||
| 'description' => __( 'Plugin that registered the template.' ), | ||
| 'readonly' => true, | ||
| 'context' => array( 'view', 'edit', 'embed' ), | ||
| ); | ||
| return $schema; | ||
| } | ||
|
|
||
| public function get_items( $request ) { | ||
| $query = array(); | ||
| if ( isset( $request['area'] ) ) { | ||
| $query['area'] = $request['area']; | ||
| } | ||
| if ( isset( $request['post_type'] ) ) { | ||
| $query['post_type'] = $request['post_type']; | ||
| } | ||
| $query_result = get_registered_block_templates( $query ); | ||
| $templates = array(); | ||
| foreach ( $query_result as $template ) { | ||
| $item = $this->prepare_item_for_response( $template, $request ); | ||
| $item->data['type'] = 'wp_registered_template'; | ||
| $templates[] = $this->prepare_response_for_collection( $item ); | ||
| } | ||
|
|
||
| return rest_ensure_response( $templates ); | ||
| } | ||
|
|
||
| public function get_item( $request ) { | ||
| $template = get_block_file_template( $request['id'], 'wp_template' ); | ||
|
|
||
| if ( ! $template ) { | ||
| return new WP_Error( 'rest_template_not_found', __( 'No templates exist with that id.' ), array( 'status' => 404 ) ); | ||
| } | ||
|
|
||
| $item = $this->prepare_item_for_response( $template, $request ); | ||
| // adjust the template type here instead | ||
| $item->data['type'] = 'wp_registered_template'; | ||
| return rest_ensure_response( $item ); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add
globaldocumentation forglobal $wp_post_types?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would that documentation be? Looking through other instances of
global $wp_post_types, I'm not seeing anything for those. 🤔Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like this
@global array $wp_post_types List of post types.?Reference PR: #72020
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's ok I will leave this for a follow-up because we're very close to code freeze.