PluginProbe ʕ •ᴥ•ʔ
Photo Gallery by FooGallery : Responsive Image Gallery, Masonry Gallery & Carousel / trunk
Photo Gallery by FooGallery : Responsive Image Gallery, Masonry Gallery & Carousel vtrunk
trunk 1.10.3 2.0.24 2.1.34 2.2.44 2.3.3 2.4.32 3.0.6 3.1.11 3.1.12 3.1.13 3.1.20 3.1.25 3.1.26 3.1.26.1 3.1.26.2
foogallery / includes / public / class-foogallery-template-loader.php
foogallery / includes / public Last commit date
class-admin-bar.php 7 months ago class-aioseo-sitemaps.php 7 months ago class-css-load-optimizer.php 7 months ago class-foogallery-template-loader.php 7 months ago class-public.php 7 months ago class-rank-math-seo-sitemaps.php 7 months ago class-shortcodes.php 7 months ago class-yoast-seo-sitemaps.php 7 months ago index.php 12 years ago
class-foogallery-template-loader.php
336 lines
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 esc_html_e( 'The gallery was not found!', 'foogallery' );
35 $current_foogallery = null;
36 return;
37 }
38
39 // check if the gallery is password protected
40 if ( post_password_required( $current_foogallery->_post ) ) {
41 echo get_the_password_form( $current_foogallery->_post ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
42 $current_foogallery = null;
43 return;
44 }
45
46 //find the gallery template we will use to render the gallery
47 $current_foogallery_template = $this->get_arg( $args, 'template', $current_foogallery->gallery_template );
48 //set a default if we have no gallery template
49 if ( empty( $current_foogallery_template ) ) {
50 $current_foogallery_template = foogallery_get_default( 'gallery_template' );
51 }
52
53 // Allow for the template of the gallery to be overridden!
54 $current_foogallery_template = apply_filters( 'foogallery_render_template_template_override', $current_foogallery_template, $current_foogallery, $current_foogallery_arguments );
55
56 //override the template if needed
57 if ( $current_foogallery->gallery_template !== $current_foogallery_template ) {
58 $old_gallery_template = $current_foogallery->gallery_template;
59
60 $current_foogallery->gallery_template = $current_foogallery_template;
61
62 //we need to move the settings across
63 if ( isset( $current_foogallery->settings ) && is_array( $current_foogallery->settings ) ) {
64 $new_settings = array();
65 foreach ( $current_foogallery->settings as $setting_key => $setting_value ) {
66 $setting_key = $this->str_replace_first( "{$old_gallery_template}_", "{$current_foogallery_template}_", $setting_key );
67 $new_settings[ $setting_key ] = $setting_value;
68 }
69
70 $current_foogallery->settings = $new_settings;
71 }
72 }
73
74 //potentially override attachment_ids from arguments
75 $attachment_ids = $this->get_arg( $args, 'attachment_ids', false );
76 if ( $attachment_ids ) {
77 if ( !is_array( $attachment_ids ) ) {
78 $attachment_ids = explode( ',', $attachment_ids);
79 }
80 $current_foogallery->attachment_ids = $attachment_ids;
81 }
82
83 //allow for any other gallery argument overrides
84 $current_foogallery = apply_filters( 'foogallery_render_template_argument_overrides', $current_foogallery, $args );
85
86 //check that we have a datasource. If not then set it to the default
87 if ( empty( $current_foogallery->datasource_name ) ) {
88 $current_foogallery->datasource_name = foogallery_default_datasource();
89 }
90
91 //check if we have any items
92 if ( ! $current_foogallery->has_items() ) {
93 //no attachments!
94 do_action( "foogallery_template_no_attachments-($current_foogallery_template)", $current_foogallery );
95 do_action( "foogallery_template_no_attachments", $current_foogallery );
96 } else {
97
98 //create locator instance
99 $loader = $this->create_locator_instance();
100
101 if ( false !== ( $template_location = $loader->locate_file( "gallery-{$current_foogallery_template}.php" ) ) ) {
102
103 //we have found a template!
104 do_action( 'foogallery_located_template', $current_foogallery );
105 do_action( "foogallery_located_template-{$current_foogallery_template}", $current_foogallery );
106
107 $template_object = foogallery_get_gallery_template( $current_foogallery_template );
108 if ( isset( $template_object ) && is_array( $template_object ) && array_key_exists( 'enqueue_core', $template_object ) && $template_object['enqueue_core'] ) {
109 //enqueue core files
110 foogallery_enqueue_core_gallery_template_style();
111 foogallery_enqueue_core_gallery_template_script();
112 } else {
113 //try to include some JS, but allow template to opt-out based on some condition
114 if ( false !== apply_filters( "foogallery_template_load_js-{$current_foogallery_template}", true, $current_foogallery ) ) {
115 if ( false !== ( $js_location = $loader->locate_file( "gallery-{$current_foogallery_template}.js" ) ) ) {
116 $js_deps = apply_filters( "foogallery_template_js_deps-{$current_foogallery_template}", array(), $current_foogallery );
117 $js_ver = apply_filters( "foogallery_template_js_ver-{$current_foogallery_template}", FOOGALLERY_VERSION, $current_foogallery );
118 wp_enqueue_script( "foogallery-template-{$current_foogallery_template}", $js_location['url'], $js_deps, $js_ver );
119 do_action( 'foogallery_template_enqueue_script', $current_foogallery_template, $js_location['url'] );
120 }
121 }
122
123 //try to include some CSS, but allow template to opt-out based on some condition
124 if ( false !== apply_filters( "foogallery_template_load_css-{$current_foogallery_template}", true, $current_foogallery ) ) {
125 if ( false !== ( $css_location = $loader->locate_file( "gallery-{$current_foogallery_template}.css" ) ) ) {
126 $css_deps = apply_filters( "foogallery_template_css_deps-{$current_foogallery_template}", array(), $current_foogallery );
127 $css_ver = apply_filters( "foogallery_template_css_ver-{$current_foogallery_template}", FOOGALLERY_VERSION, $current_foogallery );
128 foogallery_enqueue_style( "foogallery-template-{$current_foogallery_template}", $css_location['url'], $css_deps, $css_ver );
129 }
130 }
131 }
132
133 //finally include the actual php template!
134 if ( $template_location ) {
135 do_action( 'foogallery_loaded_template_before', $current_foogallery );
136 $this->add_style_block( $current_foogallery, $current_foogallery_template );
137 $this->load_gallery_template( $current_foogallery, $template_location['path'] );
138 do_action( 'foogallery_loaded_template_after', $current_foogallery );
139 }
140
141 //cater for lightbox extensions needing to add styles and javascript
142 $lightbox = foogallery_gallery_template_setting_lightbox();
143 if ( !empty( $lightbox ) ) {
144 do_action( "foogallery_template_lightbox-{$lightbox}", $current_foogallery );
145 }
146
147 //we have loaded all files, now let extensions do some stuff
148 do_action( "foogallery_loaded_template", $current_foogallery );
149 do_action( "foogallery_loaded_template-($current_foogallery_template)", $current_foogallery );
150 } else {
151 //we could not find a template!
152 esc_html_e( 'No gallery layout found!', 'foogallery' );
153 }
154 }
155
156 if ( apply_filters( 'foogallery_render_template_clear_globals', true ) ) {
157 //cleanup globals in case there are multiple galleries on a page
158 $current_foogallery = null;
159 $current_foogallery_arguments = null;
160 $current_foogallery_template = null;
161 }
162 }
163
164 /**
165 * Replaces the first occurrence of a string
166 *
167 * @param $search
168 * @param $replace
169 * @param $subject
170 *
171 * @return string|string[]
172 */
173 function str_replace_first($search, $replace, $subject) {
174 $pos = strpos($subject, $search);
175 if ($pos !== false) {
176 return substr_replace($subject, $replace, $pos, strlen($search));
177 }
178 return $subject;
179 }
180
181 /***
182 * Loads a gallery template location and wraps the calls so that it can be intercepted
183 *
184 * @param FooGallery $gallery
185 * @param string $template_location
186 */
187 function load_gallery_template($gallery, $template_location) {
188
189 $override_load_template = apply_filters( 'foogallery_load_gallery_template', false, $gallery, $template_location );
190
191 if ( $override_load_template ) {
192 //if we have overridden the loading of the template, then we can exit without doing anything further
193 return;
194 }
195
196 //if we get to this point, then we need to load the template as per normal
197 load_template( $template_location, false );
198 }
199
200 /**
201 * Creates a locator instance used for including template files
202 *
203 *
204 */
205 public function create_locator_instance() {
206 $instance_name = FOOGALLERY_SLUG . '_gallery_templates';
207 $loader = new Foo_Plugin_File_Locator_v1( $instance_name, FOOGALLERY_FILE, 'templates', FOOGALLERY_SLUG );
208
209 //allow extensions to very easily add pickup locations for their files
210 $this->add_extension_pickup_locations( $loader, apply_filters( $instance_name . '_files', array() ) );
211
212 return $loader;
213 }
214
215 /**
216 * Add pickup locations to the loader to make it easier for extensions
217 *
218 * @param $loader Foo_Plugin_File_Locator_v1
219 * @param $extension_files array
220 */
221 function add_extension_pickup_locations( $loader, $extension_files ) {
222 if ( count( $extension_files ) > 0 ) {
223 $position = 120;
224 foreach ( $extension_files as $file ) {
225
226 //add pickup location for php template
227 $loader->add_location( $position, array(
228 'path' => trailingslashit( plugin_dir_path( $file ) ),
229 'url' => trailingslashit( plugin_dir_url( $file ) )
230 ) );
231
232 $position++;
233
234 //add pickup location for extensions js folder
235 $loader->add_location( $position, array(
236 'path' => trailingslashit( plugin_dir_path( $file ) . 'js' ),
237 'url' => trailingslashit( plugin_dir_url( $file ) . 'js' )
238 ) );
239
240 $position++;
241
242 //add pickup location for extension css folder
243 $loader->add_location( $position, array(
244 'path' => trailingslashit( plugin_dir_path( $file ) . 'css' ),
245 'url' => trailingslashit( plugin_dir_url( $file ) . 'css' )
246 ) );
247
248 $position++;
249
250 }
251 }
252 }
253
254 /**
255 * load the gallery based on either the id or slug, passed in via arguments
256 *
257 * @param $args array Arguments passed in from the shortcode
258 *
259 * @return bool|FooGallery The gallery object we want to render
260 */
261 function find_gallery( $args ) {
262
263 $id = intval( $this->get_arg( $args, 'id' ), 0 );
264 $gallery = $this->get_arg( $args, 'gallery', 0 );
265
266 if ( $id > 0 ) {
267 //load gallery by ID
268 return FooGallery::get_by_id( $id );
269 }
270
271 //take into account the cases where id is passed in via the 'gallery' attribute
272 if ( intval( $gallery ) > 0 ) {
273 //we have an id, so load
274 return FooGallery::get_by_id( intval( $gallery ) );
275 } else if ( !empty( $gallery ) ) {
276 //we are dealing with a slug
277 return FooGallery::get_by_slug( $gallery );
278 }
279
280 //if we get here then we have no id or gallery attribute, so try to build a dynamic gallery
281
282 //we can only build up a dynamic gallery if attachment_ids are passed in
283 $attachment_ids = $this->get_arg( $args, 'attachment_ids', false );
284
285 if ( $attachment_ids ) {
286 $template = $this->get_arg( $args, 'template', foogallery_get_default( 'gallery_template' ) );
287
288 if ( !is_array( $attachment_ids ) ) {
289 $attachment_ids = explode( ',', $attachment_ids);
290 }
291 return FooGallery::dynamic( $template, $attachment_ids );
292 }
293
294 //allow for other forms of dynamic galleries (e.g. for other datasources)
295 return apply_filters('foogallery_build_dynamic_gallery', false, $args);
296 }
297
298 /**
299 * Helper to get an argument value from an array arguments
300 *
301 * @param $args Array the array of arguments to search
302 * @param $key string the key of the argument you are looking for
303 * @param $default string a default value if the argument is not found
304 *
305 * @return string
306 */
307 function get_arg( $args, $key, $default = '' ) {
308 if ( empty($args) || ! array_key_exists( $key, $args ) ) {
309 return $default;
310 }
311
312 return $args[ $key ];
313 }
314
315 /**
316 * Add a style block just before the gallery based on the template
317 *
318 * @param $gallery FooGallery
319 * @param $template string
320 */
321 function add_style_block( $gallery, $template ) {
322 $css = array();
323 $css = apply_filters( "foogallery_template_style_block-{$template}", $css, $gallery );
324
325 if ( !empty( $css ) ) {
326 // @formatter:off
327 ?>
328 <style type="text/css">
329 <?php echo implode( "\n", $css ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
330 </style>
331 <?php
332 // @formatter:on
333 }
334 }
335 }
336