Make WordPress Core


Ignore:
Timestamp:
12/12/2018 02:11:22 AM (6 years ago)
Author:
jeremyfelt
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

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

    r43979 r43980  
    15881588        }
    15891589
     1590        $post_type_obj = get_post_type_object( $post->post_type );
     1591        if ( is_post_type_viewable( $post_type_obj ) && $post_type_obj->public ) {
     1592
     1593            if ( ! function_exists( 'get_sample_permalink' ) ) {
     1594                require_once ABSPATH . '/wp-admin/includes/post.php';
     1595            }
     1596
     1597            $sample_permalink = get_sample_permalink( $post->ID, $post->post_title, '' );
     1598
     1599            if ( in_array( 'permalink_template', $fields, true ) ) {
     1600                $data['permalink_template'] = $sample_permalink[0];
     1601            }
     1602            if ( in_array( 'generated_slug', $fields, true ) ) {
     1603                $data['generated_slug'] = $sample_permalink[1];
     1604            }
     1605        }
     1606
    15901607        $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
    15911608        $data    = $this->add_additional_fields_to_object( $data, $request );
     
    19191936
    19201937        $post_type_obj = get_post_type_object( $this->post_type );
     1938        if ( is_post_type_viewable( $post_type_obj ) && $post_type_obj->public ) {
     1939            $schema['properties']['permalink_template'] = array(
     1940                'description' => __( 'Permalink template for the object.' ),
     1941                'type'        => 'string',
     1942                'context'     => array( 'edit' ),
     1943                'readonly'    => true,
     1944            );
     1945
     1946            $schema['properties']['generated_slug'] = array(
     1947                'description' => __( 'Slug automatically generated from the object title.' ),
     1948                'type'        => 'string',
     1949                'context'     => array( 'edit' ),
     1950                'readonly'    => true,
     1951            );
     1952        }
    19211953
    19221954        if ( $post_type_obj->hierarchical ) {
Note: See TracChangeset for help on using the changeset viewer.