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