PluginProbe
Meow Gallery / 4.2.0
Meow Gallery v4.2.0
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.0, at classes/builders/core.php

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