| @@ -1,90 +1,90 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | -* | |
| 3 | +* | |
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | -* | |
| 7 | +* | |
| 8 | 8 | */ |
| 9 | 9 | abstract class CJTAjaxController extends CJTController { |
| 10 | - | |
| 10 | + | |
| 11 | 11 | /** */ |
| 12 | 12 | const ACTION_PREFIX = 'wp_ajax_cjtoolbox_'; |
| 13 | - | |
| 13 | + | |
| 14 | 14 | /** |
| 15 | 15 | * put your comment there... |
| 16 | - * | |
| 16 | + * | |
| 17 | 17 | * @var mixed |
| 18 | 18 | */ |
| 19 | 19 | protected $methodName; |
| 20 | - | |
| 20 | + | |
| 21 | 21 | /** |
| 22 | 22 | * put your comment there... |
| 23 | - * | |
| 23 | + * | |
| 24 | 24 | * @var mixed |
| 25 | 25 | */ |
| 26 | 26 | protected $actionsMap = array(); |
| 27 | - | |
| 27 | + | |
| 28 | 28 | /** |
| 29 | 29 | * put your comment there... |
| 30 | - * | |
| 30 | + * | |
| 31 | 31 | * @var mixed |
| 32 | 32 | */ |
| 33 | 33 | protected $defaultCapability = array('administrator'); |
| 34 | - | |
| 34 | + | |
| 35 | 35 | /** |
| 36 | 36 | * put your comment there... |
| 37 | - * | |
| 37 | + * | |
| 38 | 38 | * @var mixed |
| 39 | 39 | */ |
| 40 | 40 | protected $impersonated = false; |
| 41 | - | |
| 41 | + | |
| 42 | 42 | /** |
| 43 | 43 | * put your comment there... |
| 44 | - * | |
| 44 | + * | |
| 45 | 45 | * @var mixed |
| 46 | 46 | */ |
| 47 | 47 | public $httpCode = '200 OK'; |
| 48 | - | |
| 48 | + | |
| 49 | 49 | /** |
| 50 | 50 | * put your comment there... |
| 51 | - * | |
| 51 | + * | |
| 52 | 52 | * @var mixed |
| 53 | 53 | */ |
| 54 | 54 | public $httpContentType = 'text/plain'; |
| 55 | - | |
| 55 | + | |
| 56 | 56 | /** |
| 57 | 57 | * put your comment there... |
| 58 | - * | |
| 58 | + * | |
| 59 | 59 | * @var mixed |
| 60 | 60 | */ |
| 61 | 61 | protected $onauthorize = array('parameters' => array('authorized')); |
| 62 | - | |
| 62 | + | |
| 63 | 63 | /** |
| 64 | 64 | * put your comment there... |
| 65 | - * | |
| 65 | + * | |
| 66 | 66 | * @var mixed |
| 67 | 67 | */ |
| 68 | 68 | protected $onregisteraction = array('parameters' => array('callback', 'action')); |
| 69 | - | |
| 69 | + | |
| 70 | 70 | /** |
| 71 | 71 | * put your comment there... |
| 72 | - * | |
| 72 | + * | |
| 73 | 73 | * @var mixed |
| 74 | 74 | */ |
| 75 | 75 | protected $onresponse = array('hookType' => CJTWordpressEvents::HOOK_ACTION); |
| 76 | - | |
| 76 | + | |
| 77 | 77 | /** |
| 78 | 78 | * put your comment there... |
| 79 | - * | |
| 79 | + * | |
| 80 | 80 | * @var mixed |
| 81 | 81 | */ |
| 82 | 82 | public $response = false; |
| 83 | - | |
| 83 | + | |
| 84 | 84 | /** |
| 85 | 85 | * put your comment there... |
| 86 | - * | |
| 86 | + * | |
| 87 | 87 | */ |
| 88 | 88 | public function _doAction() { |
| 89 | 89 | // Authorize request. |
| 90 | 90 | $authorized = $this->onauthorize(check_ajax_referer(self::NONCE_ACTION, 'security', false)); |
| @@ -91,8 +91,17 @@ | ||
| 91 | 91 | if (!$authorized || !call_user_func_array('current_user_can', $this->defaultCapability)) { |
| 92 | 92 | $this->httpCode = "403 Not Authorized"; |
| 93 | 93 | } |
| 94 | 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(); | |
| 95 | 104 | // Dispatch action. |
| 96 | 105 | $action = current_filter(); |
| 97 | 106 | // Get method name from frrom Wordpress action name! |
| 98 | 107 | $method = $this->ongetactionname(str_replace(self::ACTION_PREFIX, '', $action)); |
| @@ -99,9 +108,9 @@ | ||
| 99 | 108 | // Get method name from action name. |
| 100 | 109 | $method = ucfirst(str_replace('_', ' ', $method)); |
| 101 | 110 | $method = str_replace(' ', '', $method); |
| 102 | 111 | // Lower case the first character. |
| 103 | - $method = strtolower($method{0}) . substr($method, 1); | |
| 112 | + $method = strtolower($method[0]) . substr($method, 1); | |
| 104 | 113 | // Cahe method name for child classes to use! |
| 105 | 114 | $this->methodName = $method; |
| 106 | 115 | // Relying on the trailer "Action" for security. |
| 107 | 116 | // Derivded class should not never use trailer "Action" |
| @@ -140,9 +149,9 @@ | ||
| 140 | 149 | switch ($this->httpContentType) { |
| 141 | 150 | case 'text/plain': |
| 142 | 151 | echo json_encode($this->response); |
| 143 | 152 | break; |
| 144 | - | |
| 153 | + | |
| 145 | 154 | default : |
| 146 | 155 | echo $this->response; |
| 147 | 156 | } |
| 148 | 157 | die(); |
| @@ -149,26 +158,26 @@ | ||
| 149 | 158 | } |
| 150 | 159 | |
| 151 | 160 | /** |
| 152 | 161 | * Display templates manager form. |
| 153 | - * | |
| 162 | + * | |
| 154 | 163 | */ |
| 155 | - protected function displayAction() { | |
| 164 | + protected function displayAction() { | |
| 156 | 165 | // Return view. |
| 157 | 166 | $this->httpContentType = 'text/html'; |
| 158 | 167 | $this->response = parent::displayAction(); |
| 159 | 168 | } |
| 160 | - | |
| 169 | + | |
| 161 | 170 | /** |
| 162 | 171 | * Redirect the request to another controller. |
| 163 | - * | |
| 172 | + * | |
| 164 | 173 | * Why this method is created anyway is to allow |
| 165 | 174 | * deprecating old controllers and start to create new one |
| 166 | 175 | * a quiet manner! |
| 167 | - * | |
| 176 | + * | |
| 168 | 177 | * The idea is to create the new controller, adding new Action there |
| 169 | - * and redirect the call throught current deprecated controller. | |
| 170 | - * | |
| 178 | + * and redirect the call through current deprecated controller. | |
| 179 | + * | |
| 171 | 180 | * @param mixed $controller |
| 172 | 181 | */ |
| 173 | 182 | protected function redirect($controller) { |
| 174 | 183 | // Initialize vars. |
| @@ -179,12 +188,12 @@ | ||
| 179 | 188 | CJTController::getInstance($controller); |
| 180 | 189 | // Fire the action manually. |
| 181 | 190 | do_action($currentFilter); |
| 182 | 191 | } |
| 183 | - | |
| 192 | + | |
| 184 | 193 | /** |
| 185 | 194 | * put your comment there... |
| 186 | - * | |
| 195 | + * | |
| 187 | 196 | * @param mixed $action |
| 188 | 197 | * @param mixed $priority |
| 189 | 198 | * @param mixed $paramsCount |
| 190 | 199 | */ |
| @@ -194,9 +203,9 @@ | ||
| 194 | 203 | // Adding action! |
| 195 | 204 | add_action($action, $callback , $priority, $paramsCount); |
| 196 | 205 | return $this; |
| 197 | 206 | } |
| 198 | - | |
| 207 | + | |
| 199 | 208 | } // End class. |
| 200 | 209 | |
| 201 | 210 | // Hookable! |
| 202 | 211 | CJTAjaxController::define('CJTAjaxController', array('hookType' => CJTWordpressEvents::HOOK_FILTER)); |