Making WordPress.org

Changeset 10801


Ignore:
Timestamp:
03/10/2021 05:21:46 AM (5 years ago)
Author:
dd32
Message:

Plugin Directory: Allow Plugin_Directory::get_plugin_post() to rely upon the global $post object when..

  • It's a plugin post_type, and the slug matches the requested slug,
  • It's a plugin post_type, be called with a 0 / null slug input, assume the global plugin post object.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-directory.php

    r10564 r10801  
    16821682         * @return \WP_Post|bool
    16831683         */
    1684         public static function get_plugin_post( $plugin_slug ) {
     1684        public static function get_plugin_post( $plugin_slug = null ) {
    16851685                global $post;
    16861686
     
    16991699                }
    17001700
    1701                 // Use the global $post object when it matches to avoid hitting the database.
    1702                 if ( ! empty( $post ) && 'plugin' == $post->post_type && $plugin_slug == $post->post_name ) {
    1703                         return $post;
     1701                // Use the global $post object when appropriate
     1702                if ( ! empty( $post ) && 'plugin' == $post->post_type ) {
     1703                        // Default to the global object.
     1704                        if ( is_null( $plugin_slug ) || 0 === $plugin_slug ) {
     1705                                return get_post( $post->ID );
     1706                        }
     1707
     1708                        // Avoid hitting the database if it matches.
     1709                        if ( $plugin_slug == $post->post_name ) {
     1710                                return get_post( $post->ID );
     1711                        }
    17041712                }
    17051713
    17061714                $plugin_slug = sanitize_title_for_query( $plugin_slug );
    1707 
    1708                 if ( false !== ( $post_id = wp_cache_get( $plugin_slug, 'plugin-slugs' ) ) && ( $post = get_post( $post_id ) ) ) {
    1709                         // We have a $post.
    1710                 } else {
    1711 
     1715                if ( ! $plugin_slug ) {
     1716                        return false;
     1717                }
     1718
     1719                $post    = false;
     1720                $post_id = wp_cache_get( $plugin_slug, 'plugin-slugs' );
     1721                if ( 0 === $post_id ) {
     1722                        // Unknown plugin slug.
     1723                        return false;
     1724                } else if ( $post_id ) {
     1725                        $post = get_post( $post_id );
     1726                }
     1727
     1728                if ( ! $post ) {
    17121729                        // get_post_by_slug();
    17131730                        $posts = get_posts( array(
Note: See TracChangeset for help on using the changeset viewer.