Passed
Push — hotfix/fix-counts ( 4b43d1...cc9e05 )
by Paul
03:52
created

Style::customize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 10
ccs 0
cts 8
cp 0
crap 6
rs 10
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules;
4
5
use GeminiLabs\SiteReviews\Database\OptionManager;
6
use GeminiLabs\SiteReviews\Defaults\PaginationDefaults;
7
use GeminiLabs\SiteReviews\Defaults\StyleFieldsDefaults;
8
use GeminiLabs\SiteReviews\Defaults\StyleValidationDefaults;
9
use GeminiLabs\SiteReviews\Helper;
10
use GeminiLabs\SiteReviews\Modules\Html\Builder;
11
12
class Style
13
{
14
    /**
15
     * @var array
16
     */
17
    public $fields;
18
19
    /**
20
     * @var string
21
     */
22
    public $style;
23
24
    /**
25
     * @var array
26
     */
27
    public $pagination;
28
29
    /**
30
     * @var array
31
     */
32
    public $validation;
33
34 7
    public function __construct()
35
    {
36 7
        $this->style = glsr(OptionManager::class)->get('settings.general.style', 'default');
37 7
        $this->setConfig();
38 7
    }
39
40
    /**
41
     * @param string $view
42
     * @return string
43
     */
44 7
    public function filterView($view)
45
    {
46
        $styledViews = [
47 7
            'templates/form/field',
48
            'templates/form/response',
49
            'templates/form/submit-button',
50
            'templates/reviews-form',
51
        ];
52 7
        if (!preg_match('('.implode('|', $styledViews).')', $view)) {
53 7
            return $view;
54
        }
55
        $views = $this->generatePossibleViews($view);
56
        foreach ($views as $possibleView) {
57
            if (!file_exists(glsr()->file($possibleView))) {
58
                continue;
59
            }
60
            return glsr(Helper::class)->removePrefix('views/', $possibleView);
61
        }
62
        return $view;
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function get()
69
    {
70
        return apply_filters('site-reviews/style', $this->style);
71
    }
72
73
    /**
74
     * @return array
75
     */
76 7
    public function setConfig()
77
    {
78 7
        $config = shortcode_atts(
79 7
            array_fill_keys(['fields', 'pagination', 'validation'], []),
80 7
            glsr()->config('styles/'.$this->style)
81
        );
82 7
        $this->fields = glsr(StyleFieldsDefaults::class)->restrict($config['fields']);
83 7
        $this->pagination = glsr(PaginationDefaults::class)->restrict($config['pagination']);
84 7
        $this->validation = glsr(StyleValidationDefaults::class)->restrict($config['validation']);
85 7
    }
86
87
    /**
88
     * @return void
89
     */
90
    public function modifyField(Builder $instance)
91
    {
92
        if (!$this->isPublicInstance($instance) || empty(array_filter($this->fields))) {
93
            return;
94
        }
95
        call_user_func_array([$this, 'customize'], [$instance]);
96
    }
97
98
    /**
99
     * @return array
100
     */
101
    public function paginationArgs(array $args)
102
    {
103
        return wp_parse_args($args, $this->pagination);
104
    }
105
106
    /**
107
     * @return void
108
     */
109
    protected function customize(Builder $instance)
110
    {
111
        if (!array_key_exists($instance->tag, $this->fields)) {
112
            return;
113
        }
114
        $args = wp_parse_args($instance->args, array_fill_keys(['class', 'type'], ''));
115
        $key = $instance->tag.'_'.$args['type'];
116
        $classes = glsr_get($this->fields, $key, glsr_get($this->fields, $instance->tag));
117
        $instance->args['class'] = trim($args['class'].' '.$classes);
118
        do_action_ref_array('site-reviews/customize/'.$this->style, [$instance]);
119
    }
120
121
    /**
122
     * @param string $view
123
     * @return array
124
     */
125
    protected function generatePossibleViews($view)
126
    {
127
        $basename = basename($view);
128
        $basepath = rtrim($view, $basename);
129
        $customPath = 'views/partials/styles/'.$this->style.'/';
130
        $parts = explode('_', $basename);
131
        $views = [
132
            $customPath.$basename,
133
            $customPath.$parts[0],
134
            $view,
135
            $basepath.$parts[0],
136
        ];
137
        return array_filter($views);
138
    }
139
140
    /**
141
     * @return bool
142
     */
143
    protected function isPublicInstance(Builder $instance)
144
    {
145
        $args = wp_parse_args($instance->args, [
146
            'is_public' => false,
147
            'is_raw' => false,
148
        ]);
149
        if (is_admin() || !$args['is_public'] || $args['is_raw']) {
150
            return false;
151
        }
152
        return true;
153
    }
154
}
155