Skip to content

Commit 51c473e

Browse files
RequestRouter: POST Requests - Process POST request inside admin_init (#35)
* RequestRouter: POST Requests - Make sure to process POST request early enough as they trigger redirections * RequestRouter: POST Requests - Switch to new request object inside the maybe_redirection
1 parent 6c82bca commit 51c473e

2 files changed

Lines changed: 49 additions & 28 deletions

File tree

src/DataView/DataView.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ public function register(): static {
295295
] );
296296
}
297297

298+
add_action( 'admin_init', [ $this->router, 'maybe_redirect' ] );
299+
298300
// Register admin menu.
299301
$this->register_admin_menu();
300302

src/DataView/RequestRouter.php

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)