WPDK  1.5.0
WordPress Development Kit
 All Data Structures Files Functions Variables Pages
wpdk-ajax.php
Go to the documentation of this file.
1 <?php
2 
3 if ( wpdk_is_ajax() ) {
4 
26  class WPDKAjax {
27 
35  public function __construct()
36  {
37  $this->registerActions();
38  }
39 
46  public function registerActions()
47  {
48  $actions = $this->actions();
49  foreach ( $actions as $method => $nopriv ) {
50  add_action( 'wp_ajax_' . $method, array( $this, $method ) );
51  if ( $nopriv ) {
52  add_action( 'wp_ajax_nopriv_' . $method, array( $this, $method ) );
53  }
54  }
55  }
56 
67  public static function add( $method, $callable, $nopriv = false )
68  {
69  add_action( 'wp_ajax_' . $method, $callable );
70  if ( $nopriv ) {
71  add_action( 'wp_ajax_nopriv_' . $method, $callable );
72  }
73  }
74 
83  protected function actions()
84  {
85  /* To override. */
86  return array();
87  }
88 
89  } // class WPDKAjax
90 
91 
103  class WPDKAjaxResponse extends WPDKObject {
104 
112  public $__version = '1.0.3';
113 
121  public $error = '';
122 
130  public $message = '';
131 
139  public $data = '';
140 
148  public function __construct()
149  {
150  }
151 
157  public function json()
158  {
159  header( 'Cache-Control: no-cache, must-revalidate' );
160  header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
161  header( 'Content-Type: application/json' );
162 
163  echo json_encode( $this );
164 
165  if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
166  wp_die();
167  }
168  else {
169  die();
170  }
171  }
172 
173  }
174 
175 
176 }