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
|
|
|
$setting_key = "{$gallery_template}_thumbnail_dimensions"; |
30
|
|
|
|
31
|
|
|
$default_thumbnail_dimensions = $foogallery->get_meta( $setting_key, false ); |
32
|
|
|
|
33
|
|
|
$thumbnail_dimensions = apply_filters( 'foogallery_template_thumbnail_dimensions-' . $gallery_template, $default_thumbnail_dimensions, $foogallery ); |
34
|
|
|
|
35
|
|
|
if ( isset( $thumbnail_dimensions ) && is_array( $thumbnail_dimensions ) ) { |
36
|
|
|
|
37
|
|
|
//$thumbnail_dimensions |
38
|
|
|
$thumb_width = (int) $thumbnail_dimensions['width']; |
39
|
|
|
$thumb_height = (int) $thumbnail_dimensions['height']; |
40
|
|
|
$thumb_crop = (bool) $thumbnail_dimensions['crop']; |
41
|
|
|
|
42
|
|
|
foreach ( $foogallery->attachments() as $attachment ) { |
43
|
|
|
|
44
|
|
|
$size_array = image_resize_dimensions( $attachment->width, $attachment->height, $thumb_width, $thumb_height, $thumb_crop ); |
45
|
|
|
|
46
|
|
|
$size = array( |
47
|
|
|
'width' => $size_array[4], |
48
|
|
|
'height' => $size_array[5] |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
$existing_size = get_post_meta( $attachment->ID, FOOGALLERY_META_THUMB_DIMENSIONS, true ); |
52
|
|
|
$existing_size[$foogallery->ID] = $size; |
53
|
|
|
|
54
|
|
|
//save the post meta against the attachment |
55
|
|
|
update_post_meta( $attachment->ID, FOOGALLERY_META_THUMB_DIMENSIONS, $existing_size ); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Load the thumb dimension attributes onto the attachment |
62
|
|
|
* @param $foogallery_attachment |
63
|
|
|
* @param $foogallery |
64
|
|
|
* |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
|
|
function load_thumbnail_dimensions( $foogallery_attachment, $foogallery ) { |
|
|
|
|
68
|
|
|
$size = get_post_meta( $foogallery_attachment->ID, FOOGALLERY_META_THUMB_DIMENSIONS, true ); |
69
|
|
|
if ( isset( $size ) && is_array( $size ) && array_key_exists( $foogallery->ID, $size ) ) { |
70
|
|
|
$size = $size[$foogallery->ID]; |
71
|
|
|
|
72
|
|
|
$foogallery_attachment->foogallery_id = $foogallery->ID; |
73
|
|
|
$foogallery_attachment->thumb_width = $size['width']; |
74
|
|
|
$foogallery_attachment->thumb_height = $size['height']; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $foogallery_attachment; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Include the thumb dimension html attributes in the rendered HTML |
82
|
|
|
* |
83
|
|
|
* @param $attr |
84
|
|
|
* @param $args |
85
|
|
|
* @param $foogallery_attachment |
86
|
|
|
* |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
function include_thumb_dimension_attributes( $attr, $args, $foogallery_attachment ) { |
|
|
|
|
90
|
|
|
//do a check to see if the template has changed |
91
|
|
|
global $current_foogallery_arguments; |
92
|
|
|
global $current_foogallery; |
|
|
|
|
93
|
|
|
if ( isset( $current_foogallery_arguments ) && isset( $current_foogallery_arguments['template'] ) ) { |
94
|
|
|
|
95
|
|
|
//we need to calculate new dynamic dimensions for the thumb |
96
|
|
|
$thumbnail_dimensions = apply_filters( 'foogallery_calculate_thumbnail_dimensions-' . $current_foogallery_arguments['template'], false, $current_foogallery_arguments ); |
97
|
|
|
|
98
|
|
|
if ( $thumbnail_dimensions ) { |
99
|
|
|
//$thumbnail_dimensions |
100
|
|
|
$thumb_width = (int) $thumbnail_dimensions['width']; |
101
|
|
|
$thumb_height = (int) $thumbnail_dimensions['height']; |
102
|
|
|
$thumb_crop = (bool) $thumbnail_dimensions['crop']; |
103
|
|
|
|
104
|
|
|
$size_array = image_resize_dimensions( $foogallery_attachment->width, $foogallery_attachment->height, $thumb_width, $thumb_height, $thumb_crop ); |
105
|
|
|
$foogallery_attachment->foogallery_id = $current_foogallery->ID; |
106
|
|
|
$foogallery_attachment->thumb_width = $size_array[4]; |
107
|
|
|
$foogallery_attachment->thumb_height = $size_array[5]; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ( isset( $foogallery_attachment->foogallery_id ) ) { |
112
|
|
|
if ( $foogallery_attachment->thumb_width > 0 ) { |
113
|
|
|
$attr['width'] = $foogallery_attachment->thumb_width; |
114
|
|
|
} |
115
|
|
|
if ( $foogallery_attachment->thumb_height > 0 ) { |
116
|
|
|
$attr['height'] = $foogallery_attachment->thumb_height; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $attr; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
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.