Skip to content

Instantly share code, notes, and snippets.

@stephenharris
Forked from franz-josef-kaiser/ajax.js
Last active December 17, 2015 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenharris/5673756 to your computer and use it in GitHub Desktop.
Save stephenharris/5673756 to your computer and use it in GitHub Desktop.
;( 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 ajax_exit_cb( $data ){
exit();
}
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' ) );
add_action( "wp_ajax_{$this->action}", array( $this, 'ajax_exit_cb' ), 11 );
add_action( "wp_ajax_nopriv_{$this->action}", array( $this, 'ajax_exit_cb' ), 11 );
}
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();
}
}
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();
}
}
@stephenharris
Copy link
Author

Removed exit() calls from child classes. Moved it into base class which calls it after other (child class) callbacks have been called.

@stephenharris
Copy link
Author

Given the ajax_exit_cb() method a lower priority (higher number) just for good measure. Must run after all child class callbacks on the same hook.

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