@@ -109,13 +109,19 @@ public function set_renderer( Renderer $renderer ): void {
109109 }
110110
111111 /**
112- * Route the current request to the appropriate handler .
112+ * Check capability .
113113 */
114- public function route (): void {
115- // Check capability.
114+ protected function check_capability () {
116115 if ( ! current_user_can ( $ this ->config ->capability ) ) {
117116 wp_die ( __ ( 'You do not have permission to access this page. ' ) );
118117 }
118+ }
119+
120+ /**
121+ * Route the current request to the appropriate handler.
122+ */
123+ public function route (): void {
124+ $ this ->check_capability ();
119125
120126 $ action = $ this ->request ->get_current_action ();
121127 $ id = $ this ->request ->get_current_id ();
@@ -131,33 +137,51 @@ public function route(): void {
131137 }
132138
133139 /**
134- * Route plural (multi-entity) requests.
140+ * POST requests will trigger a redirect so they have to be processed
141+ * earlier than GET request, before any content is displayed
135142 *
136143 * @param string $action Current action.
137144 * @param int|null $id Entity ID.
138145 */
139- protected function route_plural ( string $ action , ?int $ id ): void {
140- // Handle POST submissions.
141- if ( $ this ->request ->is_post () ) {
142- switch ( $ action ) {
143- case 'create ' :
144- $ this ->handle_create_submit ();
146+ public function maybe_redirect (): void {
147+ $ this ->check_capability ();
148+
149+ if ( ! $ this ->request ->is_post () ) return ;
150+
151+ if ( $ this ->config ->is_singular () ) {
152+ $ this ->handle_settings_submit ();
153+ return ;
154+ }
155+
156+ $ action = $ this ->request ->get_current_action ();
157+ $ id = $ this ->request ->get_current_id ();
158+
159+ switch ( $ action ) {
160+ case 'create ' :
161+ $ this ->handle_create_submit ();
162+ return ;
163+ case 'edit ' :
164+ if ( $ id !== null ) {
165+ $ this ->handle_edit_submit ( $ id );
145166 return ;
146- case 'edit ' :
147- if ( $ id !== null ) {
148- $ this ->handle_edit_submit ( $ id );
149- return ;
150- }
151- break ;
152- case 'delete ' :
153- if ( $ id !== null ) {
154- $ this ->handle_delete ( $ id );
155- return ;
156- }
157- break ;
158- }
167+ }
168+ break ;
169+ case 'delete ' :
170+ if ( $ id !== null ) {
171+ $ this ->handle_delete ( $ id );
172+ return ;
173+ }
174+ break ;
159175 }
176+ }
160177
178+ /**
179+ * Route plural (multi-entity) requests.
180+ *
181+ * @param string $action Current action.
182+ * @param int|null $id Entity ID.
183+ */
184+ protected function route_plural ( string $ action , ?int $ id ): void {
161185 // Handle GET requests.
162186 switch ( $ action ) {
163187 case 'create ' :
@@ -187,11 +211,6 @@ protected function route_plural( string $action, ?int $id ): void {
187211 * Route singular (single-entity) requests.
188212 */
189213 protected function route_singular (): void {
190- if ( $ this ->request ->is_post () ) {
191- $ this ->handle_settings_submit ();
192- return ;
193- }
194-
195214 $ this ->render_settings_form ();
196215 }
197216
0 commit comments