WordPress.org

Make WordPress Core

Changeset 49299


Ignore:
Timestamp:
10/24/2020 01:18:50 PM (9 months ago)
Author:
TimothyBlynJacobs
Message:

REST API: Support generating comment up links to custom posts controllers.

The comments controller now uses the rest_get_route_for_post function introduced in WordPress 5.5 to generate the link. This function is filterable to allow for custom controllers to properly define their REST API route.

Props dsifford, TimothyBlynJacobs.
Fixes #44152.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    r48782 r49299  
    11601160
    11611161        if ( 0 !== (int) $comment->comment_post_ID ) {
    1162             $post = get_post( $comment->comment_post_ID );
    1163 
    1164             if ( ! empty( $post->ID ) ) {
    1165                 $obj  = get_post_type_object( $post->post_type );
    1166                 $base = ! empty( $obj->rest_base ) ? $obj->rest_base : $obj->name;
    1167 
     1162            $post       = get_post( $comment->comment_post_ID );
     1163            $post_route = rest_get_route_for_post( $post );
     1164
     1165            if ( ! empty( $post->ID ) && $post_route ) {
    11681166                $links['up'] = array(
    1169                     'href'       => rest_url( 'wp/v2/' . $base . '/' . $comment->comment_post_ID ),
     1167                    'href'       => rest_url( $post_route ),
    11701168                    'embeddable' => true,
    11711169                    'post_type'  => $post->post_type,
  • trunk/tests/phpunit/tests/rest-api/rest-comments-controller.php

    r49051 r49299  
    32553255        );
    32563256
     3257        if ( $comment->comment_post_ID ) {
     3258            $this->assertEquals( rest_url( '/wp/v2/posts/' . $comment->comment_post_ID ), $links['up'][0]['href'] );
     3259        }
     3260
    32573261        if ( 'edit' === $context ) {
    32583262            $this->assertSame( $comment->comment_author_email, $data['author_email'] );
Note: See TracChangeset for help on using the changeset viewer.