Making WordPress.org


Ignore:
Timestamp:
05/05/2016 08:18:29 AM (10 years ago)
Author:
dd32
Message:

Plugin Directory: Cache the $post_id of the plugin slugs in Plugin_Directory::get_plugin_post().

See #1584

File:
1 edited

Legend:

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

    r3040 r3084  
    516516                }
    517517
    518                 // TODO: Add caching here.
    519 
    520                 // get_post_by_slug();
    521                 $posts = get_posts( array(
    522                         'post_type'   => 'plugin',
    523                         'name'        => $plugin_slug,
    524                         'post_status' => array( 'publish', 'pending', 'disabled', 'closed', 'draft', 'approved' ),
    525                 ) );
    526                 if ( ! $posts ) {
    527                         return false;
    528                 }
    529 
    530                 return reset( $posts );
     518                $plugin_slug = sanitize_title_for_query( $plugin_slug );
     519
     520                if ( false !== ( $post_id = wp_cache_get( $plugin_slug, 'plugin-slugs' ) ) && ( $post = get_post( $post_id ) ) ) {
     521                        // We have a $post.
     522                } else {
     523                        // get_post_by_slug();
     524                        $posts = get_posts( array(
     525                                'post_type'   => 'plugin',
     526                                'name'        => $plugin_slug,
     527                                'post_status' => array( 'publish', 'pending', 'disabled', 'closed', 'draft', 'approved' ),
     528                        ) );
     529                        if ( ! $posts ) {
     530                                $post = false;
     531                                wp_cache_add( 0, $plugin_slug, 'plugin-slugs' );
     532                        } else {
     533                                $post = reset( $posts );
     534                                wp_cache_add( $post->ID, $plugin_slug, 'plugin-slugs' );
     535                        }
     536                }
     537
     538                return $post;
    531539        }
    532540
    … …  
    574582                }
    575583
     584                wp_cache_set( $id, $slug, 'plugin-slugs' );
     585
    576586                return get_post( $id );
    577587        }
Note: See TracChangeset for help on using the changeset viewer.