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, 'persist_enqueued_style' ), 10, 5 ); |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Get the current post ids for the view that is being shown |
16
|
|
|
*/ |
17
|
|
|
function get_post_ids_from_query() { |
18
|
|
|
global $wp_query; |
19
|
|
|
|
20
|
|
|
if ( is_singular() ) { |
21
|
|
|
return array( $wp_query->post->ID ); |
22
|
|
|
} else if ( is_array( $wp_query->posts ) ) { |
23
|
|
|
return wp_list_pluck( $wp_query->posts, 'ID' ); |
24
|
|
|
} else { |
25
|
|
|
return array(); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Checks the post meta for any FooGallery CSS that needs to be added to the head |
31
|
|
|
* |
32
|
|
|
*/ |
33
|
|
|
function include_gallery_css() { |
34
|
|
|
global $enqueued_foogallery_styles; |
35
|
|
|
|
36
|
|
|
$enqueued_foogallery_styles = array(); |
37
|
|
|
|
38
|
|
|
foreach( $this->get_post_ids_from_query() as $post_id ) { |
39
|
|
|
$this->include_gallery_stylesheets_for_post( $post_id ); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* includes any CSS that needs to be added for a post |
45
|
|
|
* |
46
|
|
|
* @param $post_id int ID of the post |
47
|
|
|
*/ |
48
|
|
|
function include_gallery_stylesheets_for_post( $post_id ) { |
49
|
|
|
global $enqueued_foogallery_styles; |
50
|
|
|
|
51
|
|
|
if ( $post_id ) { |
52
|
|
|
//get any foogallery stylesheets that the post might need to include |
53
|
|
|
$css = get_post_meta($post_id, FOOGALLERY_META_POST_USAGE_CSS); |
54
|
|
|
|
55
|
|
|
foreach ($css as $css_item) { |
56
|
|
|
if (!$css_item) continue; |
57
|
|
|
foreach ($css_item as $handle => $style) { |
58
|
|
|
//only enqueue the stylesheet once |
59
|
|
|
if ( !array_key_exists( $handle, $enqueued_foogallery_styles ) ) { |
60
|
|
|
$cache_buster_key = $handle; |
61
|
|
|
if ( is_array( $style ) ) { |
62
|
|
|
$cache_buster_key = $this->create_cache_buster_key( $handle, $style['ver'], array_key_exists( 'site', $style ) ? $style['site'] : '' ); |
63
|
|
|
wp_enqueue_style( $handle, $style['src'], $style['deps'], $style['ver'], $style['media'] ); |
64
|
|
|
} else { |
65
|
|
|
wp_enqueue_style( $handle, $style ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$enqueued_foogallery_styles[$handle] = $cache_buster_key; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Check to make sure we have added the stylesheets to our custom post meta field, |
77
|
|
|
* so that on next render the stylesheet will be added to the page header |
78
|
|
|
* |
79
|
|
|
* @param $style_handle string The stylesheet handle |
80
|
|
|
* @param $src string The location for the stylesheet |
81
|
|
|
* @param array $deps |
82
|
|
|
* @param bool $ver |
83
|
|
|
* @param string $media |
84
|
|
|
*/ |
85
|
|
|
function persist_enqueued_style($style_handle, $src, $deps = array(), $ver = false, $media = 'all') { |
86
|
|
|
global $wp_query, $enqueued_foogallery_styles; |
87
|
|
|
|
88
|
|
|
//we only want to do this if we are looking at a single post |
89
|
|
|
if ( ! is_singular() ) { |
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$post_id = $wp_query->post->ID; |
94
|
|
|
if ( $post_id ) { |
95
|
|
|
|
96
|
|
|
//check if the saved stylesheet needs to be cache busted |
97
|
|
|
if ( is_array( $enqueued_foogallery_styles ) && array_key_exists( $style_handle, $enqueued_foogallery_styles ) ) { |
98
|
|
|
$registered_cache_buster_key = $enqueued_foogallery_styles[$style_handle]; |
99
|
|
|
|
100
|
|
|
//generate the key we want |
101
|
|
|
$cache_buster_key = $this->create_cache_buster_key( $style_handle, $ver, home_url() ); |
102
|
|
|
|
103
|
|
|
if ( $registered_cache_buster_key !== $cache_buster_key ) { |
104
|
|
|
//we need to bust this cached stylesheet! |
105
|
|
|
$style = $this->get_old_style_post_meta_value( $post_id, $style_handle ); |
106
|
|
|
|
107
|
|
|
if ( false !== $style ) { |
108
|
|
|
delete_post_meta( $post_id, FOOGALLERY_META_POST_USAGE_CSS, array( $style_handle => $style ) ); |
109
|
|
|
|
110
|
|
|
//unset the handle from, to force the save of the post meta |
111
|
|
|
unset( $enqueued_foogallery_styles[$style_handle] ); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
//first check that the template has not been enqueued before |
117
|
|
|
if ( is_array( $enqueued_foogallery_styles ) && ! array_key_exists( $style_handle, $enqueued_foogallery_styles ) ) { |
118
|
|
|
|
119
|
|
|
$style = array( |
120
|
|
|
'src' => $src, |
121
|
|
|
'deps' => $deps, |
122
|
|
|
'ver' => $ver, |
123
|
|
|
'media' => $media, |
124
|
|
|
'site' => home_url() |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
add_post_meta( $post_id, FOOGALLERY_META_POST_USAGE_CSS, array( $style_handle => $style ), false ); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
function create_cache_buster_key( $name, $version, $site = '' ) { |
|
|
|
|
133
|
|
|
return "{$site}::{$name}_{$version}"; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
function get_old_style_post_meta_value( $post_id, $handle_to_find ) { |
|
|
|
|
137
|
|
|
$css = get_post_meta($post_id, FOOGALLERY_META_POST_USAGE_CSS); |
138
|
|
|
|
139
|
|
|
foreach ($css as $css_item) { |
140
|
|
|
if ( ! $css_item ) { |
141
|
|
|
continue; |
142
|
|
|
} |
143
|
|
|
foreach ( $css_item as $handle => $style ) { |
144
|
|
|
//only enqueue the stylesheet once |
145
|
|
|
if ( $handle_to_find === $handle ) { |
146
|
|
|
return $style; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return false; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
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.