class-admin-bar.php
4 weeks ago
class-aioseo-sitemaps.php
4 weeks ago
class-css-load-optimizer.php
4 weeks ago
class-foogallery-template-loader.php
4 weeks ago
class-public.php
4 weeks ago
class-rank-math-seo-sitemaps.php
4 weeks ago
class-shortcodes.php
4 weeks ago
class-yoast-seo-sitemaps.php
4 weeks ago
index.php
4 weeks ago
class-css-load-optimizer.php
344 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | /** |
| 8 | * FooGallery_CSS_Load_Optimizer class which enqueues CSS in the head |
| 9 | */ |
| 10 | if (!class_exists('class-css-load-optimizer.php')) { |
| 11 | |
| 12 | class FooGallery_CSS_Load_Optimizer { |
| 13 | |
| 14 | function __construct() { |
| 15 | add_action( 'wp_enqueue_scripts', array( $this, 'include_gallery_css' ) ); |
| 16 | add_action( 'foogallery_enqueue_style', array( $this, 'enqueue_style_to_persist' ), 10, 5 ); |
| 17 | add_action( 'wp_footer', array( $this, 'persist_enqueued_styles' ) ); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Persist any styles that are enqueued to be persisted |
| 22 | */ |
| 23 | function persist_enqueued_styles() { |
| 24 | global $wp_query, $foogallery_styles_to_persist; |
| 25 | |
| 26 | //we only want to do this if we are looking at a single post |
| 27 | if ( ! is_singular() ) { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | $post_id = $wp_query->post->ID; |
| 32 | if ( $post_id && is_array( $foogallery_styles_to_persist ) ) { |
| 33 | foreach( $foogallery_styles_to_persist as $style_handle => $style ) { |
| 34 | add_post_meta( $post_id, FOOGALLERY_META_POST_USAGE_CSS, array( $style_handle => $style ), false ); |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Get the current post ids for the view that is being shown |
| 41 | */ |
| 42 | function get_post_ids_from_query() { |
| 43 | global $wp_query; |
| 44 | |
| 45 | if ( is_singular() ) { |
| 46 | return array( $wp_query->post->ID ); |
| 47 | } else if ( is_array( $wp_query->posts ) ) { |
| 48 | return wp_list_pluck( $wp_query->posts, 'ID' ); |
| 49 | } else { |
| 50 | return array(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Checks the post meta for any FooGallery CSS that needs to be added to the head |
| 56 | */ |
| 57 | function include_gallery_css() { |
| 58 | global $enqueued_foogallery_styles; |
| 59 | |
| 60 | $enqueued_foogallery_styles = array(); |
| 61 | |
| 62 | foreach( $this->get_post_ids_from_query() as $post_id ) { |
| 63 | $this->include_gallery_stylesheets_for_post( $post_id ); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * includes any CSS that needs to be added for a post |
| 69 | * |
| 70 | * @param $post_id int ID of the post |
| 71 | */ |
| 72 | function include_gallery_stylesheets_for_post( $post_id ) { |
| 73 | global $enqueued_foogallery_styles; |
| 74 | |
| 75 | if ( $post_id ) { |
| 76 | //get any foogallery stylesheets that the post might need to include |
| 77 | $css = get_post_meta($post_id, FOOGALLERY_META_POST_USAGE_CSS); |
| 78 | |
| 79 | if ( empty( $css ) || !is_array( $css ) ) return; |
| 80 | |
| 81 | foreach ($css as $css_item) { |
| 82 | if ( !$css_item ) continue; |
| 83 | if ( empty( $css_item ) || !is_array( $css_item ) ) return; //make sure we are dealing with an array |
| 84 | foreach ($css_item as $handle => $style) { |
| 85 | //only enqueue the stylesheet once |
| 86 | if ( !array_key_exists( $handle, $enqueued_foogallery_styles ) ) { |
| 87 | $cache_buster_key = $handle; |
| 88 | if ( is_array( $style ) ) { |
| 89 | $cache_buster_key = $this->create_cache_buster_key( $handle, $style['ver'], array_key_exists( 'site', $style ) ? $style['site'] : '' ); |
| 90 | wp_enqueue_style( $handle, $this->normalize_stylesheet_src_scheme( $style['src'] ), $style['deps'], $style['ver'], $style['media'] ); |
| 91 | } else { |
| 92 | wp_enqueue_style( $handle, $this->normalize_stylesheet_src_scheme( $style ) ); |
| 93 | } |
| 94 | |
| 95 | $enqueued_foogallery_styles[$handle] = $cache_buster_key; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Check to make sure we have added the stylesheets to our custom post meta field, |
| 104 | * so that on next render the stylesheet will be added to the page header |
| 105 | * |
| 106 | * @param $style_handle string The stylesheet handle |
| 107 | * @param $src string The location for the stylesheet |
| 108 | * @param array $deps |
| 109 | * @param bool $ver |
| 110 | * @param string $media |
| 111 | */ |
| 112 | function enqueue_style_to_persist($style_handle, $src, $deps = array(), $ver = false, $media = 'all') { |
| 113 | global $wp_query, $enqueued_foogallery_styles, $foogallery_styles_to_persist; |
| 114 | |
| 115 | //we only want to do this if we are looking at a single post |
| 116 | if ( ! is_singular() ) { |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | $post_id = $wp_query->post->ID; |
| 121 | if ( $post_id ) { |
| 122 | |
| 123 | //check if the saved stylesheet needs to be cache busted |
| 124 | if ( is_array( $enqueued_foogallery_styles ) && array_key_exists( $style_handle, $enqueued_foogallery_styles ) ) { |
| 125 | $registered_cache_buster_key = $enqueued_foogallery_styles[$style_handle]; |
| 126 | |
| 127 | //generate the key we want |
| 128 | $cache_buster_key = $this->create_cache_buster_key( $style_handle, $ver, home_url() ); |
| 129 | |
| 130 | if ( $registered_cache_buster_key !== $cache_buster_key ) { |
| 131 | //we need to bust this cached stylesheet! |
| 132 | $style = $this->get_old_style_post_meta_value( $post_id, $style_handle ); |
| 133 | |
| 134 | if ( false !== $style ) { |
| 135 | //delete it from the post |
| 136 | delete_post_meta( $post_id, FOOGALLERY_META_POST_USAGE_CSS, array( $style_handle => $style ) ); |
| 137 | |
| 138 | //unset the handle, to force the save of the post meta |
| 139 | unset( $enqueued_foogallery_styles[$style_handle] ); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | //first check that the template has not been enqueued before |
| 145 | if ( is_array( $enqueued_foogallery_styles ) && ! array_key_exists( $style_handle, $enqueued_foogallery_styles ) ) { |
| 146 | |
| 147 | $style = array( |
| 148 | 'src' => $this->normalize_stylesheet_src_scheme( $src ), |
| 149 | 'deps' => $deps, |
| 150 | 'ver' => $ver, |
| 151 | 'media' => $media, |
| 152 | 'site' => home_url() |
| 153 | ); |
| 154 | |
| 155 | if ( !is_array( $foogallery_styles_to_persist ) ) { |
| 156 | $foogallery_styles_to_persist = array(); |
| 157 | } |
| 158 | |
| 159 | if ( !array_key_exists( $style_handle, $foogallery_styles_to_persist ) ) { |
| 160 | $foogallery_styles_to_persist[$style_handle] = $style; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Create a key that will be used to cache |
| 168 | * |
| 169 | * @param $name |
| 170 | * @param $version |
| 171 | * @param string $site |
| 172 | * |
| 173 | * @return string |
| 174 | */ |
| 175 | function create_cache_buster_key( $name, $version, $site = '' ) { |
| 176 | return "{$site}::{$name}_{$version}"; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Normalize cached stylesheet URLs to the configured front-end site scheme. |
| 181 | * |
| 182 | * The optimizer stores full URLs in post meta. If the same page is ever rendered under |
| 183 | * a different request scheme, WordPress can build plugin URLs with that scheme and the |
| 184 | * stale cached URL may later be emitted on the canonical front end. |
| 185 | * |
| 186 | * @param string $src The stylesheet URL. |
| 187 | * |
| 188 | * @return string |
| 189 | */ |
| 190 | function normalize_stylesheet_src_scheme( $src ) { |
| 191 | if ( ! is_string( $src ) || ! preg_match( '#^https?://#i', $src ) ) { |
| 192 | return $src; |
| 193 | } |
| 194 | |
| 195 | if ( 'on' === foogallery_get_setting( 'force_https' ) ) { |
| 196 | return set_url_scheme( $src, 'https' ); |
| 197 | } |
| 198 | |
| 199 | $home_url_parts = wp_parse_url( home_url( '/' ) ); |
| 200 | if ( ! is_array( $home_url_parts ) || empty( $home_url_parts['scheme'] ) ) { |
| 201 | return $src; |
| 202 | } |
| 203 | |
| 204 | $scheme = strtolower( $home_url_parts['scheme'] ); |
| 205 | if ( ! in_array( $scheme, array( 'http', 'https' ), true ) ) { |
| 206 | return $src; |
| 207 | } |
| 208 | |
| 209 | if ( ! $this->matches_site_url_host_and_port( $src ) || ! $this->matches_local_asset_url_root( $src ) ) { |
| 210 | return $src; |
| 211 | } |
| 212 | |
| 213 | return set_url_scheme( $src, $scheme ); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Check if a stylesheet URL is on the configured home or site host. |
| 218 | * |
| 219 | * @param string $src The stylesheet URL. |
| 220 | * |
| 221 | * @return bool |
| 222 | */ |
| 223 | function matches_site_url_host_and_port( $src ) { |
| 224 | $src_parts = wp_parse_url( $src ); |
| 225 | if ( ! is_array( $src_parts ) || empty( $src_parts['host'] ) ) { |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | foreach ( array_unique( array( home_url( '/' ), site_url( '/' ) ) ) as $site_url ) { |
| 230 | $site_url_parts = wp_parse_url( $site_url ); |
| 231 | if ( ! is_array( $site_url_parts ) || empty( $site_url_parts['host'] ) ) { |
| 232 | continue; |
| 233 | } |
| 234 | |
| 235 | if ( strtolower( $src_parts['host'] ) !== strtolower( $site_url_parts['host'] ) ) { |
| 236 | continue; |
| 237 | } |
| 238 | |
| 239 | $src_port = array_key_exists( 'port', $src_parts ) ? (int) $src_parts['port'] : null; |
| 240 | $site_url_port = array_key_exists( 'port', $site_url_parts ) ? (int) $site_url_parts['port'] : null; |
| 241 | if ( $src_port === $site_url_port ) { |
| 242 | return true; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Check if a stylesheet URL is under a local WordPress asset root. |
| 251 | * |
| 252 | * @param string $src The stylesheet URL. |
| 253 | * |
| 254 | * @return bool |
| 255 | */ |
| 256 | function matches_local_asset_url_root( $src ) { |
| 257 | $roots = array( |
| 258 | plugins_url( '/' ), |
| 259 | get_stylesheet_directory_uri(), |
| 260 | get_template_directory_uri(), |
| 261 | ); |
| 262 | |
| 263 | if ( defined( 'FOOGALLERY_URL' ) ) { |
| 264 | $roots[] = FOOGALLERY_URL; |
| 265 | } |
| 266 | |
| 267 | $upload_dir = wp_upload_dir(); |
| 268 | if ( ! empty( $upload_dir['baseurl'] ) ) { |
| 269 | $roots[] = trailingslashit( $upload_dir['baseurl'] ) . 'foogallery/'; |
| 270 | } |
| 271 | |
| 272 | foreach ( array_unique( $roots ) as $root ) { |
| 273 | if ( $this->matches_url_root_ignoring_scheme( $src, $root ) ) { |
| 274 | return true; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Check if a URL matches a root URL while ignoring the scheme only. |
| 283 | * |
| 284 | * @param string $url The URL to check. |
| 285 | * @param string $root The root URL. |
| 286 | * |
| 287 | * @return bool |
| 288 | */ |
| 289 | function matches_url_root_ignoring_scheme( $url, $root ) { |
| 290 | $url_parts = wp_parse_url( $url ); |
| 291 | $root_parts = wp_parse_url( $root ); |
| 292 | |
| 293 | if ( ! is_array( $url_parts ) || ! is_array( $root_parts ) || empty( $url_parts['host'] ) || empty( $root_parts['host'] ) ) { |
| 294 | return false; |
| 295 | } |
| 296 | |
| 297 | if ( strtolower( $url_parts['host'] ) !== strtolower( $root_parts['host'] ) ) { |
| 298 | return false; |
| 299 | } |
| 300 | |
| 301 | $url_port = array_key_exists( 'port', $url_parts ) ? (int) $url_parts['port'] : null; |
| 302 | $root_port = array_key_exists( 'port', $root_parts ) ? (int) $root_parts['port'] : null; |
| 303 | if ( $url_port !== $root_port ) { |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | $url_path = isset( $url_parts['path'] ) ? $url_parts['path'] : '/'; |
| 308 | $root_path = isset( $root_parts['path'] ) ? trailingslashit( $root_parts['path'] ) : '/'; |
| 309 | |
| 310 | if ( '/' === $root_path ) { |
| 311 | return false; |
| 312 | } |
| 313 | |
| 314 | return 0 === strpos( $url_path, $root_path ); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Get the old style handle that was linked to the post |
| 319 | * |
| 320 | * @param $post_id |
| 321 | * @param $handle_to_find |
| 322 | * |
| 323 | * @return false|mixed |
| 324 | */ |
| 325 | function get_old_style_post_meta_value( $post_id, $handle_to_find ) { |
| 326 | $css = get_post_meta($post_id, FOOGALLERY_META_POST_USAGE_CSS); |
| 327 | |
| 328 | foreach ($css as $css_item) { |
| 329 | if ( ! $css_item ) { |
| 330 | continue; |
| 331 | } |
| 332 | foreach ( $css_item as $handle => $style ) { |
| 333 | //only enqueue the stylesheet once |
| 334 | if ( $handle_to_find === $handle ) { |
| 335 | return $style; |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | return false; |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 |