Making WordPress.org

Changeset 11535


Ignore:
Timestamp:
02/10/2022 12:41:19 AM (3 years ago)
Author:
dd32
Message:

Plugin Directory: API: Allow clients to disable the versions and download_link fields.

Full changes:

  • Can disable the versions response key.
  • Can disable the download_link response key.
  • Incorrectly documented downloadlink key will affect the download_link key.
  • Requesting reviews without sections will now result in sections.reviews being returned still
  • Typos in documentation & code cleanup.

See #6032.

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

    r10200 r11535  
    1919        'donate_link'         => false,
    2020        'downloaded'          => false,
    21         'downloadlink'        => false,
     21        'download_link'       => false,
    2222        'homepage'            => false,
    2323        'icons'               => false,
     
    2525        'rating'              => false,
    2626        'ratings'             => false,
    27         'reviews'             => false,
     27        'reviews'             => false, // NOTE: sub-key of 'sections'.
    2828        'requires'            => false,
    2929        'requires_php'        => false,
     
    3838        'author_block_rating' => false,
    3939        'language_packs'      => false,
     40        'versions'            => false,
    4041    );
    4142
     
    4647        'bare_contributors' => true,
    4748        'downloaded'        => true,
    48         'downloadlink'      => true,
     49        'download_link'     => true,
    4950        'donate_link'       => true,
    5051        'homepage'          => true,
     
    7677        'downloaded'        => true,
    7778        'description'       => true,
    78         'downloadlink'      => true,
     79        'download_link'     => true,
    7980        'donate_link'       => true,
    8081        'homepage'          => true,
     
    109110        'author_block_count'   => true,
    110111        'author_block_rating'  => true,
     112    );
     113
     114    // Typo/incorrect field name transforms.
     115    static $field_aliases = array(
     116        // from => to
     117        'downloadlink' => 'download_link', // Incorrectly documented in plugins_api().
    111118    );
    112119
     
    160167
    161168        // In WordPress 4.0+ we request the icons field however we don't use the
    162         // description and compatibility fields so we exclue those by default unless requested.
     169        // description and compatibility fields so we exclude those by default unless requested.
    163170        if ( ! empty( $this->requested_fields['icons'] ) ) {
    164171            $fields['compatibility'] = false;
     
    192199                }
    193200            }
     201
     202            // If the field is an aliased field, redirect to the proper field.
     203            $field = self::$field_aliases[ $field ] ?? $field;
     204
     205            // If it's a valid field, include it.
    194206            if ( isset( self::$fields[ $field ] ) ) {
    195207                $requested_fields[ $field ] = (bool) $include;
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/standalone/class-plugins-info-api.php

    r11219 r11535  
    155155     * @param string $method   The requested method, used to determine the default fields to include.
    156156     *
    157      * @return array The $resonse with the extra fields removed.
    158      */
    159     protected function remove_unexpected_fields( $response, $request, $method = '' ) {
    160         $fields = $request->get_expected_fields( $method );
     157     * @return array The $response with the extra fields removed.
     158     */
     159    protected function remove_unexpected_fields( $full_response, $request, $method = '' ) {
     160        $response = $full_response;
     161        $fields   = $request->get_expected_fields( $method );
    161162        foreach ( $fields as $field => $include ) {
    162163            if ( ! $include ) {
     
    168169        }
    169170
     171        // Support requesting reviews without sections.
     172        if ( ! empty( $fields['reviews'] ) && empty( $fields['sections'] ) ) {
     173            $response['sections'] = array(
     174                'reviews' => $full_response['sections']['reviews']
     175            );
     176        }
     177
    170178        // Back-compatible routines.
    171179        // WordPress 4.9 and older need a "bare" contributor map [ user => profile ]
    172180        if ( ! empty( $fields['bare_contributors'] ) ) {
    173             $contribs                 = $response['contributors'] ?? [];
     181            $contributors             = $response['contributors'] ?? [];
    174182            $response['contributors'] = array();
    175             if ( $contribs ) {
    176                 foreach ( $contribs as $user => $data ) {
    177                     $response['contributors'][ $user ] = $data['profile'];
    178                 }
     183            foreach ( $contributors as $user => $data ) {
     184                $response['contributors'][ $user ] = $data['profile'];
    179185            }
    180186        }
     
    182188        // Only include block translation data for the selected locale.
    183189        if ( ! empty( $response['block_translations'] ) ) {
    184             $response['block_translations'] = !empty( $response['block_translations'][ $request->locale ] ) ? $response['block_translations'][ $request->locale ] : [];
     190            $response['block_translations'] = $response['block_translations'][ $request->locale ] ?? [];
    185191        }
    186192
Note: See TracChangeset for help on using the changeset viewer.