From a0446fdfe87aac0e3c08d840de165bd95edb7881 Mon Sep 17 00:00:00 2001 From: Jeremy Felt Date: Wed, 12 Dec 2018 02:12:24 +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. 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 --- .../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 875b0518eb..93b6608e7a 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 @@ -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( diff --git a/wp-includes/version.php b/wp-includes/version.php index 23f2d15481..2b02a833c7 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.