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 ) { |
||
133 | |||
134 | /** |
||
135 | * Add additional context to the shortcode. |
||
136 | * |
||
137 | * This can be used to pass more data to the view to be rendered. |
||
138 | * |
||
139 | * This is especially useful for additional preparation through a DI. |
||
140 | * |
||
141 | * @param array $context Associative array of context information to add. |
||
142 | */ |
||
143 | public function add_context( array $context ) { |
||
146 | |||
147 | /** |
||
148 | * Get the shortcode tag. |
||
149 | * |
||
150 | * @since 0.1.0 |
||
151 | * |
||
152 | * @return string Shortcode tag. |
||
153 | */ |
||
154 | public function get_tag() { |
||
157 | |||
158 | /** |
||
159 | * Render the shortcode. |
||
160 | * |
||
161 | * @since 0.1.0 |
||
162 | * |
||
163 | * @throws DomainException |
||
164 | * |
||
165 | * @param array $atts Attributes to modify the standard behavior |
||
166 | * of the shortcode. |
||
167 | * @param string|null $content Optional. Content between enclosing |
||
168 | * shortcodes. |
||
169 | * @param string|null $tag Optional. The tag of the shortcode to |
||
170 | * render. Ignored by current code. |
||
171 | * @return string The shortcode's HTML output. |
||
172 | */ |
||
173 | public function render( $atts, $content = null, $tag = null ) { |
||
185 | |||
186 | /** |
||
187 | * Enqueue the dependencies that the shortcode needs. |
||
188 | * |
||
189 | * @since 0.2.9 |
||
190 | * |
||
191 | * @param array $handles Array of dependency handles to enqueue. |
||
192 | * @param mixed $context Optional. Context in which to enqueue. |
||
193 | */ |
||
194 | protected function enqueue_dependencies( $handles, $context = null ) { |
||
217 | |||
218 | /** |
||
219 | * Get an array of dependency handles for the current shortcode. |
||
220 | * |
||
221 | * @since 0.2.7 |
||
222 | * |
||
223 | * @return array Array of strings that are registered dependency handles. |
||
224 | */ |
||
225 | protected function get_dependency_handles() { |
||
231 | |||
232 | /** |
||
233 | * Get the rendered HTML for a given view. |
||
234 | * |
||
235 | * @since 0.2.6 |
||
236 | * |
||
237 | * @param string $uri URI of the view to render. |
||
238 | * @param mixed $context The context to pass through to the view. |
||
239 | * @param array $atts The shortcode attribute values to pass |
||
240 | * through to the view. |
||
241 | * @param string|null $content Optional. The inner content of the shortcode. |
||
242 | * @return string HTML rendering of the view. |
||
243 | */ |
||
244 | protected function render_view( $uri, $context, $atts, $content = null ) { |
||
254 | |||
255 | /** |
||
256 | * Get the name of the view to render. |
||
257 | * |
||
258 | * @since 0.2.6 |
||
259 | * |
||
260 | * @return string Name of the view to render. |
||
261 | */ |
||
262 | protected function get_view() { |
||
270 | |||
271 | /** |
||
272 | * Execute this shortcode directly from code. |
||
273 | * |
||
274 | * @since 0.2.4 |
||
275 | * |
||
276 | * @param array $atts Array of attributes to pass to the shortcode. |
||
277 | * @param string|null $content Inner content to pass to the shortcode. |
||
278 | * @return string|false Rendered HTML. |
||
279 | */ |
||
280 | public function do_this( array $atts = [ ], $content = null ) { |
||
283 | } |
||
284 |
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: