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 b7b2dd79f6..41c95e946c 100644
a
|
b
|
class WP_REST_Posts_Controller extends WP_REST_Controller { |
1479 | 1479 | $data['link'] = get_permalink( $post->ID ); |
1480 | 1480 | } |
1481 | 1481 | |
| 1482 | if ( in_array( 'preview_link', $fields, true ) ) { |
| 1483 | $data['preview_link'] = get_preview_post_link( $post ); |
| 1484 | } |
| 1485 | |
1482 | 1486 | if ( in_array( 'title', $fields, true ) ) { |
1483 | 1487 | add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) ); |
1484 | 1488 | |
… |
… |
class WP_REST_Posts_Controller extends WP_REST_Controller { |
1841 | 1845 | ); |
1842 | 1846 | } |
1843 | 1847 | |
| 1848 | if ( is_post_type_viewable( $this->post_type ) ) { |
| 1849 | $schema['properties']['preview_link'] = array( |
| 1850 | 'description' => __( 'Preview URL for the object.' ), |
| 1851 | 'type' => 'string', |
| 1852 | 'format' => 'uri', |
| 1853 | 'context' => array( 'edit' ), |
| 1854 | 'readonly' => true, |
| 1855 | ); |
| 1856 | } |
| 1857 | |
1844 | 1858 | $post_type_attributes = array( |
1845 | 1859 | 'title', |
1846 | 1860 | 'editor', |
diff --git a/tests/phpunit/includes/testcase-rest-post-type-controller.php b/tests/phpunit/includes/testcase-rest-post-type-controller.php
index 1336074840..900b8e6772 100644
a
|
b
|
abstract class WP_Test_REST_Post_Type_Controller_Testcase extends WP_Test_REST_C |
49 | 49 | $this->assertFalse( isset( $data['parent'] ) ); |
50 | 50 | } |
51 | 51 | |
| 52 | // preview_link |
| 53 | if ( is_post_type_viewable( $post_type_obj->name ) && 'edit' === $context ) { |
| 54 | $this->assertEquals( get_preview_post_link( $post ), $data['preview_link'] ); |
| 55 | } else { |
| 56 | $this->assertFalse( isset( $data['preview_link'] ) ); |
| 57 | } |
| 58 | |
52 | 59 | // page attributes |
53 | 60 | if ( $post_type_obj->hierarchical && post_type_supports( $post->post_type, 'page-attributes' ) ) { |
54 | 61 | $this->assertEquals( $post->menu_order, $data['menu_order'] ); |
diff --git a/tests/phpunit/tests/rest-api/rest-pages-controller.php b/tests/phpunit/tests/rest-api/rest-pages-controller.php
index dcc7c15297..67fb1bb5cb 100644
a
|
b
|
class WP_Test_REST_Pages_Controller extends WP_Test_REST_Post_Type_Controller_Te |
673 | 673 | $response = rest_get_server()->dispatch( $request ); |
674 | 674 | $data = $response->get_data(); |
675 | 675 | $properties = $data['schema']['properties']; |
676 | | $this->assertEquals( 22, count( $properties ) ); |
| 676 | $this->assertEquals( 23, count( $properties ) ); |
677 | 677 | $this->assertArrayHasKey( 'author', $properties ); |
678 | 678 | $this->assertArrayHasKey( 'comment_status', $properties ); |
679 | 679 | $this->assertArrayHasKey( 'content', $properties ); |
… |
… |
class WP_Test_REST_Pages_Controller extends WP_Test_REST_Post_Type_Controller_Te |
691 | 691 | $this->assertArrayHasKey( 'parent', $properties ); |
692 | 692 | $this->assertArrayHasKey( 'password', $properties ); |
693 | 693 | $this->assertArrayHasKey( 'ping_status', $properties ); |
| 694 | $this->assertArrayHasKey( 'preview_link', $properties ); |
694 | 695 | $this->assertArrayHasKey( 'slug', $properties ); |
695 | 696 | $this->assertArrayHasKey( 'status', $properties ); |
696 | 697 | $this->assertArrayHasKey( 'template', $properties ); |
diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php
index 721b08292a..0dbf15d622 100644
a
|
b
|
class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te |
3380 | 3380 | $response = rest_get_server()->dispatch( $request ); |
3381 | 3381 | $data = $response->get_data(); |
3382 | 3382 | $properties = $data['schema']['properties']; |
3383 | | $this->assertEquals( 24, count( $properties ) ); |
| 3383 | $this->assertEquals( 25, count( $properties ) ); |
3384 | 3384 | $this->assertArrayHasKey( 'author', $properties ); |
3385 | 3385 | $this->assertArrayHasKey( 'comment_status', $properties ); |
3386 | 3386 | $this->assertArrayHasKey( 'content', $properties ); |
… |
… |
class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te |
3397 | 3397 | $this->assertArrayHasKey( 'modified_gmt', $properties ); |
3398 | 3398 | $this->assertArrayHasKey( 'password', $properties ); |
3399 | 3399 | $this->assertArrayHasKey( 'ping_status', $properties ); |
| 3400 | $this->assertArrayHasKey( 'preview_link', $properties ); |
3400 | 3401 | $this->assertArrayHasKey( 'slug', $properties ); |
3401 | 3402 | $this->assertArrayHasKey( 'status', $properties ); |
3402 | 3403 | $this->assertArrayHasKey( 'sticky', $properties ); |
… |
… |
class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te |
3473 | 3474 | 'modified_gmt', |
3474 | 3475 | 'password', |
3475 | 3476 | 'ping_status', |
| 3477 | 'preview_link', |
3476 | 3478 | 'slug', |
3477 | 3479 | 'status', |
3478 | 3480 | 'sticky', |