Skip to content

Instantly share code, notes, and snippets.

@franz-josef-kaiser
Created May 29, 2013 20:30
Show Gist options
  • Select an option

  • Save franz-josef-kaiser/5673575 to your computer and use it in GitHub Desktop.

Select an option

Save franz-josef-kaiser/5673575 to your computer and use it in GitHub Desktop.
Test to prove that I can't register two callbacks to the same `wp_ajax_{$action}`
;( function( $, obj ) {
var uno = function( e ) {
e.preventDefault();
var $this = $( this );
$.ajax( {
type: "POST",
url: obj.ajaxurl
+ "?action=" + obj.action
+ "&" + obj.nonce_query_arg + "=" + obj.nonce,
nonce: obj.nonce,
data: {
obj: obj
},
beforeSend: function( jqXHR, settings ) {
console.log( 'beforeSend' );
}
} ).done( function( data, textStatus, jqXHR ) {
console.log( textStatus );
} ).fail( function( jqXHR, textStatus, errorThrown ) {
console.log( textStatus );
console.log( errorThrown );
} );
};
$.each( $( ".uno" ), function( index, el ) {
$( el ).on( 'click', uno );
} );
$.each( $( ".due" ), function( index, el ) {
$( el ).on( 'click', uno );
} );
} )( jQuery, user_actions_obj || {} );
<?php
/* Plugin Name: AJAX callback test */
# ======= ADD MARKUP TO LOOP
add_action( 'plugins_loaded', 'setup_test_plugin' );
function setup_test_plugin()
{
require_once 'classes.php';
foreach ( array( 'testUNO', 'testDUE' ) as $callback )
add_filter( 'the_content', $callback );
}
function testUNO()
{
print apply_filters(
'user_action_markup_uno',
get_the_ID()
);
}
function testDUE()
{
print apply_filters(
'user_action_markup_due',
get_the_ID()
);
}
<?php
# ======= PARENT CLASS
abstract class AbstractBaseClass
{
public $file_name = 'ajax';
public $handle = 'user_actions';
public $action = '';
public $nonce_val = 'user_actions';
public $nonce_query_arg = 'user_actions_nonce';
public $markup_filter = '';
public $classes = array(
'add' => 'theme-add-link',
'fav' => 'theme-fav-link',
'unfav' => 'theme-unfav-link',
'download' => 'theme-download-link',
'wait' => 'theme-wait-state',
'error' => 'theme-error-state',
'done' => 'theme-done-state',
);
public function __construct()
{
$this->action = "{$this->handle}_action";
add_action( 'wp_enqueue_scripts', array( $this, 'scripts' ) );
$this->markup_filter = $this->set_markup_filter_name();
add_filter( $this->markup_filter, array( $this, 'get_markup' ) );
}
public function scripts()
{
wp_enqueue_script(
$this->handle,
plugin_dir_url( __FILE__ )."/{$this->file_name}.js",
array( 'jquery' ),
null,
true
);
wp_localize_script(
$this->handle,
"{$this->handle}_obj",
array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'nonce_query_arg' => $this->nonce_query_arg,
'nonce' => wp_create_nonce( $this->nonce_val ),
'action' => $this->action,
'classes' => $this->classes,
)
);
}
abstract function set_markup_filter_name();
abstract function ajax_cb( $data );
public function check_ajax()
{
check_ajax_referer( $this->nonce_val, $this->nonce_query_arg );
if (
! isset( $_POST['obj']['action'] )
OR $this->action !== $_POST['obj']['action']
)
exit( -1 );
}
abstract function get_markup( $post_id );
}
# ======== EXTENDING CLASSES
add_action( 'plugins_loaded', array( 'ChildClassUNO', 'init' ), 30 );
class ChildClassUNO extends AbstractBaseClass
{
protected static $instance = null;
public static function init()
{
null === self::$instance AND self::$instance = new self;
return self::$instance;
}
public function __construct()
{
parent::__construct();
add_action( "wp_ajax_{$this->action}", array( $this, 'ajax_cb' ) );
add_action( "wp_ajax_nopriv_{$this->action}", array( $this, 'ajax_cb' ) );
}
public function set_markup_filter_name()
{
return 'user_action_markup_uno';
}
public function get_markup( $post_id )
{
return sprintf(
'<a href="#" class="uno" id="uno-%s"> UNO </a>',
$post_id
);
}
public function ajax_cb( $data )
{
print __CLASS__;
$this->check_ajax();
exit;
}
}
add_action( 'plugins_loaded', array( 'ChildClassDUE', 'init' ), 30 );
class ChildClassDUE extends AbstractBaseClass
{
protected static $instance = null;
public static function init()
{
null === self::$instance AND self::$instance = new self;
return self::$instance;
}
public function __construct()
{
parent::__construct();
add_action( "wp_ajax_{$this->action}", array( $this, 'ajax_cb' ) );
add_action( "wp_ajax_nopriv_{$this->action}", array( $this, 'ajax_cb' ) );
}
public function set_markup_filter_name()
{
return 'user_action_markup_due';
}
public function get_markup( $post_id )
{
return sprintf(
'<a href="#" class="due" id="due-%s"> DUE </a>',
$post_id
);
}
public function ajax_cb( $data )
{
print __CLASS__;
$this->check_ajax();
exit;
}
}
@bueltge
Copy link
Copy Markdown

bueltge commented May 29, 2013

and, it is possible?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment