1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Template loader for FooGallery |
5
|
|
|
* |
6
|
|
|
* @package FooGallery |
7
|
|
|
* @author Brad vincent |
8
|
|
|
*/ |
9
|
|
|
class FooGallery_Template_Loader { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Locates and renders the gallery based on the template |
13
|
|
|
* Will look in the following locations |
14
|
|
|
* wp-content/themes/{child-theme}/foogallery/gallery-{template}.php |
15
|
|
|
* wp-content/themes/{theme}/foogallery/gallery-{template}.php |
16
|
|
|
* wp-content/plugins/foogallery/templates/gallery-{template}.php |
17
|
|
|
* |
18
|
|
|
* @param $args array Arguments passed in from the shortcode |
19
|
|
|
*/ |
20
|
|
|
public function render_template( $args ) { |
21
|
|
|
//do some work before we locate the template |
22
|
|
|
global $current_foogallery; |
23
|
|
|
global $current_foogallery_arguments; |
24
|
|
|
global $current_foogallery_template; |
25
|
|
|
|
26
|
|
|
//set the arguments |
27
|
|
|
$current_foogallery_arguments = $args; |
28
|
|
|
|
29
|
|
|
//load our gallery |
30
|
|
|
$current_foogallery = $this->find_gallery( $args ); |
31
|
|
|
|
32
|
|
|
if ( false === $current_foogallery ) { |
33
|
|
|
//we could not find the gallery! |
34
|
|
|
_e( 'The gallery was not found!', 'foogallery' ); |
35
|
|
|
return; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
//check if the gallery is password protected |
39
|
|
|
if ( post_password_required( $current_foogallery->_post ) ) { |
40
|
|
|
echo get_the_password_form( $current_foogallery->_post ); |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
//find the gallery template we will use to render the gallery |
45
|
|
|
$current_foogallery_template = $this->get_arg( $args, 'template', $current_foogallery->gallery_template ); |
46
|
|
|
//set a default if we have no gallery template |
47
|
|
|
if ( empty( $current_foogallery_template ) ) { |
48
|
|
|
$current_foogallery_template = foogallery_get_default( 'gallery_template' ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
//potentially override attachment_ids from arguments |
52
|
|
|
$attachment_ids = $this->get_arg( $args, 'attachment_ids', false ); |
|
|
|
|
53
|
|
|
if ( $attachment_ids ) { |
54
|
|
|
$current_foogallery->attachment_ids = explode( ',', $attachment_ids ); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
//check if we have any attachments |
58
|
|
|
if ( ! $current_foogallery->has_attachments() ) { |
59
|
|
|
//no attachments! |
60
|
|
|
do_action( "foogallery_template_no_attachments-($current_foogallery_template)", $current_foogallery ); |
61
|
|
|
} else { |
62
|
|
|
|
63
|
|
|
//create locator instance |
64
|
|
|
$loader = $this->create_locator_instance(); |
65
|
|
|
|
66
|
|
|
if ( false !== ( $template_location = $loader->locate_file( "gallery-{$current_foogallery_template}.php" ) ) ) { |
67
|
|
|
|
68
|
|
|
//we have found a template! |
69
|
|
|
do_action( 'foogallery_located_template', $current_foogallery ); |
70
|
|
|
do_action( "foogallery_located_template-{$current_foogallery_template}", $current_foogallery ); |
71
|
|
|
|
72
|
|
|
//try to include some JS, but allow template to opt-out based on some condition |
73
|
|
|
if ( false !== apply_filters( "foogallery_template_load_js-{$current_foogallery_template}", true, $current_foogallery ) ) { |
74
|
|
|
if ( false !== ( $js_location = $loader->locate_file( "gallery-{$current_foogallery_template}.js" ) ) ) { |
75
|
|
|
$js_deps = apply_filters( "foogallery_template_js_deps-{$current_foogallery_template}", array(), $current_foogallery ); |
76
|
|
|
$js_ver = apply_filters( "foogallery_template_js_ver-{$current_foogallery_template}", FOOGALLERY_VERSION, $current_foogallery ); |
77
|
|
|
wp_enqueue_script( "foogallery-template-{$current_foogallery_template}", $js_location['url'], $js_deps, $js_ver ); |
78
|
|
|
do_action( 'foogallery_template_enqueue_script', $current_foogallery_template, $js_location['url'] ); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
//try to include some CSS, but allow template to opt-out based on some condition |
83
|
|
|
if ( false !== apply_filters( "foogallery_template_load_css-{$current_foogallery_template}", true, $current_foogallery ) ) { |
84
|
|
|
if ( false !== ( $css_location = $loader->locate_file( "gallery-{$current_foogallery_template}.css" ) ) ) { |
85
|
|
|
$css_deps = apply_filters( "foogallery_template_css_deps-{$current_foogallery_template}", array(), $current_foogallery ); |
86
|
|
|
$css_ver = apply_filters( "foogallery_template_css_ver-{$current_foogallery_template}", FOOGALLERY_VERSION, $current_foogallery ); |
87
|
|
|
foogallery_enqueue_style( "foogallery-template-{$current_foogallery_template}", $css_location['url'], $css_deps, $css_ver ); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
//finally include the actual php template! |
92
|
|
|
if ( $template_location ) { |
93
|
|
|
$this->load_gallery_template( $current_foogallery, $template_location['path'] ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
//cater for lightbox extensions needing to add styles and javascript |
97
|
|
|
$lightbox = foogallery_gallery_template_setting( 'lightbox' ); |
98
|
|
|
if ( !empty( $lightbox ) ) { |
99
|
|
|
do_action( "foogallery_template_lightbox-{$lightbox}", $current_foogallery ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
//we have loaded all files, now let extensions do some stuff |
103
|
|
|
do_action( "foogallery_loaded_template", $current_foogallery ); |
104
|
|
|
do_action( "foogallery_loaded_template-($current_foogallery_template)", $current_foogallery ); |
105
|
|
|
} else { |
106
|
|
|
//we could not find a template! |
107
|
|
|
_e( 'No gallery template found!', 'foogallery' ); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/*** |
113
|
|
|
* Loads a gallery template location and wraps the calls so that it can be intercepted |
114
|
|
|
* |
115
|
|
|
* @param FooGallery $gallery |
116
|
|
|
* @param string $template_location |
117
|
|
|
*/ |
118
|
|
|
function load_gallery_template($gallery, $template_location) { |
|
|
|
|
119
|
|
|
|
120
|
|
|
$override_load_template = apply_filters( 'foogallery_load_gallery_template', false, $gallery, $template_location ); |
121
|
|
|
|
122
|
|
|
if ( $override_load_template ) { |
123
|
|
|
//if we have overridden the loading of the template, then we can exit without doing anything further |
124
|
|
|
return; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
//if we get to this point, then we need to load the template as per normal |
128
|
|
|
load_template( $template_location, false ); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Creates a locator instance used for including template files |
133
|
|
|
* |
134
|
|
|
* |
135
|
|
|
*/ |
136
|
|
|
public function create_locator_instance() { |
137
|
|
|
$instance_name = FOOGALLERY_SLUG . '_gallery_templates'; |
138
|
|
|
$loader = new Foo_Plugin_File_Locator_v1( $instance_name, FOOGALLERY_FILE, 'templates', FOOGALLERY_SLUG ); |
139
|
|
|
|
140
|
|
|
//allow extensions to very easily add pickup locations for their files |
141
|
|
|
$this->add_extension_pickup_locations( $loader, apply_filters( $instance_name . '_files', array() ) ); |
142
|
|
|
|
143
|
|
|
return $loader; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Add pickup locations to the loader to make it easier for extensions |
148
|
|
|
* |
149
|
|
|
* @param $loader Foo_Plugin_File_Locator_v1 |
150
|
|
|
* @param $extension_files array |
151
|
|
|
*/ |
152
|
|
|
function add_extension_pickup_locations( $loader, $extension_files ) { |
153
|
|
|
if ( count( $extension_files ) > 0 ) { |
154
|
|
|
$position = 120; |
155
|
|
|
foreach ( $extension_files as $file ) { |
156
|
|
|
|
157
|
|
|
//add pickup location for php template |
158
|
|
|
$loader->add_location( $position, array( |
159
|
|
|
'path' => trailingslashit( plugin_dir_path( $file ) ), |
160
|
|
|
'url' => trailingslashit( plugin_dir_url( $file ) ) |
161
|
|
|
) ); |
162
|
|
|
|
163
|
|
|
$position++; |
164
|
|
|
|
165
|
|
|
//add pickup location for extensions js folder |
166
|
|
|
$loader->add_location( $position, array( |
167
|
|
|
'path' => trailingslashit( plugin_dir_path( $file ) . 'js' ), |
168
|
|
|
'url' => trailingslashit( plugin_dir_url( $file ) . 'js' ) |
169
|
|
|
) ); |
170
|
|
|
|
171
|
|
|
$position++; |
172
|
|
|
|
173
|
|
|
//add pickup location for extension css folder |
174
|
|
|
$loader->add_location( $position, array( |
175
|
|
|
'path' => trailingslashit( plugin_dir_path( $file ) . 'css' ), |
176
|
|
|
'url' => trailingslashit( plugin_dir_url( $file ) . 'css' ) |
177
|
|
|
) ); |
178
|
|
|
|
179
|
|
|
$position++; |
180
|
|
|
|
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* load the gallery based on either the id or slug, passed in via arguments |
187
|
|
|
* |
188
|
|
|
* @param $args array Arguments passed in from the shortcode |
189
|
|
|
* |
190
|
|
|
* @return bool|FooGallery The gallery object we want to render |
191
|
|
|
*/ |
192
|
|
|
function find_gallery( $args ) { |
193
|
|
|
|
194
|
|
|
$id = intval( $this->get_arg( $args, 'id' ), 0 ); |
195
|
|
|
$gallery = $this->get_arg( $args, 'gallery', 0 ); |
196
|
|
|
|
197
|
|
|
if ( $id > 0 ) { |
198
|
|
|
//load gallery by ID |
199
|
|
|
return FooGallery::get_by_id( $id ); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
//take into account the cases where id is passed in via the 'gallery' attribute |
203
|
|
|
if ( intval( $gallery ) > 0 ) { |
204
|
|
|
//we have an id, so load |
205
|
|
|
return FooGallery::get_by_id( intval( $gallery ) ); |
206
|
|
|
} else if ( !empty( $gallery ) ) { |
207
|
|
|
//we are dealing with a slug |
208
|
|
|
return FooGallery::get_by_slug( $gallery ); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
//if we get here then we have no id or gallery attribute, so try to build a dynamic gallery |
212
|
|
|
|
213
|
|
|
//we can only build up a dynamic gallery if attachment_ids are passed in |
214
|
|
|
$attachment_ids = $this->get_arg( $args, 'attachment_ids', false ); |
|
|
|
|
215
|
|
|
|
216
|
|
|
if ( $attachment_ids ) { |
217
|
|
|
$template = $this->get_arg( $args, 'template', foogallery_get_default( 'gallery_template' ) ); |
218
|
|
|
return FooGallery::dynamic( $template, explode( ',', $attachment_ids) ); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return false; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Helper to get an argument value from an array arguments |
226
|
|
|
* |
227
|
|
|
* @param $args Array the array of arguments to search |
228
|
|
|
* @param $key string the key of the argument you are looking for |
229
|
|
|
* @param $default string a default value if the argument is not found |
230
|
|
|
* |
231
|
|
|
* @return string |
232
|
|
|
*/ |
233
|
|
|
function get_arg( $args, $key, $default = '' ) { |
234
|
|
|
if ( empty($args) || ! array_key_exists( $key, $args ) ) { |
235
|
|
|
return $default; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return $args[ $key ]; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: