($action, $callback = false) { $prefix = $this->config->get('app.hook_prefix'); return has_filter($prefix.$action, $callback); } /** * Register a short code * @param string $action * @param null */ public function addShortcode($action, $handler) { return add_shortcode( $action, $this->parseHookHandler($handler) ); } /** * Execute a shortcode * @param mixed $content * @param boolean $ignore_html * @return mixed */ public function doShortcode($content, $ignore_html = false) { return do_shortcode($content, $ignore_html); } /** * Parse a hookm handler * @param mixed $handler * @return mixed */ public function parseHookHandler($handler) { if (is_string($handler)) { if (function_exists($handler)) return $handler; if (count($array = preg_split('/::|@/', $handler)) < 2) { $array[] = 'handle'; } list($class, $method) = $array; $class = $this->makeInstance($class); return is_callable($class) ? $class : [$class, $method]; } else if (is_array($handler)) { list($class, $method) = $handler; if (is_string($class)) { $class = $this->makeInstance($class); } return [$class, $method]; } return $handler; } /** * Chdeck if handler has fqn * @param string|Closure $handler * @return boolean */ public function hasNamespace($handler) { if ($handler instanceof \Closure) { return false; }; $parts = array_filter(explode('\\', $handler)); return count($parts) > 1; } /** * Resolve the namespace for a controller * @param string $handler * @return mixed */ public function getControllerNamespace($handler) { if ($this->hasNamespace($handler)) { return ''; } return $this->controllerNamespace; } /** * Resolve the namespace for a policy * @param string $handler * @return mixed */ public function getPolicyNamespace($handler) { if ($this->hasNamespace($handler)) { return ''; } return $this->policyNamespace; } /** * Make an instance by the container * @param string $class * @return mixed */ public function makeInstance($class) { if ($this->hasNamespace($class)) { $instance = $this->make($class); } else { $instance = $this->make($this->handlerNamespace . '\\' . $class); } return $instance; } /** * Retrieve the base url * @param string $url * @return string */ public function url($url = '') { return $this->baseUrl.ltrim($url, '/'); } /** * Add ajax action * @param string $action * @param string|Clousure $handler * @param int $priority * @param string $scope */ private function addAjaxAction($action, $handler, $priority, $scope) { if ($scope == 'admin') { return add_action( 'wp_ajax_'.$action, $this->parseHookHandler($handler), $priority ); } if ($scope == 'public') { return add_action( 'wp_ajax_nopriv_'.$action, $this->parseHookHandler($handler), $priority ); } } /** * Add ajax actions including non_prive * @param string $action * @param string|Clousure $handler * @param int $priority */ public function addAjaxActions($action, $handler, $priority = 10) { $this->addAjaxAction($action, $handler, $priority, 'admin'); $this->addAjaxAction($action, $handler, $priority, 'public'); } /** * Add ajax action for privilaged user * @param string $action * @param string|Clousure $handler * @param int $priority */ public function addAdminAjaxAction($action, $handler, $priority = 10) { return $this->addAjaxAction($action, $handler, $priority, 'admin'); } /** * Add ajax action for non-privilaged user * @param string $action * @param string|Clousure $handler * @param int $priority */ public function addPublicAjaxAction($action, $handler, $priority = 10) { return $this->addAjaxAction($action, $handler, $priority, 'public'); } }