|
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_before_save_gallery', array( $this, 'start_watching_for_thumbnail_generation_calls' ), 10, 2 ); |
|
13
|
|
|
add_action( 'foogallery_after_save_gallery', array( $this, 'stop_watching_for_thumbnail_generation_calls' ), 99, 2 ); |
|
14
|
|
|
} |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Start watching for calls to the thumbnail generator so we can intercept and store info about the thumbs |
|
19
|
|
|
* |
|
20
|
|
|
* @param $post_id |
|
21
|
|
|
* @param $form_post |
|
22
|
|
|
*/ |
|
23
|
|
|
function start_watching_for_thumbnail_generation_calls( $post_id, $form_post ) { |
|
|
|
|
|
|
24
|
|
|
global $foogallery_thumb_dimensions; |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
//clear the array |
|
27
|
|
|
if ( empty( $foogallery_thumb_dimensions ) ) { |
|
28
|
|
|
$foogallery_thumb_dimensions[$post_id] = array(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
//and hook up actions |
|
32
|
|
|
add_filter( 'wpthumb_image_post', array( $this, 'intercept_thumb_generation' ), 10, 2 ); |
|
33
|
|
|
add_filter( 'foogallery_thumbnail_resize_args', array( $this, 'change_thumbnail_resize_args' ), 10, 3 ); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Stop watching for calls to the thumbnail generator |
|
38
|
|
|
* |
|
39
|
|
|
* @param $post_id |
|
40
|
|
|
* @param $form_post |
|
41
|
|
|
*/ |
|
42
|
|
|
function stop_watching_for_thumbnail_generation_calls( $post_id, $form_post ) { |
|
|
|
|
|
|
43
|
|
|
global $foogallery_thumb_dimensions; |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
//if we have nothing to store then get out |
|
46
|
|
|
if ( empty( $foogallery_thumb_dimensions ) || !array_key_exists( $post_id, $foogallery_thumb_dimensions ) ) { |
|
47
|
|
|
return; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
//check if we have data and store it against the foogallery |
|
51
|
|
|
update_post_meta( $post_id, FOOGALLERY_META_THUMB_DIMENSIONS, $foogallery_thumb_dimensions[$post_id] ); |
|
52
|
|
|
|
|
53
|
|
|
//unhook the action |
|
54
|
|
|
remove_filter( 'wpthumb_image_post', array( $this, 'intercept_thumb_generation' ), 10 ); |
|
55
|
|
|
remove_filter( 'foogallery_thumbnail_resize_args', array( $this, 'change_thumbnail_resize_args' ), 10 ); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Intercept calls to the thumb generation and store dimensions values |
|
60
|
|
|
* |
|
61
|
|
|
* @param $editor |
|
62
|
|
|
* @param $args |
|
63
|
|
|
*/ |
|
64
|
|
|
function intercept_thumb_generation( $editor, $args ) { |
|
|
|
|
|
|
65
|
|
|
global $foogallery_thumb_dimensions; |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
//if we have nothing stored then get out |
|
68
|
|
|
if ( empty( $foogallery_thumb_dimensions ) ) { |
|
69
|
|
|
return; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
//if we are not generating a thumb for a foogallery then get out |
|
73
|
|
|
if ( !array_key_exists( 'foogallery_id', $args ) ) { |
|
74
|
|
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
//if we are not generating a thumb for a foogallery attachment then get out |
|
78
|
|
|
if ( !array_key_exists( 'foogallery_attachment_id', $args ) ) { |
|
79
|
|
|
return; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$foogallery_id = $args['foogallery_id']; |
|
83
|
|
|
$foogallery_attachment_id = $args['foogallery_attachment_id']; |
|
84
|
|
|
$size = $editor->get_size(); |
|
85
|
|
|
|
|
86
|
|
|
$foogallery_thumb_dimensions[$foogallery_id][$foogallery_attachment_id] = $size; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Change any arguments passed to the thumb generation class |
|
91
|
|
|
* |
|
92
|
|
|
* @param $args |
|
93
|
|
|
* @param $original_image_src |
|
94
|
|
|
* @param $thumbnail_object |
|
95
|
|
|
* |
|
96
|
|
|
* @return mixed |
|
97
|
|
|
*/ |
|
98
|
|
|
function change_thumbnail_resize_args($args, $original_image_src, $thumbnail_object) { |
|
|
|
|
|
|
99
|
|
|
//we only want to force the thumb to be generated even if done so before |
|
100
|
|
|
$args['cache'] = false; |
|
101
|
|
|
return $args; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
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.