, $priority ); } if ($scope == 'public') { return remove_action( 'wp_ajax_no_priv_'.$tag, $this->parseHandler($handler), $priority ); } } /** * Remove an ajax action for authenticated user * @param string $tag [action name] * @param mixed $handler [previously stored reference when added the action] * @param integer $priority [optional] * @return bool [true on success or false on failure] */ public function removeAdminAjaxAction($tag, $handler, $priority = 10) { return $this->removeAjaxAction($tag, $handler, 'admin', $priority); } /** * Remove an ajax action for unauthenticated user * @param string $tag [action name] * @param mixed $handler [previously stored reference when added the action] * @param integer $priority [optional] * @return bool [true on success or false on failure] */ public function removePublicAjaxAction($tag, $handler, $priority = 10) { return $this->removeAjaxAction($tag, $handler, 'public', $priority); } /** * Add WordPress Filter * @param string $tag * @param mixed $handler * @param integer $priority * @param integer $acceptedArgs * @return Framework\Foundation\HookReference */ public function addfilter($tag, $handler, $priority = 10, $acceptedArgs = 1) { add_filter( $tag, $ref = $this->parseHandler($handler), $priority, $acceptedArgs ); return $this->setHookReference($ref, $tag); } /** * Remove WordPress Filter. * @param string $tag * @param mixed $handler * @param integer $priority * @return true */ public function removeFilter($tag, $handler, $priority = 10) { return remove_filter( $tag, $this->parseHandler($handler), $priority ); } /** * Remove WordPress' All Filters. * @param string $tag * @param boolean $priority * @return bool */ public function removeFilters($tag, $priority = false) { return remove_all_filters($tag, $priority); } /** * Apply WordPress Filter. * @return mixed [filtered content] */ public function applyFilters() { return call_user_func_array('apply_filters', func_get_args()); } /** * Add WordPress Action * @param string $tag * @param mixed $handler * @param integer $priority * @param integer $acceptedArgs * @return Framework\Foundation\HookReference */ public function addAction($tag, $handler, $priority = 10, $acceptedArgs = 1) { add_action( $tag, $ref = $this->parseHandler($handler), $priority, $acceptedArgs ); return $this->setHookReference($ref, $tag); } /** * Remove WordPress' Action. * @param string $tag * @param boolean $priority * @return bool */ public function removeAction($tag, $handler, $priority = 10) { return remove_action( $tag, $this->parseHandler($handler), $priority ); } /** * Remove WordPress' All Actions. * @param string $tag * @param boolean $priority * @return bool */ public function removeActions($tag, $priority = false) { return remove_all_actions($tag, $priority); } /** * Do WordPress Action. * @return void */ public function doAction() { call_user_func_array('do_action', func_get_args()); } /** * Add WordPress Short Code. * @param string $tag * @param mixed $handler */ public function addShortCode($tag, $handler) { add_shortcode( $tag, $this->parseHandler($handler) ); } /** * Remove WordPress Short Code. * @param string $content * @param bool $ignoreHtml */ public function removeShortCode($tag) { remove_shortcode($tag); } /** * Do WordPress Short Code. * @param string $content * @param bool $ignoreHtml */ public function doShortCode($tag, $atts, $content = null, $ignoreHtml = false) { return do_shortcode( $this->formatShortCode($tag, $atts, $content), $ignoreHtml ); } /** * Format the short content (make shortcode content string) * @param string $tag * @param array $atts * @param string $content * @return string */ public function formatShortCode($tag, $atts, $content = null) { $str = ''; foreach ($atts as $key => $value) { $str .= " {$key}={$value}"; } return "[{$tag}{$str}]{$content}[/{$tag}]"; } /** * Store a reference of last action handler * @param reference $ref (Reference of registered function/handler) * @param string $tag * @return $this */ public function setHookReference($ref, $tag) { $this->hookReference = new HookReference($this, $ref, $tag); return $this; } /** * Save the hook's handler reference * @param string $key * @return reference */ public function saveReference($key = null) { if (!is_null($this->hookReference)) { $this->hookReference->saveReference($key); $this->hookReference = null; return $this; } throw new \Exception( 'No reference is available. Call this method only after you add any action/filter/ajax hook.' ); } /** * Register any callback/middleware to run before ajax callback * @param string $actionName * @param mixed $fn (WordPress permission name/closure) * @return $this */ public function before($action, $fn) { $this->bindInstance("__ajax_before__{$action}", is_callable($fn) ? $fn : function() use ($fn) { if (!current_user_can($fn)) { wp_send_json_error(array( 'message' => "Forbidden! WordPress permission required." ), 403); } }); return $this; } }