Passed
Push — master ( 7f2931...8eca98 )
by Paul
08:06 queued 03:49
created

Style::setConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
ccs 7
cts 7
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
		$args = wp_parse_args( $instance->args, array_fill_keys( ['class', 'type'], '' ));
109
		$key = $instance->tag.'_'.$args['type'];
110
		$classes = !isset( $this->fields[$key] )
111
			? $this->fields[$instance->tag]
112
			: $this->fields[$key];
113
		$instance->args['class'] = trim( $args['class'].' '.$classes );
114
		do_action_ref_array( 'site-reviews/customize/'.$this->style, [&$instance] );
115
	}
116
117
	/**
118
	 * @param string $view
119
	 * @return array
120
	 */
121
	protected function generatePossibleViews( $view )
122
	{
123
		$basename = basename( $view );
124
		$basepath = rtrim( $view, $basename );
125
		$customPath = 'views/partials/styles/'.$this->style.'/';
126
		$parts = explode( '_', $basename );
127
		$views = [
128
			$customPath.$basename,
129
			$customPath.$parts[0],
130
			$view,
131
			$basepath.$parts[0],
132
		];
133
		return array_filter( $views );
134
	}
135
136
	/**
137
	 * @return bool
138
	 */
139
	protected function isPublicInstance( Builder $instance )
140
	{
141
		$args = wp_parse_args( $instance->args, [
142
			'is_public' => false,
143
			'is_raw' => false,
144
		]);
145
		if( is_admin() || !$args['is_public'] || $args['is_raw'] ) {
146
			return false;
147
		}
148
		return true;
149
	}
150
}
151