Changeset 43438
- Timestamp:
- 07/11/2018 09:20:10 AM (6 years ago)
- Location:
- branches/4.9
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/4.9
-
branches/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
r42450 r43438 347 347 $data = $this->filter_response_by_context( $data, $context ); 348 348 349 $links = $response->get_links(); 350 349 351 // Wrap the data in a response object. 350 352 $response = rest_ensure_response( $data ); 351 352 $response->add_links( $this->prepare_links( $post ) ); 353 $response->add_links( $links ); 353 354 354 355 /** -
branches/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
r41979 r43438 1583 1583 $response = rest_ensure_response( $data ); 1584 1584 1585 $response->add_links( $this->prepare_links( $post ) ); 1585 $links = $this->prepare_links( $post ); 1586 $response->add_links( $links ); 1587 1588 if ( ! empty( $links['self']['href'] ) ) { 1589 $actions = $this->get_available_actions( $post, $request ); 1590 1591 $self = $links['self']['href']; 1592 1593 foreach ( $actions as $rel ) { 1594 $response->add_link( $rel, $self ); 1595 } 1596 } 1586 1597 1587 1598 /** … … 1720 1731 1721 1732 return $links; 1733 } 1734 1735 /** 1736 * Get the link relations available for the post and current user. 1737 * 1738 * @since 4.9.7 1739 * 1740 * @param WP_Post $post Post object. 1741 * @param WP_REST_Request Request object. 1742 * 1743 * @return array List of link relations. 1744 */ 1745 protected function get_available_actions( $post, $request ) { 1746 1747 if ( 'edit' !== $request['context'] ) { 1748 return array(); 1749 } 1750 1751 $rels = array(); 1752 1753 $post_type = get_post_type_object( $post->post_type ); 1754 1755 if ( 'attachment' !== $this->post_type && current_user_can( $post_type->cap->publish_posts ) ) { 1756 $rels[] = 'https://api.w.org/action-publish'; 1757 } 1758 1759 if ( 'post' === $post_type->name ) { 1760 if ( current_user_can( $post_type->cap->edit_others_posts ) && current_user_can( $post_type->cap->publish_posts ) ) { 1761 $rels[] = 'https://api.w.org/action-sticky'; 1762 } 1763 } 1764 1765 if ( post_type_supports( $post_type->name, 'author' ) ) { 1766 if ( current_user_can( $post_type->cap->edit_others_posts ) ) { 1767 $rels[] = 'https://api.w.org/action-assign-author'; 1768 } 1769 } 1770 1771 $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) ); 1772 1773 foreach ( $taxonomies as $tax ) { 1774 $tax_base = ! empty( $tax->rest_base ) ? $tax->rest_base : $tax->name; 1775 $create_cap = is_taxonomy_hierarchical( $tax->name ) ? $tax->cap->edit_terms : $tax->cap->assign_terms; 1776 1777 if ( current_user_can( $create_cap ) ) { 1778 $rels[] = 'https://api.w.org/action-create-' . $tax_base; 1779 } 1780 1781 if ( current_user_can( $tax->cap->assign_terms ) ) { 1782 $rels[] = 'https://api.w.org/action-assign-' . $tax_base; 1783 } 1784 } 1785 1786 return $rels; 1722 1787 } 1723 1788 … … 2062 2127 } 2063 2128 2129 $schema_links = $this->get_schema_links(); 2130 2131 if ( $schema_links ) { 2132 $schema['links'] = $schema_links; 2133 } 2134 2064 2135 return $this->add_additional_fields_schema( $schema ); 2136 } 2137 2138 /** 2139 * Retrieve Link Description Objects that should be added to the Schema for the posts collection. 2140 * 2141 * @since 4.9.7 2142 * 2143 * @return array 2144 */ 2145 protected function get_schema_links() { 2146 2147 $href = rest_url( "{$this->namespace}/{$this->rest_base}/{id}" ); 2148 2149 $links = array(); 2150 2151 if ( 'attachment' !== $this->post_type ) { 2152 $links[] = array( 2153 'rel' => 'https://api.w.org/action-publish', 2154 'title' => __( 'The current user can publish this post.' ), 2155 'href' => $href, 2156 'targetSchema' => array( 2157 'type' => 'object', 2158 'properties' => array( 2159 'status' => array( 2160 'type' => 'string', 2161 'enum' => array( 'publish', 'future' ), 2162 ), 2163 ), 2164 ), 2165 ); 2166 } 2167 2168 if ( 'post' === $this->post_type ) { 2169 $links[] = array( 2170 'rel' => 'https://api.w.org/action-sticky', 2171 'title' => __( 'The current user can sticky this post.' ), 2172 'href' => $href, 2173 'targetSchema' => array( 2174 'type' => 'object', 2175 'properties' => array( 2176 'sticky' => array( 2177 'type' => 'boolean', 2178 ), 2179 ), 2180 ), 2181 ); 2182 } 2183 2184 if ( post_type_supports( $this->post_type, 'author' ) ) { 2185 $links[] = array( 2186 'rel' => 'https://api.w.org/action-assign-author', 2187 'title' => __( 'The current user can change the author on this post.' ), 2188 'href' => $href, 2189 'targetSchema' => array( 2190 'type' => 'object', 2191 'properties' => array( 2192 'author' => array( 2193 'type' => 'integer', 2194 ), 2195 ), 2196 ), 2197 ); 2198 } 2199 2200 $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) ); 2201 2202 foreach ( $taxonomies as $tax ) { 2203 $tax_base = ! empty( $tax->rest_base ) ? $tax->rest_base : $tax->name; 2204 2205 /* translators: %s: taxonomy name */ 2206 $assign_title = sprintf( __( 'The current user can assign terms in the %s taxonomy.' ), $tax->name ); 2207 /* translators: %s: taxonomy name */ 2208 $create_title = sprintf( __( 'The current user can create terms in the %s taxonomy.' ), $tax->name ); 2209 2210 $links[] = array( 2211 'rel' => 'https://api.w.org/action-assign-' . $tax_base, 2212 'title' => $assign_title, 2213 'href' => $href, 2214 'targetSchema' => array( 2215 'type' => 'object', 2216 'properties' => array( 2217 $tax_base => array( 2218 'type' => 'array', 2219 'items' => array( 2220 'type' => 'integer', 2221 ), 2222 ), 2223 ), 2224 ), 2225 ); 2226 2227 $links[] = array( 2228 'rel' => 'https://api.w.org/action-create-' . $tax_base, 2229 'title' => $create_title, 2230 'href' => $href, 2231 'targetSchema' => array( 2232 'type' => 'object', 2233 'properties' => array( 2234 $tax_base => array( 2235 'type' => 'array', 2236 'items' => array( 2237 'type' => 'integer', 2238 ), 2239 ), 2240 ), 2241 ), 2242 ); 2243 } 2244 2245 return $links; 2065 2246 } 2066 2247 -
branches/4.9/tests/phpunit/tests/rest-api/rest-attachments-controller.php
r42427 r43438 1212 1212 } 1213 1213 1214 public function test_links_exist() { 1215 1216 wp_set_current_user( self::$editor_id ); 1217 1218 $post = self::factory()->attachment->create( array( 'post_author' => self::$editor_id ) ); 1219 $this->assertGreaterThan( 0, $post ); 1220 1221 $request = new WP_REST_Request( 'GET', "/wp/v2/media/{$post}" ); 1222 $request->set_query_params( array( 'context' => 'edit' ) ); 1223 1224 $response = rest_get_server()->dispatch( $request ); 1225 $links = $response->get_links(); 1226 1227 $this->assertArrayHasKey( 'self', $links ); 1228 } 1229 1230 public function test_publish_action_ldo_not_registered() { 1231 1232 $response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/media' ) ); 1233 $data = $response->get_data(); 1234 $schema = $data['schema']; 1235 1236 $this->assertArrayHasKey( 'links', $schema ); 1237 $publish = wp_list_filter( $schema['links'], array( 'rel' => 'https://api.w.org/action-publish' ) ); 1238 1239 $this->assertCount( 0, $publish, 'LDO not found on schema.' ); 1240 } 1241 1242 public function test_publish_action_link_does_not_exists() { 1243 1244 wp_set_current_user( self::$editor_id ); 1245 1246 $post = self::factory()->attachment->create( array( 'post_author' => self::$editor_id ) ); 1247 $this->assertGreaterThan( 0, $post ); 1248 1249 $request = new WP_REST_Request( 'GET', "/wp/v2/media/{$post}" ); 1250 $request->set_query_params( array( 'context' => 'edit' ) ); 1251 1252 $response = rest_get_server()->dispatch( $request ); 1253 $links = $response->get_links(); 1254 1255 $this->assertArrayNotHasKey( 'https://api.w.org/action-publish', $links ); 1256 } 1257 1214 1258 public function tearDown() { 1215 1259 parent::tearDown(); -
branches/4.9/tests/phpunit/tests/rest-api/rest-posts-controller.php
r43072 r43438 3247 3247 } 3248 3248 3249 public function test_publish_action_ldo_registered() { 3250 3251 $response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ) ); 3252 $data = $response->get_data(); 3253 $schema = $data['schema']; 3254 3255 $this->assertArrayHasKey( 'links', $schema ); 3256 $publish = wp_list_filter( $schema['links'], array( 'rel' => 'https://api.w.org/action-publish' ) ); 3257 3258 $this->assertCount( 1, $publish, 'LDO found on schema.' ); 3259 } 3260 3261 public function test_sticky_action_ldo_registered_for_posts() { 3262 3263 $response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ) ); 3264 $data = $response->get_data(); 3265 $schema = $data['schema']; 3266 3267 $this->assertArrayHasKey( 'links', $schema ); 3268 $publish = wp_list_filter( $schema['links'], array( 'rel' => 'https://api.w.org/action-sticky' ) ); 3269 3270 $this->assertCount( 1, $publish, 'LDO found on schema.' ); 3271 } 3272 3273 public function test_sticky_action_ldo_not_registered_for_non_posts() { 3274 3275 $response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/pages' ) ); 3276 $data = $response->get_data(); 3277 $schema = $data['schema']; 3278 3279 $this->assertArrayHasKey( 'links', $schema ); 3280 $publish = wp_list_filter( $schema['links'], array( 'rel' => 'https://api.w.org/action-sticky' ) ); 3281 3282 $this->assertCount( 0, $publish, 'LDO found on schema.' ); 3283 } 3284 3285 public function test_author_action_ldo_registered_for_post_types_with_author_support() { 3286 3287 $response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ) ); 3288 $data = $response->get_data(); 3289 $schema = $data['schema']; 3290 3291 $this->assertArrayHasKey( 'links', $schema ); 3292 $publish = wp_list_filter( $schema['links'], array( 'rel' => 'https://api.w.org/action-assign-author' ) ); 3293 3294 $this->assertCount( 1, $publish, 'LDO found on schema.' ); 3295 } 3296 3297 public function test_author_action_ldo_not_registered_for_post_types_without_author_support() { 3298 3299 remove_post_type_support( 'post', 'author' ); 3300 3301 $response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ) ); 3302 $data = $response->get_data(); 3303 $schema = $data['schema']; 3304 3305 $this->assertArrayHasKey( 'links', $schema ); 3306 $publish = wp_list_filter( $schema['links'], array( 'rel' => 'https://api.w.org/action-assign-author' ) ); 3307 3308 $this->assertCount( 0, $publish, 'LDO found on schema.' ); 3309 } 3310 3311 public function test_term_action_ldos_registered() { 3312 3313 $response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ) ); 3314 $data = $response->get_data(); 3315 $schema = $data['schema']; 3316 3317 $this->assertArrayHasKey( 'links', $schema ); 3318 $rels = array_flip( wp_list_pluck( $schema['links'], 'rel' ) ); 3319 3320 $this->assertArrayHasKey( 'https://api.w.org/action-assign-categories', $rels ); 3321 $this->assertArrayHasKey( 'https://api.w.org/action-create-categories', $rels ); 3322 $this->assertArrayHasKey( 'https://api.w.org/action-assign-tags', $rels ); 3323 $this->assertArrayHasKey( 'https://api.w.org/action-create-tags', $rels ); 3324 3325 $this->assertArrayNotHasKey( 'https://api.w.org/action-assign-post_format', $rels ); 3326 $this->assertArrayNotHasKey( 'https://api.w.org/action-create-post_format', $rels ); 3327 $this->assertArrayNotHasKey( 'https://api.w.org/action-assign-nav_menu', $rels ); 3328 $this->assertArrayNotHasKey( 'https://api.w.org/action-create-nav_menu', $rels ); 3329 } 3330 3331 public function test_action_links_only_available_in_edit_context() { 3332 3333 wp_set_current_user( self::$author_id ); 3334 3335 $post = self::factory()->post->create( array( 'post_author' => self::$author_id ) ); 3336 $this->assertGreaterThan( 0, $post ); 3337 3338 $request = new WP_REST_Request( 'GET', "/wp/v2/posts/{$post}" ); 3339 $request->set_query_params( array( 'context' => 'view' ) ); 3340 3341 $response = rest_get_server()->dispatch( $request ); 3342 $links = $response->get_links(); 3343 3344 $this->assertArrayNotHasKey( 'https://api.w.org/action-publish', $links ); 3345 } 3346 3347 public function test_publish_action_link_exists_for_author() { 3348 3349 wp_set_current_user( self::$author_id ); 3350 3351 $post = self::factory()->post->create( array( 'post_author' => self::$author_id ) ); 3352 $this->assertGreaterThan( 0, $post ); 3353 3354 $request = new WP_REST_Request( 'GET', "/wp/v2/posts/{$post}" ); 3355 $request->set_query_params( array( 'context' => 'edit' ) ); 3356 3357 $response = rest_get_server()->dispatch( $request ); 3358 $links = $response->get_links(); 3359 3360 $this->assertArrayHasKey( 'https://api.w.org/action-publish', $links ); 3361 } 3362 3363 public function test_publish_action_link_does_not_exist_for_contributor() { 3364 3365 wp_set_current_user( self::$contributor_id ); 3366 3367 $post = self::factory()->post->create( array( 'post_author' => self::$contributor_id ) ); 3368 $this->assertGreaterThan( 0, $post ); 3369 3370 $request = new WP_REST_Request( 'GET', "/wp/v2/posts/{$post}" ); 3371 $request->set_query_params( array( 'context' => 'edit' ) ); 3372 3373 $response = rest_get_server()->dispatch( $request ); 3374 $links = $response->get_links(); 3375 3376 $this->assertArrayNotHasKey( 'https://api.w.org/action-publish', $links ); 3377 } 3378 3379 public function test_sticky_action_exists_for_editor() { 3380 3381 wp_set_current_user( self::$editor_id ); 3382 3383 $post = self::factory()->post->create( array( 'post_author' => self::$author_id ) ); 3384 $this->assertGreaterThan( 0, $post ); 3385 3386 $request = new WP_REST_Request( 'GET', "/wp/v2/posts/{$post}" ); 3387 $request->set_query_params( array( 'context' => 'edit' ) ); 3388 3389 $response = rest_get_server()->dispatch( $request ); 3390 $links = $response->get_links(); 3391 3392 $this->assertArrayHasKey( 'https://api.w.org/action-sticky', $links ); 3393 } 3394 3395 public function test_sticky_action_does_not_exist_for_author() { 3396 3397 wp_set_current_user( self::$author_id ); 3398 3399 $post = self::factory()->post->create( array( 'post_author' => self::$author_id ) ); 3400 $this->assertGreaterThan( 0, $post ); 3401 3402 $request = new WP_REST_Request( 'GET', "/wp/v2/posts/{$post}" ); 3403 $request->set_query_params( array( 'context' => 'edit' ) ); 3404 3405 $response = rest_get_server()->dispatch( $request ); 3406 $links = $response->get_links(); 3407 3408 $this->assertArrayNotHasKey( 'https://api.w.org/action-sticky', $links ); 3409 } 3410 3411 public function test_sticky_action_does_not_exist_for_non_post_posts() { 3412 3413 wp_set_current_user( self::$editor_id ); 3414 3415 $post = self::factory()->post->create( 3416 array( 3417 'post_author' => self::$author_id, 3418 'post_type' => 'page', 3419 ) 3420 ); 3421 $this->assertGreaterThan( 0, $post ); 3422 3423 $request = new WP_REST_Request( 'GET', "/wp/v2/posts/{$post}" ); 3424 $request->set_query_params( array( 'context' => 'edit' ) ); 3425 3426 $response = rest_get_server()->dispatch( $request ); 3427 $links = $response->get_links(); 3428 3429 $this->assertArrayNotHasKey( 'https://api.w.org/action-sticky', $links ); 3430 } 3431 3432 3433 public function test_assign_author_action_exists_for_editor() { 3434 3435 wp_set_current_user( self::$editor_id ); 3436 3437 $post = self::factory()->post->create( array( 'post_author' => self::$author_id ) ); 3438 $this->assertGreaterThan( 0, $post ); 3439 3440 $request = new WP_REST_Request( 'GET', "/wp/v2/posts/{$post}" ); 3441 $request->set_query_params( array( 'context' => 'edit' ) ); 3442 3443 $response = rest_get_server()->dispatch( $request ); 3444 $links = $response->get_links(); 3445 3446 $this->assertArrayHasKey( 'https://api.w.org/action-assign-author', $links ); 3447 } 3448 3449 public function test_assign_author_action_does_not_exist_for_author() { 3450 3451 wp_set_current_user( self::$author_id ); 3452 3453 $post = self::factory()->post->create( array( 'post_author' => self::$author_id ) ); 3454 $this->assertGreaterThan( 0, $post ); 3455 3456 $request = new WP_REST_Request( 'GET', "/wp/v2/posts/{$post}" ); 3457 $request->set_query_params( array( 'context' => 'edit' ) ); 3458 3459 $response = rest_get_server()->dispatch( $request ); 3460 $links = $response->get_links(); 3461 3462 $this->assertArrayNotHasKey( 'https://api.w.org/action-assign-author', $links ); 3463 } 3464 3465 public function test_assign_author_action_does_not_exist_for_post_types_without_author_support() { 3466 3467 remove_post_type_support( 'post', 'author' ); 3468 3469 wp_set_current_user( self::$editor_id ); 3470 3471 $post = self::factory()->post->create(); 3472 $this->assertGreaterThan( 0, $post ); 3473 3474 $request = new WP_REST_Request( 'GET', "/wp/v2/posts/{$post}" ); 3475 $request->set_query_params( array( 'context' => 'edit' ) ); 3476 3477 $response = rest_get_server()->dispatch( $request ); 3478 $links = $response->get_links(); 3479 3480 $this->assertArrayNotHasKey( 'https://api.w.org/action-assign-author', $links ); 3481 } 3482 3483 public function test_create_term_action_exists_for_editor() { 3484 3485 wp_set_current_user( self::$editor_id ); 3486 3487 $post = self::factory()->post->create( array( 'post_author' => self::$author_id ) ); 3488 $this->assertGreaterThan( 0, $post ); 3489 3490 $request = new WP_REST_Request( 'GET', "/wp/v2/posts/{$post}" ); 3491 $request->set_query_params( array( 'context' => 'edit' ) ); 3492 3493 $response = rest_get_server()->dispatch( $request ); 3494 $links = $response->get_links(); 3495 3496 $this->assertArrayHasKey( 'https://api.w.org/action-create-categories', $links ); 3497 $this->assertArrayHasKey( 'https://api.w.org/action-create-tags', $links ); 3498 $this->assertArrayNotHasKey( 'https://api.w.org/action-create-post_format', $links ); 3499 } 3500 3501 public function test_create_term_action_non_hierarchical_exists_for_author() { 3502 3503 wp_set_current_user( self::$author_id ); 3504 3505 $post = self::factory()->post->create( array( 'post_author' => self::$author_id ) ); 3506 $this->assertGreaterThan( 0, $post ); 3507 3508 $request = new WP_REST_Request( 'GET', "/wp/v2/posts/{$post}" ); 3509 $request->set_query_params( array( 'context' => 'edit' ) ); 3510 3511 $response = rest_get_server()->dispatch( $request ); 3512 $links = $response->get_links(); 3513 3514 $this->assertArrayHasKey( 'https://api.w.org/action-create-tags', $links ); 3515 } 3516 3517 public function test_create_term_action_hierarchical_does_not_exists_for_author() { 3518 3519 wp_set_current_user( self::$author_id ); 3520 3521 $post = self::factory()->post->create( array( 'post_author' => self::$author_id ) ); 3522 $this->assertGreaterThan( 0, $post ); 3523 3524 $request = new WP_REST_Request( 'GET', "/wp/v2/posts/{$post}" ); 3525 $request->set_query_params( array( 'context' => 'edit' ) ); 3526 3527 $response = rest_get_server()->dispatch( $request ); 3528 $links = $response->get_links(); 3529 3530 $this->assertArrayNotHasKey( 'https://api.w.org/action-create-categories', $links ); 3531 } 3532 3533 public function test_assign_term_action_exists_for_contributor() { 3534 3535 wp_set_current_user( self::$contributor_id ); 3536 3537 $post = self::factory()->post->create( 3538 array( 3539 'post_author' => self::$contributor_id, 3540 'post_status' => 'draft', 3541 ) 3542 ); 3543 $this->assertGreaterThan( 0, $post ); 3544 3545 $request = new WP_REST_Request( 'GET', "/wp/v2/posts/{$post}" ); 3546 $request->set_query_params( array( 'context' => 'edit' ) ); 3547 3548 $response = rest_get_server()->dispatch( $request ); 3549 $links = $response->get_links(); 3550 3551 $this->assertArrayHasKey( 'https://api.w.org/action-assign-categories', $links ); 3552 $this->assertArrayHasKey( 'https://api.w.org/action-assign-tags', $links ); 3553 } 3554 3249 3555 public function tearDown() { 3250 3556 _unregister_post_type( 'youseeeme' );
Note: See TracChangeset
for help on using the changeset viewer.