Ticket #45017: 45017.4.diff
File 45017.4.diff, 11.1 KB (added by , 7 years ago) |
---|
-
src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index 42ceec93d8..3465271a11 100644
a b class WP_REST_Posts_Controller extends WP_REST_Controller { 1575 1575 } 1576 1576 } 1577 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]; 1592 } 1593 } 1594 1578 1595 $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 1579 1596 $data = $this->add_additional_fields_to_object( $data, $request ); 1580 1597 $data = $this->filter_response_by_context( $data, $context ); … … class WP_REST_Posts_Controller extends WP_REST_Controller { 1907 1924 ); 1908 1925 1909 1926 $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 } 1910 1942 1911 1943 if ( $post_type_obj->hierarchical ) { 1912 1944 $schema['properties']['parent'] = array( -
tests/phpunit/includes/testcase-rest-controller.php
diff --git a/tests/phpunit/includes/testcase-rest-controller.php b/tests/phpunit/includes/testcase-rest-controller.php index 04e1cf2f96..3087bcd7b7 100644
a b abstract class WP_Test_REST_Controller_Testcase extends WP_Test_REST_TestCase { 40 40 abstract public function test_get_item_schema(); 41 41 42 42 public function filter_rest_url_for_leading_slash( $url, $path ) { 43 if ( is_multisite() ) {43 if ( is_multisite() || get_option( 'permalink_structure' ) ) { 44 44 return $url; 45 45 } 46 46 -
tests/phpunit/tests/rest-api/rest-attachments-controller.php
diff --git a/tests/phpunit/tests/rest-api/rest-attachments-controller.php b/tests/phpunit/tests/rest-api/rest-attachments-controller.php index d17dfdf20b..a5ca6b8c74 100644
a b class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control 1114 1114 $response = $this->server->dispatch( $request ); 1115 1115 $data = $response->get_data(); 1116 1116 $properties = $data['schema']['properties']; 1117 $this->assertEquals( 2 4, count( $properties ) );1117 $this->assertEquals( 26, count( $properties ) ); 1118 1118 $this->assertArrayHasKey( 'author', $properties ); 1119 1119 $this->assertArrayHasKey( 'alt_text', $properties ); 1120 1120 $this->assertArrayHasKey( 'caption', $properties ); … … class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control 1126 1126 $this->assertArrayHasKey( 'comment_status', $properties ); 1127 1127 $this->assertArrayHasKey( 'date', $properties ); 1128 1128 $this->assertArrayHasKey( 'date_gmt', $properties ); 1129 $this->assertArrayHasKey( 'generated_slug', $properties ); 1129 1130 $this->assertArrayHasKey( 'guid', $properties ); 1130 1131 $this->assertArrayHasKey( 'id', $properties ); 1131 1132 $this->assertArrayHasKey( 'link', $properties ); … … class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control 1137 1138 $this->assertArrayHasKey( 'modified_gmt', $properties ); 1138 1139 $this->assertArrayHasKey( 'post', $properties ); 1139 1140 $this->assertArrayHasKey( 'ping_status', $properties ); 1141 $this->assertArrayHasKey( 'permalink_template', $properties ); 1140 1142 $this->assertArrayHasKey( 'status', $properties ); 1141 1143 $this->assertArrayHasKey( 'slug', $properties ); 1142 1144 $this->assertArrayHasKey( 'source_url', $properties ); -
tests/phpunit/tests/rest-api/rest-pages-controller.php
diff --git a/tests/phpunit/tests/rest-api/rest-pages-controller.php b/tests/phpunit/tests/rest-api/rest-pages-controller.php index 6df9d968dc..0b02ad138b 100644
a b class WP_Test_REST_Pages_Controller extends WP_Test_REST_Post_Type_Controller_Te 525 525 $response = $this->server->dispatch( $request ); 526 526 $data = $response->get_data(); 527 527 $properties = $data['schema']['properties']; 528 $this->assertEquals( 2 2, count( $properties ) );528 $this->assertEquals( 24, count( $properties ) ); 529 529 $this->assertArrayHasKey( 'author', $properties ); 530 530 $this->assertArrayHasKey( 'comment_status', $properties ); 531 531 $this->assertArrayHasKey( 'content', $properties ); 532 532 $this->assertArrayHasKey( 'date', $properties ); 533 533 $this->assertArrayHasKey( 'date_gmt', $properties ); 534 $this->assertArrayHasKey( 'generated_slug', $properties ); 534 535 $this->assertArrayHasKey( 'guid', $properties ); 535 536 $this->assertArrayHasKey( 'excerpt', $properties ); 536 537 $this->assertArrayHasKey( 'featured_media', $properties ); … … class WP_Test_REST_Pages_Controller extends WP_Test_REST_Post_Type_Controller_Te 542 543 $this->assertArrayHasKey( 'modified_gmt', $properties ); 543 544 $this->assertArrayHasKey( 'parent', $properties ); 544 545 $this->assertArrayHasKey( 'password', $properties ); 546 $this->assertArrayHasKey( 'permalink_template', $properties ); 545 547 $this->assertArrayHasKey( 'ping_status', $properties ); 546 548 $this->assertArrayHasKey( 'slug', $properties ); 547 549 $this->assertArrayHasKey( 'status', $properties ); -
tests/phpunit/tests/rest-api/rest-posts-controller.php
diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index ec38bd7a85..a51fa59d13 100644
a b class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 3094 3094 $response = $this->server->dispatch( $request ); 3095 3095 $data = $response->get_data(); 3096 3096 $properties = $data['schema']['properties']; 3097 $this->assertEquals( 2 4, count( $properties ) );3097 $this->assertEquals( 26, count( $properties ) ); 3098 3098 $this->assertArrayHasKey( 'author', $properties ); 3099 3099 $this->assertArrayHasKey( 'comment_status', $properties ); 3100 3100 $this->assertArrayHasKey( 'content', $properties ); … … class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 3102 3102 $this->assertArrayHasKey( 'date_gmt', $properties ); 3103 3103 $this->assertArrayHasKey( 'excerpt', $properties ); 3104 3104 $this->assertArrayHasKey( 'featured_media', $properties ); 3105 $this->assertArrayHasKey( 'generated_slug', $properties ); 3105 3106 $this->assertArrayHasKey( 'guid', $properties ); 3106 3107 $this->assertArrayHasKey( 'format', $properties ); 3107 3108 $this->assertArrayHasKey( 'id', $properties ); … … class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 3110 3111 $this->assertArrayHasKey( 'modified', $properties ); 3111 3112 $this->assertArrayHasKey( 'modified_gmt', $properties ); 3112 3113 $this->assertArrayHasKey( 'password', $properties ); 3114 $this->assertArrayHasKey( 'permalink_template', $properties ); 3113 3115 $this->assertArrayHasKey( 'ping_status', $properties ); 3114 3116 $this->assertArrayHasKey( 'slug', $properties ); 3115 3117 $this->assertArrayHasKey( 'status', $properties ); … … class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 3179 3181 'excerpt', 3180 3182 'featured_media', 3181 3183 'format', 3184 'generated_slug', 3182 3185 'guid', 3183 3186 'id', 3184 3187 'link', … … class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 3186 3189 'modified', 3187 3190 'modified_gmt', 3188 3191 'password', 3192 'permalink_template', 3189 3193 'ping_status', 3190 3194 'slug', 3191 3195 'status', … … class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 3684 3688 $this->assertArrayNotHasKey( 'https://api.w.org/action-unfiltered-html', $links ); 3685 3689 } 3686 3690 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 3687 3759 public function tearDown() { 3760 _unregister_post_type( 'private-post' ); 3688 3761 _unregister_post_type( 'youseeeme' ); 3689 3762 if ( isset( $this->attachment_id ) ) { 3690 3763 $this->remove_added_uploads();