Passed
Push — master ( c3c8e4...faa349 )
by Paul
03:28
created

Shortcode::normalizeLabels()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 1
dl 0
loc 16
ccs 0
cts 12
cp 0
crap 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Shortcodes;
4
5
use GeminiLabs\SiteReviews\Contracts\ShortcodeContract;
6
use GeminiLabs\SiteReviews\Helper;
7
use GeminiLabs\SiteReviews\Modules\Html\Partial;
8
use GeminiLabs\SiteReviews\Modules\Rating;
9
use ReflectionClass;
10
11
abstract class Shortcode implements ShortcodeContract
12
{
13
	/**
14
	 * @param string|array $instance
15
	 * @param string $type
16
	 * @return string
17
	 */
18
	public function build( $instance, array $args = [], $type = 'shortcode' )
19
	{
20
		$shortcodePartial = $this->getShortcodePartial();
21
		$args = wp_parse_args( $args, [
22
			'before_widget' => '<div class="glsr-'.$type.' '.$type.'-'.$shortcodePartial.'">',
23
			'after_widget' => '</div>',
24
			'before_title' => '<h3 class="glsr-'.$type.'-title">',
25
			'after_title' => '</h3>',
26
		]);
27
		$args = apply_filters( 'site-reviews/shortcode/args', $args, $type, $shortcodePartial );
28
		$instance = $this->normalize( $instance );
29
		$partial = glsr( Partial::class )->build( $shortcodePartial, $instance );
30
		if( !empty( $instance['title'] )) {
31
			$instance['title'] = $args['before_title'].$instance['title'].$args['after_title'];
32
		}
33
		return $args['before_widget'].$instance['title'].$partial.$args['after_widget'];
34
	}
35
36
	/**
37
	 * @param string|array $atts
38
	 * @return string
39
	 */
40
	public function buildShortcode( $atts = [] )
41
	{
42
		return $this->build( $atts );
43
	}
44
45
	/**
46
	 * @return array
47
	 */
48
	public function getDefaults()
49
	{
50
		$className = glsr( Helper::class )->buildClassName(
51
			str_replace( 'Shortcode', 'Defaults', (new ReflectionClass( $this ))->getShortName() ),
52
			'Defaults'
53
		);
54
		return glsr( $className )->defaults();
55
	}
56
57
	/**
58
	 * @return array
59
	 */
60 1
	public function getHideOptions()
61
	{
62 1
		$options = $this->hideOptions();
63 1
		$shortcode = $this->getShortcodeName();
64 1
		return apply_filters( 'site-reviews/shortcode/hide-options', $options, $shortcode );
65
	}
66
67
	/**
68
	 * @return string
69
	 */
70 1
	public function getShortcodeName()
71
	{
72 1
		return glsr( Helper::class )->snakeCase(
73 1
			str_replace( 'Shortcode', '', (new ReflectionClass( $this ))->getShortName() )
74
		);
75
	}
76
77
	/**
78
	 * @return string
79
	 */
80
	public function getShortcodePartial()
81
	{
82
		return glsr( Helper::class )->dashCase(
83
			str_replace( 'Shortcode', '', (new ReflectionClass( $this ))->getShortName() )
84
		);
85
	}
86
87
	/**
88
	 * @param array|string $args
89
	 * @return array
90
	 */
91
	public function normalize( $args )
92
	{
93
		$args = shortcode_atts( $this->getDefaults(), wp_parse_args( $args ));
94
		array_walk( $args, function( &$value, $key ) {
95
			$methodName = glsr( Helper::class )->buildMethodName( $key, 'normalize' );
96
			if( !method_exists( $this, $methodName ))return;
97
			$value = $this->$methodName( $value );
98
		});
99
		return $this->sanitize( $args );
100
	}
101
102
	/**
103
	 * @return array
104
	 */
105
	abstract protected function hideOptions();
106
107
	/**
108
	 * @param string $postId
109
	 * @return int|string
110
	 */
111
	protected function normalizeAssignedTo( $postId )
112
	{
113
		if( $postId == 'parent_id' ) {
114
			$postId = intval( wp_get_post_parent_id( intval( get_the_ID() )));
115
		}
116
		else if( $postId == 'post_id' ) {
117
			$postId = intval( get_the_ID() );
118
		}
119
		return $postId;
120
	}
121
122
	/**
123
	 * @param string $postId
124
	 * @return int|string
125
	 */
126
	protected function normalizeAssignTo( $postId )
127
	{
128
		return $this->normalizeAssignedTo( $postId );
129
	}
130
131
	/**
132
	 * @param string|array $hide
133
	 * @return array
134
	 */
135
	protected function normalizeHide( $hide )
136
	{
137
		if( is_string( $hide )) {
138
			$hide = explode( ',', $hide );
139
		}
140
		$hideKeys = array_keys( $this->getHideOptions() );
141
		return array_filter( array_map( 'trim', $hide ), function( $value ) use( $hideKeys ) {
142
			return in_array( $value, $hideKeys );
143
		});
144
	}
145
146
	/**
147
	 * @param string $id
148
	 * @return string
149
	 */
150
	protected function normalizeId( $id )
151
	{
152
		return sanitize_title( $id );
153
	}
154
155
	/**
156
	 * @param string $labels
157
	 * @return array
158
	 */
159
	protected function normalizeLabels( $labels )
160
	{
161
		$defaults = [
162
			__( 'Excellent', 'site-reviews' ),
163
			__( 'Very good', 'site-reviews' ),
164
			__( 'Average', 'site-reviews' ),
165
			__( 'Poor', 'site-reviews' ),
166
			__( 'Terrible', 'site-reviews' ),
167
		];
168
		$defaults = array_pad( $defaults, Rating::MAX_RATING, '' );
169
		$labels = array_map( 'trim', explode( ',', $labels ));
170
		foreach( $defaults as $i => $label ) {
171
			if( empty( $labels[$i] ))continue;
172
			$defaults[$i] = $labels[$i];
173
		}
174
		return array_combine( range( Rating::MAX_RATING, 1 ), $defaults );
175
	}
176
177
	/**
178
	 * @param string $schema
179
	 * @return bool
180
	 */
181
	protected function normalizeSchema( $schema )
182
	{
183
		return wp_validate_boolean( $schema );
184
	}
185
186
	/**
187
	 * @param string $text
188
	 * @return string
189
	 */
190
	protected function normalizeText( $text )
191
	{
192
		return trim( $text );
193
	}
194
195
	/**
196
	 * @return array
197
	 */
198
	protected function sanitize( array $args )
199
	{
200
		return $args;
201
	}
202
}
203