Making WordPress.org

Changeset 12384


Ignore:
Timestamp:
02/03/2023 06:25:09 AM (17 months ago)
Author:
coffee2code
Message:

Photo Directory, Posts: Default feeds to show photos if another post type isn't specified.

Props topher1kenobe, coffee2code.
Fixes #5987.

File:
1 edited

Legend:

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

    r12200 r12384  
    3232        // Modify REST response to include URL to small photo (used by Profiles).
    3333        add_filter( "rest_prepare_{$post_type}", [ __CLASS__, 'rest_prepare_add_photo_url' ] );
     34
     35        // Dedicate primary feed to photos.
     36        add_action( 'request',            [ __CLASS__, 'make_primary_feed_all_photos' ] );
     37        add_filter( 'the_content_feed',   [ __CLASS__, 'add_photo_to_rss_feed' ] );
     38        add_filter( 'wp_get_attachment_image_attributes', [ __CLASS__, 'feed_attachment_image_attributes' ], 10, 3 );
    3439    }
    3540
     
    316321    }
    317322
     323    /**
     324     * Changes feeds to only serve photos by default.
     325     *
     326     * @param array $query_vars The array of requested query variables.
     327     * @return array
     328     */
     329    public static function make_primary_feed_all_photos( $query_vars ) {
     330        if ( isset( $query_vars['feed'] ) && ! isset( $query_vars['post_type'] ) ) {
     331            $query_vars['post_type'] = Registrations::get_post_type();
     332        }
     333
     334        return $query_vars;
     335    }
     336
     337    /**
     338     * Outputs markup to include the photo in RSS feeds of photos.
     339     *
     340     * @param string $content Post content.
     341     * @return string
     342     */
     343    public static function add_photo_to_rss_feed( $content ) {
     344        global $post;
     345
     346        if ( $post && Registrations::get_post_type() === get_post_type( $post ) && has_post_thumbnail( $post->ID ) ) {
     347            $content = '<figure>'
     348                . get_the_post_thumbnail( $post->ID, 'medium_large', [ 'style' => 'margin-bottom: 10px;', 'srcset' => ' ' ] ) . "\n"
     349                . ( $content ? "<figcaption>{$content}</figcaption>\n" : '' )
     350                . "</figure>\n";
     351        }
     352
     353        return $content;
     354    }
     355
     356    /**
     357     * Overrides the image attributes for images shown in feed.
     358     *
     359     * @param string[]     $attr       Array of attribute values for the image markup, keyed by attribute name.
     360     *                                 See `wp_get_attachment_image()`.
     361     * @param WP_Post      $attachment Image attachment post.
     362     * @param string|int[] $size       Requested image size. Can be any registered image size name, or
     363     *                                 an array of width and height values in pixels (in that order).
     364     * @return string[]
     365     */
     366    public static function feed_attachment_image_attributes( $attr, $attachment, $size ) {
     367        if ( is_feed() ) {
     368            $attr['class'] = '';
     369            unset( $attr['srcset'] );
     370        }
     371
     372        return $attr;
     373    }
     374
    318375}
    319376
Note: See TracChangeset for help on using the changeset viewer.