1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Shortcode Base Implementation. |
4
|
|
|
* |
5
|
|
|
* @package BrightNucleus\Shortcode |
6
|
|
|
* @author Alain Schlesser <[email protected]> |
7
|
|
|
* @license GPL-2.0+ |
8
|
|
|
* @link http://www.brightnucleus.com/ |
9
|
|
|
* @copyright 2015-2016 Alain Schlesser, Bright Nucleus |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BrightNucleus\Shortcode; |
13
|
|
|
|
14
|
|
|
use Assert; |
15
|
|
|
use BrightNucleus\Config\ConfigInterface; |
16
|
|
|
use BrightNucleus\Config\ConfigTrait; |
17
|
|
|
use BrightNucleus\Dependency\DependencyManagerInterface as DependencyManager; |
18
|
|
|
use BrightNucleus\Exception\DomainException; |
19
|
|
|
use BrightNucleus\Exception\RuntimeException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Base Implementation of the Shortcode Interface. |
23
|
|
|
* |
24
|
|
|
* This is a basic implementation of the Shortcode Interface that registers one |
25
|
|
|
* view and passes all attributes unfiltered to that view. |
26
|
|
|
* |
27
|
|
|
* @since 0.1.0 |
28
|
|
|
* |
29
|
|
|
* @package BrightNucleus\Shortcode |
30
|
|
|
* @author Alain Schlesser <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class Shortcode implements ShortcodeInterface { |
33
|
|
|
|
34
|
|
|
use ConfigTrait; |
35
|
|
|
use CheckNeedTrait; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Name of the shortcode handler. |
39
|
|
|
* |
40
|
|
|
* @since 0.1.0 |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $shortcode_tag; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Parser to parse and validate the shortcode's attributes. |
48
|
|
|
* |
49
|
|
|
* @since 0.1.0 |
50
|
|
|
* |
51
|
|
|
* @var ShortcodeAttsParserInterface |
52
|
|
|
*/ |
53
|
|
|
protected $atts_parser; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Dependencies of the shortcode. |
57
|
|
|
* |
58
|
|
|
* @since 0.1.0 |
59
|
|
|
* |
60
|
|
|
* @var DependencyManager |
61
|
|
|
*/ |
62
|
|
|
protected $dependencies; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Cache context information so we can pass it on to the render() method. |
66
|
|
|
* |
67
|
|
|
* @var |
68
|
|
|
* |
69
|
|
|
* @since 0.2.3 |
70
|
|
|
*/ |
71
|
|
|
protected $context; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Instantiate Basic Shortcode. |
75
|
|
|
* |
76
|
|
|
* @since 0.1.0 |
77
|
|
|
* |
78
|
|
|
* @param string $shortcode_tag Tag that identifies the |
79
|
|
|
* shortcode. |
80
|
|
|
* @param ConfigInterface $config Configuration settings. |
81
|
|
|
* @param ShortcodeAttsParser $atts_parser Attributes parser and |
82
|
|
|
* validator. |
83
|
|
|
* @param DependencyManager|null $dependencies Optional. Dependencies of |
84
|
|
|
* the shortcode. |
85
|
|
|
* @throws RuntimeException If the config could not be processed. |
86
|
|
|
*/ |
87
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
88
|
|
|
$shortcode_tag, |
89
|
|
|
ConfigInterface $config, |
90
|
|
|
ShortcodeAttsParser $atts_parser, |
91
|
|
|
DependencyManager $dependencies = null |
92
|
|
|
) { |
93
|
|
|
|
94
|
|
|
Assert\that( $shortcode_tag )->string()->notEmpty(); |
95
|
|
|
|
96
|
|
|
$this->processConfig( $config ); |
97
|
|
|
|
98
|
|
|
$this->shortcode_tag = $shortcode_tag; |
99
|
|
|
$this->atts_parser = $atts_parser; |
100
|
|
|
$this->dependencies = $dependencies; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Register the shortcode handler function with WordPress. |
105
|
|
|
* |
106
|
|
|
* @since 0.1.0 |
107
|
|
|
* |
108
|
|
|
* @param mixed $context Optional. Arguments to pass on to the Registrable. |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
|
|
public function register( $context = null ) { |
112
|
|
|
if ( ! $this->is_needed( $context ) ) { |
113
|
|
|
return; |
114
|
|
|
} |
115
|
|
|
$this->context = $context; |
116
|
|
|
|
117
|
|
|
\add_shortcode( $this->get_tag(), [ $this, 'render' ] ); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get the shortcode tag. |
122
|
|
|
* |
123
|
|
|
* @since 0.1.0 |
124
|
|
|
* |
125
|
|
|
* @return string Shortcode tag. |
126
|
|
|
*/ |
127
|
|
|
public function get_tag() { |
128
|
|
|
return (string) $this->shortcode_tag; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Render the shortcode. |
133
|
|
|
* |
134
|
|
|
* @since 0.1.0 |
135
|
|
|
* |
136
|
|
|
* @throws DomainException |
137
|
|
|
* |
138
|
|
|
* @param array $atts Attributes to modify the standard behavior |
139
|
|
|
* of the shortcode. |
140
|
|
|
* @param string|null $content Optional. Content between enclosing |
141
|
|
|
* shortcodes. |
142
|
|
|
* @param string|null $tag Optional. The tag of the shortcode to |
143
|
|
|
* render. |
144
|
|
|
* @return string The shortcode's HTML output. |
145
|
|
|
*/ |
146
|
|
|
public function render( $atts, $content = null, $tag = null ) { |
147
|
|
|
$context = $this->context; |
148
|
|
|
$atts = $this->atts_parser->parse_atts( $atts, $this->get_tag() ); |
149
|
|
|
$this->enqueue_dependencies( $this->get_dependency_handles(), $atts ); |
150
|
|
|
|
151
|
|
|
return $this->render_view( $this->get_view(), $context ); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Enqueue the dependencies that the shortcode needs. |
156
|
|
|
* |
157
|
|
|
* @since 0.2.9 |
158
|
|
|
* |
159
|
|
|
* @param array $handles Array of dependency handles to enqueue. |
160
|
|
|
* @param mixed $context Optional. Context in which to enqueue. |
161
|
|
|
*/ |
162
|
|
|
protected function enqueue_dependencies( $handles, $context = null ) { |
163
|
|
|
if ( ! $this->dependencies || count( $handles ) < 1 ) { |
164
|
|
|
return; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
foreach ( $handles as $handle ) { |
168
|
|
|
$found = $this->dependencies->enqueue_handle( |
169
|
|
|
$handle, |
170
|
|
|
$context, |
171
|
|
|
true |
|
|
|
|
172
|
|
|
); |
173
|
|
|
if ( ! $found ) { |
174
|
|
|
$message = sprintf( |
175
|
|
|
__( 'Could not enqueue dependency "%1$s" for shortcode "%2$s".', |
176
|
|
|
'bn-shortcodes' ), |
|
|
|
|
177
|
|
|
$handle, |
178
|
|
|
$this->get_tag() |
179
|
|
|
); |
180
|
|
|
trigger_error( $message, E_USER_WARNING ); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get an array of dependency handles for the current shortcode. |
188
|
|
|
* |
189
|
|
|
* @since 0.2.7 |
190
|
|
|
* |
191
|
|
|
* @return array Array of strings that are registered dependency handles. |
192
|
|
|
*/ |
193
|
|
|
protected function get_dependency_handles() { |
194
|
|
|
if ( ! $this->hasConfigKey( 'dependencies' ) ) { |
195
|
|
|
return [ ]; |
196
|
|
|
} |
197
|
|
|
return (array) $this->getConfigKey( 'dependencies' ); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get the rendered HTML for a given view. |
202
|
|
|
* |
203
|
|
|
* @since 0.2.6 |
204
|
|
|
* |
205
|
|
|
* @param string $view The view to render. |
206
|
|
|
* @param mixed $context The context to pass through to the view. |
207
|
|
|
* @return string HTML rendering of the view. |
208
|
|
|
*/ |
209
|
|
|
protected function render_view( $view, $context ) { |
210
|
|
|
if ( empty( $view ) ) { |
211
|
|
|
return ''; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
ob_start(); |
215
|
|
|
include( $view ); |
216
|
|
|
return ob_get_clean(); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Get the name of the view to render. |
221
|
|
|
* |
222
|
|
|
* @since 0.2.6 |
223
|
|
|
* |
224
|
|
|
* @return string Name of the view to render. |
225
|
|
|
*/ |
226
|
|
|
protected function get_view() { |
227
|
|
|
if ( ! $this->hasConfigKey( 'view' ) ) { |
228
|
|
|
return ''; |
229
|
|
|
} |
230
|
|
|
$view = $this->getConfigKey( 'view' ); |
231
|
|
|
|
232
|
|
|
Assert\that( $view )->string()->notEmpty()->file(); |
233
|
|
|
|
234
|
|
|
return $view; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Execute this shortcode directly from code. |
239
|
|
|
* |
240
|
|
|
* @since 0.2.4 |
241
|
|
|
* |
242
|
|
|
* @param array $atts Array of attributes to pass to the shortcode. |
243
|
|
|
* @param string|null $content Inner content to pass to the shortcode. |
244
|
|
|
* @return string|false Rendered HTML. |
|
|
|
|
245
|
|
|
*/ |
246
|
|
|
public function do_this( array $atts = [ ], $content = null ) { |
247
|
|
|
\BrightNucleus\Shortcode\do_tag( $this->get_tag(), $atts, $content ); |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.