class-admin-bar.php
2 months ago
class-aioseo-sitemaps.php
2 months ago
class-css-load-optimizer.php
2 months ago
class-foogallery-template-loader.php
2 months ago
class-public.php
2 months ago
class-rank-math-seo-sitemaps.php
2 months ago
class-shortcodes.php
2 months ago
class-yoast-seo-sitemaps.php
2 months ago
index.php
2 months ago
class-css-load-optimizer.php
339 lines
| 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 | function include_gallery_css() { |
| 53 | global $enqueued_foogallery_styles; |
| 54 | |
| 55 | $enqueued_foogallery_styles = array(); |
| 56 | |
| 57 | foreach( $this->get_post_ids_from_query() as $post_id ) { |
| 58 | $this->include_gallery_stylesheets_for_post( $post_id ); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * includes any CSS that needs to be added for a post |
| 64 | * |
| 65 | * @param $post_id int ID of the post |
| 66 | */ |
| 67 | function include_gallery_stylesheets_for_post( $post_id ) { |
| 68 | global $enqueued_foogallery_styles; |
| 69 | |
| 70 | if ( $post_id ) { |
| 71 | //get any foogallery stylesheets that the post might need to include |
| 72 | $css = get_post_meta($post_id, FOOGALLERY_META_POST_USAGE_CSS); |
| 73 | |
| 74 | if ( empty( $css ) || !is_array( $css ) ) return; |
| 75 | |
| 76 | foreach ($css as $css_item) { |
| 77 | if ( !$css_item ) continue; |
| 78 | if ( empty( $css_item ) || !is_array( $css_item ) ) return; //make sure we are dealing with an array |
| 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, $this->normalize_stylesheet_src_scheme( $style['src'] ), $style['deps'], $style['ver'], $style['media'] ); |
| 86 | } else { |
| 87 | wp_enqueue_style( $handle, $this->normalize_stylesheet_src_scheme( $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 it from the post |
| 131 | delete_post_meta( $post_id, FOOGALLERY_META_POST_USAGE_CSS, array( $style_handle => $style ) ); |
| 132 | |
| 133 | //unset the handle, to force the save of the post meta |
| 134 | unset( $enqueued_foogallery_styles[$style_handle] ); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | //first check that the template has not been enqueued before |
| 140 | if ( is_array( $enqueued_foogallery_styles ) && ! array_key_exists( $style_handle, $enqueued_foogallery_styles ) ) { |
| 141 | |
| 142 | $style = array( |
| 143 | 'src' => $this->normalize_stylesheet_src_scheme( $src ), |
| 144 | 'deps' => $deps, |
| 145 | 'ver' => $ver, |
| 146 | 'media' => $media, |
| 147 | 'site' => home_url() |
| 148 | ); |
| 149 | |
| 150 | if ( !is_array( $foogallery_styles_to_persist ) ) { |
| 151 | $foogallery_styles_to_persist = array(); |
| 152 | } |
| 153 | |
| 154 | if ( !array_key_exists( $style_handle, $foogallery_styles_to_persist ) ) { |
| 155 | $foogallery_styles_to_persist[$style_handle] = $style; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Create a key that will be used to cache |
| 163 | * |
| 164 | * @param $name |
| 165 | * @param $version |
| 166 | * @param string $site |
| 167 | * |
| 168 | * @return string |
| 169 | */ |
| 170 | function create_cache_buster_key( $name, $version, $site = '' ) { |
| 171 | return "{$site}::{$name}_{$version}"; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Normalize cached stylesheet URLs to the configured front-end site scheme. |
| 176 | * |
| 177 | * The optimizer stores full URLs in post meta. If the same page is ever rendered under |
| 178 | * a different request scheme, WordPress can build plugin URLs with that scheme and the |
| 179 | * stale cached URL may later be emitted on the canonical front end. |
| 180 | * |
| 181 | * @param string $src The stylesheet URL. |
| 182 | * |
| 183 | * @return string |
| 184 | */ |
| 185 | function normalize_stylesheet_src_scheme( $src ) { |
| 186 | if ( ! is_string( $src ) || ! preg_match( '#^https?://#i', $src ) ) { |
| 187 | return $src; |
| 188 | } |
| 189 | |
| 190 | if ( 'on' === foogallery_get_setting( 'force_https' ) ) { |
| 191 | return set_url_scheme( $src, 'https' ); |
| 192 | } |
| 193 | |
| 194 | $home_url_parts = wp_parse_url( home_url( '/' ) ); |
| 195 | if ( ! is_array( $home_url_parts ) || empty( $home_url_parts['scheme'] ) ) { |
| 196 | return $src; |
| 197 | } |
| 198 | |
| 199 | $scheme = strtolower( $home_url_parts['scheme'] ); |
| 200 | if ( ! in_array( $scheme, array( 'http', 'https' ), true ) ) { |
| 201 | return $src; |
| 202 | } |
| 203 | |
| 204 | if ( ! $this->matches_site_url_host_and_port( $src ) || ! $this->matches_local_asset_url_root( $src ) ) { |
| 205 | return $src; |
| 206 | } |
| 207 | |
| 208 | return set_url_scheme( $src, $scheme ); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Check if a stylesheet URL is on the configured home or site host. |
| 213 | * |
| 214 | * @param string $src The stylesheet URL. |
| 215 | * |
| 216 | * @return bool |
| 217 | */ |
| 218 | function matches_site_url_host_and_port( $src ) { |
| 219 | $src_parts = wp_parse_url( $src ); |
| 220 | if ( ! is_array( $src_parts ) || empty( $src_parts['host'] ) ) { |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | foreach ( array_unique( array( home_url( '/' ), site_url( '/' ) ) ) as $site_url ) { |
| 225 | $site_url_parts = wp_parse_url( $site_url ); |
| 226 | if ( ! is_array( $site_url_parts ) || empty( $site_url_parts['host'] ) ) { |
| 227 | continue; |
| 228 | } |
| 229 | |
| 230 | if ( strtolower( $src_parts['host'] ) !== strtolower( $site_url_parts['host'] ) ) { |
| 231 | continue; |
| 232 | } |
| 233 | |
| 234 | $src_port = array_key_exists( 'port', $src_parts ) ? (int) $src_parts['port'] : null; |
| 235 | $site_url_port = array_key_exists( 'port', $site_url_parts ) ? (int) $site_url_parts['port'] : null; |
| 236 | if ( $src_port === $site_url_port ) { |
| 237 | return true; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | return false; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Check if a stylesheet URL is under a local WordPress asset root. |
| 246 | * |
| 247 | * @param string $src The stylesheet URL. |
| 248 | * |
| 249 | * @return bool |
| 250 | */ |
| 251 | function matches_local_asset_url_root( $src ) { |
| 252 | $roots = array( |
| 253 | plugins_url( '/' ), |
| 254 | get_stylesheet_directory_uri(), |
| 255 | get_template_directory_uri(), |
| 256 | ); |
| 257 | |
| 258 | if ( defined( 'FOOGALLERY_URL' ) ) { |
| 259 | $roots[] = FOOGALLERY_URL; |
| 260 | } |
| 261 | |
| 262 | $upload_dir = wp_upload_dir(); |
| 263 | if ( ! empty( $upload_dir['baseurl'] ) ) { |
| 264 | $roots[] = trailingslashit( $upload_dir['baseurl'] ) . 'foogallery/'; |
| 265 | } |
| 266 | |
| 267 | foreach ( array_unique( $roots ) as $root ) { |
| 268 | if ( $this->matches_url_root_ignoring_scheme( $src, $root ) ) { |
| 269 | return true; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Check if a URL matches a root URL while ignoring the scheme only. |
| 278 | * |
| 279 | * @param string $url The URL to check. |
| 280 | * @param string $root The root URL. |
| 281 | * |
| 282 | * @return bool |
| 283 | */ |
| 284 | function matches_url_root_ignoring_scheme( $url, $root ) { |
| 285 | $url_parts = wp_parse_url( $url ); |
| 286 | $root_parts = wp_parse_url( $root ); |
| 287 | |
| 288 | if ( ! is_array( $url_parts ) || ! is_array( $root_parts ) || empty( $url_parts['host'] ) || empty( $root_parts['host'] ) ) { |
| 289 | return false; |
| 290 | } |
| 291 | |
| 292 | if ( strtolower( $url_parts['host'] ) !== strtolower( $root_parts['host'] ) ) { |
| 293 | return false; |
| 294 | } |
| 295 | |
| 296 | $url_port = array_key_exists( 'port', $url_parts ) ? (int) $url_parts['port'] : null; |
| 297 | $root_port = array_key_exists( 'port', $root_parts ) ? (int) $root_parts['port'] : null; |
| 298 | if ( $url_port !== $root_port ) { |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | $url_path = isset( $url_parts['path'] ) ? $url_parts['path'] : '/'; |
| 303 | $root_path = isset( $root_parts['path'] ) ? trailingslashit( $root_parts['path'] ) : '/'; |
| 304 | |
| 305 | if ( '/' === $root_path ) { |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | return 0 === strpos( $url_path, $root_path ); |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Get the old style handle that was linked to the post |
| 314 | * |
| 315 | * @param $post_id |
| 316 | * @param $handle_to_find |
| 317 | * |
| 318 | * @return false|mixed |
| 319 | */ |
| 320 | function get_old_style_post_meta_value( $post_id, $handle_to_find ) { |
| 321 | $css = get_post_meta($post_id, FOOGALLERY_META_POST_USAGE_CSS); |
| 322 | |
| 323 | foreach ($css as $css_item) { |
| 324 | if ( ! $css_item ) { |
| 325 | continue; |
| 326 | } |
| 327 | foreach ( $css_item as $handle => $style ) { |
| 328 | //only enqueue the stylesheet once |
| 329 | if ( $handle_to_find === $handle ) { |
| 330 | return $style; |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | return false; |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 |