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/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r43694 r43720  
    30953095        $data = $response->get_data();
    30963096        $properties = $data['schema']['properties'];
    3097         $this->assertEquals( 24, count( $properties ) );
     3097        $this->assertEquals( 26, count( $properties ) );
    30983098        $this->assertArrayHasKey( 'author', $properties );
    30993099        $this->assertArrayHasKey( 'comment_status', $properties );
     
    31033103        $this->assertArrayHasKey( 'excerpt', $properties );
    31043104        $this->assertArrayHasKey( 'featured_media', $properties );
     3105        $this->assertArrayHasKey( 'generated_slug', $properties );
    31053106        $this->assertArrayHasKey( 'guid', $properties );
    31063107        $this->assertArrayHasKey( 'format', $properties );
     
    31113112        $this->assertArrayHasKey( 'modified_gmt', $properties );
    31123113        $this->assertArrayHasKey( 'password', $properties );
     3114        $this->assertArrayHasKey( 'permalink_template', $properties );
    31133115        $this->assertArrayHasKey( 'ping_status', $properties );
    31143116        $this->assertArrayHasKey( 'slug', $properties );
     
    31803182            'featured_media',
    31813183            'format',
     3184            'generated_slug',
    31823185            'guid',
    31833186            'id',
     
    31873190            'modified_gmt',
    31883191            'password',
     3192            'permalink_template',
    31893193            'ping_status',
    31903194            'slug',
     
    36853689    }
    36863690
     3691    public function test_generated_permalink_template_generated_slug_for_non_viewable_posts() {
     3692        register_post_type(
     3693            'private-post',
     3694            array(
     3695                'label'              => 'Private Posts',
     3696                'supports'           => array( 'title', 'editor', 'author' ),
     3697                'show_in_rest'       => true,
     3698                'publicly_queryable' => false,
     3699                'public'             => true,
     3700                'rest_base'          => 'private-post',
     3701            )
     3702        );
     3703        create_initial_rest_routes();
     3704
     3705        wp_set_current_user( self::$editor_id );
     3706
     3707        $post_id = $this->factory->post->create(
     3708            array(
     3709                'post_title'  => 'Permalink Template',
     3710                'post_type'   => 'private-post',
     3711                'post_status' => 'draft',
     3712            )
     3713        );
     3714
     3715        // Neither 'permalink_template' and 'generated_slug' are expected for this post type.
     3716        $request = new WP_REST_Request( 'GET', '/wp/v2/private-post/' . $post_id );
     3717        $request->set_param( 'context', 'edit' );
     3718        $response = rest_get_server()->dispatch( $request );
     3719        $data     = $response->get_data();
     3720        $this->assertEquals( 200, $response->get_status() );
     3721        $this->assertArrayNotHasKey( 'permalink_template', $data );
     3722        $this->assertArrayNotHasKey( 'generated_slug', $data );
     3723    }
     3724
     3725    public function test_generated_permalink_template_generated_slug_for_posts() {
     3726        $this->set_permalink_structure( '/%postname%/' );
     3727        $expected_permalink_template = trailingslashit( home_url( '/%postname%/' ) );
     3728
     3729        wp_set_current_user( self::$editor_id );
     3730
     3731        $post_id = $this->factory->post->create(
     3732            array(
     3733                'post_title'  => 'Permalink Template',
     3734                'post_type'   => 'post',
     3735                'post_status' => 'draft',
     3736            )
     3737        );
     3738
     3739        // Both 'permalink_template' and 'generated_slug' are expected for context=edit.
     3740        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id );
     3741        $request->set_param( 'context', 'edit' );
     3742        $response = rest_get_server()->dispatch( $request );
     3743        $data     = $response->get_data();
     3744        $this->assertEquals( 200, $response->get_status() );
     3745        $this->assertEquals( $expected_permalink_template, $data['permalink_template'] );
     3746        $this->assertEquals( 'permalink-template', $data['generated_slug'] );
     3747
     3748        // Neither 'permalink_template' and 'generated_slug' are expected for context=view.
     3749        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id );
     3750        $request->set_param( 'context', 'view' );
     3751        $response = rest_get_server()->dispatch( $request );
     3752        $data     = $response->get_data();
     3753        $this->assertEquals( 200, $response->get_status() );
     3754        $this->assertArrayNotHasKey( 'permalink_template', $data );
     3755        $this->assertArrayNotHasKey( 'generated_slug', $data );
     3756
     3757    }
     3758
    36873759    public function tearDown() {
     3760        _unregister_post_type( 'private-post' );
    36883761        _unregister_post_type( 'youseeeme' );
    36893762        if ( isset( $this->attachment_id ) ) {
Note: See TracChangeset for help on using the changeset viewer.