Test Failed
Push — master ( 8a9607...6de795 )
by Paul
05:45
created

Plugin::themePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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 15
            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 $view
93
     * @return string
94
     */
95 15
    public function build($view, array $data = [])
96
    {
97 15
        ob_start();
98 15
        $this->render($view, $data);
99 15
        return ob_get_clean();
100
    }
101
102
    /**
103
     * @return void
104
     */
105
    public function catchFatalError()
106
    {
107
        $error = error_get_last();
108
        if (E_ERROR === Arr::get($error, 'type') && Str::contains($this->path(), Arr::get($error, 'message'))) {
109
            glsr_log()->error($error['message']);
110
        }
111
    }
112
113
    /**
114
     * @param string $name
115
     * @param bool $filtered
116
     * @return array
117
     */
118 9
    public function config($name, $filtered = true)
119
    {
120 9
        $path = $this->filterString('config', 'config/'.$name.'.php');
121 9
        $configFile = $this->path($path);
122 9
        $config = file_exists($configFile)
123 9
            ? include $configFile
124 9
            : [];
125 9
        return $filtered
126 9
            ? $this->filterArray('config/'.$name, $config)
127 9
            : $config;
128
    }
129
130
    /**
131
     * @param string $property
132
     * @return string
133
     */
134 21
    public function constant($property, $className = 'static')
135
    {
136 21
        $property = strtoupper($property);
137 21
        $constant = $className.'::'.$property;
138 21
        return defined($constant)
139 21
            ? $this->filterString('const/'.$property, constant($constant))
140 21
            : '';
141
    }
142
143
    /**
144
     * @param string $view
145
     * @return void|string
146
     */
147 15
    public function file($view)
148
    {
149 15
        $view .= '.php';
150 15
        $filePaths = [];
151 15
        if (Str::startsWith('templates/', $view)) {
152 14
            $filePaths[] = $this->themePath(Str::removePrefix($view, 'templates/'));
153
        }
154 15
        $filePaths[] = $this->path($view);
155 15
        $filePaths[] = $this->path('views/'.$view);
156 15
        foreach ($filePaths as $file) {
157 15
            if (file_exists($file)) {
158 15
                return $file;
159
            }
160
        }
161
    }
162
163
    /**
164
     * @param string $hook
165
     * @param mixed ...$args
166
     * @return mixed
167
     */
168 27
    public function filter($hook, ...$args)
169
    {
170 27
        return apply_filters_ref_array($this->id.'/'.$hook, $args);
171
    }
172
173
    /**
174
     * @param string $hook
175
     * @param mixed ...$args
176
     * @return array
177
     */
178 7
    public function filterArrayUnique($hook, ...$args)
179
    {
180 7
        $filtered = apply_filters_ref_array($this->id.'/'.$hook, $args);
181 7
        return array_unique(array_filter(Cast::toArray($filtered)));
182
    }
183
184
    /**
185
     * @return static
186
     */
187 27
    public static function load()
188
    {
189 27
        if (empty(static::$instance)) {
190
            static::$instance = new static();
191
        }
192 27
        return static::$instance;
193
    }
194
195
    /**
196
     * @param string $file
197
     * @return string
198
     */
199 15
    public function path($file = '', $realpath = true)
200
    {
201 15
        $path = plugin_dir_path($this->file);
202 15
        if (!$realpath) {
203 9
            $path = trailingslashit(WP_PLUGIN_DIR).basename(dirname($this->file));
204
        }
205 15
        $path = trailingslashit($path).ltrim(trim($file), '/');
206 15
        return $this->filterString('path', $path, $file);
207
    }
208
209
    /**
210
     * @param string $view
211
     * @return void
212
     */
213 15
    public function render($view, array $data = [])
214
    {
215 15
        $view = $this->filterString('render/view', $view, $data);
216 15
        $file = $this->filterString('views/file', $this->file($view), $view, $data);
217 15
        if (!file_exists($file)) {
218
            glsr_log()->error(sprintf('File not found: (%s) %s', $view, $file));
219
            return;
220
        }
221 15
        $data = $this->filterArray('views/data', $data, $view);
222 15
        extract($data);
223 15
        include $file;
224 15
    }
225
226
    /**
227
     * @param string $className
228
     * @return mixed|false
229
     */
230
    public function runIf($className, ...$args)
231
    {
232
        return class_exists($className)
233
            ? call_user_func_array([glsr($className), 'handle'], $args)
234
            : false;
235
    }
236
237
    /**
238
     * @param string $file
239
     * @return string
240
     */
241 14
    public function themePath($file = '')
242
    {
243 14
        return get_stylesheet_directory().'/'.$this->id.'/'.ltrim(trim($file), '/');
244
    }
245
246
    /**
247
     * @param string $path
248
     * @return string
249
     */
250
    public function url($path = '')
251
    {
252
        $url = esc_url(plugin_dir_url($this->file).ltrim(trim($path), '/'));
253
        return $this->filterString('url', $url, $path);
254
    }
255
256
    /**
257
     * @param string $versionLevel
258
     * @return string
259
     */
260 7
    public function version($versionLevel = '')
261
    {
262 7
        $pattern = '/^v?(\d{1,5})(\.\d++)?(\.\d++)?(.+)?$/i';
263 7
        preg_match($pattern, $this->version, $matches);
264 7
        switch ($versionLevel) {
265 7
            case 'major':
266
                $version = Arr::get($matches, 1);
267
                break;
268 7
            case 'minor':
269 7
                $version = Arr::get($matches, 1).Arr::get($matches, 2);
270 7
                break;
271
            case 'patch':
272
                $version = Arr::get($matches, 1).Arr::get($matches, 2).Arr::get($matches, 3);
273
                break;
274
        }
275 7
        return empty($version)
276
            ? $this->version
277 7
            : $version;
278
    }
279
}
280