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