#45017 closed enhancement (fixed)
REST API: Expose permalink_template in the Posts controller
Reported by: | danielbachhuber | Owned by: | danielbachhuber |
---|---|---|---|
Milestone: | 5.0 | Priority: | normal |
Severity: | normal | Version: | |
Component: | REST API | Keywords: | has-patch has-unit-tests fixed-5.0 |
Focuses: | rest-api | Cc: |
Description (last modified by )
In WordPress/gutenberg#5756, editable permalink previews landed in Gutenberg.
We need to take this same information and expose it on the Posts controller:
function gutenberg_add_permalink_template_to_posts( $response, $post, $request ) { if ( 'edit' !== $request['context'] ) { return $response; } if ( ! function_exists( 'get_sample_permalink' ) ) { require_once ABSPATH . '/wp-admin/includes/post.php'; } $sample_permalink = get_sample_permalink( $post->ID, $post->post_title, '' ); $response->data['permalink_template'] = $sample_permalink[0]; $response->data['generated_slug'] = $sample_permalink[1]; return $response; }
This data should reflect the assumptions already established in edit-form-advanced.php
(e.g. post type must be viewable and public).
Related #41014
Attachments (4)
Change History (22)
#2
in reply to:
↑ 1
;
follow-up:
↓ 3
@
6 years ago
Replying to sanchothefat:
Question: shouldn't this information be part of the Post Types controller responses if not just exposed via
wp_localize_script()
or similar?
The Posts controller seems to make most sense at this point. The value of both permalink_template
and generated_slug
are dependent on the computed value of $post->ID
and $post->post_title
, and could vary from post to post (e.g. through filters).
Also, we want to expose this information through the REST API so that other clients (e.g. mobile apps) have access to it. wp_localize_script()
doesn't provide this for us.
#3
in reply to:
↑ 2
@
6 years ago
Replying to danielbachhuber:
Replying to sanchothefat:
Question: shouldn't this information be part of the Post Types controller responses if not just exposed via
wp_localize_script()
or similar?
The Posts controller seems to make most sense at this point. The value of both
permalink_template
andgenerated_slug
are dependent on the computed value of$post->ID
and$post->post_title
, and could vary from post to post (e.g. through filters).
Also, we want to expose this information through the REST API so that other clients (e.g. mobile apps) have access to it.
wp_localize_script()
doesn't provide this for us.
Thanks, I thought the permalink_template
was the generic value set when registering a post type or via the permalinks settings page.
@
6 years ago
Expose permalink_template and generated_slug from title in the Posts controller with phpunit test.
#5
@
6 years ago
Thanks @rahulsprajapati
A couple points of feedback on the patch:
- The
permalink_template
andgenerated_slug
attributes both need to be defined on the object's schema, but only when the post type is public and viewable. See: - The
permalink_template
andgenerated_slug
attributes should always be added to the response, as they're dynamically removed by the call to$this->filter_response_by_context()
.
#6
follow-up:
↓ 7
@
6 years ago
@rahulsprajapati Looking pretty good!
One last suggestion: it'd be great to have a test that registered a non-public custom post type and asserted neither permalink_template
nor generated_slug
were on the schema and in the Post response.
My hunch is that, with 45017.2.diff, permalink_template
and generated_slug
will be included on the Post response accidentally because they aren't defined in the schema. We'll need to apply the if ( is_post_type_viewable( $post_type_obj ) && $post_type_obj->public ) {
conditional in prepare_item_for_response()
too.
#7
in reply to:
↑ 6
@
6 years ago
- Keywords has-patch needs-refresh has-unit-tests added; needs-patch needs-unit-tests removed
Replying to danielbachhuber:
@rahulsprajapati Looking pretty good!
One last suggestion: it'd be great to have a test that registered a non-public custom post type and asserted neither
permalink_template
norgenerated_slug
were on the schema and in the Post response.
My hunch is that, with 45017.2.diff,
permalink_template
andgenerated_slug
will be included on the Post response accidentally because they aren't defined in the schema. We'll need to apply theif ( is_post_type_viewable( $post_type_obj ) && $post_type_obj->public ) {
conditional inprepare_item_for_response()
too.
Yeah, that is possible - will update this changes soon.
#8
@
6 years ago
- Keywords needs-refresh removed
45017.3.diff
looks great to me. Before we land this, we'll want to:
- Apply the patch to core.
- Comment out the corresponding code in Gutenberg.
- Verify Gutenberg works as expected using this patch.
#9
@
6 years ago
In 45017.4.diff
, I've:
- Cleaned up the tests to be a bit more literal and understandable.
- Fixed the response data inclusion to respect any
?_fields=
defined for the request. - Updated more tests accordingly so the entire suite passes.
- Disabled the assertion with
filter_rest_url_for_leading_slash
when we have pretty permalinks. The origin was WP-API/WP-API#2428 which wasn't designed for tests with pretty permalinks.
I've also verified Gutenberg continues to work as expected with this patch.
This ticket was mentioned in Slack in #core-restapi by danielbachhuber. View the logs.
6 years ago
This ticket was mentioned in Slack in #core by danielbachhuber. View the logs.
6 years ago
#12
@
6 years ago
- Owner set to danielbachhuber
- Resolution set to fixed
- Status changed from new to closed
In 43720:
#13
@
6 years ago
- Keywords fixed-5.0 added
- Resolution fixed deleted
- Status changed from closed to reopened
Reopening for merging to trunk.
Question: shouldn't this information be part of the Post Types controller responses if not just exposed via
wp_localize_script()
or similar?