REST API: Generate sample permalink only when a dependent field is requested.

The sample permalink will now only be generated if the derivative `permalink_template` or `generated_slug` fields are to be included in the response, preventing an unnecessary database request for each post (via `wp_unique_post_slug()`) when those fields are not requested.

Props dlh.
See #45605.

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


git-svn-id: http://core.svn.wordpress.org/trunk@45516 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
K. Adam White 2019-07-31 20:00:57 +00:00
parent ee23869c68
commit d296736b75
2 changed files with 15 additions and 10 deletions

View File

@ -1613,18 +1613,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 ) {
$permalink_template_requested = in_array( 'permalink_template', $fields, true );
$generated_slug_requested = in_array( 'generated_slug', $fields, true );
if ( ! function_exists( 'get_sample_permalink' ) ) {
require_once ABSPATH . 'wp-admin/includes/post.php';
}
if ( $permalink_template_requested || $generated_slug_requested ) {
if ( ! function_exists( 'get_sample_permalink' ) ) {
require_once ABSPATH . 'wp-admin/includes/post.php';
}
$sample_permalink = get_sample_permalink( $post->ID, $post->post_title, '' );
$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];
if ( $permalink_template_requested ) {
$data['permalink_template'] = $sample_permalink[0];
}
if ( $generated_slug_requested ) {
$data['generated_slug'] = $sample_permalink[1];
}
}
}

View File

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