Passed
Push — master ( f3cc5e...8dc102 )
by Paul
03:59
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\Helper;
8
use GeminiLabs\SiteReviews\Modules\Html\Builder;
9
10
class Style
11
{
12
	/**
13
	 * @var string
14
	 */
15
	public $style;
16
17
	/**
18
	 * @var array
19
	 */
20
	public $styles;
21
22 7
	public function __construct()
23
	{
24 7
		$this->style = glsr( OptionManager::class )->get( 'settings.submissions.style', 'default' );
25 7
		$this->styles = $this->getStyles();
26 7
	}
27
28
	/**
29
	 * @param string $view
30
	 * @return string
31
	 */
32 7
	public function filterView( $view )
33
	{
34
		$styledViews = [
35 7
			'templates/form/field',
36
			'templates/form/response',
37
			'templates/form/submit-button',
38
			'templates/reviews-form',
39
		];
40 7
		if( !preg_match( '('.implode( '|', $styledViews ).')', $view )) {
41 7
			return $view;
42
		}
43
		$views = $this->generatePossibleViews( $view );
44
		foreach( $views as $possibleView ) {
45
			if( !file_exists( glsr()->path( 'views/'.$possibleView.'.php' )))continue;
46
			return $possibleView;
47
		}
48
		return $view;
49
	}
50
51
	/**
52
	 * @return string
53
	 */
54
	public function get()
55
	{
56
		return $this->style;
57
	}
58
59
	/**
60
	 * @return array
61
	 */
62 7
	public function getStyles()
63
	{
64
		$keys = [
65 7
			'input', 'input_checkbox', 'input_radio', 'label', 'label_checkbox', 'label_radio',
66
			'select', 'textarea',
67
		];
68 7
		return wp_parse_args(
69 7
			glsr()->config( 'styles/'.$this->style ),
70 7
			array_fill_keys( $keys, '' )
71
		);
72
	}
73
74
	/**
75
	 * @return void
76
	 */
77
	public function modifyField( Builder $instance )
78
	{
79
		if( !$this->isPublicInstance( $instance ) || empty( array_filter( $this->styles )))return;
80
		call_user_func_array( [$this, 'customize'], [&$instance] );
81
	}
82
83
	/**
84
	 * @return void
85
	 */
86
	protected function customize( Builder $instance )
87
	{
88
		$args = wp_parse_args( $instance->args, array_fill_keys( ['class', 'type'], '' ));
89
		$key = $instance->tag.'_'.$args['type'];
90
		$classes = !isset( $this->styles[$key] )
91
			? $this->styles[$instance->tag]
92
			: $this->styles[$key];
93
		$instance->args['class'] = trim( $args['class'].' '.$classes );
94
		do_action_ref_array( 'site-reviews/customize/'.$this->style, [&$instance] );
95
	}
96
97
	/**
98
	 * @param string $view
99
	 * @return array
100
	 */
101
	protected function generatePossibleViews( $view )
102
	{
103
		$basename = basename( $view );
104
		$basepath = rtrim( $view, $basename );
105
		$customPath = 'partials/styles/'.$this->style.'/';
106
		$parts = explode( '_', $basename );
107
		$views = [
108
			$customPath.$basename,
109
			$customPath.$parts[0],
110
			$view,
111
			$basepath.$parts[0],
112
		];
113
		return array_filter( $views );
114
	}
115
116
	/**
117
	 * @return bool
118
	 */
119
	protected function isPublicInstance( Builder $instance )
120
	{
121
		$args = wp_parse_args( $instance->args, [
122
			'is_public' => false,
123
			'is_raw' => false,
124
		]);
125
		if( is_admin() || !$args['is_public'] || $args['is_raw'] ) {
126
			return false;
127
		}
128
		return true;
129
	}
130
}
131