actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args ); } /** * Add a filter hook to the collection. * * @since 1.0.0 * * @param string $hook The name of the WordPress filter. * @param object $component A reference to the instance of the class. * @param string $callback The name of the method on $component. * @param int $priority Optional. Hook priority. Default 10. * @param int $accepted_args Optional. Number of accepted args. Default 1. */ public function add_filter( string $hook, object $component, string $callback, int $priority = 10, int $accepted_args = 1 ): void { $this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args ); } /** * Utility method: adds an entry to a hooks collection array. * * @since 1.0.0 * @access private * * @param array $hooks Existing hook collection. * @param string $hook Hook name. * @param object $component Object containing the callback. * @param string $callback Method name on $component. * @param int $priority Hook priority. * @param int $accepted_args Number of accepted arguments. * @return array Updated hook collection. */ private function add( array $hooks, string $hook, object $component, string $callback, int $priority, int $accepted_args ): array { $hooks[] = array( 'hook' => $hook, 'component' => $component, 'callback' => $callback, 'priority' => $priority, 'accepted_args' => $accepted_args, ); return $hooks; } /** * Register all collected actions and filters with WordPress. * * @since 1.0.0 */ public function run(): void { 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'] ); } } }