Passed
Push — master ( 5feb54...24c7e5 )
by Paul
12:51 queued 05:53
created

Addon::update()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 10
ccs 0
cts 10
cp 0
rs 10
cc 4
nc 4
nop 0
crap 20
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Addons;
4
5
use GeminiLabs\SiteReviews\Contracts\DefaultsContract;
6
use GeminiLabs\SiteReviews\Helpers\Str;
7
use GeminiLabs\SiteReviews\Plugin;
8
use ReflectionClass;
9
10
/**
11
 * @property string $file
12
 * @property string $id
13
 * @property string $languages
14
 * @property bool $licensed
15
 * @property string $name
16
 * @property string $slug
17
 * @property string $testedTo
18
 * @property string $update_url
19
 * @property string $version
20
 */
21
abstract class Addon
22
{
23
    use Plugin;
24
25
    const ID = '';
26
    const LICENSED = false;
27
    const NAME = '';
28
    const POST_TYPE = '';
29
    const SLUG = '';
30
    const UPDATE_URL = 'https://niftyplugins.com';
31
32
    /**
33
     * @return void
34
     */
35
    public function init()
36
    {
37
        $reflection = new ReflectionClass($this);
38
        $className = Str::replaceLast($reflection->getShortname(), 'Hooks', $reflection->getName());
39
        if (class_exists($className)) {
40
            (new $className())->run();
41
        } else {
42
            glsr_log()->error('The '.static::NAME.' add-on is missing a Hooks class');
43
        }
44
    }
45
46
    public function make($class, array $parameters = [])
47
    {
48
        $class = Str::camelCase($class);
49
        $class = ltrim(str_replace([__NAMESPACE__, 'GeminiLabs\SiteReviews'], '', $class), '\\');
50
        $class = __NAMESPACE__.'\\'.$class;
51
        return glsr($class, $parameters);
52
    }
53
54
    /**
55
     * @param string $path
56
     * @param mixed $fallback
57
     * @param string $cast
58
     * @return mixed
59
     */
60
    public function option($path = '', $fallback = '', $cast = '')
61
    {
62
        $path = Str::removePrefix($path, 'settings.');
63
        $path = Str::prefix($path, 'addons.'.static::SLUG.'.');
64
        $path = Str::prefix($path, 'settings.');
65
        return glsr_get_option($path, $fallback, $cast);
66
    }
67
68
    /**
69
     * @param string $defaultsClass  The defaults class used to restrict the options
70
     * @return \GeminiLabs\SiteReviews\Arguments
71
     */
72
    public function options($defaultsClass = '')
73
    {
74
        $options = glsr_get_option('settings.addons.'.static::SLUG, [], 'array');
75
        if (is_a($defaultsClass, DefaultsContract::class, true)) {
76
            $options = glsr($defaultsClass)->restrict($options);
77
        }
78
        return glsr()->args($options);
79
    }
80
81
    /**
82
     * @param int $perPage
83
     * @return array
84
     */
85
    public function posts($perPage = 50)
86
    {
87
        if (empty(static::POST_TYPE)) {
88
            return [];
89
        }
90
        $posts = get_posts([
91
            'order' => 'ASC',
92
            'orderby' => 'post_title',
93
            'post_type' => static::POST_TYPE,
94
            'post_status' => 'publish',
95
            'posts_per_page' => $perPage,
96
        ]);
97
        $results = wp_list_pluck($posts, 'post_title', 'ID');
98
        foreach ($results as $id => &$title) {
99
            if (empty(trim($title))) {
100
                $title = _x('Untitled', 'admin-text', 'site-reviews');
101
            }
102
            $title = sprintf('%s (ID: %s)', $title, $id);
103
        }
104
        natsort($results);
105
        return $results;
106
    }
107
}
108