Make WordPress Core


Ignore:
Timestamp:
01/03/2021 09:45:42 PM (4 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Expose all themes in the themes controller.

Previously, only the active theme was made available. This commit allows for all themes to be queried if the user has the switch_themes or manage_network_themes capabilities.

This commit also no longer exposes the page, per_page, search and context query parameters since they are not supported by this controller.

Props spacedmonkey, lpawlik, TimothyBlynJacobs.
Fixes #50152.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/class-wp-rest-server.php

    r49918 r49925  
    12451245
    12461246        $response = new WP_REST_Response( $available );
    1247 
    12481247        $response->add_link( 'help', 'http://v2.wp-api.org/' );
     1248        $this->add_active_theme_link_to_index( $response );
    12491249
    12501250        /**
     
    12601260         */
    12611261        return apply_filters( 'rest_index', $response );
     1262    }
     1263
     1264    /**
     1265     * Adds a link to the active theme for users who have proper permissions.
     1266     *
     1267     * @since 5.7.0
     1268     *
     1269     * @param WP_REST_Response $response REST API response.
     1270     */
     1271    protected function add_active_theme_link_to_index( WP_REST_Response $response ) {
     1272        $should_add = current_user_can( 'switch_themes' ) || current_user_can( 'manage_network_themes' );
     1273
     1274        if ( ! $should_add && current_user_can( 'edit_posts' ) ) {
     1275            $should_add = true;
     1276        }
     1277
     1278        if ( ! $should_add ) {
     1279            foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
     1280                if ( current_user_can( $post_type->cap->edit_posts ) ) {
     1281                    $should_add = true;
     1282                    break;
     1283                }
     1284            }
     1285        }
     1286
     1287        if ( $should_add ) {
     1288            $theme = wp_get_theme();
     1289            $response->add_link( 'https://api.w.org/active-theme', rest_url( 'wp/v2/themes/' . $theme->get_stylesheet() ) );
     1290        }
    12621291    }
    12631292
Note: See TracChangeset for help on using the changeset viewer.