1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* FooGallery_CSS_Load_Optimizer class which enqueues CSS in the head |
4
|
|
|
*/ |
5
|
|
|
if (!class_exists('class-css-load-optimizer.php')) { |
6
|
|
|
|
7
|
|
|
class FooGallery_CSS_Load_Optimizer { |
8
|
|
|
|
9
|
|
|
function __construct() { |
10
|
|
|
add_action( 'wp_enqueue_scripts', array( $this, 'include_gallery_css' ) ); |
11
|
|
|
add_action( 'foogallery_enqueue_style', array( $this, 'enqueue_style_to_persist' ), 10, 5 ); |
12
|
|
|
add_action( 'wp_footer', array( $this, 'persist_enqueued_styles' ) ); |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Persist any styles that are enqueued to be persisted |
17
|
|
|
*/ |
18
|
|
|
function persist_enqueued_styles() { |
|
|
|
|
19
|
|
|
global $wp_query, $foogallery_styles_to_persist; |
|
|
|
|
20
|
|
|
|
21
|
|
|
//we only want to do this if we are looking at a single post |
22
|
|
|
if ( ! is_singular() ) { |
23
|
|
|
return; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$post_id = $wp_query->post->ID; |
27
|
|
|
if ( $post_id && is_array( $foogallery_styles_to_persist ) ) { |
28
|
|
|
foreach( $foogallery_styles_to_persist as $style_handle => $style ) { |
29
|
|
|
add_post_meta( $post_id, FOOGALLERY_META_POST_USAGE_CSS, array( $style_handle => $style ), false ); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get the current post ids for the view that is being shown |
36
|
|
|
*/ |
37
|
|
|
function get_post_ids_from_query() { |
38
|
|
|
global $wp_query; |
39
|
|
|
|
40
|
|
|
if ( is_singular() ) { |
41
|
|
|
return array( $wp_query->post->ID ); |
42
|
|
|
} else if ( is_array( $wp_query->posts ) ) { |
43
|
|
|
return wp_list_pluck( $wp_query->posts, 'ID' ); |
44
|
|
|
} else { |
45
|
|
|
return array(); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Checks the post meta for any FooGallery CSS that needs to be added to the head |
51
|
|
|
* |
52
|
|
|
*/ |
53
|
|
|
function include_gallery_css() { |
54
|
|
|
global $enqueued_foogallery_styles; |
55
|
|
|
|
56
|
|
|
$enqueued_foogallery_styles = array(); |
57
|
|
|
|
58
|
|
|
foreach( $this->get_post_ids_from_query() as $post_id ) { |
59
|
|
|
$this->include_gallery_stylesheets_for_post( $post_id ); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* includes any CSS that needs to be added for a post |
65
|
|
|
* |
66
|
|
|
* @param $post_id int ID of the post |
67
|
|
|
*/ |
68
|
|
|
function include_gallery_stylesheets_for_post( $post_id ) { |
69
|
|
|
global $enqueued_foogallery_styles; |
70
|
|
|
|
71
|
|
|
if ( $post_id ) { |
72
|
|
|
//get any foogallery stylesheets that the post might need to include |
73
|
|
|
$css = get_post_meta($post_id, FOOGALLERY_META_POST_USAGE_CSS); |
74
|
|
|
|
75
|
|
|
if ( empty( $css ) || !is_array( $css ) ) return; |
76
|
|
|
|
77
|
|
|
foreach ($css as $css_item) { |
78
|
|
|
if (!$css_item) continue; |
79
|
|
|
foreach ($css_item as $handle => $style) { |
80
|
|
|
//only enqueue the stylesheet once |
81
|
|
|
if ( !array_key_exists( $handle, $enqueued_foogallery_styles ) ) { |
82
|
|
|
$cache_buster_key = $handle; |
83
|
|
|
if ( is_array( $style ) ) { |
84
|
|
|
$cache_buster_key = $this->create_cache_buster_key( $handle, $style['ver'], array_key_exists( 'site', $style ) ? $style['site'] : '' ); |
85
|
|
|
wp_enqueue_style( $handle, $style['src'], $style['deps'], $style['ver'], $style['media'] ); |
86
|
|
|
} else { |
87
|
|
|
wp_enqueue_style( $handle, $style ); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$enqueued_foogallery_styles[$handle] = $cache_buster_key; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Check to make sure we have added the stylesheets to our custom post meta field, |
99
|
|
|
* so that on next render the stylesheet will be added to the page header |
100
|
|
|
* |
101
|
|
|
* @param $style_handle string The stylesheet handle |
102
|
|
|
* @param $src string The location for the stylesheet |
103
|
|
|
* @param array $deps |
104
|
|
|
* @param bool $ver |
105
|
|
|
* @param string $media |
106
|
|
|
*/ |
107
|
|
|
function enqueue_style_to_persist($style_handle, $src, $deps = array(), $ver = false, $media = 'all') { |
|
|
|
|
108
|
|
|
global $wp_query, $enqueued_foogallery_styles, $foogallery_styles_to_persist; |
|
|
|
|
109
|
|
|
|
110
|
|
|
//we only want to do this if we are looking at a single post |
111
|
|
|
if ( ! is_singular() ) { |
112
|
|
|
return; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$post_id = $wp_query->post->ID; |
116
|
|
|
if ( $post_id ) { |
117
|
|
|
|
118
|
|
|
//check if the saved stylesheet needs to be cache busted |
119
|
|
|
if ( is_array( $enqueued_foogallery_styles ) && array_key_exists( $style_handle, $enqueued_foogallery_styles ) ) { |
120
|
|
|
$registered_cache_buster_key = $enqueued_foogallery_styles[$style_handle]; |
121
|
|
|
|
122
|
|
|
//generate the key we want |
123
|
|
|
$cache_buster_key = $this->create_cache_buster_key( $style_handle, $ver, home_url() ); |
124
|
|
|
|
125
|
|
|
if ( $registered_cache_buster_key !== $cache_buster_key ) { |
126
|
|
|
//we need to bust this cached stylesheet! |
127
|
|
|
$style = $this->get_old_style_post_meta_value( $post_id, $style_handle ); |
128
|
|
|
|
129
|
|
|
if ( false !== $style ) { |
130
|
|
|
delete_post_meta( $post_id, FOOGALLERY_META_POST_USAGE_CSS, array( $style_handle => $style ) ); |
131
|
|
|
|
132
|
|
|
//unset the handle from, to force the save of the post meta |
133
|
|
|
unset( $enqueued_foogallery_styles[$style_handle] ); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
//first check that the template has not been enqueued before |
139
|
|
|
if ( is_array( $enqueued_foogallery_styles ) && ! array_key_exists( $style_handle, $enqueued_foogallery_styles ) ) { |
140
|
|
|
|
141
|
|
|
$style = array( |
142
|
|
|
'src' => $src, |
143
|
|
|
'deps' => $deps, |
144
|
|
|
'ver' => $ver, |
145
|
|
|
'media' => $media, |
146
|
|
|
'site' => home_url() |
147
|
|
|
); |
148
|
|
|
|
149
|
|
|
if ( !is_array( $foogallery_styles_to_persist ) ) { |
150
|
|
|
$foogallery_styles_to_persist = array(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if ( !array_key_exists( $style_handle, $foogallery_styles_to_persist ) ) { |
154
|
|
|
$foogallery_styles_to_persist[$style_handle] = $style; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// add_post_meta( $post_id, FOOGALLERY_META_POST_USAGE_CSS, array( $style_handle => $style ), false ); |
|
|
|
|
158
|
|
|
// |
159
|
|
|
// $cache_buster_key = $this->create_cache_buster_key( $style_handle, $ver, home_url() ); |
160
|
|
|
// $enqueued_foogallery_styles[$style_handle] = $cache_buster_key; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
function create_cache_buster_key( $name, $version, $site = '' ) { |
|
|
|
|
166
|
|
|
return "{$site}::{$name}_{$version}"; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
function get_old_style_post_meta_value( $post_id, $handle_to_find ) { |
|
|
|
|
170
|
|
|
$css = get_post_meta($post_id, FOOGALLERY_META_POST_USAGE_CSS); |
171
|
|
|
|
172
|
|
|
foreach ($css as $css_item) { |
173
|
|
|
if ( ! $css_item ) { |
174
|
|
|
continue; |
175
|
|
|
} |
176
|
|
|
foreach ( $css_item as $handle => $style ) { |
177
|
|
|
//only enqueue the stylesheet once |
178
|
|
|
if ( $handle_to_find === $handle ) { |
179
|
|
|
return $style; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return false; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.