| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\Framework\Foundation; |
| 4 |
|
| 5 |
trait HelpersTrait |
| 6 |
{ |
| 7 |
/** |
| 8 |
* $hookReference Current action reference |
| 9 |
* @var null |
| 10 |
*/ |
| 11 |
protected $hookReference = null; |
| 12 |
|
| 13 |
/** |
| 14 |
* Load a file using include_once |
| 15 |
* @return boolean |
| 16 |
*/ |
| 17 |
public function load($file) |
| 18 |
{ |
| 19 |
$app = $this; |
| 20 |
return include $file; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Checks if the user is on wp-admin area (visiting backend) |
| 25 |
* (It doesn't check whether user is authenticated ro not) |
| 26 |
* @return boolean |
| 27 |
*/ |
| 28 |
public function isUserOnAdminArea() |
| 29 |
{ |
| 30 |
return is_admin(); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Application's callbacks parser |
| 35 |
* @param mixed $args |
| 36 |
* @return mixed |
| 37 |
*/ |
| 38 |
public function parseHandler($args) |
| 39 |
{ |
| 40 |
$args = is_array($args) ? $args : func_get_args(); |
| 41 |
|
| 42 |
if (is_callable($args[0])) { |
| 43 |
return $args[0]; |
| 44 |
} elseif (is_string($args[0])) { |
| 45 |
if (strpos($args[0], '@')) { |
| 46 |
list($class, $method) = explode('@', $args[0]); |
| 47 |
$instance = $this->make($class); |
| 48 |
return [$instance, $method]; |
| 49 |
} elseif (strpos($args[0], '::')) { |
| 50 |
list($class, $method) = explode('::', $args[0]); |
| 51 |
return [$class, $method]; |
| 52 |
} elseif (isset($args[1]) && is_string($args[1])) { |
| 53 |
return $args; |
| 54 |
} |
| 55 |
} else { |
| 56 |
return $args; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Make a unique key/hook with the application slug prefix |
| 62 |
* @param string $tag |
| 63 |
* @param string $prefix [optional prefix instead of app slug] |
| 64 |
* @return string |
| 65 |
*/ |
| 66 |
public function makeKey($tag, $prefix = null) |
| 67 |
{ |
| 68 |
return ($prefix ? $prefix : $this->getSlug()).'_'.$tag; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Add/Register an ajax action |
| 73 |
* @param string $tag [action name] |
| 74 |
* @param mixed $handler |
| 75 |
* @param integer $priority [optional] |
| 76 |
* @param string $scope [specify the scope of the ajax action|internal use] |
| 77 |
* @return Framework\Foundation\HookReference |
| 78 |
*/ |
| 79 |
private function addAjaxAction($tag, $handler, $priority, $scope) |
| 80 |
{ |
| 81 |
if ($scope == 'admin') { |
| 82 |
add_action( |
| 83 |
'wp_ajax_'.$tag, |
| 84 |
$ref = $this->parseHandler($handler), |
| 85 |
$priority |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
if ($scope == 'public') { |
| 90 |
add_action( |
| 91 |
'wp_ajax_nopriv_'.$tag, |
| 92 |
$ref = $this->parseHandler($handler), |
| 93 |
$priority |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
return $this->setHookReference($ref, $tag); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Add an ajax action for authenticated user |
| 102 |
* @param string $tag [action name] |
| 103 |
* @param mixed $handler |
| 104 |
* @param integer $priority [optional] |
| 105 |
* @return mixed [a reference to the handler to remove the action later] |
| 106 |
*/ |
| 107 |
public function addAdminAjaxAction($tag, $handler, $priority = 10) |
| 108 |
{ |
| 109 |
return $this->addAjaxAction($tag, $handler, $priority, 'admin'); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Add an ajax action for unauthenticated user |
| 114 |
* @param string $tag [action name] |
| 115 |
* @param mixed $handler |
| 116 |
* @param integer $priority [optional] |
| 117 |
* @return mixed [a reference to the handler to remove the action later] |
| 118 |
*/ |
| 119 |
public function addPublicAjaxAction($tag, $handler, $priority = 10) |
| 120 |
{ |
| 121 |
return $this->addAjaxAction($tag, $handler, $priority, 'public'); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Remove/Unregister a registered ajax action |
| 126 |
* @param string $tag [action name] |
| 127 |
* @param mixed $handler [previously stored reference when added the action] |
| 128 |
* @param integer $priority [optional] |
| 129 |
* @param string $scope [specify the scope of the ajax action|internal use] |
| 130 |
* @return mixed [a reference to the handler to remove the action later] |
| 131 |
*/ |
| 132 |
private function removeAjaxAction($tag, $handler, $priority, $scope) |
| 133 |
{ |
| 134 |
if ($scope == 'admin') { |
| 135 |
return remove_action( |
| 136 |
'wp_ajax_'.$tag, |
| 137 |
$this->parseHandler($handler), |
| 138 |
$priority |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
if ($scope == 'public') { |
| 143 |
return remove_action( |
| 144 |
'wp_ajax_no_priv_'.$tag, |
| 145 |
$this->parseHandler($handler), |
| 146 |
$priority |
| 147 |
); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Remove an ajax action for authenticated user |
| 153 |
* @param string $tag [action name] |
| 154 |
* @param mixed $handler [previously stored reference when added the action] |
| 155 |
* @param integer $priority [optional] |
| 156 |
* @return bool [true on success or false on failure] |
| 157 |
*/ |
| 158 |
public function removeAdminAjaxAction($tag, $handler, $priority = 10) |
| 159 |
{ |
| 160 |
return $this->removeAjaxAction($tag, $handler, $priority, 'admin'); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Remove an ajax action for unauthenticated user |
| 165 |
* @param string $tag [action name] |
| 166 |
* @param mixed $handler [previously stored reference when added the action] |
| 167 |
* @param integer $priority [optional] |
| 168 |
* @return bool [true on success or false on failure] |
| 169 |
*/ |
| 170 |
public function removePublicAjaxAction($tag, $handler, $priority = 10) |
| 171 |
{ |
| 172 |
return $this->removeAjaxAction($tag, $handler, $priority, 'public'); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
/** |
| 177 |
* Add WordPress Filter |
| 178 |
* @param string $tag |
| 179 |
* @param mixed $handler |
| 180 |
* @param integer $priority |
| 181 |
* @param integer $acceptedArgs |
| 182 |
* @return Framework\Foundation\HookReference |
| 183 |
*/ |
| 184 |
public function addFilter($tag, $handler, $priority = 10, $acceptedArgs = 1) |
| 185 |
{ |
| 186 |
add_filter( |
| 187 |
$tag, |
| 188 |
$ref = $this->parseHandler($handler), |
| 189 |
$priority, |
| 190 |
$acceptedArgs |
| 191 |
); |
| 192 |
|
| 193 |
return $this->setHookReference($ref, $tag); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Remove WordPress Filter. |
| 198 |
* @param string $tag |
| 199 |
* @param mixed $handler |
| 200 |
* @param integer $priority |
| 201 |
* @return true |
| 202 |
*/ |
| 203 |
public function removeFilter($tag, $handler, $priority = 10) |
| 204 |
{ |
| 205 |
return remove_filter( |
| 206 |
$tag, |
| 207 |
$this->parseHandler($handler), |
| 208 |
$priority |
| 209 |
); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Remove WordPress' All Filters. |
| 214 |
* @param string $tag |
| 215 |
* @param boolean $priority |
| 216 |
* @return bool |
| 217 |
*/ |
| 218 |
public function removeFilters($tag, $priority = false) |
| 219 |
{ |
| 220 |
return remove_all_filters($tag, $priority); |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Apply WordPress Filter. |
| 225 |
* @return mixed [filtered content] |
| 226 |
*/ |
| 227 |
public function applyFilters() |
| 228 |
{ |
| 229 |
return call_user_func_array('apply_filters', func_get_args()); |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Add WordPress Action |
| 234 |
* @param string $tag |
| 235 |
* @param mixed $handler |
| 236 |
* @param integer $priority |
| 237 |
* @param integer $acceptedArgs |
| 238 |
* @return Framework\Foundation\HookReference |
| 239 |
*/ |
| 240 |
public function addAction($tag, $handler, $priority = 10, $acceptedArgs = 1) |
| 241 |
{ |
| 242 |
add_action( |
| 243 |
$tag, |
| 244 |
$ref = $this->parseHandler($handler), |
| 245 |
$priority, |
| 246 |
$acceptedArgs |
| 247 |
); |
| 248 |
|
| 249 |
return $this->setHookReference($ref, $tag); |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Remove WordPress' Action. |
| 254 |
* @param string $tag |
| 255 |
* @param boolean $priority |
| 256 |
* @return bool |
| 257 |
*/ |
| 258 |
public function removeAction($tag, $handler, $priority = 10) |
| 259 |
{ |
| 260 |
return remove_action( |
| 261 |
$tag, |
| 262 |
$this->parseHandler($handler), |
| 263 |
$priority |
| 264 |
); |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Remove WordPress' All Actions. |
| 269 |
* @param string $tag |
| 270 |
* @param boolean $priority |
| 271 |
* @return bool |
| 272 |
*/ |
| 273 |
public function removeActions($tag, $priority = false) |
| 274 |
{ |
| 275 |
return remove_all_actions($tag, $priority); |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Do WordPress Action. |
| 280 |
* @return void |
| 281 |
*/ |
| 282 |
public function doAction() |
| 283 |
{ |
| 284 |
call_user_func_array('do_action', func_get_args()); |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Add WordPress Short Code. |
| 289 |
* @param string $tag |
| 290 |
* @param mixed $handler |
| 291 |
*/ |
| 292 |
public function addShortCode($tag, $handler) |
| 293 |
{ |
| 294 |
add_shortcode( |
| 295 |
$tag, |
| 296 |
$this->parseHandler($handler) |
| 297 |
); |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Remove WordPress Short Code. |
| 302 |
* @param string $content |
| 303 |
* @param bool $ignoreHtml |
| 304 |
*/ |
| 305 |
public function removeShortCode($tag) |
| 306 |
{ |
| 307 |
remove_shortcode($tag); |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* Do WordPress Short Code. |
| 312 |
* @param string $content |
| 313 |
* @param bool $ignoreHtml |
| 314 |
*/ |
| 315 |
public function doShortCode($tag, $atts, $content = null, $ignoreHtml = false) |
| 316 |
{ |
| 317 |
return do_shortcode( |
| 318 |
$this->formatShortCode($tag, $atts, $content), $ignoreHtml |
| 319 |
); |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* Format the short content (make shortcode content string) |
| 324 |
* @param string $tag |
| 325 |
* @param array $atts |
| 326 |
* @param string $content |
| 327 |
* @return string |
| 328 |
*/ |
| 329 |
public function formatShortCode($tag, $atts, $content = null) |
| 330 |
{ |
| 331 |
$str = ''; |
| 332 |
foreach ($atts as $key => $value) { |
| 333 |
$str .= " {$key}={$value}"; |
| 334 |
} |
| 335 |
return "[{$tag}{$str}]{$content}[/{$tag}]"; |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* Store a reference of last action handler |
| 340 |
* @param reference $ref (Reference of registered function/handler) |
| 341 |
* @param string $tag |
| 342 |
* @return $this |
| 343 |
*/ |
| 344 |
public function setHookReference($ref, $tag) |
| 345 |
{ |
| 346 |
$this->hookReference = new HookReference($this, $ref, $tag); |
| 347 |
|
| 348 |
return $this; |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* Save the hook's handler reference |
| 353 |
* @param string $key |
| 354 |
* @return reference |
| 355 |
*/ |
| 356 |
public function saveReference($key = null) |
| 357 |
{ |
| 358 |
if (!is_null($this->hookReference)) { |
| 359 |
$this->hookReference->saveReference($key); |
| 360 |
$this->hookReference = null; |
| 361 |
return $this; |
| 362 |
} |
| 363 |
|
| 364 |
throw new \Exception( |
| 365 |
'No reference is available. Call this method only after you add any action/filter/ajax hook.' |
| 366 |
); |
| 367 |
} |
| 368 |
|
| 369 |
/** |
| 370 |
* Register any callback/middleware to run before ajax callback |
| 371 |
* @param string $actionName |
| 372 |
* @param mixed $fn (WordPress permission name/closure) |
| 373 |
* @return $this |
| 374 |
*/ |
| 375 |
public function before($action, $fn) |
| 376 |
{ |
| 377 |
$this->bindInstance("__ajax_before__{$action}", is_callable($fn) ? $fn : function() use ($fn) { |
| 378 |
if (!current_user_can($fn)) { |
| 379 |
wp_send_json_error(array( |
| 380 |
'message' => "You don't have permission for this." |
| 381 |
), 403); |
| 382 |
} |
| 383 |
}); |
| 384 |
|
| 385 |
return $this; |
| 386 |
} |
| 387 |
} |
| 388 |
|