Passed
Push — master ( c2e03d...f38e26 )
by Alain
02:32
created

TemplatedShortcode::get_view_slug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Templated Shortcode Implementation.
4
 *
5
 * This version of the shortcode uses Gamajo/TemplateLoader to let you override
6
 * the shortcode views from your theme.
7
 *
8
 * @package   BrightNucleus\Shortcode
9
 * @author    Alain Schlesser <[email protected]>
10
 * @license   GPL-2.0+
11
 * @link      http://www.brightnucleus.com/
12
 * @copyright 2015-2016 Alain Schlesser, Bright Nucleus
13
 */
14
15
namespace BrightNucleus\Shortcode;
16
17
use Assert;
18
use BrightNucleus\Config\ConfigInterface;
19
use BrightNucleus\Dependency\DependencyManagerInterface as DependencyManager;
20
use BrightNucleus\Exception\RuntimeException;
21
use Gamajo_Template_Loader;
22
23
/**
24
 * Templated Implementation of the Shortcode Interface.
25
 *
26
 * This version of the Shortcode
27
 *
28
 * @since   0.2.6
29
 *
30
 * @package BrightNucleus\Shortcode
31
 * @author  Alain Schlesser <[email protected]>
32
 */
33
class TemplatedShortcode extends Shortcode {
34
35
	/**
36
	 * Template loader that allows a theme to override the shortcode's views.
37
	 *
38
	 * @var Gamajo_Template_Loader|null
39
	 */
40
	protected $template_loader;
41
42
	/**
43
	 * Instantiate Basic Shortcode.
44
	 *
45
	 * @since 0.2.6
46
	 *
47
	 * @param string                 $shortcode_tag Tag that identifies the
48
	 *                                              shortcode.
49
	 * @param ConfigInterface        $config        Configuration settings.
50
	 * @param ShortcodeAttsParser    $atts_parser   Attributes parser and
51
	 *                                              validator.
52
	 * @param DependencyManager|null $dependencies  Optional. Dependencies of
53
	 *                                              the shortcode.
54
	 * @throws RuntimeException If the config could not be processed.
55
	 */
56
	public function __construct(
57
		$shortcode_tag,
58
		ConfigInterface $config,
59
		ShortcodeAttsParser $atts_parser,
60
		DependencyManager $dependencies = null
61
	) {
62
63
		parent::__construct(
64
			$shortcode_tag,
65
			$config,
66
			$atts_parser,
67
			$dependencies
68
		);
69
70
		$this->template_loader = $this->init_template_loader();
71
	}
72
73
	/**
74
	 * Initialize the template loader class.
75
	 *
76
	 * @since 0.2.6
77
	 *
78
	 * @return Gamajo_Template_Loader
0 ignored issues
show
Documentation introduced by
Should the return type not be Gamajo_Template_Loader|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...
79
	 */
80
	public function init_template_loader() {
81
		if ( ! $this->hasConfigKey( 'template' ) ) {
82
			return null;
83
		}
84
		$loader_class  = $this->hasConfigKey( 'template', 'custom_loader' )
85
			? $this->getConfigKey( 'template', 'custom_loader' )
86
			: $this->get_default_template_loader_class();
87
		$filter_prefix = $this->hasConfigKey( 'template', 'filter_prefix' )
88
			? $this->getConfigKey( 'template', 'filter_prefix' )
89
			: $this->get_default_filter_prefix();
90
		$template_dir  = $this->hasConfigKey( 'template', 'template_directory' )
91
			? $this->getConfigKey( 'template', 'template_directory' )
92
			: $this->get_default_template_directory();
93
		$view_dir      = $this->hasConfigKey( 'view' )
94
			? $this->get_directory_from_view( $this->getConfigKey( 'view' ) )
95
			: $this->get_default_view_directory();
96
97
		return new $loader_class(
98
			$filter_prefix,
99
			$template_dir,
100
			$view_dir
101
		);
102
	}
103
104
	/**
105
	 * Get the default template loader class that is used when none is defined
106
	 * in the config file.
107
	 *
108
	 * @since 0.2.6
109
	 *
110
	 * @return string The default template laoder class to use.
111
	 */
112
	protected function get_default_template_loader_class() {
113
		return 'BrightNucleus\Shortcode\ShortcodeTemplateLoader';
114
	}
115
116
	/**
117
	 * Get the default filter prefix that is used when none is defined in the
118
	 * config file.
119
	 *
120
	 * Defaults to 'bn_shortcode.'.
121
	 *
122
	 * @since 0.2.6
123
	 *
124
	 * @return string Default filter prefix to use.
125
	 */
126
	protected function get_default_filter_prefix() {
127
		return 'bn_shortcode';
128
	}
129
130
	/**
131
	 * Get the default template directory that is used when none is defined in
132
	 * the config file.
133
	 *
134
	 * Defaults to 'bn_shortcode'.
135
	 *
136
	 * @since 0.2.6
137
	 *
138
	 * @return string Default template directory to use.
139
	 */
140
	protected function get_default_template_directory() {
141
		return 'bn_shortcode';
142
	}
143
144
	/**
145
	 * Get the directory for a given view file.
146
	 *
147
	 * @since 0.2.6
148
	 *
149
	 * @param string $view View file to extract the directory from.
150
	 * @return string Directory that contains the given view.
151
	 */
152
	protected function get_directory_from_view( $view ) {
153
		return dirname( $view );
154
	}
155
156
	/**
157
	 * Get the default view directory that is used when none is defined in the
158
	 * config file.
159
	 *
160
	 * Defaults to 'views/shortcodes'. Will probably need to be changed into an
161
	 * absolute path if the shortcodes package is pulled in through Composer.
162
	 *
163
	 * @since 0.2.6
164
	 *
165
	 * @return string Default view directory to use.
166
	 */
167
	protected function get_default_view_directory() {
168
		return 'views/shortcodes';
169
	}
170
171
	/**
172
	 * Get the rendered HTML for a given view.
173
	 *
174
	 * @since 0.2.6
175
	 *
176
	 * @param string $view The view to render.
177
	 * @return string HTML rendering of the view.
178
	 */
179
	protected function render_view( $view ) {
180
		if ( empty( $view ) ) {
181
			return '';
182
		}
183
184
		$view = $this->get_view_slug( $view );
185
186
		return $this->template_loader->get_template_part( $view );
187
	}
188
189
	/**
190
	 * Get the slug for a given view.
191
	 *
192
	 * @since 0.2.6
193
	 *
194
	 * @param string $view The view to get the slug for.
195
	 * @return string Slug that can be passed into `get_template_part()`.
196
	 */
197
	protected function get_view_slug( $view ) {
198
		return $this->maybe_strip_extension( basename( $view ) );
199
	}
200
201
	/**
202
	 * Strip the extension for a given view filename if it includes an
203
	 * extension.
204
	 *
205
	 * @since 0.2.6
206
	 *
207
	 * @param string $view The view that maybe needs its extension stripped.
208
	 * @return string Extension-less view.
209
	 */
210
	protected function maybe_strip_extension( $view ) {
211
		return pathinfo( $view, PATHINFO_FILENAME );
212
	}
213
}
214