Make WordPress Core

Changeset 43980


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:
6 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 ) {
  • trunk/tests/phpunit/includes/testcase-rest-controller.php

    r42724 r43980  
    4141
    4242    public function filter_rest_url_for_leading_slash( $url, $path ) {
    43         if ( is_multisite() ) {
     43        if ( is_multisite() || get_option( 'permalink_structure' ) ) {
    4444            return $url;
    4545        }
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r43973 r43980  
    12961296        $data       = $response->get_data();
    12971297        $properties = $data['schema']['properties'];
    1298         $this->assertEquals( 24, count( $properties ) );
     1298        $this->assertEquals( 26, count( $properties ) );
    12991299        $this->assertArrayHasKey( 'author', $properties );
    13001300        $this->assertArrayHasKey( 'alt_text', $properties );
     
    13081308        $this->assertArrayHasKey( 'date', $properties );
    13091309        $this->assertArrayHasKey( 'date_gmt', $properties );
     1310        $this->assertArrayHasKey( 'generated_slug', $properties );
    13101311        $this->assertArrayHasKey( 'guid', $properties );
    13111312        $this->assertArrayHasKey( 'id', $properties );
     
    13191320        $this->assertArrayHasKey( 'post', $properties );
    13201321        $this->assertArrayHasKey( 'ping_status', $properties );
     1322        $this->assertArrayHasKey( 'permalink_template', $properties );
    13211323        $this->assertArrayHasKey( 'status', $properties );
    13221324        $this->assertArrayHasKey( 'slug', $properties );
  • trunk/tests/phpunit/tests/rest-api/rest-pages-controller.php

    r43571 r43980  
    679679        $data       = $response->get_data();
    680680        $properties = $data['schema']['properties'];
    681         $this->assertEquals( 22, count( $properties ) );
     681        $this->assertEquals( 24, count( $properties ) );
    682682        $this->assertArrayHasKey( 'author', $properties );
    683683        $this->assertArrayHasKey( 'comment_status', $properties );
     
    685685        $this->assertArrayHasKey( 'date', $properties );
    686686        $this->assertArrayHasKey( 'date_gmt', $properties );
     687        $this->assertArrayHasKey( 'generated_slug', $properties );
    687688        $this->assertArrayHasKey( 'guid', $properties );
    688689        $this->assertArrayHasKey( 'excerpt', $properties );
     
    696697        $this->assertArrayHasKey( 'parent', $properties );
    697698        $this->assertArrayHasKey( 'password', $properties );
     699        $this->assertArrayHasKey( 'permalink_template', $properties );
    698700        $this->assertArrayHasKey( 'ping_status', $properties );
    699701        $this->assertArrayHasKey( 'slug', $properties );
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r43979 r43980  
    34803480        $data       = $response->get_data();
    34813481        $properties = $data['schema']['properties'];
    3482         $this->assertEquals( 24, count( $properties ) );
     3482        $this->assertEquals( 26, count( $properties ) );
    34833483        $this->assertArrayHasKey( 'author', $properties );
    34843484        $this->assertArrayHasKey( 'comment_status', $properties );
     
    34883488        $this->assertArrayHasKey( 'excerpt', $properties );
    34893489        $this->assertArrayHasKey( 'featured_media', $properties );
     3490        $this->assertArrayHasKey( 'generated_slug', $properties );
    34903491        $this->assertArrayHasKey( 'guid', $properties );
    34913492        $this->assertArrayHasKey( 'format', $properties );
     
    34963497        $this->assertArrayHasKey( 'modified_gmt', $properties );
    34973498        $this->assertArrayHasKey( 'password', $properties );
     3499        $this->assertArrayHasKey( 'permalink_template', $properties );
    34983500        $this->assertArrayHasKey( 'ping_status', $properties );
    34993501        $this->assertArrayHasKey( 'slug', $properties );
     
    35653567            'featured_media',
    35663568            'format',
     3569            'generated_slug',
    35673570            'guid',
    35683571            'id',
     
    35723575            'modified_gmt',
    35733576            'password',
     3577            'permalink_template',
    35743578            'ping_status',
    35753579            'slug',
     
    40874091    }
    40884092
     4093    public function test_generated_permalink_template_generated_slug_for_non_viewable_posts() {
     4094        register_post_type(
     4095            'private-post',
     4096            array(
     4097                'label'              => 'Private Posts',
     4098                'supports'           => array( 'title', 'editor', 'author' ),
     4099                'show_in_rest'       => true,
     4100                'publicly_queryable' => false,
     4101                'public'             => true,
     4102                'rest_base'          => 'private-post',
     4103            )
     4104        );
     4105        create_initial_rest_routes();
     4106
     4107        wp_set_current_user( self::$editor_id );
     4108
     4109        $post_id = $this->factory->post->create(
     4110            array(
     4111                'post_title'  => 'Permalink Template',
     4112                'post_type'   => 'private-post',
     4113                'post_status' => 'draft',
     4114            )
     4115        );
     4116
     4117        // Neither 'permalink_template' and 'generated_slug' are expected for this post type.
     4118        $request = new WP_REST_Request( 'GET', '/wp/v2/private-post/' . $post_id );
     4119        $request->set_param( 'context', 'edit' );
     4120        $response = rest_get_server()->dispatch( $request );
     4121        $data     = $response->get_data();
     4122        $this->assertEquals( 200, $response->get_status() );
     4123        $this->assertArrayNotHasKey( 'permalink_template', $data );
     4124        $this->assertArrayNotHasKey( 'generated_slug', $data );
     4125    }
     4126
     4127    public function test_generated_permalink_template_generated_slug_for_posts() {
     4128        $this->set_permalink_structure( '/%postname%/' );
     4129        $expected_permalink_template = trailingslashit( home_url( '/%postname%/' ) );
     4130
     4131        wp_set_current_user( self::$editor_id );
     4132
     4133        $post_id = $this->factory->post->create(
     4134            array(
     4135                'post_title'  => 'Permalink Template',
     4136                'post_type'   => 'post',
     4137                'post_status' => 'draft',
     4138            )
     4139        );
     4140
     4141        // Both 'permalink_template' and 'generated_slug' are expected for context=edit.
     4142        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id );
     4143        $request->set_param( 'context', 'edit' );
     4144        $response = rest_get_server()->dispatch( $request );
     4145        $data     = $response->get_data();
     4146        $this->assertEquals( 200, $response->get_status() );
     4147        $this->assertEquals( $expected_permalink_template, $data['permalink_template'] );
     4148        $this->assertEquals( 'permalink-template', $data['generated_slug'] );
     4149
     4150        // Neither 'permalink_template' and 'generated_slug' are expected for context=view.
     4151        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id );
     4152        $request->set_param( 'context', 'view' );
     4153        $response = rest_get_server()->dispatch( $request );
     4154        $data     = $response->get_data();
     4155        $this->assertEquals( 200, $response->get_status() );
     4156        $this->assertArrayNotHasKey( 'permalink_template', $data );
     4157        $this->assertArrayNotHasKey( 'generated_slug', $data );
     4158
     4159    }
     4160
    40894161    public function tearDown() {
     4162        _unregister_post_type( 'private-post' );
    40904163        _unregister_post_type( 'youseeeme' );
    40914164        if ( isset( $this->attachment_id ) ) {
Note: See TracChangeset for help on using the changeset viewer.