PluginProbe
Virtue/Ascend/Pinnacle Toolkit / 4.9.12.2
Virtue/Ascend/Pinnacle Toolkit v4.9.12.2
4.9.12.2 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 All 48 releases
virtue-toolkit / gallery.php

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

501 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 = ( isset( $sizes[5] ) ? $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 = ( isset( $sizes[4] ) ? $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 if(!isset($post)) {
231 $post_id = null;
232 } else {
233 $post_id = $post->ID;
234 }
235
236 extract(shortcode_atts(array(
237 'order' => 'ASC',
238 'orderby' => 'menu_order ID',
239 'id' => $post_id,
240 'itemtag' => '',
241 'icontag' => '',
242 'captiontag' => '',
243 'columns' => 3,
244 'link' => 'file',
245 'size' => 'full',
246 'include' => '',
247 'attachment_page' => 'false',
248 'use_image_alt' => 'false',
249 'gallery_id' => ( rand( 10, 100 ) ),
250 'lightboxsize' => 'full',
251 'exclude' => ''
252 ), $attr));
253
254 $id = intval($id);
255
256 if ($order === 'RAND') {
257 $orderby = 'none';
258 }
259
260 $gallery_rn = (rand(10,100));
261
262 if (!empty($include)) {
263 $_attachments = get_posts(array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
264
265 $attachments = array();
266 foreach ($_attachments as $key => $val) {
267 $attachments[$val->ID] = $_attachments[$key];
268 }
269 } elseif (!empty($exclude)) {
270 $attachments = get_children(array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
271 } else {
272 $attachments = get_children(array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
273 }
274
275 if (empty($attachments)) {
276 return '';
277 }
278
279 if (is_feed()) {
280 $output = "\n";
281 foreach ($attachments as $att_id => $attachment) {
282 $output .= wp_get_attachment_link($att_id, $size, true) . "\n";
283 }
284 return $output;
285 }
286
287 if ($columns == '2') {
288 $itemsize = 'tcol-lg-6 tcol-md-6 tcol-sm-6 tcol-xs-12 tcol-ss-12'; $imgsize = 600;
289 } else if ($columns == '1') {
290 $itemsize = 'tcol-lg-12 tcol-md-12 tcol-sm-12 tcol-xs-12 tcol-ss-12'; $imgsize = 1200;
291 } else if ($columns == '3'){
292 $itemsize = 'tcol-lg-4 tcol-md-4 tcol-sm-4 tcol-xs-6 tcol-ss-12'; $imgsize = 400;
293 } else if ($columns == '6'){
294 $itemsize = 'tcol-lg-2 tcol-md-2 tcol-sm-3 tcol-xs-4 tcol-ss-6'; $imgsize = 300;
295 } else if ($columns == '8' || $columns == '9' || $columns == '7'){
296 $itemsize = 'tcol-lg-2 tcol-md-2 tcol-sm-3 tcol-xs-4 tcol-ss-4'; $imgsize = 260;
297 } else if ($columns == '12' || $columns == '11'){
298 $itemsize = 'tcol-lg-1 tcol-md-1 tcol-sm-2 tcol-xs-2 tcol-ss-3'; $imgsize = 240;
299 } else if ($columns == '5'){
300 $itemsize = 'tcol-lg-25 tcol-md-25 tcol-sm-3 tcol-xs-4 tcol-ss-6'; $imgsize = 300;
301 } else {
302 $itemsize = 'tcol-lg-3 tcol-md-3 tcol-sm-4 tcol-xs-6 tcol-ss-12'; $imgsize = 300;
303 }
304
305 $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">';
306
307 foreach ($attachments as $id => $attachment) {
308 if($use_image_alt == 'true') {
309 $alt = get_post_meta($id, '_wp_attachment_image_alt', true);
310 } else {
311 $alt = $attachment->post_excerpt;
312 }
313
314 $img = kadence_toolkit_get_image_array($imgsize, $imgsize, true, 'light-dropshaddow', $alt, $id);
315 $attachment_url = $img['full'];
316
317 if($lightboxsize != 'full') {
318 $attachment_url = wp_get_attachment_image_src( $id, $lightboxsize);
319 $attachment_url = $attachment_url[0];
320 }
321 $lightbox_data = 'data-rel="lightbox"';
322 if($link == 'attachment_page' || $attachment_page == 'true') {
323 $attachment_url = get_permalink($id);
324 $lightbox_data = '';
325 }
326
327 $output .= '<div class="'.esc_attr($itemsize).' g_item"><div class="grid_item kad_gallery_fade_in gallery_item">';
328 $output .= '<a href="'.esc_url($attachment_url).'" '.$lightbox_data.' class="lightboxhover">';
329 $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'].'"/>';
330 $output .= '</a>';
331 $output .= '</div></div>';
332 }
333 $output .= '</div>';
334
335 return $output;
336 }
337 add_action('init', 'kt_tool_gallery_setup_init');
338 function kt_tool_gallery_setup_init() {
339 $pinnacle = get_option( 'pinnacle' );
340 $virtue = get_option( 'virtue' );
341 if(! function_exists( 'kadence_gallery' ) ) {
342 if( (isset($pinnacle['pinnacle_gallery']) && $pinnacle['pinnacle_gallery'] == '1') || (isset($virtue['virtue_gallery']) && $virtue['virtue_gallery'] == '1') ) {
343 remove_shortcode('gallery');
344 add_shortcode('gallery', 'kadence_toolkit_shortcode_gallery');
345 }
346 }
347 }
348 function kt_toolkit_shortcode_gallery($attr) {
349 $post = get_post();
350 static $instance = 0;
351 $instance++;
352
353 if (!empty($attr['ids'])) {
354 if (empty($attr['orderby'])) {
355 $attr['orderby'] = 'post__in';
356 }
357 $attr['include'] = $attr['ids'];
358 }
359
360 $output = apply_filters('post_gallery', '', $attr);
361
362 if ($output != '') {
363 return $output;
364 }
365
366 if (isset($attr['orderby'])) {
367 $attr['orderby'] = sanitize_sql_orderby($attr['orderby']);
368 if (!$attr['orderby']) {
369 unset($attr['orderby']);
370 }
371 }
372 if(!isset($post)) {
373 $post_id = null;
374 } else {
375 $post_id = $post->ID;
376 }
377
378 extract(shortcode_atts(array(
379 'order' => 'ASC',
380 'orderby' => 'menu_order ID',
381 'id' => $post_id,
382 'columns' => 3,
383 'link' => 'file',
384 'size' => 'full',
385 'include' => '',
386 'use_image_alt' => 'true',
387 'gallery_id' => (rand(10,100)),
388 'lightboxsize' => 'full',
389 'exclude' => ''
390 ), $attr));
391
392 $id = intval($id);
393
394 if ($order === 'RAND') {
395 $orderby = 'none';
396 }
397 $caption = 'false';
398 if (!empty($include)) {
399 $_attachments = get_posts(array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
400 $attachments = array();
401 foreach ($_attachments as $key => $val) {
402 $attachments[$val->ID] = $_attachments[$key];
403 }
404 } elseif (!empty($exclude)) {
405 $attachments = get_children(array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
406 } else {
407 $attachments = get_children(array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
408 }
409 if (empty($attachments)) {
410 return '';
411 }
412 if (is_feed()) {
413 $output = "\n";
414 foreach ($attachments as $att_id => $attachment) {
415 $output .= wp_get_attachment_link($att_id, $size, true) . "\n";
416 }
417 return $output;
418 }
419 $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">';
420 if ($columns == '1') {
421 $itemsize = 'col-xxl-12 col-xl-12 col-lg-12 col-md-12 col-sm-12 col-xs-12 col-ss-12';
422 $imgsize = 1140;
423 } else if ($columns == '2') {
424 $itemsize = 'col-xxl-3 col-xl-4 col-lg-6 col-md-6 col-sm-6 col-xs-12 col-ss-12';
425 $imgsize = 600;
426 } else if ($columns == '3'){
427 $itemsize = 'col-xxl-25 col-xl-3 col-lg-4 col-md-4 col-sm-4 col-xs-6 col-ss-12';
428 $imgsize = 400;
429 } else if ($columns == '4'){
430 $itemsize = 'col-xxl-2 col-xl-25 col-lg-3 col-md-3 col-sm-4 col-xs-6 col-ss-12';
431 $imgsize = 300;
432 } else if ($columns == '5'){
433 $itemsize = 'col-xxl-2 col-xl-2 col-lg-25 col-md-25 col-sm-3 col-xs-4 col-ss-6';
434 $imgsize = 240;
435 } else if ($columns == '6'){
436 $itemsize = 'col-xxl-15 col-xl-2 col-lg-2 col-md-2 col-sm-3 col-xs-4 col-ss-6';
437 $imgsize = 240;
438 } else {
439 $itemsize = 'col-xxl-1 col-xl-15 col-lg-2 col-md-2 col-sm-3 col-xs-4 col-ss-4';
440 $imgsize = 240;
441 }
442
443 $i = 0;
444 foreach ($attachments as $id => $attachment) {
445 // Get alt or caption for alt
446 if($use_image_alt == 'true') {
447 $alt = get_post_meta($id, '_wp_attachment_image_alt', true);
448 } else {
449 $alt = $attachment->post_excerpt;
450 }
451
452 $img = kadence_toolkit_get_image_array($imgsize, $imgsize, true, 'kt-gallery-img', $alt, $id);
453 $attachment_url = $img['full'];
454
455 if($lightboxsize != 'full') {
456 $attachment_lb = wp_get_attachment_image_src( $id, $lightboxsize);
457 $attachment_url = $attachment_lb[0];
458 }
459 $lightbox_data = 'data-rel="lightbox"';
460 if($link == 'attachment_page') {
461 $attachment_url = get_permalink($id);
462 $lightbox_data = '';
463 }
464
465 $paddingbtn = ($img['height']/$img['width']) * 100;
466 $output .= '<div class="'.esc_attr($itemsize).' g_item"><div class="grid_item gallery_item">';
467 if($link != 'none') {
468 $output .='<a href="'.esc_url($attachment_url).'" '.$lightbox_data.' class="gallery-link">';
469 }
470 $output .= '<div class="kt-intrinsic" style="padding-bottom:'.esc_attr($paddingbtn).'%;" itemprop="image" itemscope itemtype="http://schema.org/ImageObject">';
471 $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" />';
472 $output .= '<meta itemprop="url" content="'.esc_url($img['src']).'">';
473 $output .= '<meta itemprop="width" content="'.esc_attr($img['width']).'">';
474 $output .= '<meta itemprop="height" content="'.esc_attr($img['height']).'>">';
475 $output .= '</div>';
476 if (trim($attachment->post_excerpt) && $caption == 'true') {
477 $output .= '<div class="photo-caption-bg"></div>';
478 $output .= '<div class="caption kad_caption">';
479 $output .= '<div class="kad_caption_inner">' . wptexturize($attachment->post_excerpt) . '</div>';
480 $output .= '</div>';
481 }
482 if($link != 'none') {
483 $output .= '</a>';
484 }
485 $output .= '</div></div>';
486 }
487 $output .= '</div>';
488
489 return $output;
490 }
491 add_action('init', 'kt_tool_ascend_gallery_setup_init');
492 function kt_tool_ascend_gallery_setup_init() {
493 $the_theme = wp_get_theme();
494 if($the_theme->get( 'Name' ) == 'Ascend' || ($the_theme->get( 'Template') == 'ascend') ) {
495 $ascend = get_option( 'ascend' );
496 if(isset($ascend['kadence_gallery']) && $ascend['kadence_gallery'] == '1') {
497 remove_shortcode('gallery');
498 add_shortcode('gallery', 'kt_toolkit_shortcode_gallery');
499 }
500 }
501 }