class-admin-bar.php
7 months ago
class-aioseo-sitemaps.php
7 months ago
class-css-load-optimizer.php
7 months ago
class-foogallery-template-loader.php
7 months ago
class-public.php
7 months ago
class-rank-math-seo-sitemaps.php
7 months ago
class-shortcodes.php
7 months ago
class-yoast-seo-sitemaps.php
7 months ago
index.php
11 years ago
class-css-load-optimizer.php
200 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, $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 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' => $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 | * Get the old style handle that was linked to the post |
| 176 | * |
| 177 | * @param $post_id |
| 178 | * @param $handle_to_find |
| 179 | * |
| 180 | * @return false|mixed |
| 181 | */ |
| 182 | function get_old_style_post_meta_value( $post_id, $handle_to_find ) { |
| 183 | $css = get_post_meta($post_id, FOOGALLERY_META_POST_USAGE_CSS); |
| 184 | |
| 185 | foreach ($css as $css_item) { |
| 186 | if ( ! $css_item ) { |
| 187 | continue; |
| 188 | } |
| 189 | foreach ( $css_item as $handle => $style ) { |
| 190 | //only enqueue the stylesheet once |
| 191 | if ( $handle_to_find === $handle ) { |
| 192 | return $style; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | return false; |
| 198 | } |
| 199 | } |
| 200 | } |