108 lines
2.8 KiB
PHP
108 lines
2.8 KiB
PHP
<?php
|
|
/**
|
|
* Registers all WordPress hooks for the plugin.
|
|
*
|
|
* @package Robotstxt_Manager
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Class Robotstxt_Manager_Loader
|
|
*
|
|
* Collects all actions and filters to be registered with WordPress and
|
|
* registers them in bulk when run() is called.
|
|
*/
|
|
class Robotstxt_Manager_Loader {
|
|
|
|
/**
|
|
* Registered action hooks.
|
|
*
|
|
* @var array<int, array{hook: string, component: object, callback: string, priority: int, accepted_args: int}>
|
|
*/
|
|
private array $actions = array();
|
|
|
|
/**
|
|
* Registered filter hooks.
|
|
*
|
|
* @var array<int, array{hook: string, component: object, callback: string, priority: int, accepted_args: int}>
|
|
*/
|
|
private array $filters = array();
|
|
|
|
/**
|
|
* Queues an action hook for registration.
|
|
*
|
|
* @param string $hook The name of the WordPress action.
|
|
* @param object $component The object instance containing the callback.
|
|
* @param string $callback The method name to call on $component.
|
|
* @param int $priority Hook priority. Default 10.
|
|
* @param int $accepted_args Number of arguments the callback accepts. Default 1.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function add_action(
|
|
string $hook,
|
|
object $component,
|
|
string $callback,
|
|
int $priority = 10,
|
|
int $accepted_args = 1
|
|
): void {
|
|
$this->actions[] = array(
|
|
'hook' => $hook,
|
|
'component' => $component,
|
|
'callback' => $callback,
|
|
'priority' => $priority,
|
|
'accepted_args' => $accepted_args,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Queues a filter hook for registration.
|
|
*
|
|
* @param string $hook The name of the WordPress filter.
|
|
* @param object $component The object instance containing the callback.
|
|
* @param string $callback The method name to call on $component.
|
|
* @param int $priority Hook priority. Default 10.
|
|
* @param int $accepted_args Number of arguments the callback accepts. Default 1.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function add_filter(
|
|
string $hook,
|
|
object $component,
|
|
string $callback,
|
|
int $priority = 10,
|
|
int $accepted_args = 1
|
|
): void {
|
|
$this->filters[] = array(
|
|
'hook' => $hook,
|
|
'component' => $component,
|
|
'callback' => $callback,
|
|
'priority' => $priority,
|
|
'accepted_args' => $accepted_args,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Registers all queued actions and filters with WordPress.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function run(): void {
|
|
foreach ( $this->actions as $hook ) {
|
|
$cb = array( $hook['component'], $hook['callback'] );
|
|
if ( is_callable( $cb ) ) {
|
|
add_action( $hook['hook'], $cb, $hook['priority'], $hook['accepted_args'] );
|
|
}
|
|
}
|
|
|
|
foreach ( $this->filters as $hook ) {
|
|
$cb = array( $hook['component'], $hook['callback'] );
|
|
if ( is_callable( $cb ) ) {
|
|
add_filter( $hook['hook'], $cb, $hook['priority'], $hook['accepted_args'] );
|
|
}
|
|
}
|
|
}
|
|
}
|