Skip to content

Commit

Permalink
Pulling across changes from #6022
Browse files Browse the repository at this point in the history
Co-authored-by: ramonjd <[email protected]>
Co-authored-by: mukeshpanchal27 <[email protected]>
Co-authored-by: spacedmonkey <[email protected]>
  • Loading branch information
4 people committed Feb 7, 2024
1 parent f7cf55a commit d4d7e82
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 1,048 deletions.
3 changes: 2 additions & 1 deletion src/wp-includes/class-wp-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,7 @@ public function get_revisions_rest_controller() {
* Will only instantiate the controller class once per request.
*
* @since 6.4.0
* @since 6.5.0 Prevents autosave class instantiation for wp_global_styles post types.
*
* @return WP_REST_Controller|null The controller instance, or null if the post type
* is set not to show in rest.
Expand All @@ -922,7 +923,7 @@ public function get_autosave_rest_controller() {
return null;
}

if ( 'attachment' === $this->name ) {
if ( in_array( $this->name, array( 'attachment', 'wp_global_styles' ), true ) ) {
return null;
}

Expand Down
26 changes: 15 additions & 11 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,15 +473,19 @@ function create_initial_post_types() {
register_post_type(
'wp_global_styles',
array(
'label' => _x( 'Global Styles', 'post type general name' ),
'description' => __( 'Global styles to include in themes.' ),
'public' => false,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'_edit_link' => '/site-editor.php?canvas=edit', /* internal use only. don't use this when registering your own post type. */
'show_ui' => false,
'show_in_rest' => false,
'rewrite' => false,
'capabilities' => array(
'label' => _x( 'Global Styles', 'post type general name' ),
'description' => __( 'Global styles to include in themes.' ),
'public' => false,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'_edit_link' => '/site-editor.php?canvas=edit', /* internal use only. don't use this when registering your own post type. */
'show_ui' => false,
'show_in_rest' => true,
'rewrite' => false,
'rest_base' => 'global-styles',
'rest_controller_class' => 'WP_REST_Global_Styles_Controller',
'revisions_rest_controller_class' => 'WP_REST_Global_Styles_Revisions_Controller',
'late_route_registration' => true,
'capabilities' => array(
'read' => 'edit_theme_options',
'create_posts' => 'edit_theme_options',
'edit_posts' => 'edit_theme_options',
Expand All @@ -490,8 +494,8 @@ function create_initial_post_types() {
'edit_others_posts' => 'edit_theme_options',
'delete_others_posts' => 'edit_theme_options',
),
'map_meta_cap' => true,
'supports' => array(
'map_meta_cap' => true,
'supports' => array(
'title',
'editor',
'revisions',
Expand Down
8 changes: 0 additions & 8 deletions src/wp-includes/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,6 @@ function create_initial_rest_routes() {
$controller = new WP_REST_Block_Types_Controller();
$controller->register_routes();

// Global Styles revisions.
$controller = new WP_REST_Global_Styles_Revisions_Controller();
$controller->register_routes();

// Global Styles.
$controller = new WP_REST_Global_Styles_Controller();
$controller->register_routes();

// Settings.
$controller = new WP_REST_Settings_Controller();
$controller->register_routes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,20 @@
/**
* Base Global Styles REST API Controller.
*/
class WP_REST_Global_Styles_Controller extends WP_REST_Controller {

/**
* Post type.
*
* @since 5.9.0
* @var string
*/
protected $post_type;

class WP_REST_Global_Styles_Controller extends WP_REST_Posts_Controller {
/**
* Constructor.
*
* @since 5.9.0
* @since 6.5.0 Extends class from WP_REST_Posts_Controller.
*
* @param string $post_type Post type.
*/
public function __construct() {
$this->namespace = 'wp/v2';
$this->rest_base = 'global-styles';
$this->post_type = 'wp_global_styles';
public function __construct( $post_type ) {
$this->post_type = $post_type;
$obj = get_post_type_object( $post_type );
$this->rest_base = ! empty( $obj->rest_base ) ? $obj->rest_base : $obj->name;
$this->namespace = ! empty( $obj->rest_namespace ) ? $obj->rest_namespace : 'wp/v2';
}

/**
Expand Down Expand Up @@ -194,7 +190,7 @@ public function get_item_permissions_check( $request ) {
* @param WP_Post $post Post object.
* @return bool Whether the post can be read.
*/
protected function check_read_permission( $post ) {
public function check_read_permission( $post ) {
return current_user_can( 'read_post', $post->ID );
}

Expand Down Expand Up @@ -241,18 +237,6 @@ public function update_item_permissions_check( $request ) {
return true;
}

/**
* Checks if a global style can be edited.
*
* @since 5.9.0
*
* @param WP_Post $post Post object.
* @return bool Whether the post can be edited.
*/
protected function check_update_permission( $post ) {
return current_user_can( 'edit_post', $post->ID );
}

/**
* Updates a single global style config.
*
Expand Down Expand Up @@ -407,7 +391,7 @@ public function prepare_item_for_response( $post, $request ) {
$links = $this->prepare_links( $post->ID );
$response->add_links( $links );
if ( ! empty( $links['self']['href'] ) ) {
$actions = $this->get_available_actions();
$actions = $this->get_available_actions( $post, $request );
$self = $links['self']['href'];
foreach ( $actions as $rel ) {
$response->add_link( $rel, $self );
Expand All @@ -431,9 +415,12 @@ protected function prepare_links( $id ) {
$base = sprintf( '%s/%s', $this->namespace, $this->rest_base );

$links = array(
'self' => array(
'self' => array(
'href' => rest_url( trailingslashit( $base ) . $id ),
),
'about' => array(
'href' => rest_url( 'wp/v2/types/' . $this->post_type ),
),
);

if ( post_type_supports( $this->post_type, 'revisions' ) ) {
Expand All @@ -457,10 +444,10 @@ protected function prepare_links( $id ) {
*
* @return array List of link relations.
*/
protected function get_available_actions() {
protected function get_available_actions( $post, $request ) {
$rels = array();

$post_type = get_post_type_object( $this->post_type );
$post_type = get_post_type_object( $post->post_type );
if ( current_user_can( $post_type->cap->publish_posts ) ) {
$rels[] = 'https://api.w.org/action-publish';
}
Expand All @@ -472,21 +459,6 @@ protected function get_available_actions() {
return $rels;
}

/**
* Overwrites the default protected title format.
*
* By default, WordPress will show password protected posts with a title of
* "Protected: %s", as the REST API communicates the protected status of a post
* in a machine readable format, we remove the "Protected: " prefix.
*
* @since 5.9.0
*
* @return string Protected title format.
*/
public function protected_title_format() {
return '%s';
}

/**
* Retrieves the query params for the global styles collection.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,21 @@ class WP_REST_Global_Styles_Revisions_Controller extends WP_REST_Revisions_Contr
* Constructor.
*
* @since 6.3.0
* @since 6.5.0 Extends class from WP_REST_Revisions_Controller.
*/
public function __construct() {
parent::__construct( 'wp_global_styles' );
$this->parent_controller = new WP_REST_Global_Styles_Controller();
public function __construct( $parent_post_type ) {
parent::__construct( $parent_post_type );
$post_type_object = get_post_type_object( $parent_post_type );
$parent_controller = $post_type_object->get_rest_controller();

if ( ! $parent_controller ) {
$parent_controller = new WP_REST_Global_Styles_Controller( $parent_post_type );
}

$this->parent_controller = $parent_controller;
$this->rest_base = 'revisions';
$this->parent_base = $this->parent_controller->rest_base;
$this->namespace = $this->parent_controller->namespace;
$this->parent_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
$this->namespace = ! empty( $post_type_object->rest_namespace ) ? $post_type_object->rest_namespace : 'wp/v2';
}

/**
Expand Down
Loading

0 comments on commit d4d7e82

Please sign in to comment.