Passed
Push — master ( 7d2cd0...16abb1 )
by Paul
04:36
created

Style::setConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

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