PluginProbe
Virtue/Ascend/Pinnacle Toolkit / 4.4
Virtue/Ascend/Pinnacle Toolkit v4.4
trunk 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.7 3.8 All 47 releases
virtue-toolkit / gallery.php

gallery.php in Virtue/Ascend/Pinnacle Toolkit 4.4, at gallery.php

496 lines 17.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! class_exists( 'Kadence_Toolkit_Get_Image' ) ) {
3 class Kadence_Toolkit_Get_Image {
4 /**
5 * The singleton instance
6 */
7 static private $instance = null;
8 /**
9 * No initialization allowed
10 */
11 private function __construct() {}
12
13 /**
14 * No cloning allowed
15 */
16 private function __clone() {}
17
18 static public function getInstance() {
19 if(self::$instance == null) {
20 self::$instance = new self;
21 }
22
23 return self::$instance;
24 }
25
26 public function process($id = null, $width = null, $height = null) {
27 // return if no ID
28 if(empty($id)) {
29 return false;
30 }
31 // return with orginal if no width or height set.
32 if(empty($width) && empty($height) ) {
33 return self::toolkit_get_full_image($id);
34 }
35 // Find width or height if one or the other is not set.
36 $org_height = true;
37 if(empty($height) ) {
38 $org_height = false;
39 $image_attributes = wp_get_attachment_image_src( $id, 'full' );
40 $sizes = image_resize_dimensions($image_attributes[1], $image_attributes[2], $width, null, false );
41 $height = $sizes[5];
42 } else if(empty($width) ) {
43 $image_attributes = wp_get_attachment_image_src( $id, 'full' );
44 $sizes = image_resize_dimensions($image_attributes[1], $image_attributes[2], null, $height, false );
45 $width = $sizes[4];
46 }
47 // Now we checked for an ID, made sure the width and height have values lets check if we can make the size at all
48 if ( self::toolkit_image_size_larger_than_original( $id, $width, $height ) ) {
49 return self::toolkit_get_full_image($id);
50 }
51 //Check for jetpack
52 if( class_exists( 'Jetpack' ) && Jetpack::is_module_active( 'photon' ) ) {
53 $args = array( 'resize' => $width . ',' . $height );
54 $image_url = wp_get_attachment_image_url($id, 'full');
55 return array (
56 0 => jetpack_photon_url( $image_url, $args ),
57 1 => $width,
58 2 => $height,
59 3 => self::toolkit_get_srcset_output($id, $image_url, $width, $height),
60 4 => $image_url,
61 5 => $id
62 );
63 } else if( self::toolkit_image_size_already_exists( $id, $width, $height ) ) {
64
65 return self::toolkit_get_image_at_size($id, $width, $height );
66
67 } else if(class_exists( 'Kadence_Image_Processing' )) {
68 // lets process the image
69 $Kadence_Image_Processing = Kadence_Image_Processing::getInstance();
70 $created = $Kadence_Image_Processing->process($id, $width, $height);
71 if($created) {
72 return self::toolkit_get_image_at_size($id, $width, $height );
73 } else {
74 return self::toolkit_get_full_image($id);
75 }
76 } else {
77 return self::toolkit_get_full_image($id);
78 }
79 }
80 public static function toolkit_get_image_srcset($id = null, $url = null, $width = null, $height = null) {
81 if(empty($id) || empty($url) || empty($width) || empty($height)) {
82 return false;
83 }
84
85 $image_meta = self::toolkit_get_image_meta($id);
86 if ( ! $image_meta ) {
87 return false;
88 }
89
90 if(function_exists ( 'wp_calculate_image_srcset') ){
91 $output = wp_calculate_image_srcset(array( $width, $height), $url, $image_meta, $id);
92 } else {
93 $output = '';
94 }
95
96 return $output;
97 }
98 public static function toolkit_get_srcset_output($id = null, $url = null, $width = null, $height = null) {
99 $img_srcset = self::toolkit_get_image_srcset($id, $url, $width, $height);
100 if(!empty($img_srcset) ) {
101 $output = 'srcset="'.esc_attr($img_srcset).'" sizes="(max-width: '.esc_attr($width).'px) 100vw, '.esc_attr($width).'px"';
102 } else {
103 $output = '';
104 }
105 return $output;
106 }
107 public static function toolkit_image_size_larger_than_original($id, $width, $height) {
108 $image_meta = self::toolkit_get_image_meta( $id );
109
110 if ( ! isset( $image_meta['width'] ) || ! isset( $image_meta['height'] ) ) {
111 return true;
112 }
113 if ( $width > $image_meta['width'] || $height > $image_meta['height'] ) {
114 return true;
115 }
116
117 return false;
118 }
119 public static function toolkit_get_full_image($id) {
120 $src = wp_get_attachment_image_src($id, 'full' );
121 // array return.
122 $image = array (
123 0 => $src[0],
124 1 => $src[1],
125 2 => $src[2],
126 3 => self::toolkit_get_srcset_output($id, $src[0], $src[1], $src[2]),
127 4 => $src[0],
128 5 => $id
129 );
130 return $image;
131 }
132 public static function toolkit_get_image_at_size($id, $width, $height) {
133 $size = array(
134 0 => $width,
135 1 => $height
136 );
137 $src = wp_get_attachment_image_src($id, $size );
138 $full = wp_get_attachment_image_url($id, 'full' );
139 // array return.
140 $image = array (
141 0 => $src[0],
142 1 => $src[1],
143 2 => $src[2],
144 3 => self::toolkit_get_srcset_output($id, $src[0], $src[1], $src[2]),
145 4 => $full,
146 5 => $id
147 );
148 return $image;
149 }
150 public static function toolkit_get_image_meta( $id ) {
151 return wp_get_attachment_metadata( $id );
152 }
153 public static function toolkit_image_size_already_exists( $id, $width, $height ) {
154 $image_meta = self::toolkit_get_image_meta( $id );
155 $kip_size_name = self::kip_get_size_name( array( $width, $height ));
156 if(isset( $image_meta['sizes'][ $kip_size_name ] ) ) {
157 return true;
158 } else {
159 return false;
160 }
161 }
162 public static function kip_get_size_name( $size ) {
163 return 'kip-' . $size[0] . 'x' . $size[1];
164 }
165 }
166 }
167
168 function kadence_toolkit_get_image_array($width = null, $height = null, $crop = true, $class = null, $alt = null, $id = null) {
169 if(empty($id)) {
170 $id = get_post_thumbnail_id();
171 }
172 if(!empty($id)) {
173 $Kadence_Toolkit_Get_Image = Kadence_Toolkit_Get_Image::getInstance();
174 $image = $Kadence_Toolkit_Get_Image->process( $id, $width, $height);
175 if(empty($alt)) {
176 $alt = get_post_meta($id, '_wp_attachment_image_alt', true);
177 }
178 $return_array = array(
179 'src' => $image[0],
180 'width' => $image[1],
181 'height' => $image[2],
182 'srcset' => $image[3],
183 'class' => $class,
184 'alt' => $alt,
185 'full' => $image[4],
186 );
187 } else {
188 $return_array = array(
189 'src' => '',
190 'width' => '',
191 'height' => '',
192 'srcset' => '',
193 'class' => '',
194 'alt' => '',
195 'full' => '',
196 );
197 }
198
199 return $return_array;
200 }
201 /**
202 *
203 * Re-create the [gallery] shortcode and use thumbnails styling from kadencethemes
204 *
205 */
206 function kadence_toolkit_shortcode_gallery($attr) {
207 $post = get_post();
208 static $instance = 0;
209 $instance++;
210
211 if (!empty($attr['ids'])) {
212 if (empty($attr['orderby'])) {
213 $attr['orderby'] = 'post__in';
214 }
215 $attr['include'] = $attr['ids'];
216 }
217
218 $output = apply_filters('post_gallery', '', $attr);
219
220 if ($output != '') {
221 return $output;
222 }
223
224 if (isset($attr['orderby'])) {
225 $attr['orderby'] = sanitize_sql_orderby($attr['orderby']);
226 if (!$attr['orderby']) {
227 unset($attr['orderby']);
228 }
229 }
230
231 extract(shortcode_atts(array(
232 'order' => 'ASC',
233 'orderby' => 'menu_order ID',
234 'id' => $post->ID,
235 'itemtag' => '',
236 'icontag' => '',
237 'captiontag' => '',
238 'columns' => 3,
239 'link' => 'file',
240 'size' => 'full',
241 'include' => '',
242 'attachment_page' => 'false',
243 'use_image_alt' => 'false',
244 'gallery_id' => (rand(10,100)),
245 'lightboxsize' => 'full',
246 'exclude' => ''
247 ), $attr));
248
249 $id = intval($id);
250
251 if ($order === 'RAND') {
252 $orderby = 'none';
253 }
254
255 $gallery_rn = (rand(10,100));
256
257 if (!empty($include)) {
258 $_attachments = get_posts(array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
259
260 $attachments = array();
261 foreach ($_attachments as $key => $val) {
262 $attachments[$val->ID] = $_attachments[$key];
263 }
264 } elseif (!empty($exclude)) {
265 $attachments = get_children(array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
266 } else {
267 $attachments = get_children(array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
268 }
269
270 if (empty($attachments)) {
271 return '';
272 }
273
274 if (is_feed()) {
275 $output = "\n";
276 foreach ($attachments as $att_id => $attachment) {
277 $output .= wp_get_attachment_link($att_id, $size, true) . "\n";
278 }
279 return $output;
280 }
281
282 if ($columns == '2') {
283 $itemsize = 'tcol-lg-6 tcol-md-6 tcol-sm-6 tcol-xs-12 tcol-ss-12'; $imgsize = 600;
284 } else if ($columns == '1') {
285 $itemsize = 'tcol-lg-12 tcol-md-12 tcol-sm-12 tcol-xs-12 tcol-ss-12'; $imgsize = 1200;
286 } else if ($columns == '3'){
287 $itemsize = 'tcol-lg-4 tcol-md-4 tcol-sm-4 tcol-xs-6 tcol-ss-12'; $imgsize = 400;
288 } else if ($columns == '6'){
289 $itemsize = 'tcol-lg-2 tcol-md-2 tcol-sm-3 tcol-xs-4 tcol-ss-6'; $imgsize = 300;
290 } else if ($columns == '8' || $columns == '9' || $columns == '7'){
291 $itemsize = 'tcol-lg-2 tcol-md-2 tcol-sm-3 tcol-xs-4 tcol-ss-4'; $imgsize = 260;
292 } else if ($columns == '12' || $columns == '11'){
293 $itemsize = 'tcol-lg-1 tcol-md-1 tcol-sm-2 tcol-xs-2 tcol-ss-3'; $imgsize = 240;
294 } else if ($columns == '5'){
295 $itemsize = 'tcol-lg-25 tcol-md-25 tcol-sm-3 tcol-xs-4 tcol-ss-6'; $imgsize = 300;
296 } else {
297 $itemsize = 'tcol-lg-3 tcol-md-3 tcol-sm-4 tcol-xs-6 tcol-ss-12'; $imgsize = 300;
298 }
299
300 $output .= '<div id="kad-wp-gallery'.esc_attr($gallery_rn).'" class="kad-wp-gallery kad-light-wp-gallery clearfix kt-gallery-column-'.esc_attr($columns).' rowtight">';
301
302 foreach ($attachments as $id => $attachment) {
303 if($use_image_alt == 'true') {
304 $alt = get_post_meta($id, '_wp_attachment_image_alt', true);
305 } else {
306 $alt = $attachment->post_excerpt;
307 }
308
309 $img = kadence_toolkit_get_image_array($imgsize, $imgsize, true, 'light-dropshaddow', $alt, $id);
310 $attachment_url = $img['full'];
311
312 if($lightboxsize != 'full') {
313 $attachment_url = wp_get_attachment_image_src( $id, $lightboxsize);
314 $attachment_url = $attachment_url[0];
315 }
316 $lightbox_data = 'data-rel="lightbox"';
317 if($link == 'attachment_page' || $attachment_page == 'true') {
318 $attachment_url = get_permalink($id);
319 $lightbox_data = '';
320 }
321
322 $output .= '<div class="'.esc_attr($itemsize).' g_item"><div class="grid_item kad_gallery_fade_in gallery_item">';
323 $output .= '<a href="'.esc_url($attachment_url).'" '.$lightbox_data.' class="lightboxhover">';
324 $output .= '<img src="'.esc_url($img['src']).'" width="'.esc_attr($img['width']).'" height="'.esc_attr($img['height']).'" alt="'.esc_attr($img['alt']).'" '.$img['srcset'].' class="'.$img['class'].'"/>';
325 $output .= '</a>';
326 $output .= '</div></div>';
327 }
328 $output .= '</div>';
329
330 return $output;
331 }
332 add_action('init', 'kt_tool_gallery_setup_init');
333 function kt_tool_gallery_setup_init() {
334 $pinnacle = get_option( 'pinnacle' );
335 $virtue = get_option( 'virtue' );
336 if(! function_exists( 'kadence_gallery' ) ) {
337 if( (isset($pinnacle['pinnacle_gallery']) && $pinnacle['pinnacle_gallery'] == '1') || (isset($virtue['virtue_gallery']) && $virtue['virtue_gallery'] == '1') ) {
338 remove_shortcode('gallery');
339 add_shortcode('gallery', 'kadence_toolkit_shortcode_gallery');
340 }
341 }
342 }
343 function kt_toolkit_shortcode_gallery($attr) {
344 $post = get_post();
345 static $instance = 0;
346 $instance++;
347
348 if (!empty($attr['ids'])) {
349 if (empty($attr['orderby'])) {
350 $attr['orderby'] = 'post__in';
351 }
352 $attr['include'] = $attr['ids'];
353 }
354
355 $output = apply_filters('post_gallery', '', $attr);
356
357 if ($output != '') {
358 return $output;
359 }
360
361 if (isset($attr['orderby'])) {
362 $attr['orderby'] = sanitize_sql_orderby($attr['orderby']);
363 if (!$attr['orderby']) {
364 unset($attr['orderby']);
365 }
366 }
367 if(!isset($post)) {
368 $post_id = null;
369 } else {
370 $post_id = $post->ID;
371 }
372
373 extract(shortcode_atts(array(
374 'order' => 'ASC',
375 'orderby' => 'menu_order ID',
376 'id' => $post_id,
377 'columns' => 3,
378 'link' => 'file',
379 'size' => 'full',
380 'include' => '',
381 'use_image_alt' => 'true',
382 'gallery_id' => (rand(10,100)),
383 'lightboxsize' => 'full',
384 'exclude' => ''
385 ), $attr));
386
387 $id = intval($id);
388
389 if ($order === 'RAND') {
390 $orderby = 'none';
391 }
392 $caption = 'false';
393 if (!empty($include)) {
394 $_attachments = get_posts(array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
395 $attachments = array();
396 foreach ($_attachments as $key => $val) {
397 $attachments[$val->ID] = $_attachments[$key];
398 }
399 } elseif (!empty($exclude)) {
400 $attachments = get_children(array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
401 } else {
402 $attachments = get_children(array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
403 }
404 if (empty($attachments)) {
405 return '';
406 }
407 if (is_feed()) {
408 $output = "\n";
409 foreach ($attachments as $att_id => $attachment) {
410 $output .= wp_get_attachment_link($att_id, $size, true) . "\n";
411 }
412 return $output;
413 }
414 $output .= '<div id="kad-wp-gallery'.esc_attr($gallery_id).'" class="kad-wp-gallery kt-gallery-column-'.esc_attr($columns).' kad-light-gallery clearfix row-margin-small">';
415 if ($columns == '1') {
416 $itemsize = 'col-xxl-12 col-xl-12 col-lg-12 col-md-12 col-sm-12 col-xs-12 col-ss-12';
417 $imgsize = 1140;
418 } else if ($columns == '2') {
419 $itemsize = 'col-xxl-3 col-xl-4 col-lg-6 col-md-6 col-sm-6 col-xs-12 col-ss-12';
420 $imgsize = 600;
421 } else if ($columns == '3'){
422 $itemsize = 'col-xxl-25 col-xl-3 col-lg-4 col-md-4 col-sm-4 col-xs-6 col-ss-12';
423 $imgsize = 400;
424 } else if ($columns == '4'){
425 $itemsize = 'col-xxl-2 col-xl-25 col-lg-3 col-md-3 col-sm-4 col-xs-6 col-ss-12';
426 $imgsize = 300;
427 } else if ($columns == '5'){
428 $itemsize = 'col-xxl-2 col-xl-2 col-lg-25 col-md-25 col-sm-3 col-xs-4 col-ss-6';
429 $imgsize = 240;
430 } else if ($columns == '6'){
431 $itemsize = 'col-xxl-15 col-xl-2 col-lg-2 col-md-2 col-sm-3 col-xs-4 col-ss-6';
432 $imgsize = 240;
433 } else {
434 $itemsize = 'col-xxl-1 col-xl-15 col-lg-2 col-md-2 col-sm-3 col-xs-4 col-ss-4';
435 $imgsize = 240;
436 }
437
438 $i = 0;
439 foreach ($attachments as $id => $attachment) {
440 // Get alt or caption for alt
441 if($use_image_alt == 'true') {
442 $alt = get_post_meta($id, '_wp_attachment_image_alt', true);
443 } else {
444 $alt = $attachment->post_excerpt;
445 }
446
447 $img = kadence_toolkit_get_image_array($imgsize, $imgsize, true, 'kt-gallery-img', $alt, $id);
448 $attachment_url = $img['full'];
449
450 if($lightboxsize != 'full') {
451 $attachment_lb = wp_get_attachment_image_src( $id, $lightboxsize);
452 $attachment_url = $attachment_lb[0];
453 }
454 $lightbox_data = 'data-rel="lightbox"';
455 if($link == 'attachment_page') {
456 $attachment_url = get_permalink($id);
457 $lightbox_data = '';
458 }
459
460 $paddingbtn = ($img['height']/$img['width']) * 100;
461 $output .= '<div class="'.esc_attr($itemsize).' g_item"><div class="grid_item gallery_item">';
462 if($link != 'none') {
463 $output .='<a href="'.esc_url($attachment_url).'" '.$lightbox_data.' class="gallery-link">';
464 }
465 $output .= '<div class="kt-intrinsic" style="padding-bottom:'.esc_attr($paddingbtn).'%;" itemprop="image" itemscope itemtype="http://schema.org/ImageObject">';
466 $output .= '<img src="'.esc_url($img['src']).'" width="'.esc_attr($img['width']).'" height="'.esc_attr($img['height']).'" alt="'.esc_attr($img['alt']).'" '.$img['srcset'].' class="'.$img['class'].'" itemprop="contentUrl" />';
467 $output .= '<meta itemprop="url" content="'.esc_url($img['src']).'">';
468 $output .= '<meta itemprop="width" content="'.esc_attr($img['width']).'">';
469 $output .= '<meta itemprop="height" content="'.esc_attr($img['height']).'>">';
470 $output .= '</div>';
471 if (trim($attachment->post_excerpt) && $caption == 'true') {
472 $output .= '<div class="photo-caption-bg"></div>';
473 $output .= '<div class="caption kad_caption">';
474 $output .= '<div class="kad_caption_inner">' . wptexturize($attachment->post_excerpt) . '</div>';
475 $output .= '</div>';
476 }
477 if($link != 'none') {
478 $output .= '</a>';
479 }
480 $output .= '</div></div>';
481 }
482 $output .= '</div>';
483
484 return $output;
485 }
486 add_action('init', 'kt_tool_ascend_gallery_setup_init');
487 function kt_tool_ascend_gallery_setup_init() {
488 $the_theme = wp_get_theme();
489 if($the_theme->get( 'Name' ) == 'Ascend' || ($the_theme->get( 'Template') == 'ascend') ) {
490 $ascend = get_option( 'ascend' );
491 if(isset($ascend['kadence_gallery']) && $ascend['kadence_gallery'] == '1') {
492 remove_shortcode('gallery');
493 add_shortcode('gallery', 'kt_toolkit_shortcode_gallery');
494 }
495 }
496 }