class-plugin-abstract.php
6.17 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
/**
* Plugin abstract.
*
* @package Block_Lab
* @copyright Copyright(c) 2020, Block Lab
* @license http://opensource.org/licenses/GPL-2.0 GNU General Public License, version 2 (GPL-2.0)
*/
namespace Block_Lab;
/**
* Class Plugin_Abstract
*/
abstract class Plugin_Abstract implements Plugin_Interface {
/**
* Plugin components.
*
* @var array
*/
protected $components = [];
/**
* Plugin basename.
*
* @since 1.0.0
* @var string
*/
protected $basename;
/**
* Absolute path to the main plugin directory.
*
* @since 1.0.0
* @var string
*/
protected $directory;
/**
* Absolute path to the main plugin file.
*
* @since 1.0.0
* @var string
*/
protected $file;
/**
* Plugin identifier.
*
* @since 1.0.0
* @var string
*/
protected $slug;
/**
* URL to the main plugin directory.
*
* @since 1.0.0
* @var string
*/
protected $url;
/**
* The plugin version.
*
* @since 1.0.2
* @var string
*/
protected $version;
/**
* Allows calling methods in the Util class, directly in this class.
*
* When calling a method in this class that isn't defined, this calls it in $this->util if it exists.
* For example, on calling ->example_method() in this class,
* this looks for $this->util->example_method().
*
* @param string $name The name of the method called in this class.
* @param array $arguments The arguments passed to the method.
* @return mixed The result of calling the util method, if it exists.
* @throws \Exception On calling a method that isn't defined in this class or Util.
*/
public function __call( $name, $arguments ) {
if ( method_exists( $this->util, $name ) ) {
return call_user_func_array( [ $this->util, $name ], $arguments );
}
if ( ! method_exists( $this, $name ) ) {
$class = get_class( $this );
throw new \Exception( "Call to undefined method {$class}::{$name}()" );
}
}
/**
* Get the plugin basename.
*
* @return string The basename.
*/
public function get_basename() {
return $this->basename;
}
/**
* Set the plugin basename.
*
* @param string $basename The basename.
*
* @return Plugin_Abstract The plugin instance.
*/
public function set_basename( $basename ) {
$this->basename = $basename;
return $this;
}
/**
* Get the plugin's directory.
*
* @return string The directory.
*/
public function get_directory() {
return $this->directory;
}
/**
* Set the plugin's directory.
*
* @param string $directory The directory.
*
* @return Plugin_Abstract The plugin instance.
*/
public function set_directory( $directory ) {
$this->directory = rtrim( $directory, DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR;
return $this;
}
/**
* Get the relative path to the plugin's directory.
*
* @param string $path Relative path to return.
*
* @return string The path.
*/
public function get_path( $path = '' ) {
return $this->directory . ltrim( $path, DIRECTORY_SEPARATOR );
}
/**
* Get the plugin file.
*
* @return string The file.
*/
public function get_file() {
return $this->file;
}
/**
* Set the plugin file.
*
* @param string $file The plugin file.
*
* @return Plugin_Abstract The plugin instance.
*/
public function set_file( $file ) {
$this->file = $file;
return $this;
}
/**
* Get the plugin's slug.
*
* @return string The slug.
*/
public function get_slug() {
return $this->slug;
}
/**
* Set the plugin's slug.
*
* @param string $slug The slug.
*
* @return Plugin_Abstract The plugin instance.
*/
public function set_slug( $slug ) {
$this->slug = $slug;
return $this;
}
/**
* Get the relative url.
*
* @param string $path The relative url to get.
*
* @return string The url.
*/
public function get_url( $path = '' ) {
return $this->url . ltrim( $path, '/' );
}
/**
* Set the plugin's url.
*
* @param string $url The url.
*
* @return Plugin_Abstract The plugin instance.
*/
public function set_url( $url ) {
$this->url = rtrim( $url, '/' ) . '/';
return $this;
}
/**
* Get the plugin's version.
*
* @return string The url.
*/
public function get_version() {
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
return time();
}
return $this->version;
}
/**
* Set the plugin's version.
*
* @param string $file The absolute path to the plugin file.
*
* @return Plugin_Abstract The plugin instance.
*/
public function set_version( $file ) {
$headers = [ 'Version' => 'Version' ];
$file_data = get_file_data( $file, $headers, 'plugin' );
if ( isset( $file_data['Version'] ) ) {
$this->version = $file_data['Version'];
};
return $this;
}
/**
* Get url relative to assets url.
*
* @param string $path The relative url to get.
*
* @return string The url.
*/
public function get_assets_url( $path = '' ) {
return $this->url . 'assets/' . ltrim( $path, '/' );
}
/**
* Get the relative path to the assets directory.
*
* @param string $path Relative path to return.
*
* @return string The path.
*/
public function get_assets_path( $path = '' ) {
return $this->directory . 'assets' . DIRECTORY_SEPARATOR . ltrim( $path, DIRECTORY_SEPARATOR );
}
/**
* Register a new Component.
*
* @param Component_Interface $component The new component.
*
* @return Plugin_Abstract The plugin instance.
*/
public function register_component( Component_Interface $component ) {
$component_class = get_class( $component );
// If component already registered, then there is nothing left to do.
if ( array_key_exists( $component_class, $this->components ) ) {
return $this;
}
// Make sure the plugin is available.
if ( method_exists( $component, 'set_plugin' ) ) {
$component->set_plugin( $this );
}
// Run component init method.
if ( method_exists( $component, 'init' ) ) {
$component->init( $this );
}
$component->register_hooks();
$this->components[ $component_class ] = $component;
return $this;
}
/**
* Runs as early as possible.
*
* @return void Nothing to return.
*/
abstract public function init();
/**
* Runs once 'plugins_loaded' hook fires.
*
* @return void Nothing to return.
*/
abstract public function plugin_loaded();
}