Skip to content

Commit 6b325e9

Browse files
committed
Hooks: Standardize naming of dynamic hooks using values derived from superglobals to use interpolation vs concatenation.
This is a continuation of the work that happened in [38307] for #37748. Props ramiy. Fixes #42698. git-svn-id: https://develop.svn.wordpress.org/trunk@42349 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 5d45b61 commit 6b325e9

4 files changed

Lines changed: 28 additions & 20 deletions

File tree

src/wp-admin/admin-ajax.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,36 +146,38 @@
146146

147147
add_action( 'wp_ajax_nopriv_heartbeat', 'wp_ajax_nopriv_heartbeat', 1 );
148148

149+
$action = ( isset( $_REQUEST['action'] ) ) ? $_REQUEST['action'] : '';
150+
149151
if ( is_user_logged_in() ) {
150152
// If no action is registered, return a Bad Request response.
151-
if ( ! has_action( 'wp_ajax_' . $_REQUEST['action'] ) ) {
153+
if ( ! has_action( "wp_ajax_{$action}" ) ) {
152154
wp_die( '0', 400 );
153155
}
154156

155157
/**
156158
* Fires authenticated Ajax actions for logged-in users.
157159
*
158-
* The dynamic portion of the hook name, `$_REQUEST['action']`,
159-
* refers to the name of the Ajax action callback being fired.
160+
* The dynamic portion of the hook name, `$action`, refers
161+
* to the name of the Ajax action callback being fired.
160162
*
161163
* @since 2.1.0
162164
*/
163-
do_action( 'wp_ajax_' . $_REQUEST['action'] );
165+
do_action( "wp_ajax_{$action}" );
164166
} else {
165167
// If no action is registered, return a Bad Request response.
166-
if ( ! has_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] ) ) {
168+
if ( ! has_action( "wp_ajax_nopriv_{$action}" ) ) {
167169
wp_die( '0', 400 );
168170
}
169171

170172
/**
171173
* Fires non-authenticated Ajax actions for logged-out users.
172174
*
173-
* The dynamic portion of the hook name, `$_REQUEST['action']`,
174-
* refers to the name of the Ajax action callback being fired.
175+
* The dynamic portion of the hook name, `$action`, refers
176+
* to the name of the Ajax action callback being fired.
175177
*
176178
* @since 2.8.0
177179
*/
178-
do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
180+
do_action( "wp_ajax_nopriv_{$action}" );
179181
}
180182
// Default status
181183
wp_die( '0' );

src/wp-admin/admin.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,13 +372,15 @@
372372
}
373373

374374
if ( ! empty( $_REQUEST['action'] ) ) {
375+
$action = $_REQUEST['action'];
376+
375377
/**
376378
* Fires when an 'action' request variable is sent.
377379
*
378-
* The dynamic portion of the hook name, `$_REQUEST['action']`,
379-
* refers to the action derived from the `GET` or `POST` request.
380+
* The dynamic portion of the hook name, `$action`, refers to
381+
* the action derived from the `GET` or `POST` request.
380382
*
381383
* @since 2.6.0
382384
*/
383-
do_action( 'admin_action_' . $_REQUEST['action'] );
385+
do_action( "admin_action_{$action}" );
384386
}

src/wp-admin/network/edit.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
/** Load WordPress Administration Bootstrap */
1111
require_once( dirname( __FILE__ ) . '/admin.php' );
1212

13-
if ( empty( $_GET['action'] ) ) {
13+
$action = ( isset( $_GET['action'] ) ) ? $_GET['action'] : '';
14+
15+
if ( empty( $action ) ) {
1416
wp_redirect( network_admin_url() );
1517
exit;
1618
}
@@ -28,12 +30,12 @@
2830
/**
2931
* Fires the requested handler action.
3032
*
31-
* The dynamic portion of the hook name, `$_GET['action']`, refers to the name
32-
* of the requested action.
33+
* The dynamic portion of the hook name, `$action`, refers to the name
34+
* of the requested action derived from the `GET` request.
3335
*
3436
* @since 3.1.0
3537
*/
36-
do_action( 'network_admin_edit_' . $_GET['action'] );
38+
do_action( "network_admin_edit_{$action}" );
3739

3840
wp_redirect( network_admin_url() );
3941
exit();

src/wp-admin/network/sites.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,9 @@
278278

279279
$msg = '';
280280
if ( isset( $_GET['updated'] ) ) {
281-
switch ( $_GET['updated'] ) {
281+
$action = $_GET['updated'];
282+
283+
switch ( $action ) {
282284
case 'all_notspam':
283285
$msg = __( 'Sites removed from spam.' );
284286
break;
@@ -314,16 +316,16 @@
314316
break;
315317
default:
316318
/**
317-
* Filters a specific, non-default site-updated message in the Network admin.
319+
* Filters a specific, non-default, site-updated message in the Network admin.
318320
*
319-
* The dynamic portion of the hook name, `$_GET['updated']`, refers to the
320-
* non-default site update action.
321+
* The dynamic portion of the hook name, `$action`, refers to the non-default
322+
* site update action.
321323
*
322324
* @since 3.1.0
323325
*
324326
* @param string $msg The update message. Default 'Settings saved'.
325327
*/
326-
$msg = apply_filters( 'network_sites_updated_message_' . $_GET['updated'], __( 'Settings saved.' ) );
328+
$msg = apply_filters( "network_sites_updated_message_{$action}", __( 'Settings saved.' ) );
327329
break;
328330
}
329331

0 commit comments

Comments
 (0)