Making WordPress.org

Changeset 12108


Ignore:
Timestamp:
10/07/2022 06:40:11 AM (20 months ago)
Author:
coffee2code
Message:

Photo Directory, Posts: Amend REST API response for photo posts to include URL to 'medium' size of photo.

Will be used by profiles.wordpress.org to show photo thumbnail in profiles.

See #5981.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/inc/posts.php

    r11679 r12108  
    1414     */
    1515    public static function init() {
    16         add_action( 'publish_' . Registrations::get_post_type(), [ __CLASS__, 'make_photo_attachment_available' ] );
     16        $post_type = Registrations::get_post_type();
     17
     18        add_action( "publish_{$post_type}", [ __CLASS__, 'make_photo_attachment_available' ] );
    1719        add_action( 'before_delete_post', [ __CLASS__, 'delete_attachments' ], 10, 2 );
    1820        add_action( 'delete_attachment',  [ __CLASS__, 'delete_photo_post' ], 10, 2 );
     
    2729        // Fix pages count for front page paginations.
    2830        add_filter( 'the_posts',          [ __CLASS__, 'fix_front_page_pagination_count' ], 10, 2 );
     31
     32        // Modify REST response to include URL to small photo (used by Profiles).
     33        add_filter( "rest_prepare_{$post_type}", [ __CLASS__, 'rest_prepare_add_photo_url' ] );
     34    }
     35
     36    /**
     37     * Amends REST response for photos to include URL to small version of photo.
     38     *
     39     * @param WP_REST_Response $response
     40     * @return WP_REST_Response
     41     */
     42    public static function rest_prepare_add_photo_url( $response ) {
     43        $data = $response->get_data();
     44
     45        $data['photo-thumbnail-url'] = wp_get_attachment_image_src( get_post_thumbnail_id( $data['id'] ), 'medium' )[0];
     46
     47        $response->set_data( $data );
     48
     49        return $response;
    2950    }
    3051
Note: See TracChangeset for help on using the changeset viewer.