Changeset 2256985
- Timestamp:
- 03/09/2020 12:04:28 AM (6 years ago)
- Location:
- rocket-async-css/trunk
- Files:
-
- 1 added
- 7 edited
-
README.txt (modified) (1 diff)
-
lib/Rocket/Async/CSS.php (modified) (4 diffs)
-
lib/Rocket/Async/CSS/Cache/Manager.php (modified) (6 diffs)
-
lib/Rocket/Async/CSS/Integration/Manager.php (modified) (1 diff)
-
lib/Rocket/Async/CSS/Integration/ResponsiveImages.php (modified) (8 diffs)
-
lib/Rocket/Async/CSS/Integration/VisualComposer.php (added)
-
lib/Rocket/Async/CSS/Request.php (modified) (4 diffs)
-
rocket-async-css.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
rocket-async-css/trunk/README.txt
r2186619 r2256985 56 56 57 57 == Changelog == 58 59 ### 0.7.1.3 ### 60 61 * Bug: Handle edge case where file does not have an extension when fetching remote files 62 * Bug: If wpautop is hooked, call it early to prevent DOMDocument from nuking newlines in ResponsiveImages module 63 * Enhancement: Optimize loading of picturefill by doing feature detection to conditionally load script 64 * Enhancement: Add support for purging cache via cron, using rocket_aync_css_background_cache_purge_item_threshold filter, and preload cache if enabled after 65 * Enhancement: Add object cache support to fetching the attachment ID's for a url to reduce unneeded db strain, in ResponsiveImages module 66 * Enhancement: Use the remote server to detect the mime type and map it to our registered types if available when fetching files 67 * Enhancement: Add filter, rocket_async_css_do_download_remote_file, to allow skipping fetching a remote file 68 * Compatibility: Add Visual Composer compatibility 58 69 59 70 -
rocket-async-css/trunk/lib/Rocket/Async/CSS.php
r2186619 r2256985 20 20 * Plugin version 21 21 */ 22 const VERSION = '0.7.1. 2';22 const VERSION = '0.7.1.3'; 23 23 24 24 /** … … 1356 1356 } 1357 1357 $fixed_match = http_build_url( $this->get_url_parts( $match, $url ) ); 1358 $data = $this->remote_fetch( $fixed_match ); 1358 1359 if ( ! apply_filters( 'rocket_async_css_do_download_remote_file', true, $fixed_match, $css ) ) { 1360 return $css; 1361 } 1362 1363 $data = $this->remote_fetch( $fixed_match ); 1364 1359 1365 if ( ! empty( $data ) ) { 1360 1366 $content_hash = md5( $data ); … … 1368 1374 } 1369 1375 1376 if ( empty( $info['extension'] ) ) { 1377 $request = wp_remote_head( $fixed_match, [ 1378 'user-agent' => 'WP-Rocket', 1379 'sslverify' => false, 1380 ] ); 1381 1382 $mime_type = wp_remote_retrieve_header( $request, 'content-type' ); 1383 if ( empty( $mime_type ) ) { 1384 $mime_type = wp_remote_retrieve_header( $request, 'Content-Type' ); 1385 } 1386 $mimes = array_flip( wp_get_mime_types() ); 1387 if ( isset( $mimes[ $mime_type ] ) ) { 1388 $info['extension'] = $mimes[ $mime_type ]; 1389 } 1390 } 1391 1370 1392 $hash = md5( $match_parts['scheme'] . '://' . $info['dirname'] . ( ! empty( $match_parts['port'] ) ? ":{$match_parts['port']}" : '' ) . '/' . $info['filename'] ); 1371 $filename = $this->get_cache_path() . $hash . '.' . $info['extension'];1393 $filename = $this->get_cache_path() . $hash . ( ! empty( $info['extension'] ) ? '.' . $info['extension'] : '' ); 1372 1394 $final_url = parse_url( get_rocket_cdn_url( set_url_scheme( str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, $filename ) ), [ 1373 1395 'all', … … 1508 1530 private function parse_css_imports( $matches, $match, $index, $match_parts, $css, $url ) { 1509 1531 if ( $this->is_url_parts_remote( $match_parts ) ) { 1510 $imported_data = $this->remote_fetch( $match);1532 $imported_data = $this->remote_fetch( http_build_url( $match_parts ) ); 1511 1533 } else { 1512 $match = $this->strip_cdn( $match);1534 $match = $this->strip_cdn( http_build_url( $match_parts ) ); 1513 1535 // Is this a URL? If so replace with ABSPATH 1514 1536 $path = str_replace( $this->home, untrailingslashit( ABSPATH ), $match ); -
rocket-async-css/trunk/lib/Rocket/Async/CSS/Cache/Manager.php
r2175464 r2256985 34 34 add_action( 'after_rocket_clean_term', [ $this, 'purge_term' ] ); 35 35 add_action( 'after_rocket_clean_file', [ $this, 'purge_url' ] ); 36 add_action( 'after_rocket_clean_domain', [ $this, 'do_preload' ], 10, 0 );37 36 add_action( 'wp_rocket_start_preload', 'run_rocket_sitemap_preload', 10, 0 ); 37 add_action( 'rocket_async_css_purge_cache', [ $this, 'do_purge_cache' ] ); 38 if ( is_admin() && $this->get_admin_cache_flag() ) { 39 add_action( 'admin_notices', [ $this, 'cache_purge_notice' ] ); 40 } 38 41 $this->store->set_prefix( CSS::TRANSIENT_PREFIX ); 39 42 $interval = 0; … … 45 48 } 46 49 50 private function get_admin_cache_flag() { 51 return get_transient( $this->get_admin_cache_flag_name() ); 52 } 53 54 private function get_admin_cache_flag_name() { 55 return "{$this->plugin->get_safe_slug()}_cache_purging"; 56 } 57 58 /** 59 * 60 */ 61 public function purge_cache() { 62 if ( $this->get_admin_cache_flag() && ! wp_next_scheduled( 'rocket_async_css_purge_cache' ) ) { 63 $this->clear_admin_cache_flag(); 64 } 65 if ( $this->get_admin_cache_flag() ) { 66 return; 67 } 68 $size = $this->store->get_cache_fragment( [ 'count' ] ); 69 70 if ( $size > apply_filters( 'rocket_aync_css_background_cache_purge_item_threshold', 25, $size ) && ! wp_doing_cron() ) { 71 wp_schedule_single_event( time(), 'rocket_async_css_purge_cache' ); 72 $this->set_admin_cache_flag(); 73 return; 74 } 75 $this->do_purge_cache(); 76 } 77 78 private function clear_admin_cache_flag() { 79 delete_transient( $this->get_admin_cache_flag_name() ); 80 } 81 82 private function set_admin_cache_flag() { 83 set_transient( $this->get_admin_cache_flag_name(), true, DAY_IN_SECONDS ); 84 } 85 86 public function do_purge_cache() { 87 $this->set_admin_cache_flag(); 88 $this->store->delete_cache_branch(); 89 $this->delete_minify_files(); 90 $this->clear_admin_cache_flag(); 91 remove_action( 'after_rocket_clean_domain', [ $this, 'purge_cache' ] ); 92 rocket_clean_domain(); 93 $this->do_preload(); 94 } 95 96 private function delete_minify_files() { 97 rocket_rrmdir( $this->plugin->get_cache_path() ); 98 } 47 99 48 100 function do_preload() { … … 55 107 } 56 108 57 /** 58 * 59 */ 60 public function purge_cache() { 61 $this->store->delete_cache_branch(); 62 rocket_rrmdir( $this->plugin->get_cache_path() ); 109 public function cache_purge_notice() { 110 $class = 'notice notice-info'; 111 $message = __( sprintf( '%s is currently purging the CSS minify cache in the background', $this->plugin->get_plugin_info( 'Name' ) ), $this->plugin->get_safe_slug() ); 112 113 printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) ); 63 114 } 64 115 … … 67 118 */ 68 119 public function purge_post( $post ) { 69 $this->store->delete_cache_branch( [ 'cache', "post_{$post->ID}" ] ); 120 $path = [ 'cache', "post_{$post->ID}" ]; 121 $this->delete_cache_file( $path ); 122 } 123 124 private function delete_cache_file( $path ) { 125 $cache = $this->store->get_cache_fragment( array_merge( $path, [ 'cache_1' ] ) ); 126 if ( ! empty( $cache ) ) { 127 foreach ( $cache as $item ) { 128 $item = explode( '_', $item ); 129 $item = end( $item ); 130 $item = $this->store->get_cache_fragment( array_merge( $path, [ $item ] ) ); 131 if ( ! empty( $item['filename'] ) ) { 132 $this->plugin->wp_filesystem->delete( $item['filename'] ); 133 } 134 } 135 } 136 $this->store->delete_cache_branch( $path ); 70 137 } 71 138 … … 74 141 */ 75 142 public function purge_term( $term ) { 76 $this->store->delete_cache_branch( [ 'cache', "term_{$term->term_id}" ] ); 143 $path = [ 'cache', "term_{$term->term_id}" ]; 144 $this->delete_cache_file( $path ); 77 145 } 78 146 … … 81 149 */ 82 150 public function purge_url( $url ) { 83 $url = md5( $url ); 84 $this->store->delete_cache_branch( [ 'cache', "url_{$url}" ] ); 151 $url = md5( $url ); 152 $path = [ 'cache', "url_{$url}" ]; 153 $this->delete_cache_file( $path ); 85 154 } 86 155 -
rocket-async-css/trunk/lib/Rocket/Async/CSS/Integration/Manager.php
r2175464 r2256985 34 34 'EssentialAddonsElementor', 35 35 'Genesis', 36 'VisualComposer', 36 37 ]; 37 38 -
rocket-async-css/trunk/lib/Rocket/Async/CSS/Integration/ResponsiveImages.php
r2186619 r2256985 9 9 use Rocket\Async\CSS\DOMDocument; 10 10 11 /** 12 * Class ResponsiveImages 13 * @package Rocket\Async\CSS\Integration 14 */ 11 15 class ResponsiveImages extends Component { 16 /** 17 * 18 */ 19 const CACHE_NAME_URL_TO_ID = 'rocket_async_css_responsive_images_urls'; 20 /** 21 * 22 */ 23 const CACHE_NAME_ID_TO_URL = 'rocket_async_css_responsive_images'; 24 /** 25 * @var 26 */ 12 27 private $current_guid; 28 /** 29 * @var DOMDocument 30 */ 13 31 private $document; 14 32 … … 22 40 } 23 41 42 /** 43 * 44 */ 24 45 public function init() { 25 46 add_filter( 'wp', [ $this, 'wp_loaded' ] ); 26 47 } 27 48 49 /** 50 * 51 */ 28 52 public function wp_loaded() { 29 53 if ( is_admin() || wp_is_xml_request() || wp_is_json_request() || is_feed() || ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) ) { … … 44 68 add_filter( 'rocket_async_css_request_buffer', [ $this, 'process' ], 10000 ); 45 69 add_filter( 'wp_get_attachment_image_attributes', [ $this, 'maybe_remove_src' ], 10, 2 ); 46 } 47 70 add_action( 'attachment_updated', [ $this, 'clear_attachment_cache' ] ); 71 add_action( 'delete_attachment', [ $this, 'clear_attachment_cache' ] ); 72 } 73 74 /** 75 * @param $content 76 * 77 * @return string|string[]|null 78 */ 48 79 public function process( $content ) { 49 80 … … 57 88 58 89 $partial = false; 90 91 if ( has_filter( current_filter(), 'wpautop' ) ) { 92 $new_content = wpautop( $new_content ); 93 $new_content = shortcode_unautop( $new_content ); 94 } 59 95 60 96 if ( ! preg_match( '/<html[^>]*>/', $new_content ) ) { … … 132 168 continue; 133 169 } 134 add_filter( 'posts_where_paged', [ $this, 'filter_where' ] ); 135 $this->current_guid = $this->plugin->strip_cdn( $src ); 136 $attachments = get_posts( [ 137 'post_type' => 'attachment', 138 'suppress_filters' => false, 139 'posts_per_page' => 1, 140 'order_by' => 'none', 141 ] ); 142 remove_filter( 'posts_where_paged', [ $this, 'filter_where' ] ); 143 if ( ! empty( $attachments ) ) { 144 $attachment_id = end( $attachments )->ID; 145 } 170 $striped_src = $this->plugin->strip_cdn( $src ); 171 if ( ! ( $attachments = wp_cache_get( $striped_src, self::CACHE_NAME_URL_TO_ID ) ) ) { 172 add_filter( 'posts_where_paged', [ $this, 'filter_where' ] ); 173 $this->current_guid = $striped_src; 174 $attachments = get_posts( [ 175 'post_type' => 'attachment', 176 'suppress_filters' => false, 177 'posts_per_page' => 1, 178 'order_by' => 'none', 179 ] ); 180 remove_filter( 'posts_where_paged', [ $this, 'filter_where' ] ); 181 if ( ! empty( $attachments ) ) { 182 $attachment_id = end( $attachments )->ID; 183 } 184 wp_cache_set( $striped_src, $attachment_id, self::CACHE_NAME_URL_TO_ID ); 185 wp_cache_set( $attachment_id, $striped_src, self::CACHE_NAME_ID_TO_URL ); 186 } 187 146 188 if ( empty( $attachment_id ) ) { 147 189 $collection->next(); … … 227 269 } 228 270 271 /** 272 * @param $where 273 * 274 * @return string 275 */ 229 276 public 230 277 function filter_where( … … 243 290 } 244 291 292 /** 293 * @param $attr 294 * @param \WP_Post $attachment 295 * 296 * @return mixed 297 */ 245 298 public function maybe_remove_src( $attr, \WP_Post $attachment ) { 246 299 if ( ! empty( $attr['srcset'] ) || ! empty( $attr['data-srcset'] ) ) { … … 262 315 return $attr; 263 316 } 317 318 /** 319 * @param $post_id 320 */ 321 public function clear_attachment_cache( $post_id ) { 322 if ( $url = wp_cache_get( $post_id, self::CACHE_NAME_ID_TO_URL ) ) { 323 wp_cache_delete( $url, self::CACHE_NAME_URL_TO_ID ); 324 wp_cache_delete( $post_id, self::CACHE_NAME_ID_TO_URL ); 325 } 326 } 264 327 } -
rocket-async-css/trunk/lib/Rocket/Async/CSS/Request.php
r2129572 r2256985 28 28 add_filter( 'pre_get_rocket_option_async_css', '__return_zero' ); 29 29 add_action( 'wp_footer', [ $this, 'scripts' ], PHP_INT_MAX ); 30 add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); 30 add_action( 'wp_head', [ $this, 'polyfill_detect' ] ); 31 add_action( 'mime_types', [ $this, 'register_mimes' ] ); 31 32 } 32 33 add_filter( 'pre_get_rocket_option_minify_concatenate_css', '__return_zero' ); … … 63 64 } 64 65 65 public function enqueue_scripts() {66 wp_enqueue_script( 'picturefill', plugins_url( 'assets/js/picturefill.min.js', $this->plugin->get_plugin_file() ), '3.0.3' );67 }68 69 66 public function process_buffer( $buffer ) { 70 67 if ( apply_filters( 'rocket_async_css_do_request_buffer', true ) ) { … … 73 70 74 71 return $buffer; 72 } 73 74 public function polyfill_detect() { 75 $polyfill_url = plugins_url( 'assets/js/picturefill.min.js', $this->plugin->get_plugin_file() ); 76 ?> 77 <script type="text/javascript" data-no-minify="1"> 78 !(window.HTMLPictureElement||"sizes"in document.createElement("img"))&&function(b,a){a=b.createElement("script");a.type="text/javascript";a.async=!0;a.src=<?= wp_json_encode( $polyfill_url ) ?>;b.getElementsByTagName("head")[0].appendChild(a)}(document); 79 </script> 80 <?php 75 81 } 76 82 … … 83 89 window.dispatchEvent(new CustomEvent("JSLoaded")); 84 90 } 91 85 92 </script> 86 93 <?php 87 94 } 95 96 public function register_mimes( $mimes ) { 97 $mimes['woff2'] = 'application/font-woff2'; 98 $mimes['woff'] = 'application/font-woff'; 99 $mimes['otf'] = 'font/opentype'; 100 $mimes['ttf'] = 'font/truetype'; 101 return $mimes; 102 } 88 103 } -
rocket-async-css/trunk/rocket-async-css.php
r2186619 r2256985 17 17 * Plugin URI: https://github.com/pcfreak30/rocket-async-css 18 18 * Description: WordPress plugin to combine all CSS load async including inline scripts. Extends WP-Rocket 19 * Version: 0.7.1. 219 * Version: 0.7.1.3 20 20 * Author: Derrick Hammer 21 21 * Author URI: http://www.derrickhammer.com
Note: See TracChangeset
for help on using the changeset viewer.