Passed
Branch master (9c6ab0)
by Paul
12:17 queued 01:01
created

Addon::posts()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 15
c 0
b 0
f 0
dl 0
loc 21
ccs 0
cts 21
cp 0
rs 9.7666
cc 4
nc 4
nop 1
crap 20
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Addons;
4
5
use GeminiLabs\SiteReviews\Helpers\Str;
6
use GeminiLabs\SiteReviews\Plugin;
7
use ReflectionClass;
8
9
/**
10
 * @property string $file
11
 * @property string $id
12
 * @property string $languages
13
 * @property bool $licensed
14
 * @property string $name
15
 * @property string $slug
16
 * @property string $testedTo
17
 * @property string $update_url
18
 * @property Updater $updater
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 SLUG = '';
29
    const UPDATE_URL = '';
30
31
    protected $updater;
32
33
    /**
34
     * @return void
35
     */
36
    public function init()
37
    {
38
        $reflection = new ReflectionClass($this);
39
        $className = Str::replaceLast($reflection->getShortname(), 'Hooks', $reflection->getName());
40
        if (class_exists($className)) {
41
            (new $className())->run();
42
        } else {
43
            glsr_log()->error('The '.static::NAME.' add-on is missing a Hooks class');
44
        }
45
    }
46
47
    public function make($class, array $parameters = [])
48
    {
49
        $class = Str::camelCase($class);
50
        $class = ltrim(str_replace([__NAMESPACE__, 'GeminiLabs\SiteReviews'], '', $class), '\\');
51
        $class = __NAMESPACE__.'\\'.$class;
52
        return glsr($class, $parameters);
53
    }
54
55
    /**
56
     * @param int $perPage
57
     * @return array
58
     */
59
    public function posts($perPage = 50)
60
    {
61
        if (!defined('static::POST_TYPE')) {
62
            return [];
63
        }
64
        $posts = get_posts([
65
            'order' => 'ASC',
66
            'orderby' => 'post_title',
67
            'post_type' => static::POST_TYPE,
68
            'post_status' => 'publish',
69
            'posts_per_page' => $perPage,
70
        ]);
71
        $results = wp_list_pluck($posts, 'post_title', 'ID');
72
        foreach ($results as $id => &$title) {
73
            if (empty(trim($title))) {
74
                $title = _x('Untitled', 'admin-text', 'site-reviews');
75
            }
76
            $title = sprintf('%s (ID: %s)', $title, $id);
77
        }
78
        natsort($results);
79
        return $results;
80
    }
81
82
    /**
83
     * @return void
84
     */
85
    public function update()
86
    {
87
        $doingCron = defined('DOING_CRON') && DOING_CRON;
88
        if (!current_user_can('manage_options') && !$doingCron) {
89
            return;
90
        }
91
        $this->updater = new Updater(static::UPDATE_URL, $this->file, [
92
            'license' => glsr_get_option('licenses.'.static::ID),
93
        ]);
94
        $this->updater->init();
95
    }
96
}
97