|
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
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Calculate the exact thumb size for the gallery and save the meta data |
|
20
|
|
|
* @param $post_id |
|
21
|
|
|
* @param $form_post |
|
22
|
|
|
*/ |
|
23
|
|
|
function calculate_thumbnail_dimensions( $post_id, $form_post ) { |
|
|
|
|
|
|
24
|
|
|
$foogallery = FooGallery::get_by_id( $post_id ); |
|
25
|
|
|
|
|
26
|
|
|
$gallery_template = $foogallery->gallery_template; |
|
27
|
|
|
|
|
28
|
|
|
$setting_key = "{$gallery_template}_thumbnail_dimensions"; |
|
29
|
|
|
|
|
30
|
|
|
$thumbnail_dimensions = apply_filters( 'foogallery_template_thumbnail_dimensions-' . $gallery_template, $foogallery->get_meta( $setting_key, false ), $foogallery ); |
|
31
|
|
|
|
|
32
|
|
|
foreach ( $foogallery->attachments() as $attachment ) { |
|
33
|
|
|
//$thumbnail_dimensions |
|
34
|
|
|
$thumb_width = (int) $thumbnail_dimensions['width']; |
|
35
|
|
|
$thumb_height = (int) $thumbnail_dimensions['height']; |
|
36
|
|
|
$thumb_crop = (bool) $thumbnail_dimensions['crop']; |
|
37
|
|
|
|
|
38
|
|
|
$size_array = image_resize_dimensions( $attachment->width, $attachment->height, $thumb_width, $thumb_height, $thumb_crop ); |
|
39
|
|
|
|
|
40
|
|
|
$size = array( |
|
41
|
|
|
'width' => $size_array[4], |
|
42
|
|
|
'height' => $size_array[5] |
|
43
|
|
|
); |
|
44
|
|
|
|
|
45
|
|
|
$existing_size = get_post_meta( $attachment->ID, FOOGALLERY_META_THUMB_DIMENSIONS, true ); |
|
46
|
|
|
$existing_size[$foogallery->ID] = $size; |
|
47
|
|
|
|
|
48
|
|
|
//save the post meta against the attachment |
|
49
|
|
|
update_post_meta( $attachment->ID, FOOGALLERY_META_THUMB_DIMENSIONS, $existing_size ); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
function load_thumbnail_dimensions( $foogallery_attachment, $foogallery ) { |
|
|
|
|
|
|
54
|
|
|
$size = get_post_meta( $foogallery_attachment->ID, FOOGALLERY_META_THUMB_DIMENSIONS, true ); |
|
55
|
|
|
if ( $size && array_key_exists( $foogallery->ID, $size ) ) { |
|
56
|
|
|
$size = $size[$foogallery->ID]; |
|
57
|
|
|
|
|
58
|
|
|
$foogallery_attachment->foogallery_id = $foogallery->ID; |
|
59
|
|
|
$foogallery_attachment->thumb_width = $size['width']; |
|
60
|
|
|
$foogallery_attachment->thumb_height = $size['width']; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $foogallery_attachment; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
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.