PluginProbe
Meow Gallery / 4.2.8
Meow Gallery v4.2.8
5.5.5 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 All 157 releases
meow-gallery / classes / builders / core.php

core.php in Meow Gallery 4.2.8, at classes/builders/core.php

176 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 abstract class Meow_MGL_Builders_Core {
4
5 public $id = null;
6 public $layout = 'none';
7 public $class_id = 'mgl-gallery-none';
8 public $img_class = '';
9 public $size = 'large';
10 public $customClass = '';
11 public $align = null;
12 public $ids = array();
13 public $link = null;
14 public $atts = array();
15 public $data = array();
16 public $isPreview = false;
17 public $updir = null;
18 public $captions = false;
19 public $animation = null;
20
21 abstract function inline_css();
22
23 public function __construct( $atts, $infinite, $isPreview = false ) {
24 $wpUploadDir = wp_upload_dir();
25 $options = get_option( Meow_MGL_Core::get_plugin_option_name(), null );
26 $this->id = uniqid();
27 $this->size = isset( $atts['size'] ) ? $atts['size'] : $this->size;
28 $this->size = apply_filters( 'mgl_media_size', $this->size );
29 $this->infinite = $infinite;
30 $this->atts = $atts;
31 $this->customClass = isset( $atts['custom-class'] ) ? $atts['custom-class'] : null;
32 $this->link = isset( $atts['link'] ) ? $atts['link'] : null;
33 $this->align = isset( $atts['align'] ) ? $atts['align'] : $this->align;
34 $this->isPreview = $isPreview;
35 $this->class_id = 'mgl-gallery-' . $this->id;
36 $this->updir = trailingslashit( $wpUploadDir['baseurl'] );
37 $this->captions = isset( $atts['captions'] ) ? $atts['captions'] : ( $options['captions'] ?? 'none' );
38 $this->animation = null;
39 if ( isset( $atts['animation'] ) && $atts['animation'] != 'default' )
40 $this->animation = $atts['animation'];
41 else
42 $this->animation = $options['animation'] ?? null;
43 }
44
45 function build_inline_attributes( $id, $data ) {
46 return '';
47 }
48
49 function prepare_data( $idsStr ) {
50 global $wpdb;
51
52 // Escape the array of IDs for SQL
53 $ids = array_map( 'intval', explode(',', $idsStr ) );
54 $idsStr = implode( ',', $ids );
55
56 $query = "SELECT p.ID id, p.post_excerpt caption, m.meta_value meta
57 FROM $wpdb->posts p, $wpdb->postmeta m
58 WHERE m.meta_key = '_wp_attachment_metadata'
59 AND p.ID = m.post_id
60 AND p.ID IN (" . $idsStr . ")";
61 $res = $wpdb->get_results( $query );
62 $this->ids = explode( ',', $idsStr );
63 foreach ( $res as $r ) {
64 $this->data[$r->id] = array( 'caption' => $r->caption,'meta' => unserialize( $r->meta ) );
65 }
66 $this->ids = array_reverse( $this->ids );
67 $cleanIds = array();
68 foreach ( $this->ids as $id ) {
69 if ( isset( $this->data[$id] ) )
70 array_push( $cleanIds, $id );
71 }
72 $this->ids = apply_filters( 'mgl_sort', $cleanIds, $this->data, $this->layout, $this->atts );
73 }
74
75 function build_next_cell( $id, $data ) {
76 $src = $this->updir . $data['meta']['file'];
77 $data['caption'] = apply_filters( 'mgl_caption', $data['caption'], $id );
78 $caption = $this->captions ? $data['caption'] : '';
79
80 $image_size = Meow_MGL_Core::get_plugin_option( 'image_size', 'srcset' );
81 $imgSrc = null;
82 if ( empty( $image_size ) || $image_size === 'srcset' ) {
83 $imgSrc = wp_get_attachment_image( $id, $this->size, false,
84 $this->layout === 'carousel' ?
85 [ 'class' => 'skip-lazy' ] :
86 [ 'class' => 'wp-image-' . $id ],
87 );
88 }
89 else {
90 $info = wp_get_attachment_image_src( $id, $image_size );
91 $imgSrc = '<img loading="lazy" src="' . $info[0] . '" class="' .
92 ( $this->layout === 'carousel' ? 'skip-lazy' : ( 'wp-image-' . $id ) ) . '" />';
93 }
94 $attributes = $this->build_inline_attributes( $id, $data );
95 if ( !empty( $attributes ) ) {
96 $attributes = ' ' . $attributes;
97 }
98
99 $linkUrl = null;
100 if ( $this->link === 'attachment' )
101 $linkUrl = get_permalink( (int)$id );
102 else if ( $this->link === 'media' || $this->link === 'file' )
103 $linkUrl = $src;
104 $linkUrl = apply_filters( 'mgl_link', $linkUrl, (int)$id, $data );
105 $isPreview = $this->isPreview;
106 ob_start();
107 include dirname( __FILE__ ) . '/cell.tpl.php';
108 $html = ob_get_clean();
109 return $html;
110 }
111
112 function build_container_classes() {
113 $classes = 'mgl-' . $this->layout . '-container';
114
115 // Align
116 $classes .= $this->align ? (' align' . $this->align) : '';
117
118 return $classes;
119 }
120
121 function build_classes() {
122 $classes = 'mgl-gallery';
123
124 // Layout
125 $classes .= ' mgl-' . $this->layout;
126
127 // Custom Class
128 $classes .= $this->customClass ? ( ' ' . $this->customClass ) : '';
129
130 // Animation
131 if ( $this->animation )
132 $classes .= ' is-animated ' . $this->animation;
133
134 // Captions
135 if ( $this->captions )
136 $classes .= ' captions-' . $this->captions;
137
138 return $classes;
139 }
140
141 function build_styles() {
142 return "";
143 }
144
145 function build( $idsStr ) {
146
147 // Generate gallery
148 $classes = $this->build_classes();
149 $styles = $this->build_styles();
150 $out = "<div id='{$this->class_id}' class='{$classes}' style='{$styles}'>";
151 $this->prepare_data( $idsStr );
152 while ( count( $this->ids ) > 0 ) {
153 $id = array_pop( $this->ids );
154 $out .= $this->build_next_cell( $id, $this->data[$id] );
155 }
156 $out .= '</div>';
157 $out = apply_filters( 'mgl_gallery_written', $out, $this->layout );
158
159 // Generate gallery container
160 $container_classes = $this->build_container_classes();
161 $inline_css = $this->inline_css();
162 if ( !empty( $inline_css ) ) {
163 $inline_css = preg_replace( "/\r|\n/", "", $inline_css );
164 $container = "<div class='{$container_classes}'>{$inline_css}{$out}</div>";
165 }
166 else {
167 $container = "<div class='{$container_classes}'>{$out}</div>";
168 }
169
170 return $container;
171 }
172
173 }
174
175 ?>
176