PluginProbe
Easy Image Gallery / trunk
Easy Image Gallery vtrunk
1.0.4 1.0.5 1.0.6 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 trunk 1.0.1 1.0.2 All 26 releases
easy-image-gallery / includes / template-functions.php

template-functions.php in Easy Image Gallery trunk, at includes/template-functions.php

561 lines 15.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Template functions
4 *
5 * @since 1.0
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly.
9
10 function easy_image_gallery_db_version(){
11 $old_meta_structure = get_post_meta(get_the_ID(), '_easy_image_gallery', true);
12 $new_meta_structure = get_post_meta(get_the_ID(), '_easy_image_gallery_v2', true);
13
14 if (isset($new_meta_structure) && $new_meta_structure != null) {
15 return "new";
16 }elseif( isset($old_meta_structure) && $old_meta_structure != null ) {
17 return "old";
18 }
19
20 return;
21 }
22
23 /**
24 * Check the POST META
25 */
26 function easy_image_gallery_get_post_meta(){
27 $db_version = easy_image_gallery_db_version();
28 if ( $db_version == "new" ) {
29 $gallery_ids = get_post_meta(get_the_ID(), '_easy_image_gallery_v2', true);
30 return $gallery_ids;
31 } elseif ( $db_version == "old" ) {
32 $get_gallery_attachments = get_post_meta(get_the_ID(), '_easy_image_gallery', true);
33 $get_gallery_old_data = explode(",", $get_gallery_attachments);
34
35 $get_open_images = get_post_meta(get_the_ID(), '_easy_image_gallery_link_images');
36 if ( isset($get_open_images) && !empty($get_open_images) ){
37 $get_open_images = $get_open_images;
38 }else{
39 $get_open_images = null;
40 }
41
42 $gallery_ids = array(array(
43 "SHORTCODE" => wp_rand(100, 999),
44 "DATA" => $get_gallery_old_data,
45 "OPEN_IMAGES" => $get_open_images[0],
46 ));
47
48 return $gallery_ids;
49 }
50
51 return;
52 }
53 /**
54 * Is gallery
55 *
56 * @since 1.0
57 * @return boolean
58 */
59 function easy_image_gallery_is_gallery() {
60 $gallery_ids = easy_image_gallery_get_post_meta();
61 // Checking if we have EIG (Easy Image Gallery) Gutenberg Blocks in the post content
62 $gutenberg_galleries = easy_image_gallery_if_gutenberg_block();
63
64 if ( ! empty( $gallery_ids ) || ! empty( $gutenberg_galleries ) ) {
65 return true;
66 }
67
68 return false;
69 }
70
71 /**
72 * Get page images ids
73 *
74 * @since 1.3
75 * @return array
76 */
77 function easy_image_gallery_get_image_ids( $post_id = null, $all_galleries_images = true, $gallery_id = null ) {
78 global $post;
79
80 if ( $post_id == null ) {
81 $post_id = $post->ID;
82 }
83
84 if( ! isset( $post_id ) || $post_id == null ) return;
85
86 $db_version = easy_image_gallery_db_version();
87 if ( $db_version == "new" ) {
88
89 $new_db_structure = get_post_meta(get_the_ID(), '_easy_image_gallery_v2', true);
90 if ( $all_galleries_images == true ){
91
92 $images_ids = array();
93 if( isset( $new_db_structure ) && !empty( $new_db_structure ) ){
94 foreach( $new_db_structure as $gallery ){
95 foreach( $gallery['DATA'] as $image ){
96 $images_ids[] = $image;
97 }
98 }
99 }
100
101 return $images_ids;
102
103 }elseif( $all_galleries_images == false ){
104
105 if ( isset( $gallery_id ) && !empty( $gallery_id ) ){
106
107 if( isset( $new_db_structure ) && !empty( $new_db_structure ) ){
108 foreach( $new_db_structure as $gallery ){
109 if ( (string) easy_image_gallery_sanitize_gallery_id( $gallery['SHORTCODE'] ) === (string) easy_image_gallery_sanitize_gallery_id( $gallery_id ) ) {
110 return $gallery['DATA'];
111 }
112 }
113 }
114
115 }
116
117 }
118
119 }elseif ( $db_version == "old" ) {
120 $old_db_structure = get_post_meta(get_the_ID(), '_easy_image_gallery', true);
121 $attachment_ids = explode( ',', $old_db_structure );
122
123 return array_filter( $attachment_ids );
124 }
125
126 return;
127 }
128
129 /**
130 * Check the current post for the existence of a short code
131 *
132 * @since 1.0
133 * @return boolean
134 */
135 function easy_image_gallery_has_shortcode( $shortcode = '' ) {
136 global $post;
137
138 // false because we have to search through the post content first
139 $found = false;
140
141 // if no short code was provided, return false
142 if ( !$shortcode ) {
143 return $found;
144 }
145
146 if ( is_object( $post ) && stripos( $post->post_content, '[' . $shortcode ) !== false ) {
147 // we have found the short code
148 $found = true;
149 }
150
151 // return our final results
152 return $found;
153 }
154 /**
155 * Check the current post for the existence of a gutenberg block
156 *
157 * @since 1.0
158 * @return boolean
159 */
160 function easy_image_gallery_has_block( $block = '' ) {
161 global $post;
162
163 // false because we have to search through the post content first
164 $found = false;
165
166 // if no short code was provided, return false
167 if ( !$block ) {
168 return $found;
169 }
170 if ( is_object( $post ) && stripos( $post->post_content, '<ul class="' . $block ) !== false ) {
171 // we have found the short code
172 $found = true;
173 }
174
175 // return our final results
176 return $found;
177 }
178
179
180 /**
181 * Setup Lightbox array
182 *
183 * @since 1.0
184 * @return array
185 */
186 function easy_image_gallery_lightbox() {
187
188 $lightboxes = array(
189 'fancybox' => __( 'fancyBox', 'easy-image-gallery' ),
190 'prettyphoto' => __( 'prettyPhoto', 'easy-image-gallery' ),
191 'luminous' => __( 'Luminous', 'easy-image-gallery' ),
192 );
193
194 return apply_filters( 'easy_image_gallery_lightbox', $lightboxes );
195
196 }
197
198 /**
199 * Get lightbox from settings
200 *
201 * @since 1.0
202 * @return string
203 */
204
205 if ( !function_exists( 'easy_image_gallery_get_lightbox' ) ) :
206 function easy_image_gallery_get_lightbox() {
207
208 $settings = (array) get_option( 'easy-image-gallery' );
209
210 // set fancybox as default for when the settings page hasn't been saved
211 $lightbox = isset( $settings['lightbox'] ) ? esc_attr( $settings['lightbox'] ) : 'prettyphoto';
212
213 return $lightbox;
214
215 }
216 endif;
217
218
219 /**
220 * Sanitize a gallery/shortcode ID for safe use in HTML attributes and selectors.
221 *
222 * Gallery IDs are numeric (see wp_rand-generated SHORTCODE values in the metabox).
223 *
224 * @since 1.5.5
225 * @param mixed $gallery_id Gallery identifier.
226 * @return string Digits-only ID, or empty string when invalid.
227 */
228 function easy_image_gallery_sanitize_gallery_id( $gallery_id ) {
229 if ( null === $gallery_id || '' === $gallery_id || 'old_db' === $gallery_id ) {
230 return '';
231 }
232
233 $sanitized_gallery_id = preg_replace( '/[^0-9]/', '', (string) $gallery_id );
234
235 if ( '' === $sanitized_gallery_id ) {
236 return '';
237 }
238
239 return $sanitized_gallery_id;
240 }
241
242 /**
243 * Returns the correct rel attribute for the anchor links
244 *
245 * @since 1.0
246 * @return string
247 */
248 function easy_image_gallery_lightbox_rel( $gallery_id = null ) {
249
250 $gallery_id = easy_image_gallery_sanitize_gallery_id( $gallery_id );
251 $lightbox = easy_image_gallery_get_lightbox();
252
253 switch ( $lightbox ) {
254
255 case 'prettyphoto':
256
257 $rel = 'rel="' . esc_attr( 'prettyPhoto[group-' . $gallery_id . ']' ) . '"';
258
259 break;
260
261 case 'fancybox':
262
263 $rel = 'data-fancybox="' . esc_attr( 'gallery' . $gallery_id ) . '"';
264
265 break;
266
267 case 'luminous':
268
269 $rel = 'rel="' . esc_attr( 'luminous[group-' . $gallery_id . ']' ) . '"';
270
271 break;
272
273 default:
274
275 $rel = 'rel="' . esc_attr( 'prettyPhoto[group-' . $gallery_id . ']' ) . '"';
276 }
277
278 return $rel;
279 }
280
281 /**
282 * Get list of post types for populating the checkboxes on the admin page
283 *
284 * @since 1.0
285 * @return array
286 */
287 function easy_image_gallery_get_post_types() {
288
289 $args = array(
290 'public' => true
291 );
292
293 $post_types = array_map( 'ucfirst', get_post_types( $args ) );
294
295 // remove attachment
296 unset( $post_types[ 'attachment' ] );
297
298 return apply_filters( 'easy_image_gallery_get_post_types', $post_types );
299
300 }
301
302 /**
303 * Retrieve the allowed post types from the option row
304 * Defaults to post and page when the settings have not been saved
305 *
306 * @return array
307 * @since 1.0
308 */
309 function easy_image_gallery_allowed_post_types() {
310
311 $defaults['post_types']['post'] = 'on';
312 $defaults['post_types']['page'] = 'on';
313
314 // get the allowed post type from the DB
315 $settings = ( array ) get_option( 'easy-image-gallery', $defaults );
316 $post_types = isset( $settings['post_types'] ) ? $settings['post_types'] : '';
317
318 // post types don't exist, bail
319 if ( ! $post_types )
320 return;
321
322 return $post_types;
323
324 }
325
326
327 /**
328 * Is the currently viewed post type allowed?
329 * For use on the front-end when loading scripts etc
330 *
331 * @since 1.0
332 * @return boolean
333 */
334 function easy_image_gallery_allowed_post_type() {
335
336 // post and page defaults
337 $defaults['post_types']['post'] = 'on';
338 $defaults['post_types']['page'] = 'on';
339
340 // get currently viewed post type
341 $post_type = ( string ) get_post_type();
342
343 //echo $post_type; exit; // download
344
345 // get the allowed post type from the DB
346 $settings = ( array ) get_option( 'easy-image-gallery', $defaults );
347 $post_types = isset( $settings['post_types'] ) ? $settings['post_types'] : '';
348
349 // post types don't exist, bail
350 if ( ! $post_types )
351 return;
352
353 // check the two against each other
354 if ( array_key_exists( $post_type, $post_types ) )
355 return true;
356 }
357
358
359 /**
360 * Retrieve attachment IDs
361 *
362 * @since 1.0
363 * @return string
364 */
365 function easy_image_gallery_get_galleries() {
366 global $post;
367
368 if( !isset( $post->ID) ){
369 return;
370 }
371
372 $gallery_ids = easy_image_gallery_get_post_meta();
373
374 return $gallery_ids;
375 }
376
377
378 /**
379 * Shortcode
380 *
381 * @since 1.0
382 */
383
384 function easy_image_gallery_shortcode( $atts ) {
385
386 // return early if the post type is not allowed to have a gallery
387 if ( ! easy_image_gallery_allowed_post_type() ) {
388 return '';
389 }
390
391 $atts = shortcode_atts(
392 array(
393 'gallery' => '',
394 ),
395 $atts,
396 'easy_image_gallery'
397 );
398
399 if ( ! empty( $atts['gallery'] ) ) {
400 $gallery_id = easy_image_gallery_sanitize_gallery_id( $atts['gallery'] );
401 if ( '' !== $gallery_id ) {
402 return easy_image_gallery( $gallery_id );
403 }
404 return '';
405 }
406
407 return easy_image_gallery( 'old_db' );
408 }
409 add_shortcode( 'easy_image_gallery', 'easy_image_gallery_shortcode' );
410
411
412 /**
413 * Count number of images in array
414 *
415 * @since 1.0
416 * @return integer
417 */
418 function easy_image_gallery_count_images( $gallery_shortcode ) {
419
420 $galleries = easy_image_gallery_get_post_meta();
421
422 if ( isset($galleries) && !empty($galleries) ) {
423 foreach ( $galleries as $gallery ){
424 if ( (string) easy_image_gallery_sanitize_gallery_id( $gallery['SHORTCODE'] ) === (string) easy_image_gallery_sanitize_gallery_id( $gallery_shortcode ) ) {
425 $number = count($gallery['DATA']);
426 return $number;
427 }
428 }
429 }
430
431 return;
432 }
433
434
435 /**
436 * Output gallery
437 *
438 * @since 1.0
439 */
440 function easy_image_gallery( $gallery_id = 'old_db' ) {
441
442 $galleries = easy_image_gallery_get_galleries();
443 global $post;
444
445 if ( isset($galleries) && !empty($galleries) ){
446 ob_start();
447 foreach ($galleries as $gallery){
448
449 $current_gallery_id = $gallery_id;
450
451 if ( 'old_db' === $current_gallery_id ) {
452 $candidate = easy_image_gallery_sanitize_gallery_id( $gallery['SHORTCODE'] );
453 if ( '' === $candidate ) {
454 // Couldn't derive a valid numeric ID from this gallery; try the next one.
455 continue;
456 }
457
458 $current_gallery_id = $candidate;
459 } else {
460 $current_gallery_id = easy_image_gallery_sanitize_gallery_id( $current_gallery_id );
461 if ( '' === $current_gallery_id ) {
462 continue;
463 }
464 }
465
466 if ( (string) easy_image_gallery_sanitize_gallery_id( $gallery['SHORTCODE'] ) === (string) $current_gallery_id ) {
467 $gallery_exist = true;
468
469 $has_gallery_images = $gallery['DATA'];
470
471 if ( !$has_gallery_images )
472 return;
473
474 // clean the array (remove empty values)
475 $has_gallery_images = array_filter( $has_gallery_images );
476
477 // css classes array
478 $classes = array();
479
480 // thumbnail count
481 $classes[] = $has_gallery_images ? 'thumbnails-' . easy_image_gallery_count_images( $gallery['SHORTCODE'], $gallery ) : '';
482
483 // linked images
484 if ( isset($gallery['OPEN_IMAGES']) && $gallery['OPEN_IMAGES'] == 'on' ){
485 $classes[] = 'linked';
486 }
487
488 $classes = implode( ' ', $classes );
489 if ( isset($has_gallery_images) && !empty($has_gallery_images) ) {
490 ?>
491 <ul class="easy-image-gallery <?php echo esc_attr( $classes ); ?>">
492 <?php
493 foreach ( $has_gallery_images as $attachment_id ) {
494 $classes = array( 'eig-popup' );
495
496 // get original image
497 $image_link = wp_get_attachment_image_src( $attachment_id, apply_filters( 'easy_image_gallery_linked_image_size', 'large' ) );
498 $image_link = $image_link[0];
499
500 $image = wp_get_attachment_image( $attachment_id, apply_filters( 'easy_image_gallery_thumbnail_image_size', 'thumbnail' ), '', array( 'alt' => trim( wp_strip_all_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ) ) );
501
502 $image_caption = get_post( $attachment_id )->post_excerpt ? esc_attr( get_post( $attachment_id )->post_excerpt ) : '';
503
504 $image_class = esc_attr( implode( ' ', $classes ) );
505
506 $lightbox = easy_image_gallery_get_lightbox();
507
508 $rel = easy_image_gallery_lightbox_rel( $current_gallery_id );
509
510 if ( isset($gallery['OPEN_IMAGES']) && $gallery['OPEN_IMAGES'] == 'on' )
511 $html = sprintf( '<li><a %s href="%s" class="%s" title="%s" data-caption="%s" target="_blank"><i class="icon-view"></i><span class="overlay"></span>%s</a></li>', $rel, esc_url( $image_link ), $image_class, $image_caption, $image_caption, $image );
512 else
513 $html = sprintf( '<li>%s</li>', $image );
514
515 echo wp_kses_post( apply_filters( 'easy_image_gallery_html', $html, $rel, $image_link, $image_class, $image_caption, $image, $attachment_id, $post->ID ) );
516 }
517 echo '</ul>';
518
519 if ( easy_image_gallery_get_lightbox() === 'luminous' ) {
520 $luminous_selector = sprintf( "a[rel='luminous[group-%s]']", easy_image_gallery_sanitize_gallery_id( $current_gallery_id ) );
521 printf(
522 '<script>new LuminousGallery(document.querySelectorAll(%s));</script>',
523 wp_json_encode( $luminous_selector )
524 );
525 }
526 }
527 }
528 }
529
530 $eig_gallery = ob_get_clean();
531 return apply_filters( 'easy_image_gallery', $eig_gallery );
532 }
533 }
534
535 /**
536 * Append gallery images to page automatically
537 *
538 * @since 1.0
539 */
540 function easy_image_gallery_append_to_content( $content ) {
541 // if it is single page and supported post_type and page not have shortcode.
542 if ( is_singular() && is_main_query() && easy_image_gallery_allowed_post_type() && !easy_image_gallery_has_shortcode('easy_image_gallery') && ! easy_image_gallery_has_block('wp-block-devrix-easy-image-gallery-block') ) {
543 $new_content = easy_image_gallery( 'old_db' );
544 $content .= $new_content;
545 }
546
547 return $content;
548 }
549 add_filter( 'the_content', 'easy_image_gallery_append_to_content' );
550
551 /**
552 * Remove the_content filter if shortcode is detected on page
553 *
554 * @since 1.0
555 */
556 function easy_image_gallery_template_redirect() {
557 if ( easy_image_gallery_has_shortcode( 'easy_image_gallery' ) || easy_image_gallery_has_block('wp-block-devrix-easy-image-gallery-block') )
558 remove_filter( 'the_content', 'easy_image_gallery_append_to_content' );
559 }
560 add_action( 'template_redirect', 'easy_image_gallery_template_redirect' );
561