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