Plugin Directory

Changeset 3030898


Ignore:
Timestamp:
02/03/2024 03:00:15 PM (2 years ago)
Author:
kovshenin
Message:

Surge 1.1.0

Location:
surge/trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • surge/trunk/include/cache.php

    r2846380 r3030898  
    1010
    1111namespace Surge;
     12
     13if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
     14    return;
     15}
    1216
    1317include_once( __DIR__ . '/common.php' );
     
    2428
    2529    if ( $ttl < 1 ) {
     30        header( 'X-Cache: bypass' );
    2631        return $contents;
    2732    }
     
    7075
    7176    if ( $skip ) {
     77        header( 'X-Cache: bypass' );
    7278        return $contents;
    7379    }
     
    8490    ];
    8591
    86     $meta = json_encode( $meta );
     92    $meta_json = json_encode( $meta );
    8793    $cache_key = md5( json_encode( $key ) );
    8894    $level = substr( $cache_key, -2 );
     
    98104    // Could not create file.
    99105    if ( false === $f ) {
     106        header( 'X-Cache: bypass' );
    100107        return $contents;
    101108    }
    102109
    103110    fwrite( $f, '<?php exit; ?>' );
    104     fwrite( $f, pack( 'L', strlen( $meta ) ) );
    105     fwrite( $f, $meta );
     111    fwrite( $f, pack( 'L', strlen( $meta_json ) ) );
     112    fwrite( $f, $meta_json );
    106113    fwrite( $f, $contents );
    107114
     
    116123    }
    117124
     125    event( 'request', [ 'meta' => $meta ] );
    118126    return $contents;
    119127};
  • surge/trunk/include/common.php

    r2846380 r3030898  
    4949        // Add items to this array to add a unique cache variant.
    5050        'variants' => [],
     51
     52        // Add callbacks to events early to do crazy stuff.
     53        'events' => [],
    5154    ];
    5255
     
    246249    return true;
    247250}
     251
     252/**
     253 * Execute an event.
     254 *
     255 * @param string $event The event name.
     256 * @param array $args An array for arguments to pass to callbacks.
     257 */
     258function event( $event, $args ) {
     259    $events = config( 'events' );
     260
     261    if ( empty( $events[ $event ] ) ) {
     262        return;
     263    }
     264
     265    foreach ( $events[ $event ] as $key => $callback ) {
     266        $callback( $args );
     267    }
     268}
  • surge/trunk/include/cron.php

    r2711432 r3030898  
    4848    foreach ( $files as $filename ) {
    4949        // Some files after scandir may already be gone/renamed.
     50        if ( ! file_exists( $filename ) ) {
     51            continue;
     52        }
     53
    5054        $stat = @stat( $filename );
    5155        if ( ! $stat ) {
  • surge/trunk/include/invalidate.php

    r2846380 r3030898  
    9898    expire( sprintf( 'post:%d:%d', $blog_id, $post_id ) );
    9999}, 10, 2 );
     100
     101// Multisite network/blog flags.
     102add_action( 'init', function() {
     103    if ( is_multisite() ) {
     104        flag( sprintf( 'network:%d:%d', get_current_network_id(), get_current_blog_id() ) );
     105    }
     106} );
    100107
    101108// Last-minute expirations, save flags.
     
    118125        'update_option_posts_per_page',
    119126        'update_option_woocommerce_permalinks',
     127    ];
     128
     129    $flush_actions = apply_filters( 'surge_flush_actions', $flush_actions );
     130
     131    $ms_flush_actions = [
     132        '_core_updated_successfully',
    120133        'automatic_updates_complete',
    121         '_core_updated_successfully',
    122134    ];
    123135
     136    $expire_flag = is_multisite()
     137        ? sprintf( 'network:%d:%d', get_current_network_id(), get_current_blog_id() )
     138        : '/';
     139
    124140    foreach ( $flush_actions as $action ) {
     141        if ( did_action( $action ) ) {
     142            expire( $expire_flag );
     143            break;
     144        }
     145    }
     146
     147    // Multisite flush actions expire the entire network.
     148    foreach ( $ms_flush_actions as $action ) {
    125149        if ( did_action( $action ) ) {
    126150            expire( '/' );
     
    174198    fwrite( $f, '<?php exit; ?>' . json_encode( $flags ) );
    175199    fclose( $f );
     200
     201    event( 'expire', [ 'flags' => $expire ] );
    176202} );
    177203
  • surge/trunk/include/serve.php

    r2674872 r3030898  
    5353        // Invalidate by path.
    5454        if ( substr( $flag, 0, 1 ) == '/' ) {
    55             if ( substr( $meta['path'], 0, strlen( $flag ) ) ) {
     55            if ( substr( $meta['path'], 0, strlen( $flag ) ) === $flag ) {
    5656                header( 'X-Cache: expired' );
    5757                fclose( $f );
     
    8181
    8282header( 'X-Cache: hit' );
    83 // header( 'X-Flags: ' . implode( ', ', $meta['flags'] ) );
     83event( 'request', [ 'meta' => $meta ] );
    8484fpassthru( $f ); // Pass the remaining bytes to the output.
    8585fclose( $f );
  • surge/trunk/readme.txt

    r2968909 r3030898  
    44Tags: cache, performance, caching
    55Requires at least: 5.7
    6 Tested up to: 6.3
     6Tested up to: 6.4
    77Requires PHP: 7.3
    8 Stable tag: 1.0.5
     8Stable tag: 1.1.0
    99License: GPLv3 or later
    1010License URI: https://www.gnu.org/licenses/gpl-3.0.en.html
     
    6767== Changelog ==
    6868
     69= 1.1.0 =
     70* Improved Multisite compatibility
     71* Fixed occasional stat() warnings in cleanup routines
     72* Fixed expiration by path being too broad
     73* Added a filter for flush actions
     74* Feature: added a simple events system for s-maxage and stale-while-revalidate support
     75
    6976= 1.0.5 =
    7077* Fix woocommerce_product_title compatibility
  • surge/trunk/surge.php

    r2846380 r3030898  
    5151} );
    5252
     53// Support for 6.1+ cache headers check.
     54add_filter( 'site_status_page_cache_supported_cache_headers', function( $headers ) {
     55    $headers['x-cache'] = static function( $value ) {
     56        return false !== strpos( strtolower( $value ), 'hit' );
     57    };
     58    return $headers;
     59} );
     60
    5361// Schedule cron events.
    5462add_action( 'shutdown', function() {
Note: See TracChangeset for help on using the changeset viewer.