Changeset 2577281
- Timestamp:
- 08/03/2021 03:14:17 PM (5 years ago)
- Location:
- optimize-scripts-styles/trunk
- Files:
-
- 3 edited
-
library/functions.php (modified) (8 diffs)
-
readme.txt (modified) (2 diffs)
-
sp-optimize-scripts.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
optimize-scripts-styles/trunk/library/functions.php
r2378823 r2577281 74 74 **************************/ 75 75 76 // extend WP_Scripts - only used if $optimize_scripts == false and $remove_script_type == true 76 /** 77 * Extend WP_Scripts - only used if $optimize_scripts == false and $remove_script_type == true 78 */ 77 79 class SPOS_Scripts extends WP_Scripts { 78 80 79 // override print_extra_script from class.wp-scripts.php line 198 81 /** 82 * Override print_extra_script from class.wp-scripts.php line 198 83 * @param string $handle 84 * @param bool $echo 85 * @return string 86 */ 80 87 public function print_extra_script( $handle, $echo = true ) { 81 88 if ( !$output = $this->get_data( $handle, 'data' ) ) … … 95 102 } 96 103 104 /** 105 * Remove the type= attribute from <script> and <style> tags 106 * @param string $tag 107 * @param string $handle 108 * @return string 109 */ 97 110 function spos_remove_tag_type( $tag, $handle ) { 98 111 return preg_replace( "/\stype=['\"]text\/(javascript|css)['\"]/", '', $tag ); … … 241 254 $deps = $obj->deps; 242 255 $deps_loaded = true; 243 foreach ( $deps as $dep ) { 256 foreach ( $deps as $dep ) { // dependency check only goes one level deep 244 257 // is it already in the file? 245 258 // is it loaded from this domain? … … 399 412 $flag_path = $script_path . '.txt'; 400 413 401 // check if the file exists and is less than 48 hoursold414 // check if the file exists and is less than $cache_lifespan old 402 415 if ( file_exists( $flag_path ) && filemtime( $flag_path ) > ( time() - $cache_lifespan ) && !$disable_cache ) { 403 416 // the files have already been created. register the existing files … … 449 462 $deps = $obj->deps; 450 463 $deps_loaded = true; 451 foreach ( $deps as $dep ) { 464 foreach ( $deps as $dep ) { // dependency check only goes one level deep 452 465 // is it already in the file? 453 466 // is it loaded from this domain? … … 579 592 $flag_path = $style_path . '.txt'; 580 593 581 // check if the file exists and is less than 48 hoursold594 // check if the file exists and is less than $cache_lifespan old 582 595 if ( file_exists( $flag_path ) && filemtime( $flag_path ) > ( time() - $cache_lifespan ) && !$disable_cache ) { 583 596 // register the existing files … … 629 642 // does the file end in .css? 630 643 // this helps to eliminate problems with php files masquerading as css 631 if ( substr( $wp_styles->registered[$style_name]->src, -4 ) !== '.css' ) { 632 continue; 633 } 634 644 if ( substr( $wp_styles->registered[$style_name]->src, -4 ) !== '.css' ) continue; 645 646 // make sure it's a local stylesheet 635 647 if ( isset( $wp_styles->registered[$style_name] ) && strpos( $wp_styles->registered[$style_name]->src, get_bloginfo('url') ) !== false ) { 636 648 $obj = $wp_styles->registered[$style_name]; 637 649 638 650 // keep track of conditionals to add in afterwards 651 // these won't be included in the optimized file 639 652 if ( array_key_exists( 'conditional', $obj->extra ) ) { 640 653 $conditional_styles[] = $obj->handle; 641 //wp_deregister_style( $obj->handle ); 642 continue; 654 continue; // skip to the next stylesheet 643 655 } 644 645 // does the file exist? 646 $path = realpath( '.' . str_replace( get_bloginfo('url'), '', preg_replace( '/\?.*/', '', $wp_styles->registered[$style_name]->src ) ) ); 647 if ( $path ) { 648 $deps = $obj->deps; 649 $deps_loaded = true; 650 foreach ( $deps as $dep ) { 651 if ( !in_array( $dep, $wp_styles->queue ) ) { 652 $deps_loaded = false; 653 break; 656 657 $content = get_styles_content( 658 $obj->handle, 659 $obj->src 660 ); 661 662 if ( $content ) { 663 // check for dependencies 664 if ( $obj->deps ) { 665 $deps_content = get_deps_content( $obj->deps, $included_styles ); 666 if ( !$deps_content ) { 667 continue; // dependencies weren't able to load - skip this stylesheet 654 668 } 655 669 } 656 if ( $deps_loaded ) { 657 658 $src = file_get_contents( $path ); 659 // does the file have @imports? 660 preg_match_all( '/@import url\((.*?)\);/', $src, $matches ); 661 662 if ( !empty( $matches[0] ) ) { 663 $imports = ''; 664 foreach( $matches[0] as $at_import ) { 665 // remove the import 666 $src = str_replace( $at_import, '', $src ); 667 } 668 foreach ( $matches[1] as $import_path ) { 669 $full_path = str_replace( '\\', '/', dirname( $path ) ) . '/' . str_replace( array( '\'', '"' ), '', $import_path ); 670 $real_import_path = realpath( $full_path ); 671 if ( $real_import_path ) { 672 $import_src = file_get_contents( $real_import_path ); 673 $imports .= '/* @import url(' . $import_path . ') */' . "\r\n" . $import_src . "\r\n"; 674 } 675 } 676 $src = $imports . $src; 677 } 678 679 // update url's to point to the right place 680 $patharr = explode( '/', $wp_styles->registered[$style_name]->src ); 681 $pathlen = count( $patharr ); 682 $pattern = '/url\((.*?)\)/'; 683 $src = preg_replace_callback( 684 $pattern, 685 function( $matches ) { 686 if ( strpos( $matches[0], 'http' ) === false ) { 687 return 'url(INSERT_URL/' . str_replace( array( '\'', '"', 'url(', ')' ), '', $matches[0] ) . ')'; 688 } else { 689 return $matches[0]; 690 } 691 }, 692 $src 693 ); 694 695 $src = str_replace( 'INSERT_URL', implode( '/', array_slice( $patharr, 0, $pathlen - 1 ) ), $src ); 696 697 if ( empty( $src ) ) { 698 } else { 699 $media_type = isset( $wp_styles->registered[$style_name]->args ) ? $wp_styles->registered[$style_name]->args : 'all'; 700 $src = "\r\n\r\n" . '/* !!!!!!!!!!!!!!!!!!!! ' . $wp_styles->registered[$style_name]->handle . ' !!!!!!!!!!!!!!!!!!!!! */ ' . "\r\n" . str_replace( '@charset "utf-8";', '', $src ); 701 if ( !isset( $buffer[$media_type] ) ) { 702 $buffer[$media_type] = $src; 703 } else { 704 $buffer[$media_type] .= $src; 705 } 706 $included_styles[] = $wp_styles->registered[$style_name]->handle; 707 //wp_deregister_style( $wp_styles->registered[$style_name]->handle ); 708 wp_dequeue_style( $wp_styles->registered[$style_name]->handle ); 709 } 710 711 } // end if 712 } // end if 713 } // end if 670 671 $media_type = isset( $wp_styles->registered[$style_name]->args ) ? $wp_styles->registered[$style_name]->args : 'all'; 672 673 if ( !isset( $buffer[$media_type] ) ) { 674 $buffer[$media_type] = $content; 675 } else { 676 $buffer[$media_type] .= $content; 677 } 678 $included_styles[] = $wp_styles->registered[$style_name]->handle; 679 //wp_deregister_style( $wp_styles->registered[$style_name]->handle ); 680 wp_dequeue_style( $wp_styles->registered[$style_name]->handle ); 681 } // end if content 682 683 } // end if it's local 684 714 685 } // end foreach 715 686 … … 773 744 UTILITY 774 745 ********************/ 746 747 /** 748 * Get the path from the src of a stylesheet 749 * @param string $src - from wp_styles 750 * @return string or false if it's not valid 751 */ 752 // returns false if the file doesn't exist 753 function get_path_from_src( $src ) { 754 return realpath( '.' . str_replace( get_bloginfo('url'), '', preg_replace( '/\?.*/', '', $src ) ) ); 755 } 756 757 /** 758 * The main function to process stylesheet content 759 * @param string $handle 760 * @param string $src 761 * @return string 762 */ 763 function get_styles_content( $handle, $src ) { 764 $path = get_path_from_src( $src ); 765 if ( !$path ) { 766 return false; 767 } 768 769 // get the file's contents 770 $content = file_get_contents( $path ); 771 772 // include @imports at the beginning of the content 773 $content = include_imports( $content ); 774 775 // update url's to point to the right place 776 $content = update_urls( $content, $src ); 777 778 // remove charset 779 $content = remove_charset( $content ); 780 781 // add the handle name before the content 782 $content = "\r\n\r\n" . '/* !!!!!!!!!!!!!!!!!!!! ' . $handle . ' !!!!!!!!!!!!!!!!!!!!! */ ' . "\r\n" . $content; 783 784 return $content; 785 } 786 787 /** 788 * Find @import statements in the CSS and include those files before the 789 * rest of the content 790 * @param string $content 791 * @return string 792 */ 793 function include_imports( $content ) { 794 // does the file have @imports? 795 preg_match_all( '/@import url\((.*?)\);/', $content, $matches ); 796 797 if ( !empty( $matches[0] ) ) { 798 $imports = ''; 799 foreach( $matches[0] as $at_import ) { 800 // remove the import 801 $content = str_replace( $at_import, '', $content ); 802 } 803 foreach ( $matches[1] as $import_path ) { 804 $full_path = str_replace( '\\', '/', dirname( $path ) ) . '/' . str_replace( array( '\'', '"' ), '', $import_path ); 805 $real_import_path = realpath( $full_path ); 806 if ( $real_import_path ) { 807 $import_content = file_get_contents( $real_import_path ); 808 $imports .= '/* @import url(' . $import_path . ') */' . "\r\n" . $import_content . "\r\n"; 809 } 810 } 811 $content = $imports . $content; 812 } 813 814 return $content; 815 } 816 817 /** 818 * Update the URLs to match the new location of the cached stylesheet 819 * @param string $content 820 * @param string $src 821 * @return string 822 */ 823 function update_urls( $content, $src ) { 824 $patharr = explode( '/', $src ); 825 $pathlen = count( $patharr ); 826 $pattern = '/url\((.*?)\)/'; 827 $content = preg_replace_callback( 828 $pattern, 829 function( $matches ) { 830 if ( strpos( $matches[0], 'http' ) === false ) { 831 return 'url(INSERT_URL/' . str_replace( array( '\'', '"', 'url(', ')' ), '', $matches[0] ) . ')'; 832 } else { 833 return $matches[0]; 834 } 835 }, 836 $content 837 ); 838 839 $content = str_replace( 'INSERT_URL', implode( '/', array_slice( $patharr, 0, $pathlen - 1 ) ), $content ); 840 841 return $content; 842 } 843 844 /** 845 * Remove the charset so that it's not declared more than once 846 * @param string $content 847 * @return string 848 */ 849 function remove_charset( $content ) { 850 return str_ireplace( '@charset "utf-8";', '', $content ); 851 } 852 853 /** 854 * Recursive function to grab dependency content 855 * @global object $wp_styles 856 * @param array $deps 857 * @param array $included_styles 858 * @return string 859 */ 860 function get_deps_content( $deps, &$included_styles ) { 861 global $wp_styles; 862 $content = ''; 863 864 foreach( $deps as $dep ) { 865 if ( !in_array( $dep, $included_styles ) ) { 866 // include nested dependencies 867 if ( $wp_styles->registered[$dep]->deps ) { 868 $dep_dep_content = get_deps_content( $wp_styles->registered[$dep]->deps, $included_styles ); 869 if ( !$dep_dep_content ) { 870 return false; 871 } else { 872 $content .= $dep_dep_content; 873 } 874 } 875 876 $dep_content = get_styles_content( 877 $wp_styles->registered[$dep]->handle, 878 $wp_styles->registered[$dep]->src 879 ); 880 if ( !$dep_content ) { 881 return false; 882 } else { 883 $content .= $dep_content; 884 $included_styles[] = $dep; 885 } 886 } else { 887 // already included 888 return true; 889 } 890 } 891 892 return $content; 893 } 894 895 /** 896 * Determine the cache lifespan in seconds based on the plugin settings 897 * @global object $spos_settings 898 * @return integer 899 */ 775 900 function spos_get_cache_lifespan() { 776 901 global $spos_settings; -
optimize-scripts-styles/trunk/readme.txt
r2479980 r2577281 4 4 Tags: scripts, styles, optimize, optimization, minify, compress, seo, performance, combine 5 5 Requires at least: 4.0 6 Tested up to: 5. 66 Tested up to: 5.8 7 7 Requires PHP: 5.6 8 Stable tag: 1.9. 28 Stable tag: 1.9.3 9 9 License: GPLv2 or later 10 10 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 90 90 91 91 == Changelog == 92 93 = 1.9.3 = 94 * Added recursive stylesheet dependency inclusion 95 * Better code comments 92 96 93 97 = 1.9.2 = -
optimize-scripts-styles/trunk/sp-optimize-scripts.php
r2378823 r2577281 4 4 * Plugin URI: https://www.seismicpixels.com/optimize-scripts-styles-for-wordpress/ 5 5 * Description: Provides a quick way to combine and minify all scripts and styles and cache them in your content folder. 6 * Version: 1.9. 26 * Version: 1.9.3 7 7 * Author: Seismic Pixels 8 8 * Author URI: https://www.seismicpixels.com 9 * Copyright 202 0Sean Michaud - Seismic Pixels, LLC9 * Copyright 2021 Sean Michaud - Seismic Pixels, LLC 10 10 */ 11 11 12 12 global $spos_version; 13 $spos_version = '1.9. 2';13 $spos_version = '1.9.3'; 14 14 15 15 global $spos_settings; … … 84 84 85 85 function spos_logged_in_message() { 86 echo '<!-- Optimized scripts disabled for logged in users -->' . "\r\n";86 echo "\r\n" . '<!-- Optimized scripts disabled for logged in users -->' . "\r\n"; 87 87 } 88 88 89 89 function spos_footer_message() { 90 echo '<!-- Optimize Scripts & Styles by Seismic Pixels -->' . "\r\n";90 echo "\r\n" . '<!-- Optimize Scripts & Styles by Seismic Pixels -->' . "\r\n"; 91 91 } 92 92
Note: See TracChangeset
for help on using the changeset viewer.