1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class to calculate thumb dimensions for a gallery |
4
|
|
|
* Date: 21/03/2017 |
5
|
|
|
*/ |
6
|
|
|
if ( ! class_exists( 'FooGallery_Thumbnail_Dimensions' ) ) { |
7
|
|
|
|
8
|
|
|
class FooGallery_Thumbnail_Dimensions { |
|
|
|
|
9
|
|
|
|
10
|
|
|
function __construct() { |
|
|
|
|
11
|
|
|
if ( is_admin() ) { |
12
|
|
|
add_action( 'foogallery_after_save_gallery', array( $this, 'calculate_thumbnail_dimensions' ), 9, 2 ); |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
add_filter( 'foogallery_attachment_load', array( $this, 'load_thumbnail_dimensions' ), 10, 2 ); |
16
|
|
|
add_filter( 'foogallery_attachment_html_image_attributes', array( $this, 'include_thumb_dimension_attributes' ), 10, 3 ); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Calculate the exact thumb size for the gallery and save the meta data |
21
|
|
|
* @param $post_id |
22
|
|
|
* @param $form_post |
23
|
|
|
*/ |
24
|
|
|
function calculate_thumbnail_dimensions( $post_id, $form_post ) { |
|
|
|
|
25
|
|
|
$foogallery = FooGallery::get_by_id( $post_id ); |
26
|
|
|
|
27
|
|
|
$gallery_template = $foogallery->gallery_template; |
28
|
|
|
|
29
|
|
|
$template_data = foogallery_get_gallery_template( $gallery_template ); |
30
|
|
|
|
31
|
|
|
if ( $template_data && array_key_exists( 'thumbnail_dimensions', $template_data ) && true === $template_data['thumbnail_dimensions'] ) { |
32
|
|
|
|
33
|
|
|
$setting_key = "{$gallery_template}_thumbnail_dimensions"; |
34
|
|
|
|
35
|
|
|
$default_thumbnail_dimensions = $foogallery->get_meta( $setting_key, false ); |
36
|
|
|
|
37
|
|
|
$thumbnail_dimensions = apply_filters( 'foogallery_template_thumbnail_dimensions-' . $gallery_template, $default_thumbnail_dimensions, $foogallery ); |
38
|
|
|
|
39
|
|
|
if ( isset( $thumbnail_dimensions ) && is_array( $thumbnail_dimensions ) ) { |
40
|
|
|
|
41
|
|
|
//$thumbnail_dimensions |
42
|
|
|
$thumb_width = (int) $thumbnail_dimensions['width']; |
43
|
|
|
$thumb_height = (int) $thumbnail_dimensions['height']; |
44
|
|
|
$thumb_crop = (bool) $thumbnail_dimensions['crop']; |
45
|
|
|
|
46
|
|
|
foreach ( $foogallery->attachments() as $attachment ) { |
47
|
|
|
|
48
|
|
|
$size_array = image_resize_dimensions( $attachment->width, $attachment->height, $thumb_width, $thumb_height, $thumb_crop ); |
49
|
|
|
|
50
|
|
|
$size = array( |
51
|
|
|
'width' => $size_array[4], |
52
|
|
|
'height' => $size_array[5] |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
$existing_size = get_post_meta( $attachment->ID, FOOGALLERY_META_THUMB_DIMENSIONS, true ); |
56
|
|
|
$existing_size[$foogallery->ID] = $size; |
57
|
|
|
|
58
|
|
|
//save the post meta against the attachment |
59
|
|
|
update_post_meta( $attachment->ID, FOOGALLERY_META_THUMB_DIMENSIONS, $existing_size ); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Load the thumb dimension attributes onto the attachment |
67
|
|
|
* @param $foogallery_attachment |
68
|
|
|
* @param $foogallery |
69
|
|
|
* |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
function load_thumbnail_dimensions( $foogallery_attachment, $foogallery ) { |
|
|
|
|
73
|
|
|
$gallery_template = $foogallery->gallery_template; |
74
|
|
|
|
75
|
|
|
$template_data = foogallery_get_gallery_template( $gallery_template ); |
76
|
|
|
|
77
|
|
|
if ( $template_data && array_key_exists( 'thumbnail_dimensions', $template_data ) && true === $template_data['thumbnail_dimensions'] ) { |
78
|
|
|
|
79
|
|
|
$size = get_post_meta( $foogallery_attachment->ID, FOOGALLERY_META_THUMB_DIMENSIONS, true ); |
80
|
|
|
if ( isset( $size ) && is_array( $size ) && array_key_exists( $foogallery->ID, $size ) ) { |
81
|
|
|
$size = $size[$foogallery->ID]; |
82
|
|
|
|
83
|
|
|
$foogallery_attachment->foogallery_id = $foogallery->ID; |
84
|
|
|
$foogallery_attachment->thumb_width = $size['width']; |
85
|
|
|
$foogallery_attachment->thumb_height = $size['height']; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $foogallery_attachment; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Include the thumb dimension html attributes in the rendered HTML |
94
|
|
|
* |
95
|
|
|
* @param $attr |
96
|
|
|
* @param $args |
97
|
|
|
* @param $foogallery_attachment |
98
|
|
|
* |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
|
|
function include_thumb_dimension_attributes( $attr, $args, $foogallery_attachment ) { |
|
|
|
|
102
|
|
|
//do a check to see if the template has changed |
103
|
|
|
global $current_foogallery_arguments; |
104
|
|
|
global $current_foogallery; |
|
|
|
|
105
|
|
|
//check if we are dealing with a gallery. This check ensures this is not done for albums |
106
|
|
|
if ( isset( $current_foogallery ) ) { |
107
|
|
|
|
108
|
|
|
if ( isset( $current_foogallery_arguments ) && isset( $current_foogallery_arguments['template'] ) ) { |
109
|
|
|
//we need to calculate new dynamic dimensions for the thumb |
110
|
|
|
$thumbnail_dimensions = apply_filters( 'foogallery_calculate_thumbnail_dimensions-' . $current_foogallery_arguments['template'], false, $current_foogallery_arguments ); |
111
|
|
|
|
112
|
|
|
if ( $thumbnail_dimensions ) { |
113
|
|
|
//$thumbnail_dimensions |
114
|
|
|
$thumb_width = (int) $thumbnail_dimensions['width']; |
115
|
|
|
$thumb_height = (int) $thumbnail_dimensions['height']; |
116
|
|
|
$thumb_crop = (bool) $thumbnail_dimensions['crop']; |
117
|
|
|
|
118
|
|
|
$size_array = image_resize_dimensions( $foogallery_attachment->width, $foogallery_attachment->height, $thumb_width, $thumb_height, $thumb_crop ); |
119
|
|
|
$foogallery_attachment->foogallery_id = $current_foogallery->ID; |
120
|
|
|
$foogallery_attachment->thumb_width = $size_array[4]; |
121
|
|
|
$foogallery_attachment->thumb_height = $size_array[5]; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if ( isset( $foogallery_attachment->foogallery_id ) ) { |
126
|
|
|
if ( $foogallery_attachment->thumb_width > 0 ) { |
127
|
|
|
$attr['width'] = $foogallery_attachment->thumb_width; |
128
|
|
|
} |
129
|
|
|
if ( $foogallery_attachment->thumb_height > 0 ) { |
130
|
|
|
$attr['height'] = $foogallery_attachment->thumb_height; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $attr; |
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.