1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* Shortcode Base Implementation.
|
4
|
|
|
*
|
5
|
|
|
* @package BrightNucleus\Shortcode
|
6
|
|
|
* @author Alain Schlesser <[email protected]>
|
7
|
|
|
* @license GPL-2.0+
|
8
|
|
|
* @link http://www.brightnucleus.com/
|
9
|
|
|
* @copyright 2015-2016 Alain Schlesser, Bright Nucleus
|
10
|
|
|
*/
|
11
|
|
|
|
12
|
|
|
namespace BrightNucleus\Shortcode;
|
13
|
|
|
|
14
|
|
|
use Assert;
|
15
|
|
|
use BrightNucleus\Config\ConfigInterface;
|
16
|
|
|
use BrightNucleus\Config\ConfigTrait;
|
17
|
|
|
use BrightNucleus\Dependency\DependencyManagerInterface;
|
18
|
|
|
use BrightNucleus\Exception\DomainException;
|
19
|
|
|
use BrightNucleus\Exception\RuntimeException;
|
20
|
|
|
|
21
|
|
|
/**
|
22
|
|
|
* Base Implementation of the Shortcode Interface.
|
23
|
|
|
*
|
24
|
|
|
* This is a basic implementation of the Shortcode Interface that registers one
|
25
|
|
|
* view and passes all attributes unfiltered to that view.
|
26
|
|
|
*
|
27
|
|
|
* @since 0.1.0
|
28
|
|
|
*
|
29
|
|
|
* @package BrightNucleus\Shortcode
|
30
|
|
|
* @author Alain Schlesser <[email protected]>
|
31
|
|
|
*/
|
32
|
|
|
class Shortcode implements ShortcodeInterface {
|
33
|
|
|
|
34
|
|
|
use ConfigTrait;
|
35
|
|
|
|
36
|
|
|
/**
|
37
|
|
|
* Name of the shortcode handler.
|
38
|
|
|
*
|
39
|
|
|
* @since 0.1.0
|
40
|
|
|
*
|
41
|
|
|
* @var string
|
42
|
|
|
*/
|
43
|
|
|
protected $shortcode_tag;
|
44
|
|
|
|
45
|
|
|
/**
|
46
|
|
|
* Parser to parse and validate the shortcode's attributes.
|
47
|
|
|
*
|
48
|
|
|
* @since 0.1.0
|
49
|
|
|
*
|
50
|
|
|
* @var ShortcodeAttsParserInterface
|
51
|
|
|
*/
|
52
|
|
|
protected $atts_parser;
|
53
|
|
|
|
54
|
|
|
/**
|
55
|
|
|
* Dependencies of the shortcode.
|
56
|
|
|
*
|
57
|
|
|
* @since 0.1.0
|
58
|
|
|
*
|
59
|
|
|
* @var DependencyManagerInterface
|
60
|
|
|
*/
|
61
|
|
|
protected $dependencies;
|
62
|
|
|
|
63
|
|
|
/**
|
64
|
|
|
* Instantiate Basic Shortcode.
|
65
|
|
|
*
|
66
|
|
|
* @since 0.1.0
|
67
|
|
|
*
|
68
|
|
|
* @param string $shortcode_tag Tag that identifies
|
69
|
|
|
* the shortcode.
|
70
|
|
|
* @param ConfigInterface $config Configuration
|
71
|
|
|
* settings.
|
72
|
|
|
* @param ShortcodeAttsParserInterface $atts_parser Attributes parser and
|
73
|
|
|
* validator.
|
74
|
|
|
* @param DependencyManagerInterface $dependencies Dependencies of the
|
75
|
|
|
* shortcode.
|
76
|
|
|
* @throws RuntimeException If the config could not be processed.
|
77
|
|
|
*/
|
78
|
|
View Code Duplication |
public function __construct(
|
|
|
|
|
79
|
|
|
$shortcode_tag,
|
80
|
|
|
ConfigInterface $config,
|
81
|
|
|
ShortcodeAttsParserInterface $atts_parser,
|
82
|
|
|
DependencyManagerInterface $dependencies
|
83
|
|
|
) {
|
84
|
|
|
|
85
|
|
|
Assert\that( $shortcode_tag )->string()->notEmpty();
|
86
|
|
|
|
87
|
|
|
$this->processConfig( $config );
|
88
|
|
|
|
89
|
|
|
$this->shortcode_tag = $shortcode_tag;
|
90
|
|
|
$this->atts_parser = $atts_parser;
|
91
|
|
|
$this->dependencies = $dependencies;
|
92
|
|
|
}
|
93
|
|
|
|
94
|
|
|
/**
|
95
|
|
|
* Register the shortcode handler function with WordPress.
|
96
|
|
|
*
|
97
|
|
|
* @since 0.1.0
|
98
|
|
|
*
|
99
|
|
|
* @param mixed $args Optional. Arguments to pass on to the Registrable.
|
100
|
|
|
* (Not used with Shortcode class)
|
101
|
|
|
* @return void
|
102
|
|
|
*/
|
103
|
|
|
public function register( $args = null ) {
|
104
|
|
|
if ( ! $this->is_needed( $args ) ) {
|
105
|
|
|
return;
|
106
|
|
|
}
|
107
|
|
|
\add_shortcode( $this->get_tag(), [ $this, 'render' ] );
|
108
|
|
|
}
|
109
|
|
|
|
110
|
|
|
/**
|
111
|
|
|
* Check whether the shortcode is needed.
|
112
|
|
|
*
|
113
|
|
|
* @since 0.2.0
|
114
|
|
|
*
|
115
|
|
|
* @param mixed $context Data about the context in which the call is made.
|
116
|
|
|
* @return boolean Whether the shortcode is needed or not.
|
117
|
|
|
*/
|
118
|
|
View Code Duplication |
protected function is_needed( $context = null ) {
|
|
|
|
|
119
|
|
|
|
120
|
|
|
$is_needed = $this->hasConfigKey( 'is_needed' )
|
121
|
|
|
? $this->getConfigKey( 'is_needed' )
|
122
|
|
|
: false;
|
123
|
|
|
|
124
|
|
|
if ( is_callable( $is_needed ) ) {
|
125
|
|
|
return $is_needed( $context );
|
126
|
|
|
}
|
127
|
|
|
|
128
|
|
|
return (bool) $is_needed;
|
129
|
|
|
}
|
130
|
|
|
|
131
|
|
|
/**
|
132
|
|
|
* Get the shortcode tag.
|
133
|
|
|
*
|
134
|
|
|
* @since 0.1.0
|
135
|
|
|
*
|
136
|
|
|
* @return string Shortcode tag.
|
137
|
|
|
*/
|
138
|
|
|
public function get_tag() {
|
139
|
|
|
return (string) $this->shortcode_tag;
|
140
|
|
|
}
|
141
|
|
|
|
142
|
|
|
/**
|
143
|
|
|
* Render the shortcode.
|
144
|
|
|
*
|
145
|
|
|
* @since 0.1.0
|
146
|
|
|
*
|
147
|
|
|
* @throws DomainException
|
148
|
|
|
*
|
149
|
|
|
* @param array $atts Attributes to modify the standard behavior
|
150
|
|
|
* of the shortcode.
|
151
|
|
|
* @param string|null $content Optional. Content between enclosing
|
152
|
|
|
* shortcodes.
|
153
|
|
|
* @param string|null $tag Optional. The tag of the shortcode to
|
154
|
|
|
* render.
|
155
|
|
|
* @return string The shortcode's HTML output.
|
156
|
|
|
*/
|
157
|
|
|
public function render( $atts, $content = null, $tag = null ) {
|
158
|
|
|
$atts = $this->atts_parser->parse_atts( $atts, $this->get_tag() );
|
159
|
|
|
|
160
|
|
|
$this->dependencies->enqueue( $atts );
|
161
|
|
|
|
162
|
|
|
if ( ! $this->hasConfigKey( 'view' ) ) {
|
163
|
|
|
return '';
|
164
|
|
|
}
|
165
|
|
|
$view = $this->getConfigKey( 'view' );
|
166
|
|
|
|
167
|
|
|
Assert\that( $view )->string()->notEmpty()->file();
|
168
|
|
|
|
169
|
|
|
ob_start();
|
170
|
|
|
include( $this->config['view'] );
|
171
|
|
|
|
172
|
|
|
return ob_get_clean();
|
173
|
|
|
}
|
174
|
|
|
}
|
175
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.