Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
e002e33
First commit
spacedmonkey May 12, 2020
3346643
Merge branch 'master' into fix/themes-api
spacedmonkey Jun 9, 2020
d363522
Add statuses.
spacedmonkey Jun 9, 2020
04d2fc3
Add tests
spacedmonkey Jun 17, 2020
a1fc463
Fix tests.
spacedmonkey Jun 17, 2020
c8af5d2
Fix tests.
spacedmonkey Jun 17, 2020
1597413
Merge branch 'master' into fix/themes-api
spacedmonkey Jun 17, 2020
9bc53c9
Remove allowed.
spacedmonkey Jun 18, 2020
5cf33f7
Make the logic much simplier.
spacedmonkey Jun 18, 2020
accee13
Improve tests.
spacedmonkey Jun 22, 2020
20e9a1e
Fix linting.
spacedmonkey Jun 22, 2020
93fa7ce
Merge branch 'master' into fix/themes-api
spacedmonkey Jun 26, 2020
a02f03f
Add single theme api.
spacedmonkey Jun 26, 2020
69e872f
Fix lints.
spacedmonkey Jun 26, 2020
caec2fa
Add single tests.
spacedmonkey Jun 26, 2020
f302af4
Fix lint
spacedmonkey Jun 26, 2020
e334704
Fix unit tests.
spacedmonkey Jun 28, 2020
14ab4a3
Merge branch 'master' into fix/themes-api
spacedmonkey Jul 1, 2020
2d1cc0e
Allow reading current theme.
spacedmonkey Jul 2, 2020
0847a4c
Tweak permission checks.
spacedmonkey Jul 2, 2020
91076c7
Fix unit tests.
spacedmonkey Jul 2, 2020
640bf1f
Change method name.
spacedmonkey Jul 3, 2020
4e838b4
Merge remote-tracking branch 'remotes/origin/master' into fix/themes-api
TimothyBJacobs Jul 5, 2020
b9293ca
Merged master and resolved conflicts
Oct 19, 2020
e19cf17
Address PR feedback
Oct 19, 2020
3e5b894
Fix test
Oct 19, 2020
e1ff816
Restore theme status check for theme_supports field data
Oct 20, 2020
e06a1b7
Fix CI feedback - short array syntax is not allowed
Oct 20, 2020
c308345
Merge branch 'master' into fix/themes-api
Oct 20, 2020
f9cf70c
Merge pull request #1 from spacedmonkey/fix/themes-api-enhancements
spacedmonkey Oct 21, 2020
c2855ea
Address PR feedback and add active theme link to WP_REST_Server::get_…
Oct 22, 2020
c617a05
Address PR feedback, fix version updates, fix active theme link names…
Oct 22, 2020
7c0c870
Fixed docblocks versions
Oct 22, 2020
b025e17
Fixed docblocks versions
Oct 22, 2020
e418d97
Remove the logic to fill theme_supports for inactive themes
Oct 23, 2020
142c78e
Fix tests
Oct 23, 2020
138f9eb
Merged master and resolved conflicts
Dec 4, 2020
bb14aba
Merge branch 'fix/themes-api' of github.com:spacedmonkey/wordpress-de…
TimothyBJacobs Jan 2, 2021
458c271
Merge branch 'master' into fix/themes-api
TimothyBJacobs Jan 2, 2021
278a9b9
Miscellaneous fixes and adjustments.
TimothyBJacobs Jan 3, 2021
d672a7b
Also allow users with just switch_themes or manage_network_themes to …
TimothyBJacobs Jan 3, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address PR feedback and add active theme link to WP_REST_Server::get_…
…index
  • Loading branch information
Lukas Pawlik committed Oct 22, 2020
commit c2855ea53a2173ab2f643571ec1c3e0724e21af7
22 changes: 22 additions & 0 deletions src/wp-includes/rest-api/class-wp-rest-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,8 @@ public function get_index( $request ) {

$response->add_link( 'help', 'http://v2.wp-api.org/' );

$response = $this->maybe_add_current_theme_link( $response );

/**
* Filters the API root index data.
*
Expand Down Expand Up @@ -1507,4 +1509,24 @@ public function get_headers( $server ) {

return $headers;
}

/**
* Add current theme link to API response for users who have proper permissions.
*
* @since 5.7.0
*
* @param WP_REST_Response $response REST API response.
*
* @return WP_REST_Response REST API response.
*/
protected function maybe_add_current_theme_link( \WP_REST_Response $response ) {
if ( ! current_user_can( 'switch_themes' ) && ! current_user_can( 'manage_network_themes' ) ) {
return $response;
}

$theme = wp_get_theme();
$response->add_link( 'active_theme', rest_url( 'wp/v2/themes/' . $theme->get_stylesheet() ) );

return $response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
*
* @package WordPress
* @subpackage REST_API
* @since 5.0.0
* @since 5.7.0
*/

/**
* Core class used to manage themes via the REST API.
*
* @since 5.0.0
* @since 5.7.0
*
* @see WP_REST_Controller
*/
Expand All @@ -19,7 +19,7 @@ class WP_REST_Themes_Controller extends WP_REST_Controller {
/**
* Constructor.
*
* @since 5.0.0
* @since 5.7.0
*/
public function __construct() {
$this->namespace = 'wp/v2';
Expand All @@ -29,7 +29,7 @@ public function __construct() {
/**
* Registers the routes for the objects of the controller.
*
* @since 5.0.0
* @since 5.7.0
*
* @see register_rest_route()
*/
Expand Down Expand Up @@ -74,7 +74,7 @@ public function register_routes() {
/**
* Checks if a given request has access to read the theme.
*
* @since 5.0.0
* @since 5.7.0
*
* @param WP_REST_Request $request Full details about the request.
* @return true|WP_Error True if the request has read access for the item, otherwise WP_Error object.
Expand Down Expand Up @@ -116,7 +116,7 @@ public function get_item_permissions_check( $request ) {
}

return new WP_Error(
'rest_user_cannot_view',
'rest_cannot_view_active_theme',
__( 'Sorry, you are not allowed to view themes.' ),
array( 'status' => rest_authorization_required_code() )
);
Expand All @@ -141,7 +141,7 @@ protected function check_read_active_theme_permission() {
}

return new WP_Error(
'rest_user_cannot_view',
'rest_cannot_view_active_theme',
__( 'Sorry, you are not allowed to view active theme.' ),
array( 'status' => rest_authorization_required_code() )
);
Expand Down Expand Up @@ -172,7 +172,7 @@ public function get_item( $request ) {
/**
* Retrieves a collection of themes.
*
* @since 5.0.0
* @since 5.7.0
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
Expand Down Expand Up @@ -204,7 +204,7 @@ public function get_items( $request ) {
/**
* Prepares a single theme output for response.
*
* @since 5.0.0
* @since 5.7.0
*
* @param WP_Theme $theme Theme object.
* @param WP_REST_Request $request Request object.
Expand Down Expand Up @@ -319,7 +319,7 @@ public function prepare_item_for_response( $theme, $request ) {
/**
* Filters theme data returned from the REST API.
*
* @since 5.0.0
* @since 5.7.0
*
* @param WP_REST_Response $response The response object.
* @param WP_Theme $theme Theme object used to create response.
Expand Down Expand Up @@ -390,7 +390,7 @@ protected function prepare_theme_support( $support, $args, $feature, $request )
/**
* Retrieves the theme's schema, conforming to JSON Schema.
*
* @since 5.0.0
* @since 5.7.0
*
* @return array Item schema data.
*/
Expand Down Expand Up @@ -569,7 +569,7 @@ public function get_item_schema() {
/**
* Retrieves the search params for the themes collection.
*
* @since 5.0.0
* @since 5.7.0
*
* @return array Collection parameters.
*/
Expand All @@ -589,7 +589,7 @@ public function get_collection_params() {
/**
* Filters collection parameters for the themes controller.
*
* @since 5.0.0
* @since 5.7.0
*
* @param array $query_params JSON Schema-formatted collection parameters.
*/
Expand All @@ -599,7 +599,7 @@ public function get_collection_params() {
/**
* Sanitizes and validates the list of theme status.
*
* @since 5.0.0
* @since 5.7.0
*
* @param string|array $statuses One or more theme statuses.
* @param WP_REST_Request $request Full details about the request.
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/rest-api/rest-themes-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,7 @@ public function test_get_item_no_permission() {
wp_set_current_user( self::$subscriber_id );
$request = new WP_REST_Request( 'GET', self::$themes_route . '/' . WP_DEFAULT_THEME );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_user_cannot_view', $response, 403 );
$this->assertErrorResponse( 'rest_cannot_view_active_theme', $response, 403 );
}

/**
Expand Down Expand Up @@ -1221,7 +1221,7 @@ public function test_context_param() {}

public function data_theme_status() {
return array(
array( 'active', 'rest_user_cannot_view' ),
array( 'active', 'rest_cannot_view_active_theme' ),
array( 'active, inactive', 'rest_user_cannot_view' ),
array( 'inactive', 'rest_user_cannot_view' ),
array( '', 'rest_user_cannot_view' ),
Expand Down