| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\Framework\Foundation; |
| 4 |
|
| 5 |
trait FoundationTrait |
| 6 |
{ |
| 7 |
public function env() |
| 8 |
{ |
| 9 |
return $this->config->get('app.env'); |
| 10 |
} |
| 11 |
|
| 12 |
public function hook($prefix, $hook) |
| 13 |
{ |
| 14 |
return $prefix . $hook; |
| 15 |
} |
| 16 |
|
| 17 |
public function parseRestHandler($handler) |
| 18 |
{ |
| 19 |
if ($handler instanceof \Closure) { |
| 20 |
return $handler; |
| 21 |
} |
| 22 |
|
| 23 |
if (is_array($handler)) { |
| 24 |
|
| 25 |
if (is_object($handler[0])) { |
| 26 |
return $handler; |
| 27 |
} |
| 28 |
|
| 29 |
if (is_string($handler[0])) { |
| 30 |
$handler = $handler[0] . '@' . $handler[1]; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
return $this->getControllerNamespace($handler) . '\\' . $handler; |
| 35 |
} |
| 36 |
|
| 37 |
public function parsePolicyHandler($handler) |
| 38 |
{ |
| 39 |
if (!$handler) return; |
| 40 |
|
| 41 |
if (is_string($handler)) { |
| 42 |
$handler = $this->policyNamespace . '\\' . $handler; |
| 43 |
|
| 44 |
if ($this->isCallableWithAtSign($handler)) { |
| 45 |
list($class, $method) = explode('@', $handler); |
| 46 |
if (!method_exists($class, $method)) { |
| 47 |
$method = 'verifyRequest'; |
| 48 |
if (!method_exists($class, $method)) { |
| 49 |
$method = '__returnTrue'; |
| 50 |
} |
| 51 |
} |
| 52 |
$instance = $this->make($class); |
| 53 |
$handler = [$instance, $method]; |
| 54 |
} |
| 55 |
|
| 56 |
} else if (is_array($handler)) { |
| 57 |
list($class, $method) = $handler; |
| 58 |
|
| 59 |
if (is_string($class)) { |
| 60 |
$handler = $this->policyNamespace . '\\' . $class . '::' . $method; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
return $handler; |
| 65 |
} |
| 66 |
|
| 67 |
public function addAction($action, $handler, $priority = 10, $numOfArgs = 1) |
| 68 |
{ |
| 69 |
return add_action( |
| 70 |
$action, |
| 71 |
$this->parseHookHandler($handler), |
| 72 |
$priority, |
| 73 |
$numOfArgs |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
public function addCustomAction($action, $handler, $priority = 10, $numOfArgs = 1) |
| 78 |
{ |
| 79 |
$prefix = $this->config->get('app.hook_prefix'); |
| 80 |
|
| 81 |
return $this->addAction( |
| 82 |
$this->hook($prefix, $action), $handler, $priority, $numOfArgs |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
public function doAction() |
| 87 |
{ |
| 88 |
return call_user_func_array('do_action', func_get_args()); |
| 89 |
} |
| 90 |
|
| 91 |
public function doCustomAction() |
| 92 |
{ |
| 93 |
$args = func_get_args(); |
| 94 |
|
| 95 |
$prefix = $this->config->get('app.hook_prefix'); |
| 96 |
|
| 97 |
$args[0] = $this->hook($prefix, $args[0]); |
| 98 |
|
| 99 |
return call_user_func_array('do_action', $args); |
| 100 |
} |
| 101 |
|
| 102 |
public function addFilter($action, $handler, $priority = 10, $numOfArgs = 1) |
| 103 |
{ |
| 104 |
return add_filter( |
| 105 |
$action, |
| 106 |
$this->parseHookHandler($handler), |
| 107 |
$priority, |
| 108 |
$numOfArgs |
| 109 |
); |
| 110 |
} |
| 111 |
|
| 112 |
public function addCustomFilter($action, $handler, $priority = 10, $numOfArgs = 1) |
| 113 |
{ |
| 114 |
$prefix = $this->config->get('app.hook_prefix'); |
| 115 |
|
| 116 |
return $this->addFilter( |
| 117 |
$this->hook($prefix, $action), $handler, $priority, $numOfArgs |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
public function applyFilters() |
| 122 |
{ |
| 123 |
return call_user_func_array('apply_filters', func_get_args()); |
| 124 |
} |
| 125 |
|
| 126 |
public function applyCustomFilters() |
| 127 |
{ |
| 128 |
$args = func_get_args(); |
| 129 |
$prefix = $this->config->get('app.hook_prefix'); |
| 130 |
$args[0] = $this->hook($prefix, $args[0]); |
| 131 |
|
| 132 |
return call_user_func_array('apply_filters', $args); |
| 133 |
} |
| 134 |
|
| 135 |
public function addShortcode($action, $handler) |
| 136 |
{ |
| 137 |
return add_shortcode( |
| 138 |
$action, |
| 139 |
$this->parseHookHandler($handler) |
| 140 |
); |
| 141 |
} |
| 142 |
|
| 143 |
public function doShortcode($content, $ignore_html = false) |
| 144 |
{ |
| 145 |
return do_shortcode($content, $ignore_html); |
| 146 |
} |
| 147 |
|
| 148 |
public function parseHookHandler($handler) |
| 149 |
{ |
| 150 |
if (is_string($handler)) { |
| 151 |
list($class, $method) = preg_split('/::|@/', $handler); |
| 152 |
|
| 153 |
$class = $this->makeInstance($class); |
| 154 |
|
| 155 |
return [$class, $method]; |
| 156 |
|
| 157 |
} else if (is_array($handler)) { |
| 158 |
list($class, $method) = $handler; |
| 159 |
if (is_string($class)) { |
| 160 |
$class = $this->makeInstance($class); |
| 161 |
} |
| 162 |
return [$class, $method]; |
| 163 |
} |
| 164 |
|
| 165 |
return $handler; |
| 166 |
} |
| 167 |
|
| 168 |
public function hasNamespace($handler) |
| 169 |
{ |
| 170 |
if ($handler instanceof \Closure) { |
| 171 |
return false; |
| 172 |
}; |
| 173 |
|
| 174 |
$parts = explode('\\', $handler); |
| 175 |
|
| 176 |
return count($parts) > 1; |
| 177 |
} |
| 178 |
|
| 179 |
public function getControllerNamespace($handler) |
| 180 |
{ |
| 181 |
if ($this->hasNamespace($handler)) { |
| 182 |
return ''; |
| 183 |
} |
| 184 |
|
| 185 |
return $this->controllerNamespace; |
| 186 |
} |
| 187 |
|
| 188 |
public function makeInstance($class) |
| 189 |
{ |
| 190 |
if ($this->hasNamespace($class)) { |
| 191 |
$instance = $this->make($class); |
| 192 |
} else { |
| 193 |
$instance = $this->make($this->handlerNamespace . '\\' . $class); |
| 194 |
} |
| 195 |
|
| 196 |
return $instance; |
| 197 |
} |
| 198 |
|
| 199 |
private function addAjaxAction($tag, $handler, $priority, $scope) |
| 200 |
{ |
| 201 |
if ($scope == 'admin') { |
| 202 |
return add_action( |
| 203 |
'wp_ajax_'.$tag, |
| 204 |
$this->parseHookHandler($handler), |
| 205 |
$priority |
| 206 |
); |
| 207 |
} |
| 208 |
|
| 209 |
if ($scope == 'public') { |
| 210 |
return add_action( |
| 211 |
'wp_ajax_nopriv_'.$tag, |
| 212 |
$this->parseHookHandler($handler), |
| 213 |
$priority |
| 214 |
); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
public function addAdminAjaxAction($tag, $handler, $priority = 10) |
| 219 |
{ |
| 220 |
return $this->addAjaxAction($tag, $handler, $priority, 'admin'); |
| 221 |
} |
| 222 |
|
| 223 |
public function addPublicAjaxAction($tag, $handler, $priority = 10) |
| 224 |
{ |
| 225 |
return $this->addAjaxAction($tag, $handler, $priority, 'public'); |
| 226 |
} |
| 227 |
|
| 228 |
public function url($path = '') |
| 229 |
{ |
| 230 |
// _deprecated_function( |
| 231 |
// __METHOD__, |
| 232 |
// FLUENTFORM_FRAMEWORK_UPGRADE, |
| 233 |
// 'fluentformMix to get asset URL' |
| 234 |
// ); |
| 235 |
|
| 236 |
return $this->baseUrl.ltrim($path, '/'); |
| 237 |
} |
| 238 |
|
| 239 |
public function publicUrl($path = '') |
| 240 |
{ |
| 241 |
// _deprecated_function( |
| 242 |
// __METHOD__, |
| 243 |
// FLUENTFORM_FRAMEWORK_UPGRADE, |
| 244 |
// 'fluentformMix to get asset URL' |
| 245 |
// ); |
| 246 |
|
| 247 |
return fluentFormMix($path); |
| 248 |
} |
| 249 |
} |
| 250 |
|