close

Plugin Directory

Changeset 3494018


Ignore:
Timestamp:
03/29/2026 09:03:11 PM (8 weeks ago)
Author:
xavivars
Message:

Bypass cache

Location:
xv-random-quotes/trunk
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • xv-random-quotes/trunk/changelog.txt

    r3494013 r3494018  
    11== XV Random Quotes ==
     2
     3= 2.9.0 =
     4New feature: Cache bypass mode — AJAX-enabled quotes can now fetch a fresh quote on every page load, bypassing full-page caches like WP Rocket
     5New feature: Global "Bypass Page Cache" setting in AJAX Settings
     6New feature: Per-block "Bypass page cache" toggle in the Random Quote block editor
     7New feature: cache_bypass shortcode attribute for [stray-random]
     8Fix: AJAX refresh no longer sends a WP nonce header, preventing failures on pages cached longer than 12-24 hours
     9Fix: Admin card padding overridden to restore proper spacing when other plugins (e.g. Subscribe to Comments Reloaded) zero out .card padding
    210
    311= 2.8.0 =
  • xv-random-quotes/trunk/js/quote-refresh.js

    r3463709 r3494018  
    3232        // Set up auto-refresh timers
    3333        const containers = document.querySelectorAll('.xv-quote-container[data-timer]');
    34        
     34
    3535        containers.forEach(function(container) {
    3636            const timer = parseInt(container.getAttribute('data-timer'), 10);
    37            
     37
    3838            if (timer > 0) {
    3939                setInterval(function() {
     
    4141                }, timer * 1000); // Convert seconds to milliseconds
    4242            }
     43        });
     44
     45        // Load-on-init: fetch a fresh quote immediately for cache-bypass containers
     46        const initContainers = document.querySelectorAll('.xv-quote-container[data-load-on-init="1"]');
     47
     48        initContainers.forEach(function(container) {
     49            refreshQuote(container);
    4350        });
    4451    }
     
    95102            method: 'GET',
    96103            headers: {
    97                 'Content-Type': 'application/json',
    98                 'X-WP-Nonce': xvQuoteRefresh.restNonce
    99             },
    100             credentials: 'same-origin'
     104                'Content-Type': 'application/json'
     105            }
    101106        })
    102107        .then(function(response) {
  • xv-random-quotes/trunk/readme.txt

    r3494013 r3494018  
    66Tested up to: 6.9
    77Requires PHP: 7.4
    8 Stable tag: 2.8.0
     8Stable tag: 2.9.0
    99License: GPLv2 or later
    1010License URI: http://www.gnu.org/licenses/gpl-2.0.html
  • xv-random-quotes/trunk/src/Admin/OverviewPage.php

    r3435038 r3494018  
    231231                    max-width: 100%;
    232232                    margin-bottom: 20px;
     233                    padding: 16px 20px 20px !important;
    233234                }
    234235                .wrap .card h3 {
  • xv-random-quotes/trunk/src/Admin/Settings.php

    r3494013 r3494018  
    5252    // AJAX Settings
    5353    const OPTION_AJAX           = 'xv_quotes_ajax';
     54    const OPTION_CACHE_BYPASS   = 'xv_quotes_cache_bypass';
    5455    const OPTION_LOADER         = 'xv_quotes_loader';
    5556    const OPTION_BEFORE_LOADER  = 'xv_quotes_before_loader';
     
    196197        register_setting(
    197198            self::SETTINGS_GROUP,
     199            self::OPTION_CACHE_BYPASS,
     200            array(
     201                'type'              => 'string',
     202                'default'           => '0',
     203                'sanitize_callback' => array( $this, 'sanitize_checkbox' ),
     204            )
     205        );
     206
     207        register_setting(
     208            self::SETTINGS_GROUP,
    198209            self::OPTION_LOADER,
    199210            array(
     
    523534                'option_name' => self::OPTION_AJAX,
    524535                'description' => __( 'Check to disable AJAX dynamic loading entirely. When unchecked, AJAX can still be disabled from widgets, shortcodes, or template tags.', 'xv-random-quotes' ),
     536            )
     537        );
     538
     539        add_settings_field(
     540            self::OPTION_CACHE_BYPASS,
     541            __( 'Bypass Page Cache Globally', 'xv-random-quotes' ),
     542            array( $this, 'render_checkbox_field' ),
     543            self::PAGE_SLUG,
     544            'xv_quotes_ajax',
     545            array(
     546                'label_for'   => self::OPTION_CACHE_BYPASS,
     547                'option_name' => self::OPTION_CACHE_BYPASS,
     548                'description' => __( 'When enabled, all AJAX-enabled quotes will fetch a fresh quote on every page load, bypassing full-page caches like WP Rocket. Requires AJAX to be enabled on each quote block or shortcode.', 'xv-random-quotes' ),
    525549            )
    526550        );
     
    798822                max-width: 100%;
    799823                margin-bottom: 20px;
     824                padding: 16px 20px 20px !important;
    800825            }
    801826            .xv-quotes-settings .card h2 {
  • xv-random-quotes/trunk/src/Blocks/RandomQuote/block.json

    r3463747 r3494018  
    3737            "type": "number",
    3838            "default": 0
     39        },
     40        "cacheBypass": {
     41            "type": "boolean",
     42            "default": false
    3943        }
    4044    },
  • xv-random-quotes/trunk/src/Blocks/RandomQuote/index.tsx

    r3463747 r3494018  
    1616    enableAjax: boolean;
    1717    timer: number;
     18    cacheBypass: boolean;
    1819}
    1920
     
    6061                />
    6162                {attributes.enableAjax && (
     63                    <>
    6264                    <RangeControl
    6365                        label={__('Auto-refresh timer (seconds)', 'xv-random-quotes')}
     
    6870                        help={__('0 = no auto-refresh', 'xv-random-quotes')}
    6971                    />
     72                    <ToggleControl
     73                        label={__('Bypass page cache', 'xv-random-quotes')}
     74                        checked={attributes.cacheBypass}
     75                        onChange={(value) => setAttributes({ cacheBypass: value })}
     76                        help={__('Fetch a fresh quote on every page load, even when the page is cached', 'xv-random-quotes')}
     77                    />
     78                    </>
    7079                )}
    7180            </BlockEditor>
  • xv-random-quotes/trunk/src/Blocks/RandomQuote/render.php

    r3430809 r3494018  
    2222    $enableAjax    = $attributes['enableAjax'] ?? false;
    2323    $timer         = $attributes['timer'] ?? 0;
     24    $cacheBypass   = $attributes['cacheBypass'] ?? false;
    2425
    2526    // Use QuoteOutput class for complete rendering (including AJAX if enabled)
     
    3334            'disableaspect' => $disableaspect,
    3435            'enable_ajax'   => $enableAjax,
     36            'cache_bypass'  => $cacheBypass,
    3537            'timer'         => $timer,
    3638        )
  • xv-random-quotes/trunk/src/Output/QuoteOutput.php

    r3463709 r3494018  
    8282                'contributor'   => '',
    8383                'enable_ajax'   => false,
     84                'cache_bypass'  => false,
    8485                'link_phrase'   => '',
    8586                'timer'         => 0,
     
    118119        // Wrap with AJAX functionality if enabled
    119120        if ( $args['enable_ajax'] ) {
    120             return $this->wrap_with_ajax( $quote_html, $args, $quotes );
     121            // Apply global cache bypass setting if not already enabled per-instance
     122            $cache_bypass = $args['cache_bypass'];
     123            if ( ! $cache_bypass && get_option( Settings::OPTION_CACHE_BYPASS, false ) === '1' ) {
     124                $cache_bypass = true;
     125            }
     126            return $this->wrap_with_ajax( $quote_html, $args, $quotes, $cache_bypass );
    121127        }
    122128
     
    127133     * Wrap quote HTML with AJAX container and refresh link
    128134     *
    129      * @param string $quote_html The rendered quote HTML.
    130      * @param array  $args       Arguments array with AJAX configuration.
    131      * @param array  $quotes     Array of WP_Post quote objects being displayed.
     135     * @param string $quote_html  The rendered quote HTML.
     136     * @param array  $args        Arguments array with AJAX configuration.
     137     * @param array  $quotes      Array of WP_Post quote objects being displayed.
     138     * @param bool   $cache_bypass When true, renders an empty container so JS fetches a fresh quote on every page load.
    132139     * @return string HTML wrapped with AJAX functionality.
    133140     */
    134     private function wrap_with_ajax( $quote_html, $args, $quotes = array() ) {
     141    private function wrap_with_ajax( $quote_html, $args, $quotes = array(), $cache_bypass = false ) {
    135142        // Generate or use provided container ID
    136143        $container_id = ! empty( $args['container_id'] ) ? $args['container_id'] : 'xv-quote-container-' . uniqid();
     
    164171        $output .= ' data-timer="' . esc_attr( absint( $args['timer'] ) ) . '"';
    165172
     173        if ( $cache_bypass ) {
     174            $output .= ' data-load-on-init="1"';
     175        }
     176
    166177        $output .= '>';
    167         $output .= $quote_html;
     178
     179        if ( ! $cache_bypass ) {
     180            $output .= $quote_html;
     181        }
    168182
    169183        // Add refresh link (only if not auto-refresh only)
     
    211225                'xvQuoteRefresh',
    212226                array(
    213                     'restUrl'   => esc_url_raw( rest_url( 'xv-random-quotes/v1/quote/random' ) ),
    214                     'restNonce' => wp_create_nonce( 'wp_rest' ),
     227                    'restUrl' => esc_url_raw( rest_url( 'xv-random-quotes/v1/quote/random' ) ),
    215228                )
    216229            );
  • xv-random-quotes/trunk/src/Shortcodes/ShortcodeHandlers.php

    r3463709 r3494018  
    111111        'noajax' => '',
    112112        'enable_ajax' => '',
     113        'cache_bypass' => '',
    113114        'multi' => 1,
    114115        'timer' => '',
     
    138139    }
    139140
     141    $cache_bypass = ! empty( $atts['cache_bypass'] ) ? filter_var( $atts['cache_bypass'], FILTER_VALIDATE_BOOLEAN ) : false;
     142
    140143    // Use QuoteOutput class for complete rendering (including AJAX if enabled)
    141144    $quote_output = new QuoteOutput();
     
    149152            'contributor'   => $atts['user'],
    150153            'enable_ajax'   => $enable_ajax,
     154            'cache_bypass'  => $cache_bypass,
    151155            'link_phrase'   => $atts['linkphrase'],
    152156            'timer'         => ! empty( $atts['timer'] ) ? absint( $atts['timer'] ) : 0,
  • xv-random-quotes/trunk/src/generated/random-quote-block.asset.php

    r3463747 r3494018  
    1 <?php return array('dependencies' => array('react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-i18n', 'wp-server-side-render'), 'version' => '53d7bb16db4cdfcfcaec');
     1<?php return array('dependencies' => array('react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-i18n', 'wp-server-side-render'), 'version' => '0486856f86ec2cf71c23');
  • xv-random-quotes/trunk/src/generated/random-quote-block.js

    r3463747 r3494018  
    1 (()=>{"use strict";var e={n:o=>{var n=o&&o.__esModule?()=>o.default:()=>o;return e.d(n,{a:n}),n},d:(o,n)=>{for(var t in n)e.o(n,t)&&!e.o(o,t)&&Object.defineProperty(o,t,{enumerable:!0,get:n[t]})},o:(e,o)=>Object.prototype.hasOwnProperty.call(e,o)};const o=window.wp.blocks,n=window.wp.components,t=window.wp.i18n,a=window.wp.blockEditor,r=window.wp.serverSideRender;var s=e.n(r);const l=window.ReactJSXRuntime,d=({blockName:e,attributes:o,children:r,renderCondition:d=!0,placeholderMessage:i=null})=>{const u=(0,a.useBlockProps)();return(0,l.jsxs)(l.Fragment,{children:[(0,l.jsx)(a.InspectorControls,{children:(0,l.jsx)(n.PanelBody,{title:(0,t.__)("Quote Settings","xv-random-quotes"),children:r})}),(0,l.jsx)("div",{...u,children:d?(0,l.jsx)(s(),{block:e,attributes:o}):i&&(0,l.jsx)("p",{children:i})})]})},i=({value:e,onChange:o})=>(0,l.jsx)(n.TextControl,{label:(0,t.__)("Categories (comma-separated slugs)","xv-random-quotes"),value:e,onChange:o,help:(0,t.__)("Leave empty for all categories","xv-random-quotes")}),u=({checked:e,onChange:o})=>(0,l.jsx)(n.ToggleControl,{label:(0,t.__)("Disable styling","xv-random-quotes"),checked:e,onChange:o,help:(0,t.__)("Remove default HTML wrappers","xv-random-quotes")});(0,o.registerBlockType)("xv-random-quotes/random-quote",{edit:e=>{const{attributes:o,setAttributes:a}=e;return(0,l.jsxs)(d,{blockName:"xv-random-quotes/random-quote",attributes:o,children:[(0,l.jsx)(i,{value:o.categories,onChange:e=>a({categories:e})}),(0,l.jsx)(n.RangeControl,{label:(0,t.__)("Number of quotes","xv-random-quotes"),value:o.multi,onChange:e=>a({multi:e||1}),min:1,max:10,help:(0,t.__)("How many quotes to display","xv-random-quotes")}),(0,l.jsx)(n.ToggleControl,{label:(0,t.__)("Sequential order","xv-random-quotes"),checked:o.sequence,onChange:e=>a({sequence:e}),help:(0,t.__)("Show quotes in order instead of random","xv-random-quotes")}),(0,l.jsx)(u,{checked:o.disableaspect,onChange:e=>a({disableaspect:e})}),(0,l.jsx)(n.ToggleControl,{label:(0,t.__)("Enable AJAX refresh","xv-random-quotes"),checked:o.enableAjax,onChange:e=>a({enableAjax:e})}),o.enableAjax&&(0,l.jsx)(n.RangeControl,{label:(0,t.__)("Auto-refresh timer (seconds)","xv-random-quotes"),value:o.timer,onChange:e=>a({timer:e}),min:0,max:300,help:(0,t.__)("0 = no auto-refresh","xv-random-quotes")})]})},save:()=>null})})();
     1(()=>{"use strict";var e={n:o=>{var n=o&&o.__esModule?()=>o.default:()=>o;return e.d(n,{a:n}),n},d:(o,n)=>{for(var a in n)e.o(n,a)&&!e.o(o,a)&&Object.defineProperty(o,a,{enumerable:!0,get:n[a]})},o:(e,o)=>Object.prototype.hasOwnProperty.call(e,o)};const o=window.wp.blocks,n=window.wp.components,a=window.wp.i18n,t=window.wp.blockEditor,s=window.wp.serverSideRender;var r=e.n(s);const l=window.ReactJSXRuntime,d=({blockName:e,attributes:o,children:s,renderCondition:d=!0,placeholderMessage:c=null})=>{const u=(0,t.useBlockProps)();return(0,l.jsxs)(l.Fragment,{children:[(0,l.jsx)(t.InspectorControls,{children:(0,l.jsx)(n.PanelBody,{title:(0,a.__)("Quote Settings","xv-random-quotes"),children:s})}),(0,l.jsx)("div",{...u,children:d?(0,l.jsx)(r(),{block:e,attributes:o}):c&&(0,l.jsx)("p",{children:c})})]})},c=({value:e,onChange:o})=>(0,l.jsx)(n.TextControl,{label:(0,a.__)("Categories (comma-separated slugs)","xv-random-quotes"),value:e,onChange:o,help:(0,a.__)("Leave empty for all categories","xv-random-quotes")}),u=({checked:e,onChange:o})=>(0,l.jsx)(n.ToggleControl,{label:(0,a.__)("Disable styling","xv-random-quotes"),checked:e,onChange:o,help:(0,a.__)("Remove default HTML wrappers","xv-random-quotes")});(0,o.registerBlockType)("xv-random-quotes/random-quote",{edit:e=>{const{attributes:o,setAttributes:t}=e;return(0,l.jsxs)(d,{blockName:"xv-random-quotes/random-quote",attributes:o,children:[(0,l.jsx)(c,{value:o.categories,onChange:e=>t({categories:e})}),(0,l.jsx)(n.RangeControl,{label:(0,a.__)("Number of quotes","xv-random-quotes"),value:o.multi,onChange:e=>t({multi:e||1}),min:1,max:10,help:(0,a.__)("How many quotes to display","xv-random-quotes")}),(0,l.jsx)(n.ToggleControl,{label:(0,a.__)("Sequential order","xv-random-quotes"),checked:o.sequence,onChange:e=>t({sequence:e}),help:(0,a.__)("Show quotes in order instead of random","xv-random-quotes")}),(0,l.jsx)(u,{checked:o.disableaspect,onChange:e=>t({disableaspect:e})}),(0,l.jsx)(n.ToggleControl,{label:(0,a.__)("Enable AJAX refresh","xv-random-quotes"),checked:o.enableAjax,onChange:e=>t({enableAjax:e})}),o.enableAjax&&(0,l.jsxs)(l.Fragment,{children:[(0,l.jsx)(n.RangeControl,{label:(0,a.__)("Auto-refresh timer (seconds)","xv-random-quotes"),value:o.timer,onChange:e=>t({timer:e}),min:0,max:300,help:(0,a.__)("0 = no auto-refresh","xv-random-quotes")}),(0,l.jsx)(n.ToggleControl,{label:(0,a.__)("Bypass page cache","xv-random-quotes"),checked:o.cacheBypass,onChange:e=>t({cacheBypass:e}),help:(0,a.__)("Fetch a fresh quote on every page load, even when the page is cached","xv-random-quotes")})]})]})},save:()=>null})})();
  • xv-random-quotes/trunk/xv-random-quotes.php

    r3494013 r3494018  
    55Author: Xavi Ivars
    66Author URI: https://xavi.ivars.me/
    7 Version: 2.8.0
     7Version: 2.9.0
    88Requires at least: 6.0
    99Requires PHP: 7.4
Note: See TracChangeset for help on using the changeset viewer.