1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Hooks; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Contracts\HooksContract; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Trigger order of primary WP hooks: |
9
|
|
|
* |
10
|
|
|
* 1. plugins_loaded Fires once activated plugins have loaded. |
11
|
|
|
* 2. load_textdomain Fires before the MO translation file is loaded. |
12
|
|
|
* 3. after_setup_theme Fires after the theme is loaded. |
13
|
|
|
* 4. init Fires after WordPress has finished loading but before any headers are sent. |
14
|
|
|
* 5. wp_loaded Fires after WordPress, all plugins, and the theme are fully loaded and instantiated. |
15
|
|
|
* 6. admin_init Fires as an admin screen or script is being initialized. |
16
|
|
|
* 7. current_screen Fires after the current screen has been set. |
17
|
|
|
* 8. load-{$page_hook} Fires before a particular screen is loaded. |
18
|
|
|
*/ |
19
|
|
|
abstract class AbstractHooks implements HooksContract |
20
|
|
|
{ |
21
|
|
|
protected $basename; |
22
|
|
|
protected $id; |
23
|
|
|
protected $prefix; |
24
|
|
|
protected $taxonomy; |
25
|
|
|
protected $type; |
26
|
|
|
|
27
|
|
|
public function __construct() |
28
|
|
|
{ |
29
|
|
|
$this->basename = glsr()->basename; |
30
|
|
|
$this->id = glsr()->id; |
31
|
|
|
$this->prefix = glsr()->prefix; |
32
|
|
|
$this->taxonomy = glsr()->taxonomy; |
33
|
|
|
$this->type = glsr()->post_type; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function hook(string $classname, array $hooks): void |
37
|
|
|
{ |
38
|
|
|
glsr()->singleton($classname); // make singleton |
39
|
|
|
$controller = glsr($classname); |
40
|
|
|
foreach ($hooks as $hook) { |
41
|
|
|
if (2 > count($hook)) { |
42
|
|
|
continue; |
43
|
|
|
} |
44
|
|
|
$func = str_starts_with($hook[0], 'filter') ? 'add_filter' : 'add_action'; |
45
|
|
|
$hook = array_pad($hook, 3, 10); // priority |
46
|
|
|
$hook = array_pad($hook, 4, 1); // accepted args |
47
|
|
|
$args = [ // order is intentional! |
48
|
|
|
'hook' => $hook[1], |
49
|
|
|
'callback' => [$controller, $hook[0]], |
50
|
|
|
'priority' => (int) $hook[2], |
51
|
|
|
'args' => (int) $hook[3], |
52
|
|
|
]; |
53
|
|
|
if (!str_starts_with($args['hook'], glsr()->id) && method_exists($controller, 'proxy')) { |
54
|
|
|
$args['callback'] = $controller->proxy($hook[0]); |
55
|
|
|
} |
56
|
|
|
call_user_func_array($func, array_values($args)); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function levelInit(): ?int |
61
|
|
|
{ |
62
|
|
|
return null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function levelPluginsLoaded(): ?int |
66
|
|
|
{ |
67
|
|
|
return null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @action init |
72
|
|
|
*/ |
73
|
|
|
public function onInit(): void |
74
|
|
|
{ |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @action plugins_loaded |
79
|
|
|
*/ |
80
|
|
|
public function onPluginsLoaded(): void |
81
|
|
|
{ |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param mixed $fallback |
86
|
|
|
* |
87
|
|
|
* @return mixed |
88
|
|
|
*/ |
89
|
|
|
public function option(string $path, $fallback = '', string $cast = '') |
90
|
|
|
{ |
91
|
|
|
return glsr_get_option($path, $fallback, $cast); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function run(): void |
95
|
|
|
{ |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function runDeferred(): void |
99
|
|
|
{ |
100
|
|
|
if (null !== ($level = $this->levelInit())) { |
|
|
|
|
101
|
|
|
add_action('init', [$this, 'onInit'], $level); |
102
|
|
|
} |
103
|
|
|
if (null !== ($level = $this->levelPluginsLoaded())) { |
|
|
|
|
104
|
|
|
add_action('plugins_loaded', [$this, 'onPluginsLoaded'], $level); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|