wp/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
    13  * @since 5.0.0
    13  * @since 5.0.0
    14  *
    14  *
    15  * @see WP_REST_Controller
    15  * @see WP_REST_Controller
    16  */
    16  */
    17 class WP_REST_Themes_Controller extends WP_REST_Controller {
    17 class WP_REST_Themes_Controller extends WP_REST_Controller {
       
    18 
       
    19 	/**
       
    20 	 * Matches theme's directory: `/themes/<subdirectory>/<theme>/` or `/themes/<theme>/`.
       
    21 	 * Excludes invalid directory name characters: `/:<>*?"|`.
       
    22 	 */
       
    23 	const PATTERN = '[^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?';
    18 
    24 
    19 	/**
    25 	/**
    20 	 * Constructor.
    26 	 * Constructor.
    21 	 *
    27 	 *
    22 	 * @since 5.0.0
    28 	 * @since 5.0.0
    48 			)
    54 			)
    49 		);
    55 		);
    50 
    56 
    51 		register_rest_route(
    57 		register_rest_route(
    52 			$this->namespace,
    58 			$this->namespace,
    53 			'/' . $this->rest_base . '/(?P<stylesheet>[\w-]+)',
    59 			sprintf( '/%s/(?P<stylesheet>%s)', $this->rest_base, self::PATTERN ),
    54 			array(
    60 			array(
    55 				'args'   => array(
    61 				'args'   => array(
    56 					'stylesheet' => array(
    62 					'stylesheet' => array(
    57 						'description' => __( "The theme's stylesheet. This uniquely identifies the theme." ),
    63 						'description'       => __( "The theme's stylesheet. This uniquely identifies the theme." ),
    58 						'type'        => 'string',
    64 						'type'              => 'string',
       
    65 						'sanitize_callback' => array( $this, '_sanitize_stylesheet_callback' ),
    59 					),
    66 					),
    60 				),
    67 				),
    61 				array(
    68 				array(
    62 					'methods'             => WP_REST_Server::READABLE,
    69 					'methods'             => WP_REST_Server::READABLE,
    63 					'callback'            => array( $this, 'get_item' ),
    70 					'callback'            => array( $this, 'get_item' ),
    64 					'permission_callback' => array( $this, 'get_item_permissions_check' ),
    71 					'permission_callback' => array( $this, 'get_item_permissions_check' ),
    65 				),
    72 				),
    66 				'schema' => array( $this, 'get_public_item_schema' ),
    73 				'schema' => array( $this, 'get_public_item_schema' ),
    67 			)
    74 			)
    68 		);
    75 		);
       
    76 	}
       
    77 
       
    78 	/**
       
    79 	 * Sanitize the stylesheet to decode endpoint.
       
    80 	 *
       
    81 	 * @since 5.9.0
       
    82 	 *
       
    83 	 * @param string $stylesheet The stylesheet name.
       
    84 	 * @return string Sanitized stylesheet.
       
    85 	 */
       
    86 	public function _sanitize_stylesheet_callback( $stylesheet ) {
       
    87 		return urldecode( $stylesheet );
    69 	}
    88 	}
    70 
    89 
    71 	/**
    90 	/**
    72 	 * Checks if a given request has access to read the theme.
    91 	 * Checks if a given request has access to read the theme.
    73 	 *
    92 	 *
   202 
   221 
   203 	/**
   222 	/**
   204 	 * Prepares a single theme output for response.
   223 	 * Prepares a single theme output for response.
   205 	 *
   224 	 *
   206 	 * @since 5.0.0
   225 	 * @since 5.0.0
   207 	 *
   226 	 * @since 5.9.0 Renamed `$theme` to `$item` to match parent class for PHP 8 named parameter support.
   208 	 * @param WP_Theme        $theme   Theme object.
   227 	 *
       
   228 	 * @param WP_Theme        $item    Theme object.
   209 	 * @param WP_REST_Request $request Request object.
   229 	 * @param WP_REST_Request $request Request object.
   210 	 * @return WP_REST_Response Response object.
   230 	 * @return WP_REST_Response Response object.
   211 	 */
   231 	 */
   212 	public function prepare_item_for_response( $theme, $request ) {
   232 	public function prepare_item_for_response( $item, $request ) {
       
   233 		// Restores the more descriptive, specific name for use within this method.
       
   234 		$theme  = $item;
   213 		$data   = array();
   235 		$data   = array();
   214 		$fields = $this->get_fields_for_response( $request );
   236 		$fields = $this->get_fields_for_response( $request );
   215 
   237 
   216 		if ( rest_is_field_included( 'stylesheet', $fields ) ) {
   238 		if ( rest_is_field_included( 'stylesheet', $fields ) ) {
   217 			$data['stylesheet'] = $theme->get_stylesheet();
   239 			$data['stylesheet'] = $theme->get_stylesheet();
   308 
   330 
   309 		// Wrap the data in a response object.
   331 		// Wrap the data in a response object.
   310 		$response = rest_ensure_response( $data );
   332 		$response = rest_ensure_response( $data );
   311 
   333 
   312 		$response->add_links( $this->prepare_links( $theme ) );
   334 		$response->add_links( $this->prepare_links( $theme ) );
       
   335 
       
   336 		if ( $theme->get_stylesheet() === wp_get_theme()->get_stylesheet() ) {
       
   337 			// This creates a record for the active theme if not existent.
       
   338 			$id = WP_Theme_JSON_Resolver::get_user_global_styles_post_id();
       
   339 		} else {
       
   340 			$user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
       
   341 			$id       = isset( $user_cpt['ID'] ) ? $user_cpt['ID'] : null;
       
   342 		}
       
   343 
       
   344 		if ( $id ) {
       
   345 			$response->add_link(
       
   346 				'https://api.w.org/user-global-styles',
       
   347 				rest_url( 'wp/v2/global-styles/' . $id )
       
   348 			);
       
   349 		}
   313 
   350 
   314 		/**
   351 		/**
   315 		 * Filters theme data returned from the REST API.
   352 		 * Filters theme data returned from the REST API.
   316 		 *
   353 		 *
   317 		 * @since 5.0.0
   354 		 * @since 5.0.0