1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
7
|
|
|
use GeminiLabs\SiteReviews\Helpers\Cast; |
8
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
9
|
|
|
use ReflectionClass; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @property string $id |
13
|
|
|
* @property string $name |
14
|
|
|
* @method array filterArray($hook, ...$args) |
15
|
|
|
* @method bool filterBool($hook, ...$args) |
16
|
|
|
* @method float filterFloat($hook, ...$args) |
17
|
|
|
* @method int filterInt($hook, ...$args) |
18
|
|
|
* @method object filterObject($hook, ...$args) |
19
|
|
|
* @method string filterString($hook, ...$args) |
20
|
|
|
*/ |
21
|
|
|
trait Plugin |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var static |
25
|
|
|
*/ |
26
|
|
|
protected static $instance; |
27
|
|
|
|
28
|
|
|
protected $file; |
29
|
|
|
protected $languages; |
30
|
|
|
protected $testedTo; |
31
|
|
|
protected $version; |
32
|
|
|
|
33
|
27 |
|
public function __call($method, $args) |
34
|
|
|
{ |
35
|
27 |
|
$isFilter = Str::startsWith('filter', $method); |
36
|
27 |
|
$to = Helper::buildMethodName(Str::removePrefix($method, 'filter'), 'to'); |
37
|
27 |
|
if ($isFilter && method_exists(Cast::class, $to)) { |
38
|
27 |
|
$filtered = call_user_func_array([$this, 'filter'], $args); |
39
|
27 |
|
return Cast::$to($filtered); |
40
|
|
|
} |
41
|
|
|
throw new BadMethodCallException("Method [$method] does not exist."); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function __construct() |
45
|
|
|
{ |
46
|
|
|
$file = wp_normalize_path((new ReflectionClass($this))->getFileName()); |
47
|
|
|
$this->file = str_replace('plugin/Application', $this->id, $file); |
48
|
|
|
$plugin = get_file_data($this->file, [ |
49
|
|
|
'languages' => 'Domain Path', |
50
|
|
|
'name' => 'Plugin Name', |
51
|
|
|
'testedTo' => 'Tested up to', |
52
|
|
|
'version' => 'Version', |
53
|
|
|
], 'plugin'); |
54
|
|
|
array_walk($plugin, function ($value, $key) { |
55
|
|
|
if (property_exists($this, $key)) { |
56
|
|
|
$this->$key = $value; |
57
|
|
|
} |
58
|
|
|
}); |
59
|
|
|
} |
60
|
|
|
|
61
|
27 |
|
public function __get($property) |
62
|
|
|
{ |
63
|
27 |
|
if (property_exists($this, $property)) { |
64
|
21 |
|
return $this->$property; |
65
|
|
|
} |
66
|
27 |
|
$constant = 'static::'.strtoupper($property); |
67
|
27 |
|
if (defined($constant)) { |
68
|
27 |
|
return constant($constant); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $hook |
74
|
|
|
* @param mixed ...$args |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
26 |
|
public function action($hook, ...$args) |
78
|
|
|
{ |
79
|
26 |
|
do_action_ref_array($this->id.'/'.$hook, $args); |
80
|
26 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param mixed $args |
84
|
|
|
* @return Arguments |
85
|
|
|
*/ |
86
|
15 |
|
public function args($args = []) |
87
|
|
|
{ |
88
|
15 |
|
return new Arguments($args); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $name |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
9 |
|
public function config($name) |
96
|
|
|
{ |
97
|
9 |
|
$path = $this->filterString('config', 'config/'.$name.'.php'); |
98
|
9 |
|
$configFile = $this->path($path); |
99
|
9 |
|
$config = file_exists($configFile) |
100
|
9 |
|
? include $configFile |
101
|
9 |
|
: []; |
102
|
9 |
|
return $this->filterArray('config/'.$name, $config); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $property |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
21 |
|
public function constant($property, $className = 'static') |
110
|
|
|
{ |
111
|
21 |
|
$property = strtoupper($property); |
112
|
21 |
|
$constant = $className.'::'.$property; |
113
|
21 |
|
return defined($constant) |
114
|
21 |
|
? $this->filterString('const/'.$property, constant($constant)) |
115
|
21 |
|
: ''; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $hook |
120
|
|
|
* @param mixed ...$args |
121
|
|
|
* @return mixed |
122
|
|
|
*/ |
123
|
27 |
|
public function filter($hook, ...$args) |
124
|
|
|
{ |
125
|
27 |
|
return apply_filters_ref_array($this->id.'/'.$hook, $args); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $hook |
130
|
|
|
* @param mixed ...$args |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
7 |
|
public function filterArrayUnique($hook, ...$args) |
134
|
|
|
{ |
135
|
7 |
|
$filtered = apply_filters_ref_array($this->id.'/'.$hook, $args); |
136
|
7 |
|
return array_unique(array_filter(Cast::toArray($filtered))); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return static |
141
|
|
|
*/ |
142
|
27 |
|
public static function load() |
143
|
|
|
{ |
144
|
27 |
|
if (empty(static::$instance)) { |
145
|
|
|
static::$instance = new static(); |
146
|
|
|
} |
147
|
27 |
|
return static::$instance; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param string $file |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
15 |
|
public function path($file = '', $realpath = true) |
155
|
|
|
{ |
156
|
15 |
|
$path = plugin_dir_path($this->file); |
157
|
15 |
|
if (!$realpath) { |
158
|
2 |
|
$path = trailingslashit(WP_PLUGIN_DIR).basename(dirname($this->file)); |
159
|
|
|
} |
160
|
15 |
|
$path = trailingslashit($path).ltrim(trim($file), '/'); |
161
|
15 |
|
return $this->filterString('path', $path, $file); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param string $className |
166
|
|
|
* @return mixed|false |
167
|
|
|
*/ |
168
|
|
|
public function runIf($className, ...$args) |
169
|
|
|
{ |
170
|
|
|
return class_exists($className) |
171
|
|
|
? call_user_func_array([glsr($className), 'handle'], $args) |
172
|
|
|
: false; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param string $path |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
public function url($path = '') |
180
|
|
|
{ |
181
|
|
|
$url = esc_url(plugin_dir_url($this->file).ltrim(trim($path), '/')); |
182
|
|
|
return $this->filterString('url', $url, $path); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param string $versionLevel |
187
|
|
|
* @return string |
188
|
|
|
*/ |
189
|
|
|
public function version($versionLevel = '') |
190
|
|
|
{ |
191
|
|
|
$pattern = '/^v?(\d{1,5})(\.\d++)?(\.\d++)?(.+)?$/i'; |
192
|
|
|
preg_match($pattern, $this->version, $matches); |
193
|
|
|
switch ($versionLevel) { |
194
|
|
|
case 'major': |
195
|
|
|
$version = Arr::get($matches, 1); |
196
|
|
|
break; |
197
|
|
|
case 'minor': |
198
|
|
|
$version = Arr::get($matches, 1).Arr::get($matches, 2); |
199
|
|
|
break; |
200
|
|
|
case 'patch': |
201
|
|
|
$version = Arr::get($matches, 1).Arr::get($matches, 2).Arr::get($matches, 3); |
202
|
|
|
break; |
203
|
|
|
} |
204
|
|
|
return empty($version) |
205
|
|
|
? $this->version |
206
|
|
|
: $version; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|