Make WordPress Core

Ticket #45017: 45017.3.diff

File 45017.3.diff, 6.5 KB (added by rahulsprajapati, 7 years ago)

Resolved feedback from comment:6

  • tests/phpunit/tests/rest-api/rest-posts-controller.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    39923992                $this->assertArrayHasKey( 'https://api.w.org/action-assign-tags', $links );
    39933993        }
    39943994
     3995        public function test_generated_permalink_template_and_slug_for_non_viewable_posts() {
     3996
     3997                $post_type = 'private-post';
     3998
     3999                register_post_type(
     4000                        $post_type,
     4001                        array(
     4002                                'label'              => 'Private Posts',
     4003                                'supports'           => array( 'title', 'editor', 'author' ),
     4004                                'show_in_rest'       => true,
     4005                                'publicly_queryable' => false,
     4006                                'public'             => true,
     4007                                'rest_base'          => 'private-post',
     4008                        )
     4009                );
     4010
     4011                create_initial_rest_routes();
     4012
     4013                $this->generated_permalink_template_and_slug_tests( $post_type );
     4014
     4015                _unregister_post_type( 'private-post' );
     4016
     4017        }
     4018
     4019        public function test_generated_permalink_template_and_slug_for_non_public_posts() {
     4020
     4021                $post_type = 'private-post';
     4022
     4023                register_post_type(
     4024                        $post_type,
     4025                        array(
     4026                                'label'        => 'Private Posts',
     4027                                'supports'     => array( 'title', 'editor', 'author' ),
     4028                                'show_in_rest' => true,
     4029                                'public'       => false,
     4030                                'rest_base'    => 'private-post',
     4031                        )
     4032                );
     4033
     4034                create_initial_rest_routes();
     4035
     4036                $this->generated_permalink_template_and_slug_tests( $post_type );
     4037
     4038                _unregister_post_type( 'private-post' );
     4039
     4040        }
     4041
     4042        public function test_generated_permalink_template_and_slug_for_edit_posts() {
     4043
     4044                $this->generated_permalink_template_and_slug_tests( 'post' );
     4045        }
     4046
     4047        public function generated_permalink_template_and_slug_tests( $post_type ) {
     4048
     4049                $permalink_structure         = '%postname%';
     4050                $expected_permalink_template = trailingslashit( home_url( $permalink_structure ) );
     4051                $this->set_permalink_structure( "/$permalink_structure/" );
     4052
     4053                $post_type_obj = get_post_type_object( $post_type );
     4054
     4055                wp_set_current_user( self::$editor_id );
     4056
     4057                $post_id = $this->factory->post->create(
     4058                        array(
     4059                                'post_title' => 'Permalink Template',
     4060                                'post_type'  => $post_type,
     4061                        )
     4062                );
     4063
     4064                $post_endpoint_route = sprintf( '/wp/v2/%s/%d', $post_type_obj->rest_base, $post_id );
     4065
     4066                $request = new WP_REST_Request( 'GET', $post_endpoint_route );
     4067                $request->set_param( 'context', 'edit' );
     4068                $response = rest_get_server()->dispatch( $request );
     4069                $this->check_get_post_response( $response, 'edit' );
     4070                $read_data = $response->get_data();
     4071
     4072
     4073
     4074                if ( is_post_type_viewable( $post_type_obj ) && $post_type_obj->public  ) {
     4075
     4076                        $this->assertArrayHasKey( 'permalink_template', $read_data );
     4077                        $this->assertArrayHasKey( 'generated_slug', $read_data );
     4078
     4079                        $this->assertEquals( $expected_permalink_template, $read_data['permalink_template'] );
     4080                        $this->assertEquals( 'permalink-template', $read_data['generated_slug'] );
     4081                } else {
     4082
     4083                        $this->assertArrayNotHasKey( 'permalink_template', $read_data );
     4084                        $this->assertArrayNotHasKey( 'generated_slug', $read_data );
     4085                }
     4086
     4087                $request = new WP_REST_Request( 'POST', $post_endpoint_route );
     4088                $params  = array(
     4089                        'title' => 'Permalink Template Slug',
     4090                );
     4091                $request->set_body_params( $params );
     4092                $response     = rest_get_server()->dispatch( $request );
     4093                $updated_data = $response->get_data();
     4094
     4095                if ( is_post_type_viewable( $post_type_obj ) && $post_type_obj->public  ) {
     4096
     4097                        $this->assertArrayHasKey( 'permalink_template', $updated_data );
     4098                        $this->assertArrayHasKey( 'generated_slug', $updated_data );
     4099                        $this->assertArrayHasKey( 'slug', $updated_data );
     4100
     4101                        $this->assertEquals( $expected_permalink_template, $updated_data['permalink_template'] );
     4102                        $this->assertEquals( 'permalink-template-slug', $updated_data['generated_slug'] );
     4103                        $this->assertEquals( 'permalink-template', $updated_data['slug'] );
     4104                } else {
     4105
     4106                        $this->assertArrayNotHasKey( 'permalink_template', $updated_data );
     4107                        $this->assertArrayNotHasKey( 'generated_slug', $updated_data );
     4108                }
     4109
     4110                $request  = new WP_REST_Request( 'GET', $post_endpoint_route );
     4111                $response = rest_get_server()->dispatch( $request );
     4112                $this->check_get_post_response( $response, 'view' );
     4113                $read_data = $response->get_data();
     4114
     4115                $this->assertArrayNotHasKey( 'permalink_template', $read_data );
     4116                $this->assertArrayNotHasKey( 'generated_slug', $read_data );
     4117        }
     4118
    39954119        public function tearDown() {
    39964120                _unregister_post_type( 'youseeeme' );
    39974121                if ( isset( $this->attachment_id ) ) {
  • src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    15871587                        }
    15881588                }
    15891589
     1590                $post_type_obj = get_post_type_object( $post->post_type );
     1591
     1592                if ( is_post_type_viewable( $post_type_obj ) && $post_type_obj->public  ) {
     1593
     1594                        if ( ! function_exists( 'get_sample_permalink' ) ) {
     1595                                require_once ABSPATH . '/wp-admin/includes/post.php';
     1596                        }
     1597
     1598                        $sample_permalink = get_sample_permalink( $post->ID, $post->post_title, '' );
     1599
     1600                        $data['permalink_template'] = $sample_permalink[0];
     1601                        $data['generated_slug']     = $sample_permalink[1];
     1602                }
     1603
    15901604                $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
    15911605                $data    = $this->add_additional_fields_to_object( $data, $request );
    15921606                $data    = $this->filter_response_by_context( $data, $context );
     
    19151929
    19161930                $post_type_obj = get_post_type_object( $this->post_type );
    19171931
     1932                if ( is_post_type_viewable( $post_type_obj ) && $post_type_obj->public ) {
     1933
     1934                        $schema['properties']['permalink_template'] = array(
     1935                                'description' => __( 'Permalink template for the object.' ),
     1936                                'type'        => 'string',
     1937                                'context'     => array( 'edit' ),
     1938                                'readonly'    => true,
     1939                        );
     1940
     1941                        $schema['properties']['generated_slug'] = array(
     1942                                'description' => __( 'Autogenerated slug from post title.' ),
     1943                                'type'        => 'string',
     1944                                'context'     => array( 'edit' ),
     1945                                'readonly'    => true,
     1946                        );
     1947                }
     1948
    19181949                if ( $post_type_obj->hierarchical ) {
    19191950                        $schema['properties']['parent'] = array(
    19201951                                'description' => __( 'The ID for the parent of the object.' ),