Passed
Push — master ( 4562dd...74d0bd )
by Paul
04:12
created

Style::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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