Passed
Push — master ( cd28b2...551bb3 )
by Alain
04:04
created

Shortcode::render()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 20
ccs 0
cts 12
cp 0
rs 9.4285
cc 3
eloc 12
nc 4
nop 3
crap 12
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 as DependencyManager;
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 DependencyManager
60
	 */
61
	protected $dependencies;
62
63
	/**
64
	 * Cache context information so we can pass it on to the render() method.
65
	 *
66
	 * @var
67
	 *
68
	 * @since 0.2.3
69
	 */
70
	protected $context;
71
72
	/**
73
	 * Instantiate Basic Shortcode.
74
	 *
75
	 * @since 0.1.0
76
	 *
77
	 * @param string                 $shortcode_tag Tag that identifies the
78
	 *                                              shortcode.
79
	 * @param ConfigInterface        $config        Configuration settings.
80
	 * @param ShortcodeAttsParser    $atts_parser   Attributes parser and
81
	 *                                              validator.
82
	 * @param DependencyManager|null $dependencies  Optional. Dependencies of
83
	 *                                              the shortcode.
84
	 * @throws RuntimeException If the config could not be processed.
85
	 */
86 View Code Duplication
	public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
87
		$shortcode_tag,
88
		ConfigInterface $config,
89
		ShortcodeAttsParser $atts_parser,
90
		DependencyManager $dependencies = null
91
	) {
92
93
		Assert\that( $shortcode_tag )->string()->notEmpty();
94
95
		$this->processConfig( $config );
96
97
		$this->shortcode_tag = $shortcode_tag;
98
		$this->atts_parser   = $atts_parser;
99
		$this->dependencies  = $dependencies;
100
	}
101
102
	/**
103
	 * Register the shortcode handler function with WordPress.
104
	 *
105
	 * @since 0.1.0
106
	 *
107
	 * @param mixed $context Optional. Arguments to pass on to the Registrable.
108
	 * @return void
109
	 */
110
	public function register( $context = null ) {
111
		if ( ! $this->is_needed( $context ) ) {
112
			return;
113
		}
114
		$this->context = $context;
115
116
		\add_shortcode( $this->get_tag(), [ $this, 'render' ] );
117
	}
118
119
	/**
120
	 * Check whether the shortcode is needed.
121
	 *
122
	 * @since 0.2.0
123
	 *
124
	 * @param mixed $context Data about the context in which the call is made.
125
	 * @return boolean Whether the shortcode is needed or not.
126
	 */
127 View Code Duplication
	protected function is_needed( $context = null ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
128
129
		$is_needed = $this->hasConfigKey( 'is_needed' )
130
			? $this->getConfigKey( 'is_needed' )
131
			: false;
132
133
		if ( is_callable( $is_needed ) ) {
134
			return $is_needed( $context );
135
		}
136
137
		return (bool) $is_needed;
138
	}
139
140
	/**
141
	 * Get the shortcode tag.
142
	 *
143
	 * @since 0.1.0
144
	 *
145
	 * @return string Shortcode tag.
146
	 */
147
	public function get_tag() {
148
		return (string) $this->shortcode_tag;
149
	}
150
151
	/**
152
	 * Render the shortcode.
153
	 *
154
	 * @since 0.1.0
155
	 *
156
	 * @throws DomainException
157
	 *
158
	 * @param  array       $atts    Attributes to modify the standard behavior
159
	 *                              of the shortcode.
160
	 * @param  string|null $content Optional. Content between enclosing
161
	 *                              shortcodes.
162
	 * @param string|null  $tag     Optional. The tag of the shortcode to
163
	 *                              render.
164
	 * @return string               The shortcode's HTML output.
165
	 */
166
	public function render( $atts, $content = null, $tag = null ) {
167
		$context = $this->context;
168
		$atts    = $this->atts_parser->parse_atts( $atts, $this->get_tag() );
169
170
		if ( $this->dependencies ) {
171
			$this->dependencies->enqueue( $atts );
172
		}
173
174
		if ( ! $this->hasConfigKey( 'view' ) ) {
175
			return '';
176
		}
177
		$view = $this->getConfigKey( 'view' );
178
179
		Assert\that( $view )->string()->notEmpty()->file();
180
181
		ob_start();
182
		include( $this->config['view'] );
183
184
		return ob_get_clean();
185
	}
186
187
	/**
188
	 * Execute this shortcode directly from code.
189
	 *
190
	 * @since 0.2.4
191
	 *
192
	 * @param array       $atts    Array of attributes to pass to the shortcode.
193
	 * @param string|null $content Inner content to pass to the shortcode.
194
	 * @return string Rendered HTML.
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
195
	 */
196
	public function do_this( array $atts = [ ], $content = null ) {
197
		\BrightNucleus\Shortcode\do_tag( $this->tag, $atts, $content );
0 ignored issues
show
Bug introduced by
The property tag does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
198
	}
199
}
200