WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/11/2018 10:24:51 PM (3 years ago)
Author:
danielbachhuber
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.

Props danielbachhuber, rahulsprajapati.
Fixes #45017.

File:
1 edited

Legend:

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

    r43716 r43720  
    15731573                $terms = get_the_terms( $post, $taxonomy->name );
    15741574                $data[ $base ] = $terms ? array_values( wp_list_pluck( $terms, 'term_id' ) ) : array();
     1575            }
     1576        }
     1577
     1578        $post_type_obj = get_post_type_object( $post->post_type );
     1579        if ( is_post_type_viewable( $post_type_obj ) && $post_type_obj->public  ) {
     1580
     1581            if ( ! function_exists( 'get_sample_permalink' ) ) {
     1582                require_once ABSPATH . '/wp-admin/includes/post.php';
     1583            }
     1584
     1585            $sample_permalink = get_sample_permalink( $post->ID, $post->post_title, '' );
     1586
     1587            if ( in_array( 'permalink_template', $fields, true ) ) {
     1588                $data['permalink_template'] = $sample_permalink[0];
     1589            }
     1590            if ( in_array( 'generated_slug', $fields, true ) ) {
     1591                $data['generated_slug'] = $sample_permalink[1];
    15751592            }
    15761593        }
     
    19081925
    19091926        $post_type_obj = get_post_type_object( $this->post_type );
     1927        if ( is_post_type_viewable( $post_type_obj ) && $post_type_obj->public ) {
     1928            $schema['properties']['permalink_template'] = array(
     1929                'description' => __( 'Permalink template for the object.' ),
     1930                'type'        => 'string',
     1931                'context'     => array( 'edit' ),
     1932                'readonly'    => true,
     1933            );
     1934
     1935            $schema['properties']['generated_slug'] = array(
     1936                'description' => __( 'Slug automatically generated from the object title.' ),
     1937                'type'        => 'string',
     1938                'context'     => array( 'edit' ),
     1939                'readonly'    => true,
     1940            );
     1941        }
    19101942
    19111943        if ( $post_type_obj->hierarchical ) {
Note: See TracChangeset for help on using the changeset viewer.