PluginProbe
Meow Gallery / 4.1.6
Meow Gallery v4.1.6
5.5.4 5.5.3 5.5.2 5.5.1 5.5.0 5.4.9 5.4.8 5.4.7 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 4.3.1 All 156 releases
meow-gallery / classes / core.php

core.php in Meow Gallery 4.1.6, at classes/core.php

124 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Meow_MGL_Core {
4
5 private $gallery_process = false;
6 private $is_gallery_used = true; // TODO: Would be nice to detect if the gallery is actually used on the current page.
7
8 public function __construct() {
9 load_plugin_textdomain( MGL_DOMAIN, false, MGL_PATH . '/languages' );
10
11 // Initializes the classes needed
12 MeowCommon_Helpers::is_rest() && new Meow_MGL_Rest( $this );
13 is_admin() && new Meow_MGL_Admin();
14 class_exists( 'MeowPro_MGL_Core' ) && new MeowPro_MGL_Core();
15
16 // The gallery build process should only be enabled if the request is non-asynchronous
17 if ( !MeowCommon_Helpers::is_asynchronous_request() ) {
18 add_filter( 'wp_get_attachment_image_attributes', array( $this, 'wp_get_attachment_image_attributes' ), 25, 3 );
19 if ( is_admin() || $this->is_gallery_used ) {
20 new Meow_MGL_Run( $this );
21 }
22 }
23 }
24
25 // Use by the Gutenberg block
26
27 // Rewrite the sizes attributes of the src-set for each image
28 function wp_get_attachment_image_attributes( $attr, $attachment, $size ) {
29 if (!$this->gallery_process)
30 return $attr;
31 $sizes = null;
32 if ( $this->gallery_layout === 'tiles' )
33 $sizes = '50vw';
34 else if ( $this->gallery_layout === 'masonry' )
35 $sizes = '50vw';
36 else if ( $this->gallery_layout === 'square' )
37 $sizes = '33vw';
38 else if ( $this->gallery_layout === 'cascade' )
39 $sizes = '80vw';
40 else if ( $this->gallery_layout === 'justified' )
41 $sizes = '(max-width: 800px) 80vw, 50vw';
42 $sizes = apply_filters( 'mgl_sizes', $sizes, $this->gallery_layout, $attachment, $attr );
43 if ( !empty( $sizes ) )
44 $attr['sizes'] = $sizes;
45 return $attr;
46 }
47
48 function gallery( $atts, $isPreview = false ) {
49 $atts = apply_filters( 'shortcode_atts_gallery', $atts, null, $atts );
50
51 // Get the IDs
52 $images = array();
53 if ( isset( $atts['ids'] ) )
54 $images = $atts['ids'];
55 if ( isset( $atts['include'] ) ) {
56 $images = is_array( $atts['include'] ) ? implode( ',', $atts['include'] ) : $atts['include'];
57 $atts['include'] = $images;
58 }
59 if ( empty( $images ) ) {
60 $attachments = get_attached_media( 'image' );
61 $attachmentIds = array_map( function($x) { return $x->ID; }, $attachments );
62 if ( !empty( $attachmentIds ) )
63 $images = implode( ',', $attachmentIds );
64 else
65 return "<p class='meow-error'><b>Meow Gallery:</b> The gallery is empty.</p>";
66 }
67
68 if ( $isPreview ) {
69 $check = explode( ',', $images );
70 $check = array_slice( $check, 0, 40 );
71 $images = implode( ',', $check );
72 }
73
74 // Ordering
75 if ( isset( $atts['orderby'] ) ) {
76 $images = explode( ',', $images );
77 $images = Meow_MGL_OrderBy::run( $images, $atts['orderby'], isset( $atts['order'] ) ? $atts['order'] : 'asc' );
78 $images = implode( ',', $images );
79 }
80
81 // Layout
82 $layout = 'none';
83 if ( isset( $atts['layout'] ) && $atts['layout'] != 'default' )
84 $layout = $atts['layout'];
85 else if ( isset( $atts['mgl-layout'] ) && $atts['mgl-layout'] != 'default' )
86 $layout = $atts['mgl-layout'];
87 else
88 $layout = get_option( 'mgl_layout', 'tiles' );
89
90 // Check the settings
91 if ( $layout === 'none' )
92 return gallery_shortcode( $atts );
93 $layoutClass = 'Meow_MGL_Builders_' . ucfirst( $layout );
94 if ( !class_exists( $layoutClass ) ) {
95 error_log( "Meow Gallery: Class $layoutClass does not exist." );
96 return "<p class='meow-error'><b>Meow Gallery:</b> The layout $layout is not available in this version.</p>";
97 }
98
99 // Captions
100 if ( isset( $atts['captions'] ) && ( $atts['captions'] === false || $atts['captions'] === 'false' ) ) {
101 // This is to avoid issues linked to the old block editor for the Meow Gallery
102 $atts['captions'] = 'none';
103 }
104
105 //DEBUG: Display $atts
106 //error_log( print_r( $atts, 1 ) );
107
108 // Start the process of building the gallery
109 $this->gallery_process = true;
110 $this->gallery_layout = $layout;
111 wp_enqueue_style( 'mgl-css' );
112 $infinite = get_option( 'mgl_infinite', false ) && class_exists( 'MeowPro_MGL_Core' );
113 $gen = new $layoutClass( $atts, !$isPreview && $infinite, $isPreview );
114 $result = $gen->build( $images );
115 $this->gallery_process = false;
116 do_action( 'mgl_' . $layout . '_gallery_created', $layout );
117 //$result = apply_filters( 'post_gallery', $result, $atts, null );
118
119 return $result;
120 }
121 }
122
123 ?>
124