1010/**
1111 * Base Global Styles REST API Controller.
1212 */
13- class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
14-
15- /**
16- * Post type.
17- *
18- * @since 5.9.0
19- * @var string
20- */
21- protected $ post_type ;
22-
13+ class WP_REST_Global_Styles_Controller extends WP_REST_Posts_Controller {
2314 /**
2415 * Constructor.
16+ *
2517 * @since 5.9.0
18+ * @since 6.5.0 Extends class from WP_REST_Posts_Controller.
19+ *
20+ * @param string $post_type Post type.
2621 */
27- public function __construct () {
28- $ this ->namespace = 'wp/v2 ' ;
29- $ this ->rest_base = 'global-styles ' ;
30- $ this ->post_type = 'wp_global_styles ' ;
22+ public function __construct ( $ post_type ) {
23+ $ this ->post_type = $ post_type ;
24+ $ obj = get_post_type_object ( $ post_type );
25+ $ this ->rest_base = ! empty ( $ obj ->rest_base ) ? $ obj ->rest_base : $ obj ->name ;
26+ $ this ->namespace = ! empty ( $ obj ->rest_namespace ) ? $ obj ->rest_namespace : 'wp/v2 ' ;
3127 }
3228
3329 /**
@@ -194,7 +190,7 @@ public function get_item_permissions_check( $request ) {
194190 * @param WP_Post $post Post object.
195191 * @return bool Whether the post can be read.
196192 */
197- protected function check_read_permission ( $ post ) {
193+ public function check_read_permission ( $ post ) {
198194 return current_user_can ( 'read_post ' , $ post ->ID );
199195 }
200196
@@ -241,18 +237,6 @@ public function update_item_permissions_check( $request ) {
241237 return true ;
242238 }
243239
244- /**
245- * Checks if a global style can be edited.
246- *
247- * @since 5.9.0
248- *
249- * @param WP_Post $post Post object.
250- * @return bool Whether the post can be edited.
251- */
252- protected function check_update_permission ( $ post ) {
253- return current_user_can ( 'edit_post ' , $ post ->ID );
254- }
255-
256240 /**
257241 * Updates a single global style config.
258242 *
@@ -407,7 +391,7 @@ public function prepare_item_for_response( $post, $request ) {
407391 $ links = $ this ->prepare_links ( $ post ->ID );
408392 $ response ->add_links ( $ links );
409393 if ( ! empty ( $ links ['self ' ]['href ' ] ) ) {
410- $ actions = $ this ->get_available_actions ();
394+ $ actions = $ this ->get_available_actions ( $ post , $ request );
411395 $ self = $ links ['self ' ]['href ' ];
412396 foreach ( $ actions as $ rel ) {
413397 $ response ->add_link ( $ rel , $ self );
@@ -431,9 +415,12 @@ protected function prepare_links( $id ) {
431415 $ base = sprintf ( '%s/%s ' , $ this ->namespace , $ this ->rest_base );
432416
433417 $ links = array (
434- 'self ' => array (
418+ 'self ' => array (
435419 'href ' => rest_url ( trailingslashit ( $ base ) . $ id ),
436420 ),
421+ 'about ' => array (
422+ 'href ' => rest_url ( 'wp/v2/types/ ' . $ this ->post_type ),
423+ ),
437424 );
438425
439426 if ( post_type_supports ( $ this ->post_type , 'revisions ' ) ) {
@@ -457,10 +444,10 @@ protected function prepare_links( $id ) {
457444 *
458445 * @return array List of link relations.
459446 */
460- protected function get_available_actions () {
447+ protected function get_available_actions ( $ post , $ request ) {
461448 $ rels = array ();
462449
463- $ post_type = get_post_type_object ( $ this ->post_type );
450+ $ post_type = get_post_type_object ( $ post ->post_type );
464451 if ( current_user_can ( $ post_type ->cap ->publish_posts ) ) {
465452 $ rels [] = 'https://api.w.org/action-publish ' ;
466453 }
@@ -472,21 +459,6 @@ protected function get_available_actions() {
472459 return $ rels ;
473460 }
474461
475- /**
476- * Overwrites the default protected title format.
477- *
478- * By default, WordPress will show password protected posts with a title of
479- * "Protected: %s", as the REST API communicates the protected status of a post
480- * in a machine readable format, we remove the "Protected: " prefix.
481- *
482- * @since 5.9.0
483- *
484- * @return string Protected title format.
485- */
486- public function protected_title_format () {
487- return '%s ' ;
488- }
489-
490462 /**
491463 * Retrieves the query params for the global styles collection.
492464 *
0 commit comments