WordPress.org

Making WordPress.org

Changeset 6485


Ignore:
Timestamp:
01/31/2018 04:08:51 AM (20 months ago)
Author:
dd32
Message:

Plugin Directory: API: Allow for querying multiple plugins on the plugin_information action through the 'slugs' parameter.

Example: https://api.wordpress.org/plugins/info/1.2/?action=plugin_information&request[slugs][]=hello-dolly&request[slugs][]=wordpress-importer

See #1304

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/standalone
Files:
2 edited

Legend:

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

    r6287 r6485  
    9191        }
    9292        return null;
     93    }
     94
     95    public function __set( $field, $value ) {
     96        $this->args->{$field} = $value;
     97    }
     98
     99    public function __unset( $field ) {
     100        unset( $this->args->{$field} );
    93101    }
    94102
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/standalone/class-plugins-info-api.php

    r6464 r6485  
    6262
    6363    /**
     64     * API Endpoint that handles the Plugin_Information route when the 'slugs' parameter is present.
     65     *
     66     * @param Plugins_Info_API_Request $request    The Request object for this request.
     67     */
     68    function plugin_information_mutliple( $request ) {
     69        $slugs = is_array( $request->slugs ) ? $request->slugs : explode( ',', $request->slugs );
     70        $slugs = array_unique( array_map( 'trim', $slugs ) );
     71
     72        if ( count( $slugs ) > 100 ) {
     73            $this->output( (object) array( 'error' => 'A maximum of 100 plugins can be queried at once.' ) );
     74            return;
     75        }
     76
     77        // Remove the slugs parameter to avoid recursive calls.
     78        unset( $request->slugs );
     79
     80        $response = array();
     81        foreach ( $slugs as $slug ) {
     82            $request->slug = $slug;
     83            $plugin_details = $this->plugin_information( $request, $return_raw = true );
     84            if ( ! isset( $plugin_details['error'] ) ) {
     85                $plugin_details = $this->remove_unexpected_fields( $plugin_details, $request, 'plugin_information' );
     86            }
     87
     88            $response[ $slug ] = $plugin_details;
     89        }
     90
     91        $this->output( (object) $response );
     92    }
     93
     94    /**
    6495     * API Endpoint that handles the Plugin_Information route.
    6596     *
     
    6899     */
    69100    function plugin_information( $request, $return_raw = false ) {
    70         if ( false === ( $response = wp_cache_get( $cache_key = $this->plugin_information_cache_key( $request ), self::CACHE_GROUP ) ) ) {
     101        if ( $request->slugs ) {
     102            $this->plugin_information_mutliple( $request );
     103            return;
     104        }
     105
     106        $response = false;
     107        if ( ! $request->slug ) {
     108            $response = array( 'error' => 'Plugin slug not provided.' );
     109        }
     110
     111        if ( ! $response && false === ( $response = wp_cache_get( $cache_key = $this->plugin_information_cache_key( $request ), self::CACHE_GROUP ) ) ) {
    71112            $response = $this->internal_rest_api_call( 'plugins/v1/plugin/' . $request->slug, array( 'locale' => $request->locale ) );
    72113
Note: See TracChangeset for help on using the changeset viewer.