Making WordPress.org

Changeset 13356


Ignore:
Timestamp:
03/19/2024 09:10:46 PM (14 months ago)
Author:
coffee2code
Message:

Photo Directory, Posts: Add get_photo_post() to fetch a photo by slug.

File:
1 edited

Legend:

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

    r13295 r13356  
    483483
    484484    /**
     485     * Retrieves the WP_Post object representing a given photo.
     486     *
     487     * Adapted from /plugins/plugin-directory/class-plugin-directory.php: get_plugin_post()
     488     *
     489     * @global \WP_Post $post WordPress post object.
     490     *
     491     * @param int|string|\WP_Post $plugin_slug The slug of the photo to retrieve.
     492     * @return \WP_Post|bool
     493     */
     494    public static function get_photo_post( $photo_slug = null ) {
     495        if ( $photo_slug instanceof \WP_Post ) {
     496            return $photo_slug;
     497        }
     498
     499        // Handle int $photo_slug being passed. NOT numeric slugs
     500        if (
     501            is_int( $photo_slug ) &&
     502            ( $post = get_post( $photo_slug ) ) &&
     503            ( $post->ID === $photo_slug )
     504        ) {
     505            return $post;
     506        }
     507
     508        // Use the global $post object when appropriate
     509        if (
     510            ! empty( $GLOBALS['post']->post_type ) &&
     511            Registrations::get_post_type() === $GLOBALS['post']->post_type
     512        ) {
     513            // Default to the global object.
     514            if ( is_null( $photo_slug ) || 0 === $photo_slug ) {
     515                return get_post( $GLOBALS['post']->ID );
     516            }
     517
     518            // Avoid hitting the database if it matches.
     519            if ( $photo_slug == $GLOBALS['post']->post_name ) {
     520                return get_post( $GLOBALS['post']->ID );
     521            }
     522        }
     523
     524        $photo_slug = sanitize_title_for_query( $photo_slug );
     525        if ( ! $photo_slug ) {
     526            return false;
     527        }
     528
     529        $post    = false;
     530        $post_id = wp_cache_get( $photo_slug, 'photo-slugs' );
     531        if ( 0 === $post_id ) {
     532            // Unknown photo slug.
     533            return false;
     534        } elseif ( $post_id ) {
     535            $post = get_post( $post_id );
     536        }
     537
     538        if ( ! $post ) {
     539            // get_post_by_slug();
     540            $posts = get_posts( [
     541                'post_type'   => Registrations::get_post_type(),
     542                'name'        => $photo_slug,
     543                'post_status' => [ 'publish' ], // Only concerned with published photos.
     544            ] );
     545
     546            if ( ! $posts ) {
     547                $post = false;
     548                wp_cache_add( 0, $photo_slug, 'photo-slugs' );
     549            } else {
     550                $post = reset( $posts );
     551                wp_cache_add( $post->ID, $photo_slug, 'photo-slugs' );
     552            }
     553        }
     554
     555        return $post;
     556    }
     557
     558    /**
    485559     * The array of post types to be included in the sitemap.
    486560     *
Note: See TracChangeset for help on using the changeset viewer.