Making WordPress.org

Ticket #7217: 7217.diff

File 7217.diff, 5.5 KB (added by dd32, 14 months ago)
  • zip/class-serve.php

    class Serve { 
    4646                } elseif ( preg_match( '!^/plugin-checksums/(?P<slug>[a-z0-9-_]+)/(?P<version>.+?)(\.json)?$!i', $path, $m ) ) {
    4747                        // Checksums
    4848                        $checksum_request  = true;
    4949                        $signature_request = false;
    5050                } else {
    5151                        throw new Exception( __METHOD__ . ': Invalid URL.' );
    5252                }
    5353
    5454                $slug = strtolower( $m['slug'] );
    5555
    5656                $version = 'trunk';
    5757                if ( isset( $m['version'] ) && '' !== $m['version'] ) {
    5858                        $version = $m['version'];
    5959                }
    6060
     61                // Check to see if the plugin is closed.
     62                if ( ! $checksum_request ) {
     63                        $closed_date = $this->get_post_meta( $slug, 'plugin_closed_date' );
     64                        if ( $closed_date && strtotime( $closed_date ) + 5184000 /* 60 days */ < time() ) {
     65                                throw new Exception( __METHOD__ . ': Plugin is closed.' );
     66                        }
     67                }
     68
    6169                // If the latest-stable is requested, determine the file to serve.
    6270                $is_latest_stable = ( 'latest-stable' == $version );
    6371                if ( $is_latest_stable ) {
    64                         $version = $this->get_stable_tag( $slug );
     72                        $version = $this->get_post_meta( $slug, 'stable_tag' );
     73                        if ( ! $version ) {
     74                                throw new Exception( __METHOD__ . ': No stable version found.' );
     75                        }
    6576                }
    6677
    6778                // Checksum requests for 'trunk' are not possible.
    6879                if ( $checksum_request && 'trunk' == $version ) {
    6980                        throw new Exception( __METHOD__ . ': Checksum requests must include a version.' );
    7081                }
    7182
    7283                $args = array(
    7384                        'stats' => true,
    7485                );
    7586
    7687                if ( $checksum_request || $signature_request ) {
    7788                        $args['stats'] = false;
    7889
    7990                } elseif ( isset( $_GET['stats'] ) ) {
    8091                        $args['stats'] = (bool) $_GET['stats'];
    8192
    8293                } elseif ( isset( $_GET['nostats'] ) ) {
    8394                        $args['stats'] = ! empty( $_GET['nostats'] );
    8495                }
    8596
    86 
    8797                return compact( 'zip', 'slug', 'version', 'args', 'checksum_request', 'signature_request', 'is_latest_stable' );
    8898        }
    8999
    90100        /**
    91101         * Redirect to the latest stable version if requested.
    92102         *
    93103         * @param array $request The request array for the request.
    94104         */
    95105        protected function maybe_redirect_latest_stable( $request ) {
    96106                if ( ! $request['is_latest_stable'] ) {
    97107                        return;
    98108                }
    99109
    100110                $file     = $this->get_file( $request );
    101111                $redirect = 'https://downloads.wordpress.org/plugin/' . basename( $file );
    class Serve { 
    107117                // CORS, to match the ZIP passthrough.
    108118                header( 'Access-Control-Allow-Methods: GET, HEAD' );
    109119                header( 'Access-Control-Allow-Origin: *' );
    110120
    111121                // Tell browsers to only cache this for 5 minutes.
    112122                header( 'Cache-Control: max-age=300' );
    113123
    114124                // Redirect to the file they want.
    115125                header( 'Location: ' . $redirect, 302 );
    116126                exit;
    117127        }
    118128
    119129        /**
    120130         * Retrieve the stable_tag for a given plugin from Cache or the Database.
    121131         *
    122          * @param string $plugin_slug The plugin slug
    123          * @return string The stable_tag on success, Exception thrown on failure.
     132         * @param string $plugin_slug The plugin slug.
     133         * @param string $meta_key    The meta_key to retrieve.
     134         * @return string The meta_value on success, Exception thrown on failure.
    124135         */
    125         protected function get_stable_tag( $plugin_slug ) {
     136        protected function get_post_meta( $plugin_slug, $meta_key ) {
    126137                global $wpdb;
    127138
     139                $value   = false;
    128140                $post_id = $this->get_post_id( $plugin_slug );
    129141
    130                 // Determine the stable_tag
    131                 $meta = wp_cache_get( $post_id, 'post_meta' );
     142                // Determine the value from the object cache first.
     143                $meta     = wp_cache_get( $post_id, 'post_meta' );
     144                $in_cache = isset( $meta[ $meta_key ][0] );
    132145
    133                 $version = false;
    134                 if ( isset( $meta['stable_tag'][0] ) ) {
    135                         $version = $meta['stable_tag'][0];
    136                 }
    137                 if ( ! $version ) {
    138                         $version = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'stable_tag' LIMIT 1", $post_id ) );
    139                 }
    140                 if ( ! $version ) {
    141                         throw new Exception( __METHOD__ . ": A version for $plugin_slug cannot be determined." );
     146                if ( $in_cache ) {
     147                        $value = $meta[ $meta_key ][0];
     148                } else {
     149                        // Check our specific cache groups.
     150                        $value = wp_cache_get( $post_id, 'plugin-meta-' . $meta_key, false, $found );
     151
     152                        if ( ! $found ) {
     153                                $value = $wpdb->get_var( $wpdb->prepare(
     154                                        "SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s LIMIT 1",
     155                                        $post_id,
     156                                        $meta_key
     157                                ) );
     158
     159                                wp_cache_set( $post_id, $value, 'plugin-meta-' . $meta_key, 300 /* 5 mins */ );
     160                        }
    142161                }
    143162
    144                 return $version;
     163                return $value;
    145164        }
    146165
    147166        /**
    148167         * Retrieve the post_id for a Plugin slug.
    149168         *
    150169         * This function uses the Object Cache and $wpdb directly to avoid
    151170         * a dependency upon WordPress.
    152171         *
    153172         * @param string $plugin_slug The plugin slug.
    154173         * @return int The post_id for the plugin. Exception thrown on failure.
    155174         */
    156175        protected function get_post_id( $plugin_slug ) {
    157176                global $wpdb;
    158177
    159178                $post_id = wp_cache_get( $plugin_slug, 'plugin-slugs' );
    160179                if ( false === $post_id ) {
    161                         $post_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID from $wpdb->posts WHERE post_type = 'plugin' AND post_name = %s", $plugin_slug ) );
     180                        $post_id = $wpdb->get_var( $wpdb->prepare(
     181                                "SELECT ID FROM $wpdb->posts WHERE post_type = 'plugin' AND post_name = %s LIMIT 1",
     182                                $plugin_slug
     183                        ) );
     184
    162185                        wp_cache_add( $plugin_slug, $post_id, 'plugin-slugs' );
    163186                }
    164187
    165188                if ( ! $post_id ) {
    166189                        throw new Exception( __METHOD__ . ": A post_id for $plugin_slug cannot be determined." );
    167190                }
    168191
    169192                return $post_id;
    170193        }
    171194
    172195        /**
    173196         * Returns the file to be served for the request.
    174197         *
    175198         * @param array $request The request object for the request.
    176199         * @return array The file to serve.