Last active
August 29, 2015 14:24
-
-
Save adamplabarge/19f57bc5abcd7ca468bd to your computer and use it in GitHub Desktop.
Ajax API for WP - setup for PSR4 autoload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.ajax({ | |
type : "post", | |
dataType : 'json', | |
url : connect.ajaxurl, | |
data : ({ action: 'connect', nonce: nonce, method: requestedMethod, params: dataParams }), | |
success: function(response) { | |
console.log( response ); | |
}, | |
error: function(x, t, m) { | |
console.log(x + t + m); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Classes; | |
/*////////////////////////////////////////////////////////////////////////////// | |
================================================================================ | |
Ajax_Controller | |
- registers, enqueues, localizes ajax js file | |
- passes method and params to Ajax_Function_Suite(); | |
- returns $ajax_function_suite->respond() as JSON | |
- don't forget nonce | |
================================================================================ | |
//////////////////////////////////////////////////////////////////////////////*/ | |
class Ajax_Controller { | |
protected $method; | |
protected $params = []; | |
/** | |
* Add $this->connect() to wp_ajax_nopriv & wp_ajax | |
* Add script enqueue | |
*/ | |
public function __construct () | |
{ | |
add_action( 'wp_ajax_nopriv_connect', array ( $this, 'connect' ) ); | |
add_action( 'wp_ajax_connect', array ( $this, 'connect' ) ); | |
add_action( 'wp_enqueue_scripts', array ( $this, 'scripts' ) ); | |
} | |
/** | |
* Register, enqueue and localize ajax.js script | |
* Edit to you needs | |
*/ | |
public function scripts () | |
{ | |
wp_register_script( 'ajax', get_stylesheet_directory_uri() . '/assets/js/ajax.js', array( 'jquery' ), '1.0.0', true); | |
wp_enqueue_script( 'ajax' ); | |
wp_localize_script( 'ajax', 'connect', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); | |
} | |
/** | |
* Capture Ajax method request and return JSON | |
* @param $_REQUEST['nonce'] | |
* @param $_REQUEST['method'] | |
* @param $_REQUEST['params'] | |
* @return JSON | |
*/ | |
public function connect () | |
{ | |
if( !wp_verify_nonce( $_REQUEST['nonce'], 'ajax_user_nonce' ) ) | |
exit('You shall not pass!'); | |
$this->method = (isset($_REQUEST['method'])) | |
? $_REQUEST['method'] | |
: null; | |
$this->params = (isset($_REQUEST['params'])) | |
? $_REQUEST['params'] | |
: []; | |
// initialize Ajax_Function_Suite obj | |
$ajax_function_suite = new Ajax_Function_Suite(); | |
exit( | |
json_encode( | |
[$ajax_function_suite->respond( $this->method, $this->params )] | |
) | |
); | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Classes; | |
/*////////////////////////////////////////////////////////////////////////////// | |
================================================================================ | |
Ajax_Function_Suite | |
- compares $method to known list of methods | |
- match found, run method, return result | |
- no match found, return list of known methods | |
- add your methods at bottom | |
================================================================================ | |
//////////////////////////////////////////////////////////////////////////////*/ | |
class Ajax_Function_Suite { | |
protected $params = []; | |
protected $class_methods = []; | |
/** | |
* Create list of internal methods | |
*/ | |
public function __construct () | |
{ | |
$this->class_methods = get_class_methods( 'Classes\Ajax_Function_Suite' ); | |
} | |
/** | |
* Is method in list, else return list of methods | |
* @param $method - string to find the method to call | |
* @param $params - data for method if need be | |
* @return response back to Ajax_Controller | |
*/ | |
public function respond ( $method, $params ) | |
{ | |
if( in_array( $method, $this->class_methods ) ) : | |
$this->params = $params; | |
return $this->{$method}(); | |
endif; | |
if( ! in_array( $method, $this->class_methods ) ) | |
return $this->method_not_found_repond(); | |
if( $method === null ) | |
return $this->method_not_found_repond(); | |
} | |
/** | |
* Return list of known methods | |
* @return array | |
*/ | |
protected function method_not_found_repond () | |
{ | |
return $this->class_methods; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment