Passed
Push — master ( 4a454f...b92003 )
by Paul
03:35
created

Shortcode::normalizeId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 2
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
		return $postId == 'post_id'
114
			? intval( get_the_ID() )
115
			: $postId;
116
	}
117
118
	/**
119
	 * @param string $postId
120
	 * @return int|string
121
	 */
122
	protected function normalizeAssignTo( $postId )
123
	{
124
		return $this->normalizeAssignedTo( $postId );
125
	}
126
127
	/**
128
	 * @param string|array $hide
129
	 * @return array
130
	 */
131
	protected function normalizeHide( $hide )
132
	{
133
		if( is_string( $hide )) {
134
			$hide = explode( ',', $hide );
135
		}
136
		$hideKeys = array_keys( $this->getHideOptions() );
137
		return array_filter( array_map( 'trim', $hide ), function( $value ) use( $hideKeys ) {
138
			return in_array( $value, $hideKeys );
139
		});
140
	}
141
142
	/**
143
	 * @param string $id
144
	 * @return string
145
	 */
146
	protected function normalizeId( $id )
147
	{
148
		return sanitize_title( $id );
149
	}
150
151
	/**
152
	 * @param string $labels
153
	 * @return array
154
	 */
155
	protected function normalizeLabels( $labels )
156
	{
157
		$defaults = [
158
			__( 'Excellent', 'site-reviews' ),
159
			__( 'Very good', 'site-reviews' ),
160
			__( 'Average', 'site-reviews' ),
161
			__( 'Poor', 'site-reviews' ),
162
			__( 'Terrible', 'site-reviews' ),
163
		];
164
		$defaults = array_pad( $defaults, Rating::MAX_RATING, '' );
165
		$labels = array_map( 'trim', explode( ',', $labels ));
166
		foreach( $defaults as $i => $label ) {
167
			if( empty( $labels[$i] ))continue;
168
			$defaults[$i] = $labels[$i];
169
		}
170
		return array_combine( range( Rating::MAX_RATING, 1 ), $defaults );
171
	}
172
173
	/**
174
	 * @param string $schema
175
	 * @return bool
176
	 */
177
	protected function normalizeSchema( $schema )
178
	{
179
		return wp_validate_boolean( $schema );
180
	}
181
182
	/**
183
	 * @param string $text
184
	 * @return string
185
	 */
186
	protected function normalizeText( $text )
187
	{
188
		return trim( $text );
189
	}
190
191
	/**
192
	 * @return array
193
	 */
194
	protected function sanitize( array $args )
195
	{
196
		return $args;
197
	}
198
}
199