Make WordPress Core

Ticket #64183: 64183.patch

File 64183.patch, 4.5 KB (added by bor0, 5 months ago)
  • src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

     
    22312231
    22322232                // If we have a featured media, add that.
    22332233                $featured_media = get_post_thumbnail_id( $post->ID );
    2234                 if ( $featured_media ) {
     2234                if ( $featured_media && ( 'publish' === get_post_status( $featured_media ) || current_user_can( 'read_post', $featured_media ) ) ) {
    22352235                        $image_url = rest_url( rest_get_route_for_post( $featured_media ) );
    22362236
    22372237                        $links['https://api.w.org/featuredmedia'] = array(
  • tests/phpunit/tests/rest-api/rest-posts-controller.php

     
    33003300                $this->assertSame( 0, (int) get_post_thumbnail_id( $new_post->ID ) );
    33013301        }
    33023302
     3303        /**
     3304         * Data provider for featured media link permission tests.
     3305         *
     3306         * @return array
     3307         */
     3308        public function data_featured_media_link_permissions() {
     3309                return array(
     3310                        'unauthenticated user with draft parent attachment' => array(
     3311                                'attachment_parent_status' => 'draft',
     3312                                'attachment_status'        => 'inherit',
     3313                                'user_id'                  => 0,
     3314                                'expect_link'              => false,
     3315                        ),
     3316                        'authenticated editor with draft parent attachment' => array(
     3317                                'attachment_parent_status' => 'draft',
     3318                                'attachment_status'        => 'inherit',
     3319                                'user_id'                  => 'editor',
     3320                                'expect_link'              => true,
     3321                        ),
     3322                        'unauthenticated user with published attachment' => array(
     3323                                'attachment_parent_status' => null,
     3324                                'attachment_status'        => 'publish',
     3325                                'user_id'                  => 0,
     3326                                'expect_link'              => true,
     3327                        ),
     3328                );
     3329        }
     3330
     3331        /**
     3332         * Tests that featured media links respect attachment permissions.
     3333         *
     3334         * @ticket 64183
     3335         * @dataProvider data_featured_media_link_permissions
     3336         *
     3337         * @param string|null $attachment_parent_status Status of the attachment's parent post, or null for no parent.
     3338         * @param string      $attachment_status Status to set on the attachment.
     3339         * @param int|string  $user_id User ID (0 for unauthenticated) or 'editor' for editor role.
     3340         * @param bool        $expect_link Whether the featured media link should be included.
     3341         */
     3342        public function test_get_item_featured_media_link_permissions( $attachment_parent_status, $attachment_status, $user_id, $expect_link ) {
     3343                $file = DIR_TESTDATA . '/images/canola.jpg';
     3344
     3345                // Create attachment parent if needed.
     3346                $parent_post_id = 0;
     3347                if ( null !== $attachment_parent_status ) {
     3348                        $parent_post_id = self::factory()->post->create(
     3349                                array(
     3350                                        'post_title'  => 'Parent Post',
     3351                                        'post_status' => $attachment_parent_status,
     3352                                )
     3353                        );
     3354                }
     3355
     3356                // Create attachment.
     3357                $attachment_id = self::factory()->attachment->create_object(
     3358                        $file,
     3359                        $parent_post_id,
     3360                        array(
     3361                                'post_mime_type' => 'image/jpeg',
     3362                        )
     3363                );
     3364
     3365                // Set attachment status if different from default.
     3366                if ( 'publish' === $attachment_status ) {
     3367                        wp_update_post(
     3368                                array(
     3369                                        'ID'          => $attachment_id,
     3370                                        'post_status' => 'publish',
     3371                                )
     3372                        );
     3373                }
     3374
     3375                // Create published post with featured media.
     3376                $published_post_id = self::factory()->post->create(
     3377                        array(
     3378                                'post_title'  => 'Published Post',
     3379                                'post_status' => 'publish',
     3380                        )
     3381                );
     3382                set_post_thumbnail( $published_post_id, $attachment_id );
     3383
     3384                // Set current user.
     3385                if ( 'editor' === $user_id ) {
     3386                        wp_set_current_user( self::$editor_id );
     3387                } else {
     3388                        wp_set_current_user( $user_id );
     3389                }
     3390
     3391                // Make request.
     3392                $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $published_post_id ) );
     3393                $response = rest_get_server()->dispatch( $request );
     3394                $links    = $response->get_links();
     3395
     3396                // Assert link presence based on expectation.
     3397                if ( $expect_link ) {
     3398                        $this->assertArrayHasKey( 'https://api.w.org/featuredmedia', $links );
     3399                        $this->assertSame(
     3400                                rest_url( '/wp/v2/media/' . $attachment_id ),
     3401                                $links['https://api.w.org/featuredmedia'][0]['href']
     3402                        );
     3403                } else {
     3404                        $this->assertArrayNotHasKey( 'https://api.w.org/featuredmedia', $links );
     3405                }
     3406        }
     3407
    33033408        public function test_create_post_invalid_author() {
    33043409                wp_set_current_user( self::$editor_id );
    33053410