WordPress.org

Making WordPress.org

Changeset 4566


Ignore:
Timestamp:
12/22/2016 01:54:34 AM (3 years ago)
Author:
dd32
Message:

Plugin Directory: Handle API failures better, and output 'null' when an individual plugin API call fails, for backwards compatibility.

Fixes #2338

File:
1 edited

Legend:

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

    r4560 r4566  
    7171            if ( 200 != $response->status ) {
    7272                $response = array( 'error' => 'Plugin not found.' );
    73                 wp_cache_set( $cache_key, $response, self::CACHE_GROUP, 60*60 ); // 1 hour TTL for 404's?
     73                wp_cache_set( $cache_key, $response, self::CACHE_GROUP, 15*60 ); // shorter TTL for missing/erroring plugins.
    7474            } else {
    7575                $response = $response->data;
     
    8282        }
    8383
     84        // Backwards compatibility; the API returns null in case of error..
     85        if ( isset( $response['error'] ) ) {
     86            $this->output( null );
     87            return;
     88        }
     89
     90
    8491        // Only include the fields requested.
    85         $response = $this->remove_unexpected_fields( $response, $request, 'plugin_information' );
     92        if ( ! isset( $response['error'] ) ) {
     93            $response = $this->remove_unexpected_fields( $response, $request, 'plugin_information' );
     94        }
    8695
    8796        $this->output( (object) $response );
     
    148157            $response = $this->internal_rest_api_call( 'plugins/v1/query-plugins', $request->query_plugins_params_for_query() );
    149158            if ( 200 != $response->status ) {
    150                 $this->output( (object) array( 'error' => 'Query Failed.' ) );
    151                 return;
     159                $response = array( 'error' => 'Query Failed.' );
     160                wp_cache_set( $cache_key, $response, self::CACHE_GROUP, 30 ); // Short expiry for when we've got issues
    152161            } else {
    153162                $response = $response->data;
     
    156165        }
    157166
     167        if ( isset( $response['error'] ) ) {
     168            $this->output( $response );
     169            return;
     170        }
     171
    158172        // Fill in the plugin details
    159173        foreach ( $response['plugins'] as $i => $plugin_slug ) {
    160             $response['plugins'][ $i ] = $this->plugin_information( new Plugins_Info_API_Request( array( 'slug' => $plugin_slug, 'locale' => $request->locale ) ), true );
     174            $plugin = $this->plugin_information( new Plugins_Info_API_Request( array( 'slug' => $plugin_slug, 'locale' => $request->locale ) ), true );
     175            if ( isset( $plugin['error'] ) ) {
     176                unset( $response['plugins'][ $i ] );
     177                continue;
     178            }
     179
     180            $response['plugins'][ $i ] = $plugin;
    161181        }
    162182
     
    185205            if ( 200 != $response->status ) {
    186206                $response = array( 'error' => 'Temporarily Unavailable' );
     207                wp_cache_set( $cache_key, $response, self::CACHE_GROUP, 30 ); // Short expiry for when we've got issues
    187208            } else {
    188209                $response = $response->data;
    189210                wp_cache_set( $cache_key, $response, self::CACHE_GROUP, self::CACHE_EXPIRY );
    190211            }
     212        }
     213
     214        if ( isset( $response['error'] ) ) {
     215            $this->output( (object) $response );
     216            return;
    191217        }
    192218
Note: See TracChangeset for help on using the changeset viewer.