PluginProbe
CSS & JavaScript Toolbox / 9.4
CSS & JavaScript Toolbox v9.4
trunk 0.3 0.8 10 10.1 11 11.2 11.3 11.4 11.5 11.6 11.7 11.8 11.9 11.9.1 12 12.0 12.0.1 12.0.3 12.0.4 12.0.5 12.0.6 12.0.7 6.0 6.0.11 All 60 releases
css-javascript-toolbox / framework / mvc / controller-ajax.inc.php

controller-ajax.inc.php in CSS & JavaScript Toolbox 9.4, at framework/mvc/controller-ajax.inc.php

211 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 *
4 */
5
6 /**
7 *
8 */
9 abstract class CJTAjaxController extends CJTController {
10
11 /** */
12 const ACTION_PREFIX = 'wp_ajax_cjtoolbox_';
13
14 /**
15 * put your comment there...
16 *
17 * @var mixed
18 */
19 protected $methodName;
20
21 /**
22 * put your comment there...
23 *
24 * @var mixed
25 */
26 protected $actionsMap = array();
27
28 /**
29 * put your comment there...
30 *
31 * @var mixed
32 */
33 protected $defaultCapability = array('administrator');
34
35 /**
36 * put your comment there...
37 *
38 * @var mixed
39 */
40 protected $impersonated = false;
41
42 /**
43 * put your comment there...
44 *
45 * @var mixed
46 */
47 public $httpCode = '200 OK';
48
49 /**
50 * put your comment there...
51 *
52 * @var mixed
53 */
54 public $httpContentType = 'text/plain';
55
56 /**
57 * put your comment there...
58 *
59 * @var mixed
60 */
61 protected $onauthorize = array('parameters' => array('authorized'));
62
63 /**
64 * put your comment there...
65 *
66 * @var mixed
67 */
68 protected $onregisteraction = array('parameters' => array('callback', 'action'));
69
70 /**
71 * put your comment there...
72 *
73 * @var mixed
74 */
75 protected $onresponse = array('hookType' => CJTWordpressEvents::HOOK_ACTION);
76
77 /**
78 * put your comment there...
79 *
80 * @var mixed
81 */
82 public $response = false;
83
84 /**
85 * put your comment there...
86 *
87 */
88 public function _doAction() {
89 // Authorize request.
90 $authorized = $this->onauthorize(check_ajax_referer(self::NONCE_ACTION, 'security', false));
91 if (!$authorized || !call_user_func_array('current_user_can', $this->defaultCapability)) {
92 $this->httpCode = "403 Not Authorized";
93 }
94 else {
95 // Clear Wordpress scripts object when using AJAX
96 // This is to solve conflict caused by other Plugins
97 // That uses admin_init hook to enqueue their scripts
98 // Ajax request here is only for CJT no other Plugin
99 // should involve any scripts with CJT
100 // Remove all scritps enqueued before CJT Ajax Controller (this) received
101 // the controller!
102 global $wp_scripts;
103 $wp_scripts = new WP_Scripts();
104 // Dispatch action.
105 $action = current_filter();
106 // Get method name from frrom Wordpress action name!
107 $method = $this->ongetactionname(str_replace(self::ACTION_PREFIX, '', $action));
108 // Get method name from action name.
109 $method = ucfirst(str_replace('_', ' ', $method));
110 $method = str_replace(' ', '', $method);
111 // Lower case the first character.
112 $method = strtolower($method{0}) . substr($method, 1);
113 // Cahe method name for child classes to use!
114 $this->methodName = $method;
115 // Relying on the trailer "Action" for security.
116 // Derivded class should not never use trailer "Action"
117 // for internal methods.
118 $method = "{$method}Action";
119 // If its mapped from child classed redirect the call!
120 if ((isset($this->actionsMap[$action]) && ($use = $action)) || (isset($this->actionsMap[$method]) && ($use = $method))) {
121 $method = $this->actionsMap[$use];
122 }
123 // Filter callback method and args.
124 $callback = $this->oncallback((object) array('method' => array($this, $method), 'args' => func_get_args()), $action);
125 // Call Action Method.
126 if (!is_callable($callback->method)) {
127 $this->httpCode = '403 Not supported action';
128 }
129 else {
130 // When there are no arguments passed to Wordpress action.
131 // do_action function pass an empty string parameter at index 0.
132 if (count($callback->args) == 1 && ($callback->args[0] == '')) {
133 $callback->args = array();
134 }
135 call_user_func_array($callback->method, $callback->args);
136 // Controller loaded with Wordpress typical Ajax request (e.g meta-box-order).
137 // We shouldn't output anything in these cases.
138 if ($this->impersonated) {
139 return;
140 }
141 }
142 }
143 // Set HTTP headers.
144 header("HTTP/1.0 {$this->httpCode}");
145 header("Content-Type: {$this->httpContentType}");
146 // Allow filtering any response data/header!
147 $this->onresponse();
148 // Output type based on the content type MIME.
149 switch ($this->httpContentType) {
150 case 'text/plain':
151 echo json_encode($this->response);
152 break;
153
154 default :
155 echo $this->response;
156 }
157 die();
158 }
159
160 /**
161 * Display templates manager form.
162 *
163 */
164 protected function displayAction() {
165 // Return view.
166 $this->httpContentType = 'text/html';
167 $this->response = parent::displayAction();
168 }
169
170 /**
171 * Redirect the request to another controller.
172 *
173 * Why this method is created anyway is to allow
174 * deprecating old controllers and start to create new one
175 * a quiet manner!
176 *
177 * The idea is to create the new controller, adding new Action there
178 * and redirect the call through current deprecated controller.
179 *
180 * @param mixed $controller
181 */
182 protected function redirect($controller) {
183 // Initialize vars.
184 $currentFilter= current_filter();
185 // Remove current Action!
186 remove_action($currentFilter, array(&$this, '_doAction'));
187 // activate the target CTR!
188 CJTController::getInstance($controller);
189 // Fire the action manually.
190 do_action($currentFilter);
191 }
192
193 /**
194 * put your comment there...
195 *
196 * @param mixed $action
197 * @param mixed $priority
198 * @param mixed $paramsCount
199 */
200 protected function registryAction($action, $priority = 10, $paramsCount = 1, $prefix = self::ACTION_PREFIX) {
201 $action = "{$prefix}{$action}";
202 $callback = $this->onregisteraction(array(&$this, '_doAction'), $action);
203 // Adding action!
204 add_action($action, $callback , $priority, $paramsCount);
205 return $this;
206 }
207
208 } // End class.
209
210 // Hookable!
211 CJTAjaxController::define('CJTAjaxController', array('hookType' => CJTWordpressEvents::HOOK_FILTER));