2828
2929namespace Google \Web_Stories \REST_API ;
3030
31- use Google \Web_Stories \Infrastructure \Delayed ;
32- use Google \Web_Stories \Infrastructure \HasRequirements ;
33- use Google \Web_Stories \Infrastructure \Registerable ;
34- use Google \Web_Stories \Infrastructure \Service ;
35- use Google \Web_Stories \Story_Post_Type ;
3631use WP_Post ;
3732use WP_REST_Autosaves_Controller ;
3833use WP_REST_Controller ;
4338/**
4439 * Stories_Autosaves_Controller class.
4540 *
46- * Register using register_post_type once https://core.trac.wordpress.org/ticket/56922 is committed.
47- *
4841 * @phpstan-import-type Schema from \Google\Web_Stories\REST_API\Stories_Base_Controller
4942 */
50- class Stories_Autosaves_Controller extends WP_REST_Autosaves_Controller implements Service, Delayed, Registerable, HasRequirements {
43+ class Stories_Autosaves_Controller extends WP_REST_Autosaves_Controller {
5144
5245 /**
5346 * Parent post controller.
@@ -64,58 +57,28 @@ class Stories_Autosaves_Controller extends WP_REST_Autosaves_Controller implemen
6457 *
6558 * @since 1.0.0
6659 *
67- * @param Story_Post_Type $story_post_type Story_Post_Type instance.
68- */
69- public function __construct ( Story_Post_Type $ story_post_type ) {
70- parent ::__construct ( $ story_post_type ->get_slug () );
71-
72- $ this ->parent_controller = $ story_post_type ->get_parent_controller ();
73- $ this ->parent_base = $ story_post_type ->get_rest_base ();
74- $ this ->namespace = $ story_post_type ->get_rest_namespace ();
75- }
76-
77- /**
78- * Register the service.
79- *
80- * @since 1.7.0
60+ * @param string $parent_post_type Post type of the parent.
8161 */
82- public function register (): void {
83- $ this ->register_routes ();
84- }
62+ public function __construct ( string $ parent_post_type ) {
63+ parent ::__construct ( $ parent_post_type );
8564
86- /**
87- * Get the action to use for registering the service.
88- *
89- * @since 1.7.0
90- *
91- * @return string Registration action to use.
92- */
93- public static function get_registration_action (): string {
94- return 'rest_api_init ' ;
95- }
65+ /**
66+ * Post type instance.
67+ *
68+ * @var \WP_Post_Type $post_type_object
69+ */
70+ $ post_type_object = get_post_type_object ( $ parent_post_type );
9671
97- /**
98- * Get the action priority to use for registering the service.
99- *
100- * @since 1.7.0
101- *
102- * @return int Registration action priority to use.
103- */
104- public static function get_registration_action_priority (): int {
105- return 100 ;
106- }
72+ /**
73+ * Parent controller instance.
74+ *
75+ * @var WP_REST_Controller $parent_controller
76+ */
77+ $ parent_controller = $ post_type_object ->get_rest_controller ();
10778
108- /**
109- * Get the list of service IDs required for this service to be registered.
110- *
111- * Needed because the story post type needs to be registered first.
112- *
113- * @since 1.13.0
114- *
115- * @return string[] List of required services.
116- */
117- public static function get_requirements (): array {
118- return [ 'story_post_type ' ];
79+ $ this ->parent_controller = $ parent_controller ;
80+ $ this ->parent_base = ! empty ( $ post_type_object ->rest_base ) ? (string ) $ post_type_object ->rest_base : $ post_type_object ->name ;
81+ $ this ->namespace = ! empty ( $ post_type_object ->rest_namespace ) ? (string ) $ post_type_object ->rest_namespace : 'wp/v2 ' ;
11982 }
12083
12184 /**
@@ -195,6 +158,7 @@ public function prepare_item_for_response( $post, $request ): WP_REST_Response {
195158 $ response = new WP_REST_Response ( $ data );
196159 foreach ( $ links as $ rel => $ rel_links ) {
197160 foreach ( $ rel_links as $ link ) {
161+ // @phpstan-ignore method.internal (false positive)
198162 $ response ->add_link ( $ rel , $ link ['href ' ], $ link ['attributes ' ] );
199163 }
200164 }
0 commit comments