Making WordPress.org

Changeset 12987


Ignore:
Timestamp:
11/30/2023 09:02:33 PM (5 months ago)
Author:
coffee2code
Message:

Photo Directory, Posts: Add image enclosures for the photo directory feed.

Also adds constant for image size to use in photo feeds.

Props cagrimmett, coffee2code.
Fixes #7341.

File:
1 edited

Legend:

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

    r12967 r12987  
    1313
    1414    /**
     15     * The registered image size that should be used for photos included in feeds.
     16     */
     17    const RSS_PHOTO_SIZE = 'medium_large';
     18
     19    /**
    1520     * Initializer.
    1621     */
     
    4247        add_action( 'request',            [ __CLASS__, 'make_primary_feed_all_photos' ] );
    4348        add_filter( 'the_content_feed',   [ __CLASS__, 'add_photo_to_rss_feed' ] );
     49        add_action( 'rss2_item',          [ __CLASS__, 'add_photo_as_enclosure_to_rss_feed' ] );
    4450        add_filter( 'wp_get_attachment_image_attributes', [ __CLASS__, 'feed_attachment_image_attributes' ], 10, 3 );
    4551    }
     
    410416        if ( $post && Registrations::get_post_type() === get_post_type( $post ) && has_post_thumbnail( $post->ID ) ) {
    411417            $content = '<figure>'
    412                 . get_the_post_thumbnail( $post->ID, 'medium_large', [ 'style' => 'margin-bottom: 10px;', 'srcset' => ' ' ] ) . "\n"
     418                . get_the_post_thumbnail( $post->ID, self::RSS_PHOTO_SIZE, [ 'style' => 'margin-bottom: 10px;', 'srcset' => ' ' ] ) . "\n"
    413419                . ( $content ? "<figcaption>{$content}</figcaption>\n" : '' )
    414420                . "</figure>\n";
     
    416422
    417423        return $content;
     424    }
     425
     426    /**
     427     * Outputs an `enclosure` tag for a photo in RSS feeds of photos.
     428     */
     429    public static function add_photo_as_enclosure_to_rss_feed() {
     430        global $post;
     431
     432        // Bail if not a photo post.
     433        if ( ! $post || Registrations::get_post_type() !== get_post_type( $post ) ) {
     434            return;
     435        }
     436
     437        // Bail if somehow there is no associated photo.
     438        $photo_id = get_post_thumbnail_id( $post );
     439        if ( ! $photo_id ) {
     440            return;
     441        }
     442
     443        // Get the photo's URL.
     444        $photo_url = get_the_post_thumbnail_url( $post, self::RSS_PHOTO_SIZE );
     445
     446        // Get the photo's MIME type.
     447        $mime_type = get_post_mime_type( $photo_id );
     448
     449        // Get the photo's file size.
     450        $photo_meta = wp_get_attachment_metadata( $photo_id );
     451        if ( 'full' === self::RSS_PHOTO_SIZE ) {
     452            $filesize = $photo_meta['filesize'] ?? '';
     453        } else {
     454            $filesize = $photo_meta['sizes'][ self::RSS_PHOTO_SIZE ]['filesize'] ?? '';
     455        }
     456
     457        if ( $photo_url && $mime_type && $filesize ) {
     458            // Output the enclosure tag.
     459            printf(
     460                '<enclosure url="%s" length="%s" type="%s" />' . "\n",
     461                esc_url( $photo_url ),
     462                esc_attr( $filesize ),
     463                esc_attr( $mime_type )
     464            );
     465        }
    418466    }
    419467
Note: See TracChangeset for help on using the changeset viewer.