|
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
|
|
|
} else { |
|
36
|
|
|
|
|
37
|
|
|
//check if the gallery is password protected |
|
38
|
|
|
if ( post_password_required( $current_foogallery->_post ) ) { |
|
39
|
|
|
echo get_the_password_form( $current_foogallery->_post ); |
|
40
|
|
|
return; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
//find the gallery template we will use to render the gallery |
|
44
|
|
|
$current_foogallery_template = $this->get_arg( $args, 'template', $current_foogallery->gallery_template ); |
|
45
|
|
|
|
|
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
|
|
|
//check if we have any attachments |
|
52
|
|
|
if ( ! $current_foogallery->has_attachments() ) { |
|
53
|
|
|
//no attachments! |
|
54
|
|
|
do_action( "foogallery_template_no_attachments-($current_foogallery_template)", $current_foogallery ); |
|
55
|
|
|
} else { |
|
56
|
|
|
|
|
57
|
|
|
//create locator instance |
|
58
|
|
|
$loader = $this->create_locator_instance(); |
|
59
|
|
|
|
|
60
|
|
|
if ( false !== ( $template_location = $loader->locate_file( "gallery-{$current_foogallery_template}.php" ) ) ) { |
|
61
|
|
|
|
|
62
|
|
|
//we have found a template! |
|
63
|
|
|
do_action( 'foogallery_located_template', $current_foogallery ); |
|
64
|
|
|
do_action( "foogallery_located_template-{$current_foogallery_template}", $current_foogallery ); |
|
65
|
|
|
|
|
66
|
|
|
//try to include some JS, but allow template to opt-out based on some condition |
|
67
|
|
|
if ( false !== apply_filters( "foogallery_template_load_js-{$current_foogallery_template}", true, $current_foogallery ) ) { |
|
68
|
|
|
if ( false !== ( $js_location = $loader->locate_file( "gallery-{$current_foogallery_template}.js" ) ) ) { |
|
69
|
|
|
$js_deps = apply_filters( "foogallery_template_js_deps-{$current_foogallery_template}", array(), $current_foogallery ); |
|
70
|
|
|
$js_ver = apply_filters( "foogallery_template_js_ver-{$current_foogallery_template}", FOOGALLERY_VERSION, $current_foogallery ); |
|
71
|
|
|
wp_enqueue_script( "foogallery-template-{$current_foogallery_template}", $js_location['url'], $js_deps, $js_ver ); |
|
72
|
|
|
do_action( 'foogallery_template_enqueue_script', $current_foogallery_template, $js_location['url'] ); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
//try to include some CSS, but allow template to opt-out based on some condition |
|
77
|
|
|
if ( false !== apply_filters( "foogallery_template_load_css-{$current_foogallery_template}", true, $current_foogallery ) ) { |
|
78
|
|
|
if ( false !== ( $css_location = $loader->locate_file( "gallery-{$current_foogallery_template}.css" ) ) ) { |
|
79
|
|
|
$css_deps = apply_filters( "foogallery_template_css_deps-{$current_foogallery_template}", array(), $current_foogallery ); |
|
80
|
|
|
$css_ver = apply_filters( "foogallery_template_css_ver-{$current_foogallery_template}", FOOGALLERY_VERSION, $current_foogallery ); |
|
81
|
|
|
foogallery_enqueue_style( "foogallery-template-{$current_foogallery_template}", $css_location['url'], $css_deps, $css_ver ); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
//finally include the actual php template! |
|
86
|
|
|
if ( $template_location ) { |
|
87
|
|
|
$this->load_gallery_template( $current_foogallery, $template_location['path'] ); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
//cater for lightbox extensions needing to add styles and javascript |
|
91
|
|
|
$lightbox = foogallery_gallery_template_setting( 'lightbox' ); |
|
92
|
|
|
if ( !empty( $lightbox ) ) { |
|
93
|
|
|
do_action( "foogallery_template_lightbox-{$lightbox}", $current_foogallery ); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
//we have loaded all files, now let extensions do some stuff |
|
97
|
|
|
do_action( "foogallery_loaded_template", $current_foogallery ); |
|
98
|
|
|
do_action( "foogallery_loaded_template-($current_foogallery_template)", $current_foogallery ); |
|
99
|
|
|
} else { |
|
100
|
|
|
//we could not find a template! |
|
101
|
|
|
_e( 'No gallery template found!', 'foogallery' ); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/*** |
|
108
|
|
|
* Loads a gallery template location and wraps the calls so that it can be intercepted |
|
109
|
|
|
* |
|
110
|
|
|
* @param FooGallery $gallery |
|
111
|
|
|
* @param string $template_location |
|
112
|
|
|
*/ |
|
113
|
|
|
function load_gallery_template($gallery, $template_location) { |
|
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
$override_load_template = apply_filters( 'foogallery_load_gallery_template', false, $gallery, $template_location ); |
|
116
|
|
|
|
|
117
|
|
|
if ( $override_load_template ) { |
|
118
|
|
|
//if we have overridden the loading of the template, then we can exit without doing anything further |
|
119
|
|
|
return; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
//if we get to this point, then we need to load the template as per normal |
|
123
|
|
|
load_template( $template_location, false ); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Creates a locator instance used for including template files |
|
128
|
|
|
* |
|
129
|
|
|
* |
|
130
|
|
|
*/ |
|
131
|
|
|
public function create_locator_instance() { |
|
132
|
|
|
$instance_name = FOOGALLERY_SLUG . '_gallery_templates'; |
|
133
|
|
|
$loader = new Foo_Plugin_File_Locator_v1( $instance_name, FOOGALLERY_FILE, 'templates', FOOGALLERY_SLUG ); |
|
134
|
|
|
|
|
135
|
|
|
//allow extensions to very easily add pickup locations for their files |
|
136
|
|
|
$this->add_extension_pickup_locations( $loader, apply_filters( $instance_name . '_files', array() ) ); |
|
137
|
|
|
|
|
138
|
|
|
return $loader; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Add pickup locations to the loader to make it easier for extensions |
|
143
|
|
|
* |
|
144
|
|
|
* @param $loader Foo_Plugin_File_Locator_v1 |
|
145
|
|
|
* @param $extension_files array |
|
146
|
|
|
*/ |
|
147
|
|
|
function add_extension_pickup_locations( $loader, $extension_files ) { |
|
148
|
|
|
if ( count( $extension_files ) > 0 ) { |
|
149
|
|
|
$position = 120; |
|
150
|
|
|
foreach ( $extension_files as $file ) { |
|
151
|
|
|
|
|
152
|
|
|
//add pickup location for php template |
|
153
|
|
|
$loader->add_location( $position, array( |
|
154
|
|
|
'path' => trailingslashit( plugin_dir_path( $file ) ), |
|
155
|
|
|
'url' => trailingslashit( plugin_dir_url( $file ) ) |
|
156
|
|
|
) ); |
|
157
|
|
|
|
|
158
|
|
|
$position++; |
|
159
|
|
|
|
|
160
|
|
|
//add pickup location for extensions js folder |
|
161
|
|
|
$loader->add_location( $position, array( |
|
162
|
|
|
'path' => trailingslashit( plugin_dir_path( $file ) . 'js' ), |
|
163
|
|
|
'url' => trailingslashit( plugin_dir_url( $file ) . 'js' ) |
|
164
|
|
|
) ); |
|
165
|
|
|
|
|
166
|
|
|
$position++; |
|
167
|
|
|
|
|
168
|
|
|
//add pickup location for extension css folder |
|
169
|
|
|
$loader->add_location( $position, array( |
|
170
|
|
|
'path' => trailingslashit( plugin_dir_path( $file ) . 'css' ), |
|
171
|
|
|
'url' => trailingslashit( plugin_dir_url( $file ) . 'css' ) |
|
172
|
|
|
) ); |
|
173
|
|
|
|
|
174
|
|
|
$position++; |
|
175
|
|
|
|
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* load the gallery based on either the id or slug, passed in via arguments |
|
182
|
|
|
* |
|
183
|
|
|
* @param $args array Arguments passed in from the shortcode |
|
184
|
|
|
* |
|
185
|
|
|
* @return bool|FooGallery The gallery object we want to render |
|
186
|
|
|
*/ |
|
187
|
|
|
function find_gallery( $args ) { |
|
188
|
|
|
|
|
189
|
|
|
$id = intval( $this->get_arg( $args, 'id' ), 0 ); |
|
190
|
|
|
|
|
191
|
|
|
if ( $id > 0 ) { |
|
192
|
|
|
|
|
193
|
|
|
//load gallery by ID |
|
194
|
|
|
return FooGallery::get_by_id( $id ); |
|
195
|
|
|
|
|
196
|
|
|
} else { |
|
197
|
|
|
|
|
198
|
|
|
//take into account the cases where id is passed in via the 'gallery' attribute |
|
199
|
|
|
$gallery = $this->get_arg( $args, 'gallery', 0 ); |
|
200
|
|
|
|
|
201
|
|
|
if ( intval( $gallery ) > 0 ) { |
|
202
|
|
|
//we have an id, so load |
|
203
|
|
|
return FooGallery::get_by_id( intval( $gallery ) ); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
//we are dealing with a slug |
|
207
|
|
|
return FooGallery::get_by_slug( $gallery ); |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Helper to get an argument value from an array arguments |
|
213
|
|
|
* |
|
214
|
|
|
* @param $args Array the array of arguments to search |
|
215
|
|
|
* @param $key string the key of the argument you are looking for |
|
216
|
|
|
* @param $default string a default value if the argument is not found |
|
217
|
|
|
* |
|
218
|
|
|
* @return string |
|
219
|
|
|
*/ |
|
220
|
|
|
function get_arg( $args, $key, $default = '' ) { |
|
221
|
|
|
if ( empty($args) || ! array_key_exists( $key, $args ) ) { |
|
222
|
|
|
return $default; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
return $args[ $key ]; |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.