Changeset 44268
- Timestamp:
- 12/17/2018 05:21:05 PM (6 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
- Property svn:mergeinfo changed
/branches/5.0 merged: 43917
- Property svn:mergeinfo changed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php
r44150 r44268 37 37 return parent::check_read_permission( $post ); 38 38 } 39 40 /** 41 * Filters a response based on the context defined in the schema. 42 * 43 * @since 5.0.0 44 * 45 * @param array $data Response data to fiter. 46 * @param string $context Context defined in the schema. 47 * @return array Filtered response. 48 */ 49 public function filter_response_by_context( $data, $context ) { 50 $data = parent::filter_response_by_context( $data, $context ); 51 52 /* 53 * Remove `title.rendered` and `content.rendered` from the response. It 54 * doesn't make sense for a reusable block to have rendered content on its 55 * own, since rendering a block requires it to be inside a post or a page. 56 */ 57 unset( $data['title']['rendered'] ); 58 unset( $data['content']['rendered'] ); 59 60 return $data; 61 } 62 63 /** 64 * Retrieves the block's schema, conforming to JSON Schema. 65 * 66 * @since 5.0.0 67 * 68 * @return array Item schema data. 69 */ 70 public function get_item_schema() { 71 $schema = parent::get_item_schema(); 72 73 /* 74 * Allow all contexts to access `title.raw` and `content.raw`. Clients always 75 * need the raw markup of a reusable block to do anything useful, e.g. parse 76 * it or display it in an editor. 77 */ 78 $schema['properties']['title']['properties']['raw']['context'] = array( 'view', 'edit' ); 79 $schema['properties']['content']['properties']['raw']['context'] = array( 'view', 'edit' ); 80 81 /* 82 * Remove `title.rendered` and `content.rendered` from the schema. It doesn’t 83 * make sense for a reusable block to have rendered content on its own, since 84 * rendering a block requires it to be inside a post or a page. 85 */ 86 unset( $schema['properties']['title']['properties']['rendered'] ); 87 unset( $schema['properties']['content']['properties']['rendered'] ); 88 89 return $schema; 90 } 91 39 92 } -
trunk/tests/phpunit/tests/rest-api/rest-blocks-controller.php
r44150 r44268 30 30 31 31 /** 32 * Our fake user 's ID.33 * 34 * @since 5.0.0 35 * 36 * @var int37 */ 38 protected static $user_id ;32 * Our fake user IDs, keyed by their role. 33 * 34 * @since 5.0.0 35 * 36 * @var array 37 */ 38 protected static $user_ids; 39 39 40 40 /** … … 51 51 'post_status' => 'publish', 52 52 'post_title' => 'My cool block', 53 'post_content' => '<!-- wp: core/paragraph --><p>Hello!</p><!-- /wp:core/paragraph -->',53 'post_content' => '<!-- wp:paragraph --><p>Hello!</p><!-- /wp:paragraph -->', 54 54 ) 55 55 ); 56 56 57 self::$user_id = $factory->user->create(58 array(59 'role' => 'editor',60 )57 self::$user_ids = array( 58 'editor' => $factory->user->create( array( 'role' => 'editor' ) ), 59 'author' => $factory->user->create( array( 'role' => 'author' ) ), 60 'contributor' => $factory->user->create( array( 'role' => 'contributor' ) ), 61 61 ); 62 62 } … … 70 70 wp_delete_post( self::$post_id ); 71 71 72 self::delete_user( self::$user_id ); 72 foreach ( self::$user_ids as $user_id ) { 73 self::delete_user( $user_id ); 74 } 73 75 } 74 76 … … 115 117 public function test_capabilities( $action, $role, $expected_status ) { 116 118 if ( $role ) { 117 $user_id = $this->factory->user->create( array( 'role' => $role ) );119 $user_id = self::$user_ids[ $role ]; 118 120 wp_set_current_user( $user_id ); 119 121 } else { … … 127 129 array( 128 130 'title' => 'Test', 129 'content' => '<!-- wp: core/paragraph --><p>Test</p><!-- /wp:core/paragraph -->',131 'content' => '<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->', 130 132 ) 131 133 ); … … 150 152 'post_status' => 'publish', 151 153 'post_title' => 'My cool block', 152 'post_content' => '<!-- wp: core/paragraph --><p>Hello!</p><!-- /wp:core/paragraph -->',154 'post_content' => '<!-- wp:paragraph --><p>Hello!</p><!-- /wp:paragraph -->', 153 155 'post_author' => $user_id, 154 156 ) … … 159 161 array( 160 162 'title' => 'Test', 161 'content' => '<!-- wp: core/paragraph --><p>Test</p><!-- /wp:core/paragraph -->',163 'content' => '<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->', 162 164 ) 163 165 ); … … 180 182 array( 181 183 'title' => 'Test', 182 'content' => '<!-- wp: core/paragraph --><p>Test</p><!-- /wp:core/paragraph -->',184 'content' => '<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->', 183 185 ) 184 186 ); … … 197 199 $this->fail( "'$action' is not a valid action." ); 198 200 } 199 200 if ( isset( $user_id ) ) { 201 self::delete_user( $user_id ); 202 } 201 } 202 203 /** 204 * Check that the raw title and content of a block can be accessed when there 205 * is no set schema, and that the rendered content of a block is not included 206 * in the response. 207 */ 208 public function test_content() { 209 wp_set_current_user( self::$user_ids['author'] ); 210 211 $request = new WP_REST_Request( 'GET', '/wp/v2/blocks/' . self::$post_id ); 212 $response = rest_get_server()->dispatch( $request ); 213 $data = $response->get_data(); 214 215 $this->assertEquals( 216 array( 217 'raw' => 'My cool block', 218 ), 219 $data['title'] 220 ); 221 $this->assertEquals( 222 array( 223 'raw' => '<!-- wp:paragraph --><p>Hello!</p><!-- /wp:paragraph -->', 224 'protected' => false, 225 ), 226 $data['content'] 227 ); 203 228 } 204 229 }
Note: See TracChangeset
for help on using the changeset viewer.