Make WordPress Core

Opened 6 years ago

Closed 6 years ago

Last modified 3 years ago

#45017 closed enhancement (fixed)

REST API: Expose permalink_template in the Posts controller

Reported by: danielbachhuber's profile danielbachhuber Owned by: danielbachhuber's profile 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 danielbachhuber)

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)

45017.diff (3.7 KB) - added by rahulsprajapati 6 years ago.
Expose permalink_template and generated_slug from title in the Posts controller with phpunit test.
45017.2.diff (4.5 KB) - added by rahulsprajapati 6 years ago.
Resolved feedback from comment #5.
45017.3.diff (6.5 KB) - added by rahulsprajapati 6 years ago.
Resolved feedback from comment:6
45017.4.diff (11.1 KB) - added by danielbachhuber 6 years ago.

Download all attachments as: .zip

Change History (22)

#1 follow-up: @sanchothefat
6 years ago

Question: shouldn't this information be part of the Post Types controller responses if not just exposed via wp_localize_script() or similar?

#2 in reply to: ↑ 1 ; follow-up: @danielbachhuber
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 @sanchothefat
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 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.

Thanks, I thought the permalink_template was the generic value set when registering a post type or via the permalinks settings page.

#4 @danielbachhuber
6 years ago

  • Description modified (diff)

@rahulsprajapati
6 years ago

Expose permalink_template and generated_slug from title in the Posts controller with phpunit test.

#5 @danielbachhuber
6 years ago

Thanks @rahulsprajapati

A couple points of feedback on the patch:

  1. The permalink_template and generated_slug attributes both need to be defined on the object's schema, but only when the post type is public and viewable. See:
  2. The permalink_template and generated_slug attributes should always be added to the response, as they're dynamically removed by the call to $this->filter_response_by_context().

@rahulsprajapati
6 years ago

Resolved feedback from comment #5.

#6 follow-up: @danielbachhuber
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 @rahulsprajapati
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 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.

Yeah, that is possible - will update this changes soon.

@rahulsprajapati
6 years ago

Resolved feedback from comment:6

#8 @danielbachhuber
6 years ago

  • Keywords needs-refresh removed

45017.3.diff looks great to me. Before we land this, we'll want to:

  1. Apply the patch to core.
  2. Comment out the corresponding code in Gutenberg.
  3. Verify Gutenberg works as expected using this patch.

#9 @danielbachhuber
6 years ago

In 45017.4.diff, I've:

  1. Cleaned up the tests to be a bit more literal and understandable.
  2. Fixed the response data inclusion to respect any ?_fields= defined for the request.
  3. Updated more tests accordingly so the entire suite passes.
  4. 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 @danielbachhuber
6 years ago

  • Owner set to danielbachhuber
  • Resolution set to fixed
  • Status changed from new to closed

In 43720:

REST API: Include permalink_template/generated_slug for Posts

In order for clients to present permalink previews, the REST API must share the computed results of get_sample_permalink(). These two values are now exposed as permalink_template and generated_slug for public, viewable post types, but only for context=edit.

Props danielbachhuber, rahulsprajapati.
Fixes #45017.

#13 @danielbachhuber
6 years ago

  • Keywords fixed-5.0 added
  • Resolution fixed deleted
  • Status changed from closed to reopened

Reopening for merging to trunk.

#14 @danielbachhuber
6 years ago

In 43766:

REST API: Ensure rest_url() consistently has leading slash.

rest_url() inconsistent addes slashes to the passed path depending on whether the site has pretty permalinks enabled. Apart from being inconsistent, this also caused the unit tests to fail when pretty permalinks are enabled.

Props frank-klein.
Merges [42250] to the 5.0 branch.
Partially reverts [43720].
Fixes #42452. See #41451, #45017.

#15 @ocean90
6 years ago

#45364 was marked as a duplicate.

#16 @jeremyfelt
6 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 43980:

REST API: Include permalink_template/generated_slug for Posts

In order for clients to present permalink previews, the REST API must share the computed results of get_sample_permalink(). These two values are now exposed as permalink_template and generated_slug for public, viewable post types, but only for context=edit.

Merges [43720] to trunk.

Props danielbachhuber, rahulsprajapati.
Fixes #45017.

This ticket was mentioned in Slack in #core-restapi by earnjam. View the logs.


6 years ago

This ticket was mentioned in Slack in #core by ntsekouras. View the logs.


3 years ago

Note: See TracTickets for help on using tickets.