From bab0d57578812da2e666dd985cfbade79a09ee9e Mon Sep 17 00:00:00 2001 From: danielbachhuber Date: Sat, 13 Oct 2018 03:49:57 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/branches/5.0@43720 git-svn-id: http://core.svn.wordpress.org/branches/5.0@43549 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../class-wp-rest-posts-controller.php | 32 +++++++++++++++++++ wp-includes/version.php | 2 +- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index 42ceec93d8..3465271a11 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -1575,6 +1575,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 ); @@ -1907,6 +1924,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( diff --git a/wp-includes/version.php b/wp-includes/version.php index bfc85fbc01..a1feebf9bf 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '5.0-alpha-43719'; +$wp_version = '5.0-alpha-43720'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.