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. git-svn-id: https://develop.svn.wordpress.org/trunk@45705 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
57443a60da
commit
7aae0cfe56
@ -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];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user