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