1 | <?php |
||
34 | class Shortcode implements ShortcodeInterface { |
||
35 | |||
36 | use ConfigTrait; |
||
37 | use CheckNeedTrait; |
||
38 | |||
39 | /** |
||
40 | * Name of the shortcode handler. |
||
41 | * |
||
42 | * @since 0.1.0 |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $shortcode_tag; |
||
47 | |||
48 | /** |
||
49 | * Parser to parse and validate the shortcode's attributes. |
||
50 | * |
||
51 | * @since 0.1.0 |
||
52 | * |
||
53 | * @var ShortcodeAttsParserInterface |
||
54 | */ |
||
55 | protected $atts_parser; |
||
56 | |||
57 | /** |
||
58 | * Dependencies of the shortcode. |
||
59 | * |
||
60 | * @since 0.1.0 |
||
61 | * |
||
62 | * @var DependencyManager |
||
63 | */ |
||
64 | protected $dependencies; |
||
65 | |||
66 | /** |
||
67 | * View builder instance to use for creating views to render. |
||
68 | * |
||
69 | * @since 0.4.0 |
||
70 | * |
||
71 | * @var ViewBuilder |
||
72 | */ |
||
73 | protected $view_builder; |
||
74 | |||
75 | /** |
||
76 | * Cache context information so we can pass it on to the render() method. |
||
77 | * |
||
78 | * @var |
||
79 | * |
||
80 | * @since 0.2.3 |
||
81 | */ |
||
82 | protected $context = []; |
||
83 | |||
84 | /** |
||
85 | * Instantiate Basic Shortcode. |
||
86 | * |
||
87 | * @since 0.1.0 |
||
88 | * @since 0.4.0 Added optional $view_builder argument. |
||
89 | * |
||
90 | * @param string $shortcode_tag Tag that identifies the |
||
91 | * shortcode. |
||
92 | * @param Config $config Configuration settings. |
||
93 | * @param ShortcodeAttsParser $atts_parser Attributes parser and |
||
94 | * validator. |
||
95 | * @param DependencyManager|null $dependencies Optional. Dependencies of |
||
96 | * the shortcode. |
||
97 | * @param ViewBuilder|null $view_builder Optional. View builder |
||
98 | * instance to use. |
||
99 | * @throws RuntimeException If the config could not be processed. |
||
100 | */ |
||
101 | public function __construct( |
||
116 | |||
117 | /** |
||
118 | * Register the shortcode handler function with WordPress. |
||
119 | * |
||
120 | * @since 0.1.0 |
||
121 | * |
||
122 | * @param mixed $context Optional. Arguments to pass on to the Registrable. |
||
123 | * @return void |
||
124 | */ |
||
125 | public function register( $context = null ) { |
||
136 | |||
137 | /** |
||
138 | * Add additional context to the shortcode. |
||
139 | * |
||
140 | * This can be used to pass more data to the view to be rendered. |
||
141 | * |
||
142 | * This is especially useful for additional preparation through a DI. |
||
143 | * |
||
144 | * @param array $context Associative array of context information to add. |
||
145 | */ |
||
146 | public function add_context( $context ) { |
||
151 | |||
152 | /** |
||
153 | * Get the shortcode tag. |
||
154 | * |
||
155 | * @since 0.1.0 |
||
156 | * |
||
157 | * @return string Shortcode tag. |
||
158 | */ |
||
159 | public function get_tag() { |
||
162 | |||
163 | /** |
||
164 | * Render the shortcode. |
||
165 | * |
||
166 | * @since 0.1.0 |
||
167 | * |
||
168 | * @throws DomainException |
||
169 | * |
||
170 | * @param array $atts Attributes to modify the standard behavior |
||
171 | * of the shortcode. |
||
172 | * @param string|null $content Optional. Content between enclosing |
||
173 | * shortcodes. |
||
174 | * @param string|null $tag Optional. The tag of the shortcode to |
||
175 | * render. Ignored by current code. |
||
176 | * @return string The shortcode's HTML output. |
||
177 | */ |
||
178 | public function render( $atts, $content = null, $tag = null ) { |
||
189 | |||
190 | /** |
||
191 | * Enqueue the dependencies that the shortcode needs. |
||
192 | * |
||
193 | * @since 0.2.9 |
||
194 | * |
||
195 | * @param array $handles Array of dependency handles to enqueue. |
||
196 | * @param mixed $context Optional. Context in which to enqueue. |
||
197 | */ |
||
198 | protected function enqueue_dependencies( $handles, $context = null ) { |
||
225 | |||
226 | /** |
||
227 | * Get an array of dependency handles for the current shortcode. |
||
228 | * |
||
229 | * @since 0.2.7 |
||
230 | * |
||
231 | * @return array Array of strings that are registered dependency handles. |
||
232 | */ |
||
233 | protected function get_dependency_handles() { |
||
239 | |||
240 | /** |
||
241 | * Get the rendered HTML for a given view. |
||
242 | * |
||
243 | * @since 0.2.6 |
||
244 | * |
||
245 | * @param string $uri URI of the view to render. |
||
246 | * @param mixed $context The context to pass through to the view. |
||
247 | * @param array $atts The shortcode attribute values to pass |
||
248 | * through to the view. |
||
249 | * @param string|null $content Optional. The inner content of the shortcode. |
||
250 | * @return string HTML rendering of the view. |
||
251 | */ |
||
252 | protected function render_view( $uri, $context, $atts, $content = null ) { |
||
265 | |||
266 | /** |
||
267 | * Do additional preparations on the context before rendering. |
||
268 | * |
||
269 | * @param array $context Context to prepare. |
||
270 | * @return array Prepared context. |
||
271 | */ |
||
272 | protected function prepare_context( array $context ): array { |
||
275 | |||
276 | /** |
||
277 | * Get the name of the view to render. |
||
278 | * |
||
279 | * @since 0.2.6 |
||
280 | * |
||
281 | * @return string Name of the view to render. |
||
282 | */ |
||
283 | protected function get_view() { |
||
291 | |||
292 | /** |
||
293 | * Execute this shortcode directly from code. |
||
294 | * |
||
295 | * @since 0.2.4 |
||
296 | * |
||
297 | * @param array $atts Array of attributes to pass to the shortcode. |
||
298 | * @param string|null $content Inner content to pass to the shortcode. |
||
299 | * @return string|false Rendered HTML. |
||
300 | */ |
||
301 | public function do_this( array $atts = [ ], $content = null ) { |
||
304 | } |
||
305 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: