1 | <?php |
||
32 | class ShortcodeManager implements ShortcodeManagerInterface { |
||
33 | |||
34 | use ConfigTrait; |
||
35 | |||
36 | /* |
||
37 | * The delimiter that is used to express key-subkey relations in the config. |
||
38 | */ |
||
39 | const CONFIG_SEPARATOR = '/'; |
||
40 | |||
41 | /* |
||
42 | * Default classes that are used when omitted from the config. |
||
43 | */ |
||
44 | const DEFAULT_SHORTCODE = 'BrightNucleus\Shortcode\Shortcode'; |
||
45 | const DEFAULT_SHORTCODE_ATTS_PARSER = 'BrightNucleus\Shortcode\ShortcodeAttsParser'; |
||
46 | const DEFAULT_SHORTCODE_UI = 'BrightNucleus\Shortcode\ShortcodeUI'; |
||
47 | |||
48 | /* |
||
49 | * The names of the configuration keys. |
||
50 | */ |
||
51 | const KEY_CUSTOM_ATTS_PARSER = 'custom_atts_parser'; |
||
52 | const KEY_CUSTOM_CLASS = 'custom_class'; |
||
53 | const KEY_CUSTOM_UI = 'custom_ui'; |
||
54 | const KEY_UI = 'ui'; |
||
55 | /** |
||
56 | * Collection of ShortcodeInterface objects. |
||
57 | * |
||
58 | * @since 0.1.0 |
||
59 | * |
||
60 | * @var ShortcodeInterface[] |
||
61 | */ |
||
62 | protected $shortcodes = []; |
||
63 | |||
64 | /** |
||
65 | * DependencyManagerInterface implementation. |
||
66 | * |
||
67 | * @since 0.1.0 |
||
68 | * |
||
69 | * @var DependencyManager |
||
70 | */ |
||
71 | protected $dependencies; |
||
72 | |||
73 | /** |
||
74 | * Collection of ShortcodeUIInterface objects. |
||
75 | * |
||
76 | * @since 0.1.0 |
||
77 | * |
||
78 | * @var ShortcodeUIInterface[] |
||
79 | */ |
||
80 | protected $shortcode_uis = []; |
||
81 | |||
82 | /** |
||
83 | * Instantiate a ShortcodeManager object. |
||
84 | * |
||
85 | * @since 0.1.0 |
||
86 | * |
||
87 | * @param ConfigInterface $config Configuration to set up the |
||
88 | * shortcodes. |
||
89 | * @param DependencyManager|null $dependencies Optional. Dependencies that |
||
90 | * are needed by the shortcodes. |
||
91 | * @throws RuntimeException If the config could not be processed. |
||
92 | */ |
||
93 | public function __construct( |
||
102 | |||
103 | /** |
||
104 | * Initialize the Shortcode Manager. |
||
105 | * |
||
106 | * @since 0.1.0 |
||
107 | */ |
||
108 | protected function init_shortcodes() { |
||
113 | |||
114 | /** |
||
115 | * Initialize a single shortcode. |
||
116 | * |
||
117 | * @since 0.1.0 |
||
118 | * |
||
119 | * @param string $tag The tag of the shortcode to register. |
||
120 | * @throws FailedToInstantiateObject If the Shortcode Atts Parser object |
||
121 | * could not be instantiated. |
||
122 | * @throws FailedToInstantiateObject If the Shortcode object could not be |
||
123 | * instantiated. |
||
124 | */ |
||
125 | protected function init_shortcode( $tag ) { |
||
150 | |||
151 | /** |
||
152 | * Get the class name of an implementation of the ShortcodeInterface. |
||
153 | * |
||
154 | * @since 0.1.0 |
||
155 | * |
||
156 | * @param string $tag Shortcode tag to get the class for. |
||
157 | * @return string Class name of the Shortcode. |
||
158 | */ |
||
159 | protected function get_shortcode_class( $tag ) { |
||
165 | |||
166 | /** |
||
167 | * Get the class name of an implementation of the |
||
168 | * ShortcodeAttsParsersInterface. |
||
169 | * |
||
170 | * @since 0.1.0 |
||
171 | * |
||
172 | * @param string $tag Shortcode tag to get the class for. |
||
173 | * @return string Class name of the ShortcodeAttsParser. |
||
174 | */ |
||
175 | protected function get_shortcode_atts_parser_class( $tag ) { |
||
181 | |||
182 | /** |
||
183 | * Initialize the Shortcode UI for a single shortcode. |
||
184 | * |
||
185 | * @since 0.1.0 |
||
186 | * |
||
187 | * @param string $tag The tag of the shortcode to register |
||
188 | * the UI for. |
||
189 | * @throws FailedToInstantiateObject If the Shortcode UI object could not |
||
190 | * be instantiated. |
||
191 | */ |
||
192 | protected function init_shortcode_ui( $tag ) { |
||
205 | |||
206 | /** |
||
207 | * Get the class name of an implementation of the ShortcodeUIInterface. |
||
208 | * |
||
209 | * @since 0.1.0 |
||
210 | * |
||
211 | * @param string $tag Configuration settings. |
||
212 | * @return string Class name of the ShortcodeUI. |
||
213 | */ |
||
214 | protected function get_shortcode_ui_class( $tag ) { |
||
220 | |||
221 | /** |
||
222 | * Register all of the shortcode handlers. |
||
223 | * |
||
224 | * @since 0.1.0 |
||
225 | * |
||
226 | * @param mixed $context Optional. Context information to pass to shortcode. |
||
227 | * @return void |
||
228 | */ |
||
229 | public function register( $context = null ) { |
||
244 | |||
245 | /** |
||
246 | * Validate the context to make sure it is an array. |
||
247 | * |
||
248 | * @since 0.2.3 |
||
249 | * |
||
250 | * @param mixed $context The context as passed in by WordPress. |
||
251 | * @return array Validated context. |
||
252 | */ |
||
253 | protected function validate_context( $context ) { |
||
259 | |||
260 | /** |
||
261 | * Get the name of the page template. |
||
262 | * |
||
263 | * @since 0.1.0 |
||
264 | * |
||
265 | * @return string Name of the page template. |
||
266 | */ |
||
267 | protected function get_page_template() { |
||
275 | |||
276 | /** |
||
277 | * Register the shortcode UI handlers. |
||
278 | * |
||
279 | * @since 0.1.0 |
||
280 | */ |
||
281 | public function register_shortcode_ui() { |
||
291 | |||
292 | /** |
||
293 | * Execute a specific shortcode directly from code. |
||
294 | * |
||
295 | * @since 0.2.4 |
||
296 | * |
||
297 | * @param string $tag Tag of the shortcode to execute. |
||
298 | * @param array $atts Array of attributes to pass to the shortcode. |
||
299 | * @param null $content Inner content to pass to the shortcode. |
||
300 | * @return string|false Rendered HTML. |
||
301 | */ |
||
302 | public function do_tag( $tag, array $atts = [], $content = null ) { |
||
305 | |||
306 | /** |
||
307 | * Instantiate a new object through either a class name or a factory method. |
||
308 | * |
||
309 | * @since 0.3.0 |
||
310 | * |
||
311 | * @param string $interface Interface the object needs to |
||
312 | * implement. |
||
313 | * @param callable|string $class Fully qualified class name or factory |
||
314 | * method. |
||
315 | * @param array $args Arguments passed to the constructor or |
||
316 | * factory method. |
||
317 | * @return object Object that implements the interface. |
||
318 | * @throws FailedToInstantiateObject If no valid object could be |
||
319 | * instantiated. |
||
320 | */ |
||
321 | protected function instantiate( $interface, $class, array $args ) { |
||
341 | } |
||
342 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.