|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class to calculate thumb dimensions for a gallery. The default gallery templates |
|
4
|
|
|
* require width and height attributes on the thumb img tags. In some cases these need to be |
|
5
|
|
|
* calculated based on the aspect ratio of the individual thumbs. |
|
6
|
|
|
* |
|
7
|
|
|
* Date: 21/03/2017 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
if ( ! class_exists( 'FooGallery_Thumbnail_Dimensions' ) ) { |
|
12
|
|
|
|
|
13
|
|
|
class FooGallery_Thumbnail_Dimensions |
|
|
|
|
|
|
14
|
|
|
{ |
|
15
|
|
|
function __construct() |
|
|
|
|
|
|
16
|
|
|
{ |
|
17
|
|
|
//hook into the filter that build up the img attributes |
|
18
|
|
|
// and add the width and height attributes if the gallery requires them |
|
19
|
|
|
add_filter( 'foogallery_attachment_html_image_attributes', array( $this, 'include_thumb_dimension_attributes' ), 10, 3 ); |
|
20
|
|
|
|
|
21
|
|
|
//calculate the thumbnail dimensions for all attachments in the gallery |
|
22
|
|
|
// for the specific gallery template that is being used. |
|
23
|
|
|
add_action( 'foogallery_located_template', array( $this, 'calculate_all_thumbnail_dimensions' ) ); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Helper function to check if the gallery requires thumbnail dimensions |
|
28
|
|
|
* @param $gallery_template string |
|
29
|
|
|
* @return bool |
|
30
|
|
|
*/ |
|
31
|
|
|
function does_gallery_template_use_thumbnail_dimensions( $gallery_template ) { |
|
|
|
|
|
|
32
|
|
|
if ( empty( $gallery_template ) ) return false; |
|
33
|
|
|
|
|
34
|
|
|
//first do a check if the template needs thumbnail dimensions calculated |
|
35
|
|
|
$template_data = foogallery_get_gallery_template( $gallery_template ); |
|
36
|
|
|
|
|
37
|
|
|
if ( $template_data && array_key_exists( 'thumbnail_dimensions', $template_data ) && true === $template_data['thumbnail_dimensions'] ) { |
|
38
|
|
|
//this template requires thumb dimensions to be provided |
|
39
|
|
|
return true; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return false; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
function empty_dimensions( $thumbnail_dimensions ) { |
|
|
|
|
|
|
46
|
|
|
if ( isset( $thumbnail_dimensions ) && is_array( $thumbnail_dimensions ) ) { |
|
47
|
|
|
$thumb_width = (int)$thumbnail_dimensions['width']; |
|
48
|
|
|
$thumb_height = (int)$thumbnail_dimensions['height']; |
|
49
|
|
|
|
|
50
|
|
|
return $thumb_width === 0 && $thumb_height === 0; |
|
51
|
|
|
} |
|
52
|
|
|
return true; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Calculates all the thumbnail dimensions for the gallery |
|
57
|
|
|
* @param $foogallery FooGallery |
|
58
|
|
|
*/ |
|
59
|
|
|
function calculate_all_thumbnail_dimensions( $foogallery ) { |
|
|
|
|
|
|
60
|
|
|
global $current_foogallery; |
|
|
|
|
|
|
61
|
|
|
global $current_foogallery_template; |
|
62
|
|
|
global $current_foogallery_arguments; |
|
63
|
|
|
|
|
64
|
|
|
//check if we are dealing with a gallery. This check ensures this is not done for albums |
|
65
|
|
|
if ( isset( $current_foogallery ) && isset( $current_foogallery_template ) ) { |
|
66
|
|
|
|
|
67
|
|
|
//first do a check if the template needs thumbnail dimensions calculated |
|
68
|
|
|
if ($this->does_gallery_template_use_thumbnail_dimensions( $current_foogallery_template ) ) { |
|
69
|
|
|
|
|
70
|
|
|
//load the thumbnail dimensions specific to the gallery, taking preference to arguments |
|
71
|
|
|
$thumbnail_dimensions = apply_filters( 'foogallery_calculate_thumbnail_dimensions-' . $current_foogallery_template, array(), $current_foogallery_arguments ); |
|
72
|
|
|
|
|
73
|
|
|
//if we have no dimensions then load them from the gallery settings |
|
74
|
|
|
if ( $this->empty_dimensions( $thumbnail_dimensions ) ) { |
|
75
|
|
|
$thumbnail_dimensions = apply_filters( 'foogallery_template_thumbnail_dimensions-' . $current_foogallery_template, $thumbnail_dimensions, $current_foogallery ); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if ( isset( $thumbnail_dimensions ) && is_array( $thumbnail_dimensions ) ) { |
|
79
|
|
|
|
|
80
|
|
|
//$thumbnail_dimensions |
|
81
|
|
|
$thumb_width = (int)$thumbnail_dimensions['width']; |
|
82
|
|
|
$thumb_height = (int)$thumbnail_dimensions['height']; |
|
83
|
|
|
$thumb_crop = (bool)$thumbnail_dimensions['crop']; |
|
84
|
|
|
|
|
85
|
|
|
//set the appropriate arguments on the attachments so that they can be |
|
86
|
|
|
// picked up and used in the 'include_thumb_dimension_attributes' function below |
|
87
|
|
|
foreach ($foogallery->attachments() as $attachment) { |
|
88
|
|
|
if ( $thumb_crop && $thumb_width > 0 && $thumb_height > 0 ) { |
|
89
|
|
|
//we have set width and height and crop = true |
|
90
|
|
|
//we do not need to calculate the dimensions |
|
91
|
|
|
$calculated_thumb_width = $thumb_width; |
|
92
|
|
|
$calculated_thumb_height = $thumb_height; |
|
93
|
|
|
} else { |
|
94
|
|
|
$size_array = image_resize_dimensions($attachment->width, $attachment->height, $thumb_width, $thumb_height, $thumb_crop); |
|
95
|
|
|
$calculated_thumb_width = $size_array[4]; |
|
96
|
|
|
$calculated_thumb_height = $size_array[5]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$attachment->has_thumbnail_dimensions = true; |
|
100
|
|
|
$attachment->thumb_width = $calculated_thumb_width; |
|
101
|
|
|
$attachment->thumb_height = $calculated_thumb_height; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Include the thumb dimension html attributes in the rendered HTML |
|
110
|
|
|
* |
|
111
|
|
|
* @param $attr |
|
112
|
|
|
* @param $args |
|
113
|
|
|
* @param $foogallery_attachment |
|
114
|
|
|
* |
|
115
|
|
|
* @return array |
|
116
|
|
|
*/ |
|
117
|
|
|
function include_thumb_dimension_attributes( $attr, $args, $foogallery_attachment ) { |
|
|
|
|
|
|
118
|
|
|
global $current_foogallery; |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
//check if we are dealing with a gallery. This check ensures this is not done for albums |
|
121
|
|
|
if ( isset( $current_foogallery ) ) { |
|
122
|
|
|
|
|
123
|
|
|
//check if we have anything set |
|
124
|
|
|
if ( isset( $foogallery_attachment->has_thumbnail_dimensions ) ) { |
|
125
|
|
|
if ( $foogallery_attachment->thumb_width > 0 ) { |
|
126
|
|
|
$attr['width'] = $foogallery_attachment->thumb_width; |
|
127
|
|
|
} |
|
128
|
|
|
if ( $foogallery_attachment->thumb_height > 0 ) { |
|
129
|
|
|
$attr['height'] = $foogallery_attachment->thumb_height; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $attr; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.