-
Notifications
You must be signed in to change notification settings - Fork 0
/
zukit-addon.php
206 lines (173 loc) · 6.89 KB
/
zukit-addon.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
// Plugin Addon Class ---------------------------------------------------------]
class zukit_Addon {
protected $config;
protected $plugin;
protected $name;
protected $options;
protected $options_key;
protected $dir;
protected $uri;
protected $version;
private $nonce;
public function register($plugin) {
$this->plugin = $plugin ?? null;
if(empty($this->plugin)) {
_doing_it_wrong(__FUNCTION__, '"Addon" cannot be used without plugin!');
} else {
$this->dir = $this->plugin->dir;
$this->uri = $this->plugin->uri;
$this->version = $this->plugin->version;
$this->config = array_merge($this->config_defaults(), $this->config());
$this->name = $this->get('name') ?? 'zuaddon';
$this->nonce = $this->get_callable('nonce') ?? $this->name.'_ajax_nonce';
$this->options_key = $this->name.'_options';
$this->init_options();
$this->construct_more_inner();
$this->construct_more();
}
}
// Configuration management -----------------------------------------------]
protected function config() { return []; }
protected function config_defaults() { return []; }
// 'construct_more' is only called after the add-on is registered by the plugin!
protected function construct_more() {}
// 'construct_more_inner' is needed for classes that will inherit from 'zukit_Addon'
// but to keep 'construct_more' free for users of the framework
protected function construct_more_inner() {}
public function init() {}
public function admin_init() {}
public function enqueue() {}
public function admin_enqueue($hook) {}
public function clean() {}
public function ajax($action, $value) { return null; }
// Options management -----------------------------------------------------]
public function init_options() {
$options = $this->plugin->options();
if(!isset($options[$this->options_key]) && !is_null($this->get('options'))) {
$this->options = $this->get('options');
$this->plugin->set_option($this->options_key, $this->options, true);
} else {
$this->options($options);
}
}
public function extend_parent_options($parent_options) {
$options = $this->get('options');
$parent_options[$this->options_key] = $options;
return $parent_options;
}
public function options($options = null) {
if(!is_null($options)) $this->options = $options[$this->options_key] ?? [];
return $this->options;
}
protected function get_option($key, $default = null) {
return $this->plugin->get_option($key, $default, $this->options);
}
protected function is_option($key, $check_value = true) {
return $this->plugin->is_option($key, $check_value, $this->options);
}
protected function del_option($key) {
$this->options = $this->plugin->del_option($key, $this->options);
return $this->plugin->set_option($this->options_key, $this->options, true);
}
protected function set_option($key, $value, $rewrite_array = false) {
$this->options = $this->plugin->set_option($key, $value, $rewrite_array, $this->options);
return $this->plugin->set_option($this->options_key, $this->options, true);
}
protected function is_parent_option($key, $check_value = true) {
return $this->plugin->is_option($key, $check_value);
}
protected function get_parent_option($key, $default = null) {
return $this->plugin->get_option($key, $default);
}
// Redirect to parent methods ---------------------------------------------]
protected function extend_parent_redirects() {}
public function __call($method, $args) {
$available_methods = [
'ajax_error',
'ajax_nonce',
'ajax_send',
'array_with_defaults',
'create_notice',
'do_with_instances',
'ends_with_slug',
'enqueue_only',
'get_file_version',
'has_snippet',
'is_error',
'logd',
'prefix_it',
'register_only',
'snippets',
'_snippets',
'sprintf_dir',
'sprintf_uri',
];
if(!in_array($method, array_merge($available_methods, $this->extend_parent_redirects() ?? []))) {
// if we have 'zukit_Exchange' trait - then transfer processing further
if(method_exists($this, 'call_addon_provider')) {
return $this->call_addon_provider($method, $args);
}
$this->logc('?Trying to call an unavailable parent method', [
'method' => $method,
'args' => $args,
'available' => $available_methods,
]);
return null;
}
$func = [$this->plugin, $method];
return is_callable($func) ? call_user_func_array($func, $args) : null;
}
protected function enqueue_style($file, $params = []) {
// enforce_defaults: $is_style, $is_frontend, $params
$params_with_defaults = $this->plugin->enforce_defaults(true, true, $params);
return $this->plugin->enqueue_style($this->filename($file, $params), $params_with_defaults);
}
protected function enqueue_script($file, $params = []) {
$params_with_defaults = $this->plugin->enforce_defaults(false, true, $params);
return $this->plugin->enqueue_script($this->filename($file, $params), $params_with_defaults);
}
protected function admin_enqueue_style($file, $params = []) {
$params_with_defaults = $this->plugin->enforce_defaults(true, false, $params);
return $this->plugin->admin_enqueue_style($this->filename($file, $params), $params_with_defaults);
}
protected function admin_enqueue_script($file, $params = []) {
$params_with_defaults = $this->plugin->enforce_defaults(false, false, $params);
return $this->plugin->admin_enqueue_script($this->filename($file, $params), $params_with_defaults);
}
// we need an additional backtrace shift to compensate for the nested call
protected function log(...$params) {
$this->plugin->debug_line_shift(1);
$this->plugin->log(...$params);
$this->plugin->debug_line_shift(0);
}
protected function logc($context, ...$params) {
$this->plugin->debug_line_shift(1);
$this->plugin->logc($context, ...$params);
$this->plugin->debug_line_shift(0);
}
// Common interface to parent methods with availability check -------------]
// NOTE: only public functions and property can be called with this helper
protected function with_another($prop, $func, ...$params) {
if(property_exists($this->plugin, $prop)) {
$another = $this->plugin->{$prop};
if(method_exists($another, $func)) return call_user_func_array([$another, $func], $params);
}
return null;
}
protected function call_parent($func, ...$params) {
if(method_exists($this->plugin, $func)) return call_user_func_array([$this->plugin, $func], $params);
else return null;
}
// Helpers ----------------------------------------------------------------]
protected function get($key, $from_plugin = false, $default_value = null) {
return $this->plugin->get($key, $default_value, $from_plugin ? null : $this->config);
}
protected function get_callable($key, $from_plugin = false, $default_value = null) {
return $this->plugin->get_callable($key, $default_value, $from_plugin ? null : $this->config);
}
private function filename($file, $params) {
$with_prefix = $params['add_prefix'] ?? true;
return $with_prefix ? $this->prefix_it($file) : $file;
}
}