[wp-trac] [WordPress Trac] #20699: AJAX Actions now passes the action name as an arg
WordPress Trac
wp-trac at lists.automattic.com
Thu May 17 18:36:24 UTC 2012
#20699: AJAX Actions now passes the action name as an arg
----------------------------+--------------------------------------
Reporter: sivel | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: 3.4
Component: Administration | Version: 3.4
Severity: normal | Keywords: 2nd-opinion dev-feedback
----------------------------+--------------------------------------
Previous to the admin-ajax.php refactoring in #15327 the do_action calls
looked like:
{{{
do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
do_action( 'wp_ajax_' . $_GET['action'] );
do_action( 'wp_ajax_' . $_POST['action'] );
}}}
They now look like:
{{{
do_action( 'wp_ajax_' . $_REQUEST['action'], $_REQUEST['action'] );
do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'], $_REQUEST['action'] );
}}}
The change is to add the action name as an argument to do_action to pass
to the callback, where before there was none.
As a result,plugin authors were previously able to rely on passing an arg
to determine if the function was called by an ajax action or not based on
if the $arg was empty. Now the $arg is always populated, as we are
sending the action name as the argument, instead of allowing do_action to
send it's default empty string.
I had multiple plugins affected by this, where I was determining if the
call to the function was from the ajax call, and performing different
tasks. I don't have a problem with fixing it in my code, to perhaps do it
the right way like it should have been done from the beginning, but I am
worried that it could potentially cause issues with other plugins that may
be doing this.
Some sample code that started failing:
{{{
add_action( 'wp_ajax_nopriv_sivel', 'sivel' );
function sivel( $arg ) {
if ( $arg )
// Do something if an arg was passed
else
// Do something if an arg was not passed
}
}}}
In the above example, only the code under {{{if ( $arg )}}} is executed
since 3.4. Switching to something like {{{if ( $arg === true )}}} fixes
the issue in the plugin.
I also realize that passing the action as an arg gives us context for a
single callback called from multiple ajax actions, but that could also be
retrieved using {{{$_REQUEST['action']}}} from the callback anyway.
In case we should remove it, I've attached the diff.
--
Ticket URL: <http://core.trac.wordpress.org/ticket/20699>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list