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
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $partialName; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $shortcodeName; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string|array $atts |
25
|
|
|
* @param string $type |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
public function build( $atts, array $args = [], $type = 'shortcode' ) |
29
|
|
|
{ |
30
|
|
|
$this->partialName = $this->getShortcodePartialName(); |
31
|
|
|
$this->shortcodeName = $this->getShortcodeName(); |
32
|
|
|
$args = $this->normalizeArgs( $args, $type ); |
33
|
|
|
$atts = $this->normalizeAtts( $atts, $type ); |
34
|
|
|
$partial = glsr( Partial::class )->build( $this->partialName, $atts ); |
35
|
|
|
$title = !empty( $atts['title'] ) |
36
|
|
|
? $args['before_title'].$atts['title'].$args['after_title'] |
37
|
|
|
: ''; |
38
|
|
|
$debug = sprintf( '<glsr-%1$s hidden data-atts=\'%2$s\'></glsr-%1$s>', $type, $atts['json'] ); |
39
|
|
|
return $args['before_widget'].$title.$partial.$debug.$args['after_widget']; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string|array $atts |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public function buildShortcode( $atts = [] ) |
47
|
|
|
{ |
48
|
|
|
return $this->build( $atts ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public function getDefaults( $atts ) |
55
|
|
|
{ |
56
|
|
|
return glsr( $this->getShortcodeDefaultsClassName() )->restrict( wp_parse_args( $atts )); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
1 |
|
public function getHideOptions() |
63
|
|
|
{ |
64
|
1 |
|
$options = $this->hideOptions(); |
65
|
1 |
|
return apply_filters( 'site-reviews/shortcode/hide-options', $options, $this->shortcodeName ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function getShortcodeClassName( $replace = '', $search = 'Shortcode' ) |
72
|
|
|
{ |
73
|
|
|
return str_replace( $search, $replace, (new ReflectionClass( $this ))->getShortName() ); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function getShortcodeDefaultsClassName() |
80
|
|
|
{ |
81
|
|
|
return glsr( Helper::class )->buildClassName( |
82
|
|
|
$this->getShortcodeClassName( 'Defaults' ), |
83
|
|
|
'Defaults' |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getShortcodeName() |
91
|
|
|
{ |
92
|
|
|
return glsr( Helper::class )->snakeCase( $this->getShortcodeClassName() ); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function getShortcodePartialName() |
99
|
|
|
{ |
100
|
|
|
return glsr( Helper::class )->dashCase( $this->getShortcodeClassName() ); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param array|string $args |
105
|
|
|
* @param string $type |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
public function normalizeArgs( $args, $type = 'shortcode' ) |
109
|
|
|
{ |
110
|
|
|
$args = wp_parse_args( $args, [ |
111
|
|
|
'before_widget' => '<div class="glsr-'.$type.' '.$type.'-'.$this->partialName.'">', |
112
|
|
|
'after_widget' => '</div>', |
113
|
|
|
'before_title' => '<h3 class="glsr-'.$type.'-title">', |
114
|
|
|
'after_title' => '</h3>', |
115
|
|
|
]); |
116
|
|
|
return apply_filters( 'site-reviews/shortcode/args', $args, $type, $this->partialName ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param array|string $atts |
121
|
|
|
* @param string $type |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
public function normalizeAtts( $atts, $type = 'shortcode' ) |
125
|
|
|
{ |
126
|
|
|
$atts = apply_filters( 'site-reviews/shortcode/atts', $atts, $type, $this->partialName ); |
127
|
|
|
$atts = $this->getDefaults( $atts ); |
128
|
|
|
array_walk( $atts, function( &$value, $key ) { |
129
|
|
|
$methodName = glsr( Helper::class )->buildMethodName( $key, 'normalize' ); |
130
|
|
|
if( !method_exists( $this, $methodName ))return; |
131
|
|
|
$value = $this->$methodName( $value ); |
132
|
|
|
}); |
133
|
|
|
return $atts; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
|
|
abstract protected function hideOptions(); |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $postId |
143
|
|
|
* @return int|string |
144
|
|
|
*/ |
145
|
|
|
protected function normalizeAssignedTo( $postId ) |
146
|
|
|
{ |
147
|
|
|
if( $postId == 'parent_id' ) { |
148
|
|
|
$postId = intval( wp_get_post_parent_id( intval( get_the_ID() ))); |
149
|
|
|
} |
150
|
|
|
else if( $postId == 'post_id' ) { |
151
|
|
|
$postId = intval( get_the_ID() ); |
152
|
|
|
} |
153
|
|
|
return $postId; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $postId |
158
|
|
|
* @return int|string |
159
|
|
|
*/ |
160
|
|
|
protected function normalizeAssignTo( $postId ) |
161
|
|
|
{ |
162
|
|
|
return $this->normalizeAssignedTo( $postId ); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string|array $hide |
167
|
|
|
* @return array |
168
|
|
|
*/ |
169
|
|
|
protected function normalizeHide( $hide ) |
170
|
|
|
{ |
171
|
|
|
if( is_string( $hide )) { |
172
|
|
|
$hide = explode( ',', $hide ); |
173
|
|
|
} |
174
|
|
|
$hideKeys = array_keys( $this->getHideOptions() ); |
175
|
|
|
return array_filter( array_map( 'trim', $hide ), function( $value ) use( $hideKeys ) { |
176
|
|
|
return in_array( $value, $hideKeys ); |
177
|
|
|
}); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param string $id |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
protected function normalizeId( $id ) |
185
|
|
|
{ |
186
|
|
|
return sanitize_title( $id ); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param string $labels |
191
|
|
|
* @return array |
192
|
|
|
*/ |
193
|
|
|
protected function normalizeLabels( $labels ) |
194
|
|
|
{ |
195
|
|
|
$defaults = [ |
196
|
|
|
__( 'Excellent', 'site-reviews' ), |
197
|
|
|
__( 'Very good', 'site-reviews' ), |
198
|
|
|
__( 'Average', 'site-reviews' ), |
199
|
|
|
__( 'Poor', 'site-reviews' ), |
200
|
|
|
__( 'Terrible', 'site-reviews' ), |
201
|
|
|
]; |
202
|
|
|
$defaults = array_pad( $defaults, glsr()->constant( 'MAX_RATING', Rating::class ), '' ); |
203
|
|
|
$labels = array_map( 'trim', explode( ',', $labels )); |
204
|
|
|
foreach( $defaults as $i => $label ) { |
205
|
|
|
if( empty( $labels[$i] ))continue; |
206
|
|
|
$defaults[$i] = $labels[$i]; |
207
|
|
|
} |
208
|
|
|
return array_combine( range( glsr()->constant( 'MAX_RATING', Rating::class ), 1 ), $defaults ); |
|
|
|
|
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param string $schema |
213
|
|
|
* @return bool |
214
|
|
|
*/ |
215
|
|
|
protected function normalizeSchema( $schema ) |
216
|
|
|
{ |
217
|
|
|
return wp_validate_boolean( $schema ); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param string $text |
222
|
|
|
* @return string |
223
|
|
|
*/ |
224
|
|
|
protected function normalizeText( $text ) |
225
|
|
|
{ |
226
|
|
|
return trim( $text ); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.