PluginProbe
CSS & JavaScript Toolbox / 6.0.11
CSS & JavaScript Toolbox v6.0.11
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 6.0.11, at framework/mvc/controller-ajax.inc.php

202 lines 5.0 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 // Dispatch action.
96 $action = current_filter();
97 // Get method name from frrom Wordpress action name!
98 $method = $this->ongetactionname(str_replace(self::ACTION_PREFIX, '', $action));
99 // Get method name from action name.
100 $method = ucfirst(str_replace('_', ' ', $method));
101 $method = str_replace(' ', '', $method);
102 // Lower case the first character.
103 $method = strtolower($method{0}) . substr($method, 1);
104 // Cahe method name for child classes to use!
105 $this->methodName = $method;
106 // Relying on the trailer "Action" for security.
107 // Derivded class should not never use trailer "Action"
108 // for internal methods.
109 $method = "{$method}Action";
110 // If its mapped from child classed redirect the call!
111 if ((isset($this->actionsMap[$action]) && ($use = $action)) || (isset($this->actionsMap[$method]) && ($use = $method))) {
112 $method = $this->actionsMap[$use];
113 }
114 // Filter callback method and args.
115 $callback = $this->oncallback((object) array('method' => array($this, $method), 'args' => func_get_args()), $action);
116 // Call Action Method.
117 if (!is_callable($callback->method)) {
118 $this->httpCode = '403 Not supported action';
119 }
120 else {
121 // When there are no arguments passed to Wordpress action.
122 // do_action function pass an empty string parameter at index 0.
123 if (count($callback->args) == 1 && ($callback->args[0] == '')) {
124 $callback->args = array();
125 }
126 call_user_func_array($callback->method, $callback->args);
127 // Controller loaded with Wordpress typical Ajax request (e.g meta-box-order).
128 // We shouldn't output anything in these cases.
129 if ($this->impersonated) {
130 return;
131 }
132 }
133 }
134 // Set HTTP headers.
135 header("HTTP/1.0 {$this->httpCode}");
136 header("Content-Type: {$this->httpContentType}");
137 // Allow filtering any response data/header!
138 $this->onresponse();
139 // Output type based on the content type MIME.
140 switch ($this->httpContentType) {
141 case 'text/plain':
142 echo json_encode($this->response);
143 break;
144
145 default :
146 echo $this->response;
147 }
148 die();
149 }
150
151 /**
152 * Display templates manager form.
153 *
154 */
155 protected function displayAction() {
156 // Return view.
157 $this->httpContentType = 'text/html';
158 $this->response = parent::displayAction();
159 }
160
161 /**
162 * Redirect the request to another controller.
163 *
164 * Why this method is created anyway is to allow
165 * deprecating old controllers and start to create new one
166 * a quiet manner!
167 *
168 * The idea is to create the new controller, adding new Action there
169 * and redirect the call throught current deprecated controller.
170 *
171 * @param mixed $controller
172 */
173 protected function redirect($controller) {
174 // Initialize vars.
175 $currentFilter= current_filter();
176 // Remove current Action!
177 remove_action($currentFilter, array(&$this, '_doAction'));
178 // activate the target CTR!
179 CJTController::getInstance($controller);
180 // Fire the action manually.
181 do_action($currentFilter);
182 }
183
184 /**
185 * put your comment there...
186 *
187 * @param mixed $action
188 * @param mixed $priority
189 * @param mixed $paramsCount
190 */
191 protected function registryAction($action, $priority = 10, $paramsCount = 1, $prefix = self::ACTION_PREFIX) {
192 $action = "{$prefix}{$action}";
193 $callback = $this->onregisteraction(array(&$this, '_doAction'), $action);
194 // Adding action!
195 add_action($action, $callback , $priority, $paramsCount);
196 return $this;
197 }
198
199 } // End class.
200
201 // Hookable!
202 CJTAjaxController::define('CJTAjaxController', array('hookType' => CJTWordpressEvents::HOOK_FILTER));