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.

Built from https://develop.svn.wordpress.org/trunk@43980


git-svn-id: http://core.svn.wordpress.org/trunk@43812 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Jeremy Felt 2018-12-12 02:12:24 +00:00
parent a36830c4cb
commit a0446fdfe8
2 changed files with 33 additions and 1 deletions

View File

@ -1587,6 +1587,23 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
}
}
$post_type_obj = get_post_type_object( $post->post_type );
if ( is_post_type_viewable( $post_type_obj ) && $post_type_obj->public ) {
if ( ! function_exists( 'get_sample_permalink' ) ) {
require_once ABSPATH . '/wp-admin/includes/post.php';
}
$sample_permalink = get_sample_permalink( $post->ID, $post->post_title, '' );
if ( in_array( 'permalink_template', $fields, true ) ) {
$data['permalink_template'] = $sample_permalink[0];
}
if ( in_array( 'generated_slug', $fields, true ) ) {
$data['generated_slug'] = $sample_permalink[1];
}
}
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
@ -1918,6 +1935,21 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
);
$post_type_obj = get_post_type_object( $this->post_type );
if ( is_post_type_viewable( $post_type_obj ) && $post_type_obj->public ) {
$schema['properties']['permalink_template'] = array(
'description' => __( 'Permalink template for the object.' ),
'type' => 'string',
'context' => array( 'edit' ),
'readonly' => true,
);
$schema['properties']['generated_slug'] = array(
'description' => __( 'Slug automatically generated from the object title.' ),
'type' => 'string',
'context' => array( 'edit' ),
'readonly' => true,
);
}
if ( $post_type_obj->hierarchical ) {
$schema['properties']['parent'] = array(

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.1-alpha-43979';
$wp_version = '5.1-alpha-43980';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.