Test Failed
Push — main ( ed15c3...2f7161 )
by Paul
12:26 queued 06:03
created

Style::isPublicInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules;
4
5
use GeminiLabs\SiteReviews\Contracts\FieldContract;
6
use GeminiLabs\SiteReviews\Defaults\PaginationDefaults;
7
use GeminiLabs\SiteReviews\Defaults\StyleClassesDefaults;
8
use GeminiLabs\SiteReviews\Defaults\StyleValidationDefaults;
9
use GeminiLabs\SiteReviews\Helper;
10
use GeminiLabs\SiteReviews\Helpers\Arr;
11
use GeminiLabs\SiteReviews\Helpers\Str;
12
13
/**
14
 * @method string classes(string $key)
15
 * @method string defaultClasses(string $key)
16
 * @method string defaultValidation(string $key)
17
 * @method string validation(string $key)
18
 */
19
class Style
20
{
21
    /**
22
     * The properties that are accessible.
23
     *
24
     * @var array
25
     */
26
    protected $accessible = [
27
        'classes', 'style', 'pagination', 'validation',
28
    ];
29
30
    /**
31
     * The methods that are callable.
32
     *
33
     * @var array
34
     */
35
    protected $callable = [
36
        'classes', 'validation',
37
    ];
38
39
    /**
40
     * @var array
41
     */
42
    protected $classes;
43
44
    /**
45
     * @var string
46
     */
47
    protected $style;
48
49
    /**
50
     * @var array
51
     */
52
    protected $pagination;
53
54
    /**
55
     * @var array
56
     */
57
    protected $validation;
58
59
    public function __call($method, $args)
60
    {
61
        $property = strtolower(Str::removePrefix($method, 'default'));
62
        if (!in_array($property, $this->callable)) {
63
            return;
64
        }
65
        $key = Arr::get($args, 0);
66
        if (str_starts_with($method, 'default')) {
67
            $className = Helper::buildClassName(['style', $property, 'defaults'], 'Defaults');
68
            return glsr()->args(glsr($className)->defaults())->$key;
69
        }
70
        return glsr()->args($this->__get($property))->$key;
71
    }
72 8
73
    public function __get($property)
74 8
    {
75
        if (!in_array($property, $this->accessible)) {
76
            return;
77 8
        }
78
        if (!isset($this->$property)) {
79
            $style = glsr_get_option('general.style', 'default');
80
            $config = shortcode_atts(array_fill_keys(['classes', 'pagination', 'validation'], []),
81
                glsr()->config("styles/{$style}")
82
            );
83
            $this->classes = glsr(StyleClassesDefaults::class)->restrict($config['classes']);
84
            $this->pagination = glsr(PaginationDefaults::class)->restrict($config['pagination']);
85
            $this->style = $style;
86
            $this->validation = glsr(StyleValidationDefaults::class)->restrict($config['validation']);
87 8
        }
88
        return $this->$property;
89
    }
90 8
91
    public function fieldClass(FieldContract $field): string
92 8
    {
93 8
        if (!array_key_exists($field->tag(), $this->__get('classes'))) {
94
            return $field->class;
95
        }
96
        $key = "{$field->tag()}_{$field->type}";
97
        $fallback = Arr::get($this->classes, $field->tag());
98
        $class = Arr::getAs('string', $this->classes, $key, $fallback);
99
        return trim("{$class} {$field->class}");
100
    }
101
102
    /**
103
     * This allows us to override the pagination config in /config/styles instead of using a filter hook.
104
     */
105
    public function paginationArgs(array $args): array
106
    {
107
        return wp_parse_args($args, $this->__get('pagination'));
108
    }
109
110
    public function styleClasses(): string
111
    {
112
        $style = $this->__get('style');
113
        $classes = glsr()->filterString('style', "glsr glsr-{$style}");
114
        return glsr(Sanitizer::class)->sanitizeAttrClass($classes);
115
    }
116
117
    public function stylesheetUrl(?string $suffix = ''): string
118
    {
119
        if ($suffix) {
120
            $string = 'assets/styles/%1$s/%2$s-%1$s.css';
121
            $path = sprintf($string, $suffix, $this->__get('style'));
122
            return file_exists(glsr()->path($path))
123
                ? glsr()->url($path)
124
                : glsr()->url(sprintf($string, $suffix, 'default'));
125
        }
126
        $string = 'assets/styles/%s.css';
127 9
        $path = sprintf($string, $this->__get('style'));
128
        return file_exists(glsr()->path($path))
129 9
            ? glsr()->url($path)
130 9
            : glsr()->url(sprintf($string, 'default'));
131 9
    }
132 9
133 9
    public function view(string $view): string
134 9
    {
135 9
        $templates = [
136 9
            'templates/form/field',
137 9
            'templates/form/response',
138 9
            'templates/form/submit-button',
139 9
            'templates/form/type-checkbox',
140 9
            'templates/form/type-radio',
141 9
            'templates/form/type-toggle',
142 9
            'templates/load-more-button',
143
            'templates/pagination',
144
            'templates/reviews-form',
145
        ];
146
        $templates = glsr()->filterArray('style/templates', $templates);
147
        if (!preg_match('('.implode('|', $templates).')', $view)) {
148
            return $view;
149
        }
150
        $views = $this->generatePossibleViews($view);
151
        foreach ($views as $possibleView) {
152
            if (file_exists(glsr()->file($possibleView))) {
153
                return Str::removePrefix($possibleView, 'views/');
154
            }
155
        }
156 8
        return $view;
157
    }
158 8
159
    protected function generatePossibleViews(string $view): array
160
    {
161
        $basename = basename($view);
162
        $basepath = rtrim($view, $basename);
163
        $style = $this->__get('style');
164
        $customPath = "views/styles/{$style}/";
165
        $parts = explode('_', $basename);
166
        $views = [
167
            $customPath.$basename, // styled view
168
            $customPath.$parts[0], // styled view (base)
169
            $view, // default view
170
            $basepath.$parts[0], // default view (base)
171
        ];
172
        $views = glsr()->filterArray('style/views', $views, $view);
173
        return array_filter($views);
174
    }
175
}
176