1 | <?php |
||
35 | class ShortcodeManager implements ShortcodeManagerInterface { |
||
36 | |||
37 | use ConfigTrait; |
||
38 | use InstantiatorTrait; |
||
39 | |||
40 | /* |
||
41 | * The delimiter that is used to express key-subkey relations in the config. |
||
42 | */ |
||
43 | const CONFIG_SEPARATOR = '/'; |
||
44 | |||
45 | /* |
||
46 | * Default classes that are used when omitted from the config. |
||
47 | */ |
||
48 | const DEFAULT_SHORTCODE = 'BrightNucleus\Shortcode\Shortcode'; |
||
49 | const DEFAULT_SHORTCODE_ATTS_PARSER = 'BrightNucleus\Shortcode\ShortcodeAttsParser'; |
||
50 | const DEFAULT_SHORTCODE_UI = 'BrightNucleus\Shortcode\ShortcodeUI'; |
||
51 | |||
52 | /* |
||
53 | * The names of the configuration keys. |
||
54 | */ |
||
55 | const KEY_CUSTOM_ATTS_PARSER = 'custom_atts_parser'; |
||
56 | const KEY_CUSTOM_CLASS = 'custom_class'; |
||
57 | const KEY_CUSTOM_UI = 'custom_ui'; |
||
58 | const KEY_UI = 'ui'; |
||
59 | /** |
||
60 | * Collection of ShortcodeInterface objects. |
||
61 | * |
||
62 | * @since 0.1.0 |
||
63 | * |
||
64 | * @var ShortcodeInterface[] |
||
65 | */ |
||
66 | protected $shortcodes = []; |
||
67 | |||
68 | /** |
||
69 | * DependencyManagerInterface implementation. |
||
70 | * |
||
71 | * @since 0.1.0 |
||
72 | * |
||
73 | * @var DependencyManager |
||
74 | */ |
||
75 | protected $dependencies; |
||
76 | |||
77 | /** |
||
78 | * View builder instance to use for rendering views. |
||
79 | * |
||
80 | * @since 0.4.0 |
||
81 | * |
||
82 | * @var ViewBuilder |
||
83 | */ |
||
84 | protected $view_builder; |
||
85 | |||
86 | /** |
||
87 | * Collection of ShortcodeUIInterface objects. |
||
88 | * |
||
89 | * @since 0.1.0 |
||
90 | * |
||
91 | * @var ShortcodeUIInterface[] |
||
92 | */ |
||
93 | protected $shortcode_uis = []; |
||
94 | |||
95 | /** |
||
96 | * External injector to use. |
||
97 | * |
||
98 | * @var object |
||
99 | */ |
||
100 | protected $injector; |
||
101 | |||
102 | /** |
||
103 | * Instantiate a ShortcodeManager object. |
||
104 | * |
||
105 | * @since 0.1.0 |
||
106 | * @since 0.4.0 Add optional $viewBuilder argument. |
||
107 | * |
||
108 | * @param ConfigInterface $config Configuration to set up the |
||
109 | * shortcodes. |
||
110 | * @param DependencyManager|null $dependencies Optional. Dependencies that |
||
111 | * are needed by the |
||
112 | * shortcodes. |
||
113 | * @param ViewBuilder|null $view_builder Optional. View builder |
||
114 | * instance to use for |
||
115 | * rendering views. |
||
116 | * |
||
117 | * @throws RuntimeException If the config could not be processed. |
||
118 | */ |
||
119 | public function __construct( |
||
128 | |||
129 | /** |
||
130 | * Use an external injector to instantiate the different classes. |
||
131 | * |
||
132 | * The injector will |
||
133 | * @param object $injector Injector to use. |
||
134 | */ |
||
135 | public function with_injector( $injector ) { |
||
144 | |||
145 | /** |
||
146 | * Initialize the Shortcode Manager. |
||
147 | * |
||
148 | * @since 0.1.0 |
||
149 | */ |
||
150 | protected function init_shortcodes() { |
||
155 | |||
156 | /** |
||
157 | * Initialize a single shortcode. |
||
158 | * |
||
159 | * @since 0.1.0 |
||
160 | * |
||
161 | * @param string $tag The tag of the shortcode to register. |
||
162 | * |
||
163 | * @throws FailedToInstantiateObject If the Shortcode Atts Parser object |
||
164 | * could not be instantiated. |
||
165 | * @throws FailedToInstantiateObject If the Shortcode object could not be |
||
166 | * instantiated. |
||
167 | */ |
||
168 | protected function init_shortcode( $tag ) { |
||
194 | |||
195 | /** |
||
196 | * Get the class name of an implementation of the ShortcodeInterface. |
||
197 | * |
||
198 | * @since 0.1.0 |
||
199 | * |
||
200 | * @param string $tag Shortcode tag to get the class for. |
||
201 | * |
||
202 | * @return string Class name of the Shortcode. |
||
203 | */ |
||
204 | protected function get_shortcode_class( $tag ) { |
||
211 | |||
212 | /** |
||
213 | * Get the class name of an implementation of the |
||
214 | * ShortcodeAttsParsersInterface. |
||
215 | * |
||
216 | * @since 0.1.0 |
||
217 | * |
||
218 | * @param string $tag Shortcode tag to get the class for. |
||
219 | * |
||
220 | * @return string Class name of the ShortcodeAttsParser. |
||
221 | */ |
||
222 | protected function get_shortcode_atts_parser_class( $tag ) { |
||
229 | |||
230 | /** |
||
231 | * Initialize the Shortcode UI for a single shortcode. |
||
232 | * |
||
233 | * @since 0.1.0 |
||
234 | * |
||
235 | * @param string $tag The tag of the shortcode to register |
||
236 | * the UI for. |
||
237 | * |
||
238 | * @throws FailedToInstantiateObject If the Shortcode UI object could not |
||
239 | * be instantiated. |
||
240 | */ |
||
241 | protected function init_shortcode_ui( $tag ) { |
||
254 | |||
255 | /** |
||
256 | * Get the class name of an implementation of the ShortcodeUIInterface. |
||
257 | * |
||
258 | * @since 0.1.0 |
||
259 | * |
||
260 | * @param string $tag Configuration settings. |
||
261 | * |
||
262 | * @return string Class name of the ShortcodeUI. |
||
263 | */ |
||
264 | protected function get_shortcode_ui_class( $tag ) { |
||
271 | |||
272 | /** |
||
273 | * Register all of the shortcode handlers. |
||
274 | * |
||
275 | * @since 0.1.0 |
||
276 | * |
||
277 | * @param mixed $context Optional. Context information to pass to shortcode. |
||
278 | * |
||
279 | * @return void |
||
280 | */ |
||
281 | public function register( $context = null ) { |
||
298 | |||
299 | /** |
||
300 | * Validate the context to make sure it is an array. |
||
301 | * |
||
302 | * @since 0.2.3 |
||
303 | * |
||
304 | * @param mixed $context The context as passed in by WordPress. |
||
305 | * |
||
306 | * @return array Validated context. |
||
307 | */ |
||
308 | protected function validate_context( $context ) { |
||
315 | |||
316 | /** |
||
317 | * Get the name of the page template. |
||
318 | * |
||
319 | * @since 0.1.0 |
||
320 | * |
||
321 | * @return string Name of the page template. |
||
322 | */ |
||
323 | protected function get_page_template() { |
||
332 | |||
333 | /** |
||
334 | * Register the shortcode UI handlers. |
||
335 | * |
||
336 | * @since 0.1.0 |
||
337 | */ |
||
338 | public function register_shortcode_ui() { |
||
348 | |||
349 | /** |
||
350 | * Execute a specific shortcode directly from code. |
||
351 | * |
||
352 | * @since 0.2.4 |
||
353 | * |
||
354 | * @param string $tag Tag of the shortcode to execute. |
||
355 | * @param array $atts Array of attributes to pass to the shortcode. |
||
356 | * @param null $content Inner content to pass to the shortcode. |
||
357 | * |
||
358 | * @return string|false Rendered HTML. |
||
359 | */ |
||
360 | public function do_tag( $tag, array $atts = [], $content = null ) { |
||
363 | |||
364 | /** |
||
365 | * Instantiate a new object through either a class name or a factory method. |
||
366 | * |
||
367 | * @since 0.3.0 |
||
368 | * |
||
369 | * @param string $interface Interface the object needs to |
||
370 | * implement. |
||
371 | * @param callable|string $class Fully qualified class name or factory |
||
372 | * method. |
||
373 | * @param array $args Arguments passed to the constructor or |
||
374 | * factory method. |
||
375 | * |
||
376 | * @return object Object that implements the interface. |
||
377 | * @throws FailedToInstantiateObject If no valid object could be |
||
378 | * instantiated. |
||
379 | */ |
||
380 | protected function instantiate( $interface, $class, array $args ) { |
||
410 | } |
||
411 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.