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