Advertisement
Guest User

Untitled

a guest
Oct 17th, 2015
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 KB | None | 0 0
  1. <?php
  2. /**
  3. * WP Ajax Abstract Class.
  4. *
  5. * @package WPS_Core
  6. * @author Travis Smith <t@wpsmith.net>
  7. * @copyright 2014 Travis Smith
  8. * @license GPL-2.0+
  9. */
  10.  
  11. /**
  12. * WPS_Ajax class.
  13. *
  14. * @package WPS_Core
  15. * @author Travis Smith <t@wpsmith.net>
  16. */
  17. class WPS_Ajax {
  18.  
  19. /**
  20. * WP Nounce
  21. *
  22. * @var string
  23. */
  24. protected $nonce = '';
  25.  
  26. /**
  27. * WP AJAX Name
  28. *
  29. * @var string
  30. */
  31. protected $name = '';
  32.  
  33. /**
  34. * Whether to hook AJAX callback into front-end.
  35. *
  36. * @var string
  37. */
  38. protected $nopriv = false;
  39.  
  40. /**
  41. * Hook for scripts.
  42. * Could be: wp_enqueue_scripts or login_enqueue_scripts or admin_enqueue_scripts
  43. *
  44. * @var string
  45. */
  46. protected $script_hook = 'admin_enqueue_scripts';
  47.  
  48. /**
  49. * Whether to keep the WP Heartbeat script.
  50. *
  51. * @var string
  52. */
  53. protected $heartbeat = true;
  54.  
  55. /**
  56. * AJAX callback function name.
  57. *
  58. * @var string
  59. */
  60. protected $callback;
  61.  
  62. /**
  63. * Constructor
  64. *
  65. * @since 1.0.0
  66. * @author Travis Smith <t@wpsmith.net>
  67. *
  68. * @param string $name Name (lower-case, without spaces, use underscore) of the WP Action
  69. * @param array $script Array of script information: url, src (path), data (localized info).
  70.  
  71. * @return void.
  72. */
  73. public function __construct( $name, $script = array(), $callback ) {
  74. $this->name = str_replace( ' ', '_', strtolower( $name ) );
  75.  
  76. // if not doing ajax, load script
  77. if ( ( defined( 'DOING_AJAX' ) && !DOING_AJAX ) || !defined( 'DOING_AJAX' ) ) {
  78. $this->maybe_do_action( 'plugins_loaded', 'script' );
  79. }
  80.  
  81. // Hook up AJAX Action
  82. $this->maybe_do_action( 'plugins_loaded', 'init' );
  83.  
  84. // Hook into secured callback
  85. add_action( "{$this->name}_wp_ajax_action", $callback );
  86. }
  87.  
  88. /**
  89. * Sets object parameters.
  90. * Available parameters: nopriv, script_hook, callback
  91. *
  92. * @since 1.0.0
  93. * @author Travis Smith <t@wpsmith.net>
  94. *
  95. * @param string $param Parameter name.
  96. * @param mixed $value Value of parameter.
  97. * @return void.
  98. */
  99. public function set( $param, $value ) {
  100. switch ( $param ) {
  101. case 'nopriv':
  102. $this->nopriv = (bool) $value;
  103. break;
  104. case 'heartbeat':
  105. $this->heartbeat = (bool) $value;
  106. break;
  107. case 'script_hook':
  108. $hooks = array(
  109. 'wp_enqueue_scripts',
  110. 'login_enqueue_scripts',
  111. 'admin_enqueue_scripts',
  112. );
  113. if ( in_array( $value, $hooks ) ) {
  114. $this->script_hook = strtolower( $value );
  115. }
  116. break;
  117. case 'callback':
  118. if ( is_callable( $value ) ) {
  119. $this->callback = $value;
  120. }
  121. break;
  122. }
  123. }
  124.  
  125. /**
  126. * Gets object parameter.
  127. * Available parameters: nopriv, script_hook, callback
  128. *
  129. * @since 1.0.0
  130. * @author Travis Smith <t@wpsmith.net>
  131. *
  132. * @param string $param Parameter name.
  133. * @return mixed Value of parameter.
  134. */
  135. public function get( $param ) {
  136. switch ( $param ) {
  137. case 'nopriv':
  138. return (bool)$this->nopriv;
  139. case 'heartbeat':
  140. return (bool)$this->heartbeat;
  141. case 'script_hook':
  142. return (string)$this->script_hook;
  143. case 'callback':
  144. return (string)$this->callback;
  145. default:
  146. return null;
  147. }
  148. }
  149.  
  150. /**
  151. * Hooks up AJAX Action
  152. *
  153. * @since 1.0.0
  154. * @author Travis Smith <t@wpsmith.net>
  155. *
  156. * @return void.
  157. */
  158. protected function init() {
  159. add_action( "wp_ajax_{$this->name}_action", array( $this, 'callback' ) );
  160. if ( $this->nopriv ) {
  161. add_action( "wp_ajax_nopriv_{$this->name}_action", array( $this, 'callback' ) );
  162. }
  163. }
  164.  
  165. /**
  166. * Hooks action or executes action.
  167. *
  168. * @since 1.0.0
  169. * @author Travis Smith <t@wpsmith.net>
  170. *
  171. * @param string WordPress action to be checked with did_action().
  172. * @param string|array Function name/array to be called.
  173. * @return void.
  174. */
  175. private function maybe_do_action( $hook, $action ) {
  176. if ( !did_action( $hook ) ) {
  177. add_action( $hook, array( $this, $action ) );
  178. } elseif ( is_callable( $action ) ) {
  179. call_user_func( $action );
  180. }
  181. }
  182.  
  183. /**
  184. * Performs script operations: register, localize, and enqueue.
  185. *
  186. * @since 1.0.0
  187. * @author Travis Smith <t@wpsmith.net>
  188. *
  189. * @return void.
  190. */
  191. public function script() {
  192. // Register Script
  193. add_action( 'wp_loaded', array( $this, 'register' ) );
  194.  
  195. // Make sure we hook scripts in proper place & prevent user error.
  196. if ( $this->nopriv && 'admin_enqueue_scripts' === $this->script_hook ) {
  197. $this->script_hook = 'wp_enqueue_scripts';
  198. }
  199.  
  200. // Go SCRIPT!
  201. add_action( $this->script_hook, array( $this, 'localize' ) );
  202. add_action( $this->script_hook, array( $this, 'enqueue' ) );
  203.  
  204. if ( !$this->heartbeat ) {
  205. add_action( 'admin_enqueue_scripts', array( $this, 'no_heartbeat' ) );
  206. }
  207. }
  208.  
  209. /**
  210. * Properly Registers AJAX script.
  211. *
  212. * @since 1.0.0
  213. * @author Travis Smith <t@wpsmith.net>
  214. *
  215. * @return void.
  216. */
  217. public function register() {
  218. wp_register_script(
  219. $this->name,
  220. $this->script['url'],
  221. $this->script['deps'],
  222. filemtime( $this->script['src'] ),
  223. true
  224. );
  225. }
  226.  
  227. /**
  228. * Properly Enqueues AJAX script.
  229. *
  230. * @since 1.0.0
  231. * @author Travis Smith <t@wpsmith.net>
  232. *
  233. * @return void.
  234. */
  235. public function enqueue() {
  236. wp_enqueue_script( $this->name );
  237. }
  238.  
  239. /**
  240. * Properly provides localized data for the action.
  241. *
  242. * @since 1.0.0
  243. * @author Travis Smith <t@wpsmith.net>
  244. *
  245. * @return void.
  246. */
  247. public function localize() {
  248. // Get JS object name
  249. $object = isset( $this->script['data_object'] ) ? $this->script['data_object'] : $this->name;
  250. $this->nonce = wp_create_nonce( "{$this->name}_nonce" );
  251. $data = wp_parse_args( $this->script['data'], array(
  252. 'ajaxurl' => admin_url( 'admin-ajax.php' ),
  253. '_ajax_nonce' => $this->nonce,
  254. 'action' => "{$this->name}_action",
  255. 'screen_id' => get_current_screen()->id,
  256. ));
  257. wp_localize_script( $this->name, "$object", $data );
  258. }
  259.  
  260. public function no_heartbeat() {
  261. wp_deregister_script('heartbeat');
  262. wp_register_script('heartbeat', false);
  263. }
  264.  
  265. /**
  266. * Does proper AJAX security check & then calls "{$this->name}_wp_ajax_action" action.
  267. *
  268. * @since 1.0.0
  269. * @author Travis Smith <t@wpsmith.net>
  270. *
  271. * @return void.
  272. */
  273. public function _callback() {
  274. $data = array_map( 'esc_attr', $_GET );
  275. ! check_ajax_referer( $data['action'], "_ajax_nonce", false )
  276. AND wp_send_json_error();
  277.  
  278. do_action( "{$this->name}_wp_ajax_action", $data );
  279. }
  280.  
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement