WordPress.org

Make WordPress Core

Changeset 45705


Ignore:
Timestamp:
07/31/2019 07:59:30 PM (2 years ago)
Author:
kadamwhite
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r45590 r45705  
    16141614        $post_type_obj = get_post_type_object( $post->post_type );
    16151615        if ( is_post_type_viewable( $post_type_obj ) && $post_type_obj->public ) {
    1616 
    1617             if ( ! function_exists( 'get_sample_permalink' ) ) {
    1618                 require_once ABSPATH . 'wp-admin/includes/post.php';
    1619             }
    1620 
    1621             $sample_permalink = get_sample_permalink( $post->ID, $post->post_title, '' );
    1622 
    1623             if ( in_array( 'permalink_template', $fields, true ) ) {
    1624                 $data['permalink_template'] = $sample_permalink[0];
    1625             }
    1626             if ( in_array( 'generated_slug', $fields, true ) ) {
    1627                 $data['generated_slug'] = $sample_permalink[1];
     1616            $permalink_template_requested = in_array( 'permalink_template', $fields, true );
     1617            $generated_slug_requested     = in_array( 'generated_slug', $fields, true );
     1618
     1619            if ( $permalink_template_requested || $generated_slug_requested ) {
     1620                if ( ! function_exists( 'get_sample_permalink' ) ) {
     1621                    require_once ABSPATH . 'wp-admin/includes/post.php';
     1622                }
     1623
     1624                $sample_permalink = get_sample_permalink( $post->ID, $post->post_title, '' );
     1625
     1626                if ( $permalink_template_requested ) {
     1627                    $data['permalink_template'] = $sample_permalink[0];
     1628                }
     1629
     1630                if ( $generated_slug_requested ) {
     1631                    $data['generated_slug'] = $sample_permalink[1];
     1632                }
    16281633            }
    16291634        }
Note: See TracChangeset for help on using the changeset viewer.