-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractShortcode.php
More file actions
76 lines (66 loc) · 1.82 KB
/
Copy pathAbstractShortcode.php
File metadata and controls
76 lines (66 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/**
* Abstract Shortcode class.
*
* Class to be extended by all shortcodes. Handles registration and provides a clean render interface.
*
* @package rtCamp\WPFramework\Contracts\Abstracts
* @since 1.0.0
*/
declare( strict_types = 1 );
namespace rtCamp\WPFramework\Contracts\Abstracts;
use rtCamp\WPFramework\Contracts\Interfaces\Registrable;
/**
* Class - AbstractShortcode
*/
abstract class AbstractShortcode implements Registrable {
/**
* Get the shortcode tag name.
*
* @return non-empty-string
*/
abstract public static function get_tag(): string;
/**
* {@inheritDoc}
*/
public function register_hooks(): void {
add_action( 'init', [ $this, 'register_shortcode' ] );
}
/**
* Register the shortcode with WordPress.
*/
public function register_shortcode(): void {
add_shortcode( static::get_tag(), [ $this, 'shortcode_callback' ] );
}
/**
* Shortcode callback wrapper. Parses attributes and delegates to render().
*
* @param array|string $atts Shortcode attributes.
* @param string|null $content Enclosed content (if any).
*
* @return string Rendered shortcode output.
*/
public function shortcode_callback( $atts, ?string $content = null ): string {
$atts = shortcode_atts( $this->default_atts(), (array) $atts, static::get_tag() );
return $this->render( $atts, $content );
}
/**
* Render the shortcode output.
*
* @param array<string, mixed> $atts Parsed shortcode attributes.
* @param string|null $content Enclosed content (if any).
*
* @return string The shortcode HTML output.
*/
abstract protected function render( array $atts, ?string $content ): string;
/**
* Default shortcode attributes.
*
* Override in child class to define defaults.
*
* @return array<string, mixed>
*/
protected function default_atts(): array {
return [];
}
}