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/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.