forked from ubc/ubc-use-template-shortcode
/
ubc-use-template-shortcode.php
262 lines (190 loc) · 5.17 KB
/
ubc-use-template-shortcode.php
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
namespace UBC\Shortcode;
/**
*
* @wordpress-plugin
* Plugin Name: UBC Use Template Shortcode
* Plugin URI: http://ctlt.ubc.ca/
* Description: A shortcode to allow you to use a PHP template to output content
* Version: 1.0.1
* Author: Richard Tape
* Author URI: http://blogs.ubc.ca/mbcx9rvt
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain:
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
class Use_Template {
/**
* The version of this plugin
*
* @since 1.0.0
*
* @access public
* @var (string) $version The version of this plugin
*/
protected static $version = '1.0.0';
/**
* The text domain for this plugin
*
* @since 1.0.0
*
* @access public
* @var (string) $text_domain The text domain for this plugin
*/
protected static $text_domain = 'ubc-use-template-shortcode';
/**
* Instance of this class
*
* @since 1.0.0
*
* @access public
* @var (object) $instance
*/
protected static $instance;
/**
* The path to this file
*
* @since 1.0.0
* @access public
* @var string $plugin_path
*/
public static $plugin_path = '';
/**
* The url to this file
*
* @since 1.0.0
*
* @access public
* @var (string) $plugin_url
*/
public static $plugin_url = '';
/**
* Initialize ourselves - adds actions/filters
*
* @since 1.0.0
*
* @param null
* @return null
*/
public function init() {
if ( ! is_null( self::$instance ) ) {
return;
}
self::$instance = new self;
// Set the plugin path as where this file resides
self::$plugin_path = trailingslashit( dirname( __FILE__ ) );
// And the URL
self::$plugin_url = trailingslashit( plugins_url( '', __FILE__ ) );
// Set up our actions
$this->add_actions();
}/* init() */
/**
* Abstracted method to add actions
*
* @since 1.0.0
*
* @param null
* @return null
*/
public function add_actions() {
// Register the shortcode
add_action( 'init', array( $this, 'init__add_shortcode' ) );
}/* add_actions() */
/**
* Register the ubc_template shortcode
*
* @since 1.0.0
*
* @param null
* @return null
*/
public function init__add_shortcode() {
add_shortcode( 'ubc_template', array( $this, 'add_shortcode__ubc_template' ) );
}/* init__add_shortcode() */
/**
* Logic for the ubc_template shortcode. Looks for a specific template file
* that is requested in the shortcode atts, if it exists, fetches it.
*
* @since 1.0.0
*
* @param (array) $attr - Attributes passed to the shortcode
* @return (string) markup for this shortcode
*/
public function add_shortcode__ubc_template( $attr ) {
$attr = shortcode_atts(
array(
'department' => '',
'template' => '',
),
$attr,
'ubc_template'
);
// Need both, bail early if not set
if ( empty( $attr['department'] ) || empty( $attr['template'] ) ) {
return __( 'ubc_template shortcode requires both a department and template', $this->get_text_domain() );
}
// The root URL
$path = self::$plugin_path . 'templates/';
$department = sanitize_title_with_dashes( $attr['department'] );
$template = sanitize_title_with_dashes( $attr['template'] );
// Form the full path and then check if that template exists
$path .= trailingslashit( $department ) . $template . '.php';
$path = apply_filters( 'ubc_use_template_path', $path, $department, $template );
if ( ! file_exists( $path ) ) {
return __( 'Specified template file does not exist', $this->get_text_domain() );
}
$data = apply_filters( 'ubc_use_template_data', array(), $department, $template );
// This allows us to use odd/even counts in templates
global $usage_id;
if ( ! isset( $usage_id ) ) {
$usage_id = 0;
}
// Fetch the content of this template
$content = \UBC\Helpers::fetch_template_part( $path, $data );
// Run it through a filter so we can modify outside should someone wish to add
$content = apply_filters( 'ubc_use_template_content', $content, $data, $department, $template );
do_action( 'ubc_use_template_end', $content, $data, $department, $template );
return wp_kses_post( $content );
}/* add_shortcode__ubc_template() */
/**
* A quick getter for the plugin version number
*
* @since 1.0.0
*
* @param null
* @return (int) The version of this plugin
*/
public static function get_version() {
return static::$version;
}/* get_version() */
/**
* Quick getter for the text domain of this plugin
* Usage: \UBC\Press::get_text_domain()
*
* @since 1.0.0
*
* @param null
* @return (string) The text domain of this plugin
*/
public static function get_text_domain() {
return static::$text_domain;
}/* get_text_domain() */
}/* Class Use_Template */
// Let's get this party started
add_action( 'plugins_loaded', '\UBC\Shortcode\plugins_loaded__init_use_template', 2 );
/**
* Instantiate Use_Template
*
* @author Richard Tape <@richardtape>
* @since 1.0
* @param null
* @return null
*/
function plugins_loaded__init_use_template() {
$Use_Template = new \UBC\Shortcode\Use_Template();
$Use_Template->init();
}/* plugins_loaded__init_use_template() */