PluginProbe
Meow Gallery / 4.2.2
Meow Gallery v4.2.2
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.2.2, at classes/core.php

132 lines 4.4 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 public function can_access_settings() {
26 return apply_filters( 'mgl_allow_setup', current_user_can( 'manage_options' ) );
27 }
28
29 public function can_access_features() {
30 return apply_filters( 'mgl_allow_usage', current_user_can( 'upload_files' ) );
31 }
32
33 // Use by the Gutenberg block
34
35 // Rewrite the sizes attributes of the src-set for each image
36 function wp_get_attachment_image_attributes( $attr, $attachment, $size ) {
37 if (!$this->gallery_process)
38 return $attr;
39 $sizes = null;
40 if ( $this->gallery_layout === 'tiles' )
41 $sizes = '50vw';
42 else if ( $this->gallery_layout === 'masonry' )
43 $sizes = '50vw';
44 else if ( $this->gallery_layout === 'square' )
45 $sizes = '33vw';
46 else if ( $this->gallery_layout === 'cascade' )
47 $sizes = '80vw';
48 else if ( $this->gallery_layout === 'justified' )
49 $sizes = '(max-width: 800px) 80vw, 50vw';
50 $sizes = apply_filters( 'mgl_sizes', $sizes, $this->gallery_layout, $attachment, $attr );
51 if ( !empty( $sizes ) )
52 $attr['sizes'] = $sizes;
53 return $attr;
54 }
55
56 function gallery( $atts, $isPreview = false ) {
57 $atts = apply_filters( 'shortcode_atts_gallery', $atts, null, $atts );
58
59 // Get the IDs
60 $images = array();
61 if ( isset( $atts['ids'] ) )
62 $images = $atts['ids'];
63 if ( isset( $atts['include'] ) ) {
64 $images = is_array( $atts['include'] ) ? implode( ',', $atts['include'] ) : $atts['include'];
65 $atts['include'] = $images;
66 }
67 if ( empty( $images ) ) {
68 $attachments = get_attached_media( 'image' );
69 $attachmentIds = array_map( function($x) { return $x->ID; }, $attachments );
70 if ( !empty( $attachmentIds ) )
71 $images = implode( ',', $attachmentIds );
72 else
73 return "<p class='meow-error'><b>Meow Gallery:</b> The gallery is empty.</p>";
74 }
75
76 if ( $isPreview ) {
77 $check = explode( ',', $images );
78 $check = array_slice( $check, 0, 40 );
79 $images = implode( ',', $check );
80 }
81
82 // Ordering
83 if ( isset( $atts['orderby'] ) ) {
84 $images = explode( ',', $images );
85 $images = Meow_MGL_OrderBy::run( $images, $atts['orderby'], isset( $atts['order'] ) ? $atts['order'] : 'asc' );
86 $images = implode( ',', $images );
87 }
88
89 // Layout
90 $layout = 'none';
91 if ( isset( $atts['layout'] ) && $atts['layout'] != 'default' )
92 $layout = $atts['layout'];
93 else if ( isset( $atts['mgl-layout'] ) && $atts['mgl-layout'] != 'default' )
94 $layout = $atts['mgl-layout'];
95 else
96 $layout = get_option( 'mgl_layout', 'tiles' );
97
98 // Check the settings
99 if ( $layout === 'none' )
100 return gallery_shortcode( $atts );
101 $layoutClass = 'Meow_MGL_Builders_' . ucfirst( $layout );
102 if ( !class_exists( $layoutClass ) ) {
103 error_log( "Meow Gallery: Class $layoutClass does not exist." );
104 return "<p class='meow-error'><b>Meow Gallery:</b> The layout $layout is not available in this version.</p>";
105 }
106
107 // Captions
108 if ( isset( $atts['captions'] ) && ( $atts['captions'] === false || $atts['captions'] === 'false' ) ) {
109 // This is to avoid issues linked to the old block editor for the Meow Gallery
110 $atts['captions'] = 'none';
111 }
112
113 //DEBUG: Display $atts
114 //error_log( print_r( $atts, 1 ) );
115
116 // Start the process of building the gallery
117 $this->gallery_process = true;
118 $this->gallery_layout = $layout;
119 wp_enqueue_style( 'mgl-css' );
120 $infinite = get_option( 'mgl_infinite', false ) && class_exists( 'MeowPro_MGL_Core' );
121 $gen = new $layoutClass( $atts, !$isPreview && $infinite, $isPreview );
122 $result = $gen->build( $images );
123 $this->gallery_process = false;
124 do_action( 'mgl_' . $layout . '_gallery_created', $layout );
125 //$result = apply_filters( 'post_gallery', $result, $atts, null );
126
127 return $result;
128 }
129 }
130
131 ?>
132