Plugin Directory

Changeset 2256985


Ignore:
Timestamp:
03/09/2020 12:04:28 AM (6 years ago)
Author:
pcfreak30
Message:

*Version bump to 0.7.1.3

Location:
rocket-async-css/trunk
Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • rocket-async-css/trunk/README.txt

    r2186619 r2256985  
    5656
    5757== 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
    5869
    5970
  • rocket-async-css/trunk/lib/Rocket/Async/CSS.php

    r2186619 r2256985  
    2020     * Plugin version
    2121     */
    22     const VERSION = '0.7.1.2';
     22    const VERSION = '0.7.1.3';
    2323
    2424    /**
     
    13561356        }
    13571357        $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
    13591365        if ( ! empty( $data ) ) {
    13601366            $content_hash = md5( $data );
     
    13681374            }
    13691375
     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
    13701392            $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'] : '' );
    13721394            $final_url = parse_url( get_rocket_cdn_url( set_url_scheme( str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, $filename ) ), [
    13731395                'all',
     
    15081530    private function parse_css_imports( $matches, $match, $index, $match_parts, $css, $url ) {
    15091531        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 ) );
    15111533        } else {
    1512             $match = $this->strip_cdn( $match );
     1534            $match = $this->strip_cdn( http_build_url( $match_parts ) );
    15131535            // Is this a URL? If so replace with ABSPATH
    15141536            $path          = str_replace( $this->home, untrailingslashit( ABSPATH ), $match );
  • rocket-async-css/trunk/lib/Rocket/Async/CSS/Cache/Manager.php

    r2175464 r2256985  
    3434        add_action( 'after_rocket_clean_term', [ $this, 'purge_term' ] );
    3535        add_action( 'after_rocket_clean_file', [ $this, 'purge_url' ] );
    36         add_action( 'after_rocket_clean_domain', [ $this, 'do_preload' ], 10, 0 );
    3736        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        }
    3841        $this->store->set_prefix( CSS::TRANSIENT_PREFIX );
    3942        $interval = 0;
     
    4548    }
    4649
     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    }
    4799
    48100    function do_preload() {
     
    55107    }
    56108
    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 ) );
    63114    }
    64115
     
    67118     */
    68119    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 );
    70137    }
    71138
     
    74141     */
    75142    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 );
    77145    }
    78146
     
    81149     */
    82150    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 );
    85154    }
    86155
  • rocket-async-css/trunk/lib/Rocket/Async/CSS/Integration/Manager.php

    r2175464 r2256985  
    3434        'EssentialAddonsElementor',
    3535        'Genesis',
     36        'VisualComposer',
    3637    ];
    3738
  • rocket-async-css/trunk/lib/Rocket/Async/CSS/Integration/ResponsiveImages.php

    r2186619 r2256985  
    99use Rocket\Async\CSS\DOMDocument;
    1010
     11/**
     12 * Class ResponsiveImages
     13 * @package Rocket\Async\CSS\Integration
     14 */
    1115class 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     */
    1227    private $current_guid;
     28    /**
     29     * @var DOMDocument
     30     */
    1331    private $document;
    1432
     
    2240    }
    2341
     42    /**
     43     *
     44     */
    2445    public function init() {
    2546        add_filter( 'wp', [ $this, 'wp_loaded' ] );
    2647    }
    2748
     49    /**
     50     *
     51     */
    2852    public function wp_loaded() {
    2953        if ( is_admin() || wp_is_xml_request() || wp_is_json_request() || is_feed() || ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) ) {
     
    4468        add_filter( 'rocket_async_css_request_buffer', [ $this, 'process' ], 10000 );
    4569        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     */
    4879    public function process( $content ) {
    4980
     
    5788
    5889        $partial = false;
     90
     91        if ( has_filter( current_filter(), 'wpautop' ) ) {
     92            $new_content = wpautop( $new_content );
     93            $new_content = shortcode_unautop( $new_content );
     94        }
    5995
    6096        if ( ! preg_match( '/<html[^>]*>/', $new_content ) ) {
     
    132168                    continue;
    133169                }
    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
    146188                if ( empty( $attachment_id ) ) {
    147189                    $collection->next();
     
    227269    }
    228270
     271    /**
     272     * @param $where
     273     *
     274     * @return string
     275     */
    229276    public
    230277    function filter_where(
     
    243290    }
    244291
     292    /**
     293     * @param $attr
     294     * @param \WP_Post $attachment
     295     *
     296     * @return mixed
     297     */
    245298    public function maybe_remove_src( $attr, \WP_Post $attachment ) {
    246299        if ( ! empty( $attr['srcset'] ) || ! empty( $attr['data-srcset'] ) ) {
     
    262315        return $attr;
    263316    }
     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    }
    264327}
  • rocket-async-css/trunk/lib/Rocket/Async/CSS/Request.php

    r2129572 r2256985  
    2828            add_filter( 'pre_get_rocket_option_async_css', '__return_zero' );
    2929            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' ] );
    3132        }
    3233        add_filter( 'pre_get_rocket_option_minify_concatenate_css', '__return_zero' );
     
    6364    }
    6465
    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 
    6966    public function process_buffer( $buffer ) {
    7067        if ( apply_filters( 'rocket_async_css_do_request_buffer', true ) ) {
     
    7370
    7471        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
    7581    }
    7682
     
    8389                window.dispatchEvent(new CustomEvent("JSLoaded"));
    8490            }
     91
    8592        </script>
    8693        <?php
    8794    }
     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    }
    88103}
  • rocket-async-css/trunk/rocket-async-css.php

    r2186619 r2256985  
    1717 * Plugin URI:        https://github.com/pcfreak30/rocket-async-css
    1818 * Description:       WordPress plugin to combine all CSS load async including inline scripts. Extends WP-Rocket
    19  * Version:           0.7.1.2
     19 * Version:           0.7.1.3
    2020 * Author:            Derrick Hammer
    2121 * Author URI:        http://www.derrickhammer.com
Note: See TracChangeset for help on using the changeset viewer.