wp/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
   325 		if ( rest_is_field_included( 'title.raw', $fields ) ) {
   325 		if ( rest_is_field_included( 'title.raw', $fields ) ) {
   326 			$data['title']['raw'] = $post->post_title;
   326 			$data['title']['raw'] = $post->post_title;
   327 		}
   327 		}
   328 		if ( rest_is_field_included( 'title.rendered', $fields ) ) {
   328 		if ( rest_is_field_included( 'title.rendered', $fields ) ) {
   329 			add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
   329 			add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
       
   330 			add_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
   330 
   331 
   331 			$data['title']['rendered'] = get_the_title( $post->ID );
   332 			$data['title']['rendered'] = get_the_title( $post->ID );
   332 
   333 
   333 			remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
   334 			remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
       
   335 			remove_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
   334 		}
   336 		}
   335 
   337 
   336 		if ( rest_is_field_included( 'settings', $fields ) ) {
   338 		if ( rest_is_field_included( 'settings', $fields ) ) {
   337 			$data['settings'] = ! empty( $config['settings'] ) && $is_global_styles_user_theme_json ? $config['settings'] : new stdClass();
   339 			$data['settings'] = ! empty( $config['settings'] ) && $is_global_styles_user_theme_json ? $config['settings'] : new stdClass();
   338 		}
   340 		}
   505 
   507 
   506 	/**
   508 	/**
   507 	 * Checks if a given request has access to read a single theme global styles config.
   509 	 * Checks if a given request has access to read a single theme global styles config.
   508 	 *
   510 	 *
   509 	 * @since 5.9.0
   511 	 * @since 5.9.0
       
   512 	 * @since 6.7.0 Allow users with edit post capabilities to view theme global styles.
   510 	 *
   513 	 *
   511 	 * @param WP_REST_Request $request Full details about the request.
   514 	 * @param WP_REST_Request $request Full details about the request.
   512 	 * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise.
   515 	 * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise.
   513 	 */
   516 	 */
   514 	public function get_theme_item_permissions_check( $request ) {
   517 	public function get_theme_item_permissions_check( $request ) {
   515 		/*
   518 		/*
       
   519 		 * Verify if the current user has edit_posts capability.
       
   520 		 * This capability is required to view global styles.
       
   521 		 */
       
   522 		if ( current_user_can( 'edit_posts' ) ) {
       
   523 			return true;
       
   524 		}
       
   525 
       
   526 		foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
       
   527 			if ( current_user_can( $post_type->cap->edit_posts ) ) {
       
   528 				return true;
       
   529 			}
       
   530 		}
       
   531 
       
   532 		/*
   516 		 * Verify if the current user has edit_theme_options capability.
   533 		 * Verify if the current user has edit_theme_options capability.
   517 		 * This capability is required to edit/view/delete templates.
       
   518 		 */
   534 		 */
   519 		if ( ! current_user_can( 'edit_theme_options' ) ) {
   535 		if ( current_user_can( 'edit_theme_options' ) ) {
   520 			return new WP_Error(
   536 			return true;
   521 				'rest_cannot_manage_global_styles',
   537 		}
   522 				__( 'Sorry, you are not allowed to access the global styles on this site.' ),
   538 
   523 				array(
   539 		return new WP_Error(
   524 					'status' => rest_authorization_required_code(),
   540 			'rest_cannot_read_global_styles',
   525 				)
   541 			__( 'Sorry, you are not allowed to access the global styles on this site.' ),
   526 			);
   542 			array(
   527 		}
   543 				'status' => rest_authorization_required_code(),
   528 
   544 			)
   529 		return true;
   545 		);
   530 	}
   546 	}
   531 
   547 
   532 	/**
   548 	/**
   533 	 * Returns the given theme global styles config.
   549 	 * Returns the given theme global styles config.
   534 	 *
   550 	 *
   585 
   601 
   586 	/**
   602 	/**
   587 	 * Checks if a given request has access to read a single theme global styles config.
   603 	 * Checks if a given request has access to read a single theme global styles config.
   588 	 *
   604 	 *
   589 	 * @since 6.0.0
   605 	 * @since 6.0.0
       
   606 	 * @since 6.7.0 Allow users with edit post capabilities to view theme global styles.
   590 	 *
   607 	 *
   591 	 * @param WP_REST_Request $request Full details about the request.
   608 	 * @param WP_REST_Request $request Full details about the request.
   592 	 * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise.
   609 	 * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise.
   593 	 */
   610 	 */
   594 	public function get_theme_items_permissions_check( $request ) {
   611 	public function get_theme_items_permissions_check( $request ) {
   595 		/*
   612 		return $this->get_theme_item_permissions_check( $request );
   596 		 * Verify if the current user has edit_theme_options capability.
       
   597 		 * This capability is required to edit/view/delete templates.
       
   598 		 */
       
   599 		if ( ! current_user_can( 'edit_theme_options' ) ) {
       
   600 			return new WP_Error(
       
   601 				'rest_cannot_manage_global_styles',
       
   602 				__( 'Sorry, you are not allowed to access the global styles on this site.' ),
       
   603 				array(
       
   604 					'status' => rest_authorization_required_code(),
       
   605 				)
       
   606 			);
       
   607 		}
       
   608 
       
   609 		return true;
       
   610 	}
   613 	}
   611 
   614 
   612 	/**
   615 	/**
   613 	 * Returns the given theme global styles variations.
   616 	 * Returns the given theme global styles variations.
   614 	 *
   617 	 *
   628 				__( 'Theme not found.' ),
   631 				__( 'Theme not found.' ),
   629 				array( 'status' => 404 )
   632 				array( 'status' => 404 )
   630 			);
   633 			);
   631 		}
   634 		}
   632 
   635 
   633 		$response   = array();
   636 		$response = array();
   634 
   637 
   635 		// Register theme-defined variations e.g. from block style variation partials under `/styles`.
   638 		// Register theme-defined variations e.g. from block style variation partials under `/styles`.
   636 		$partials = WP_Theme_JSON_Resolver::get_style_variations( 'block' );
   639 		$partials = WP_Theme_JSON_Resolver::get_style_variations( 'block' );
   637 		wp_register_block_style_variations_from_theme_json_partials( $partials );
   640 		wp_register_block_style_variations_from_theme_json_partials( $partials );
   638 
   641