WordPress.org

Make WordPress Core

Changeset 43720


Ignore:
Timestamp:
10/11/2018 10:24:51 PM (13 months 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.

Location:
branches/5.0
Files:
5 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 ) {
  • branches/5.0/tests/phpunit/includes/testcase-rest-controller.php

    r39177 r43720  
    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        }
  • branches/5.0/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r43681 r43720  
    11151115        $data = $response->get_data();
    11161116        $properties = $data['schema']['properties'];
    1117         $this->assertEquals( 24, count( $properties ) );
     1117        $this->assertEquals( 26, count( $properties ) );
    11181118        $this->assertArrayHasKey( 'author', $properties );
    11191119        $this->assertArrayHasKey( 'alt_text', $properties );
     
    11271127        $this->assertArrayHasKey( 'date', $properties );
    11281128        $this->assertArrayHasKey( 'date_gmt', $properties );
     1129        $this->assertArrayHasKey( 'generated_slug', $properties );
    11291130        $this->assertArrayHasKey( 'guid', $properties );
    11301131        $this->assertArrayHasKey( 'id', $properties );
     
    11381139        $this->assertArrayHasKey( 'post', $properties );
    11391140        $this->assertArrayHasKey( 'ping_status', $properties );
     1141        $this->assertArrayHasKey( 'permalink_template', $properties );
    11401142        $this->assertArrayHasKey( 'status', $properties );
    11411143        $this->assertArrayHasKey( 'slug', $properties );
  • branches/5.0/tests/phpunit/tests/rest-api/rest-pages-controller.php

    r43445 r43720  
    526526        $data = $response->get_data();
    527527        $properties = $data['schema']['properties'];
    528         $this->assertEquals( 22, count( $properties ) );
     528        $this->assertEquals( 24, count( $properties ) );
    529529        $this->assertArrayHasKey( 'author', $properties );
    530530        $this->assertArrayHasKey( 'comment_status', $properties );
     
    532532        $this->assertArrayHasKey( 'date', $properties );
    533533        $this->assertArrayHasKey( 'date_gmt', $properties );
     534        $this->assertArrayHasKey( 'generated_slug', $properties );
    534535        $this->assertArrayHasKey( 'guid', $properties );
    535536        $this->assertArrayHasKey( 'excerpt', $properties );
     
    543544        $this->assertArrayHasKey( 'parent', $properties );
    544545        $this->assertArrayHasKey( 'password', $properties );
     546        $this->assertArrayHasKey( 'permalink_template', $properties );
    545547        $this->assertArrayHasKey( 'ping_status', $properties );
    546548        $this->assertArrayHasKey( 'slug', $properties );
  • 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.