From d296736b75c757a80b5299caaa165aeddbf6fad4 Mon Sep 17 00:00:00 2001 From: "K. Adam White" Date: Wed, 31 Jul 2019 20:00:57 +0000 Subject: [PATCH] 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 --- .../class-wp-rest-posts-controller.php | 23 +++++++++++-------- wp-includes/version.php | 2 +- 2 files changed, 15 insertions(+), 10 deletions(-) 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 eff50e8e75..090ca2a5da 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 @@ -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]; + } } } diff --git a/wp-includes/version.php b/wp-includes/version.php index 1016d6050c..efaaf3a7fd 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.