ran ) { add_action( $hook, array( $component, $callback ), (int) $priority, (int) $accepted_args ); return $this; } $this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args ); return $this; } /** * Queue a filter. * * @param string $hook Hook name. * @param object $component Object holding the callback. * @param string $callback Method name. * @param int $priority Priority. * @param int $accepted_args Number of arguments. * @return AdvisorPPC_Loader */ public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { if ( $this->ran ) { add_filter( $hook, array( $component, $callback ), (int) $priority, (int) $accepted_args ); return $this; } $this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args ); return $this; } /** * Queue a shortcode. * * @param string $tag Shortcode tag. * @param object $component Object holding the callback. * @param string $callback Method name. * @return AdvisorPPC_Loader */ public function add_shortcode( $tag, $component, $callback ) { if ( $this->ran ) { add_shortcode( $tag, array( $component, $callback ) ); return $this; } $this->shortcodes[] = array( 'tag' => $tag, 'component' => $component, 'callback' => $callback, ); return $this; } /** * Attach everything queued. * * Safe to call more than once: anything queued after the first run is * attached immediately instead of being silently dropped. * * @return void */ public function run() { foreach ( $this->filters as $hook ) { add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); } foreach ( $this->actions as $hook ) { add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); } foreach ( $this->shortcodes as $shortcode ) { add_shortcode( $shortcode['tag'], array( $shortcode['component'], $shortcode['callback'] ) ); } $this->actions = array(); $this->filters = array(); $this->shortcodes = array(); $this->ran = true; } /** * Whether the loader has already attached its queue. * * @return bool */ public function has_run() { return $this->ran; } /** * Append to a queue, or attach straight away when the loader already ran. * * @param array $hooks Existing queue. * @param string $hook Hook name. * @param object $component Object holding the callback. * @param string $callback Method name. * @param int $priority Priority. * @param int $accepted_args Number of arguments. * @return array */ protected function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) { $hooks[] = array( 'hook' => $hook, 'component' => $component, 'callback' => $callback, 'priority' => (int) $priority, 'accepted_args' => (int) $accepted_args, ); return $hooks; } }