Plugin Directory

Changeset 3356585


Ignore:
Timestamp:
09/05/2025 10:10:15 AM (7 months ago)
Author:
creativform
Message:

2.0.3

  • Code improvement
  • Fixed bugs with nonce
  • Fixed permissions
Location:
easy-auto-reload
Files:
21 added
2 edited

Legend:

Unmodified
Added
Removed
  • easy-auto-reload/trunk/easy-auto-reload.php

    r3248723 r3356585  
    44 * Plugin Name:       Easy Auto Reload
    55 * Plugin URI:        https://infinitumform.com
    6  * Description:       Auto refresh WordPress pages if there is no site activity after after any number of minutes.
    7  * Version:           2.0.2
     6 * Description:       Auto refresh WordPress pages if there is no site activity after any number of minutes.
     7 * Version:           2.0.3
    88 * Author:            Ivijan-Stefan Stipic
    99 * Author URI:        https://www.linkedin.com/in/ivijanstefanstipic/
     
    3232if ( ! defined( 'WPINC' ) ) { die( "Don't mess with us." ); }
    3333if ( ! defined( 'ABSPATH' ) ) { exit; }
    34 if ( ! defined( 'WP_AUTO_REFRESH_VERSION' ) ) { define( 'WP_AUTO_REFRESH_VERSION', '2.0.1' ); }
     34if ( ! defined( 'WP_AUTO_REFRESH_VERSION' ) ) { define( 'WP_AUTO_REFRESH_VERSION', '2.0.3' ); }
    3535
    3636final class WP_Auto_Refresh{
     
    4040     */
    4141    private static $instance;
     42    // Set options
    4243    private $options;
     44    // Calculate the undeniable truth of the universe
     45    private const ITS_TRUE  = 3/3;
     46    // Calculate the black hole
     47    private const ITS_FALSE = 3.1415-3.1415;
    4348   
    4449    /*
     
    4752    private function __construct () {
    4853        // Include textdomain and other plugin features
    49         add_action('plugins_loaded', [&$this, 'plugins_loaded'], 1, 0);
     54        add_action('plugins_loaded', [$this, 'plugins_loaded'], 1, 0);
    5055        // Add reload scripts to the site
    51         add_action('wp_head', [&$this, 'add_script'], 1, 0);
     56        add_action('wp_head', [$this, 'add_script'], 1, 0);
    5257        if( $this->enable_in_admin() ) {
    53             add_action('admin_head', [&$this, 'add_script'], 1, 0);
     58            add_action('admin_head', [$this, 'add_script'], 1, 0);
    5459        }
    5560        // Admin functionalities
    56         add_action('admin_init', [&$this, 'admin_init'], 10, 0);
    57         add_action('admin_menu', [&$this, 'admin_menu'], 10, 0);
    58         add_action('add_meta_boxes', [&$this, 'add_meta_box'], 10, 0);
    59         add_action('save_post', [&$this, 'save_meta_box_data'], 10, 1);
     61        add_action('admin_init', [$this, 'admin_init'], 10, 0);
     62        add_action('admin_menu', [$this, 'admin_menu'], 10, 0);
     63        add_action('add_meta_boxes', [$this, 'add_meta_box'], 10, 0);
     64        add_action('save_post', [$this, 'save_meta_box_data'], 10, 1);
    6065        // Deactivation
    6166        register_deactivation_hook(__FILE__, function(){
     
    99104        // Add nonce life update
    100105        if( apply_filters('autorefresh_nonce_life_enable', true) && DAY_IN_SECONDS !== $this->get_nonce_life() ) {
    101             add_filter('nonce_life', [&$this, 'nonce_life'], 10, 1);
     106            add_filter('nonce_life', [$this, 'nonce_life'], 10, 1);
    102107        }
    103108    }
     
    106111     * Nonce Life
    107112     */
    108     public function nonce_life ( $default_nonce_life ) {
    109         return $this->get_nonce_life();
     113    public function nonce_life( $default_nonce_life ) {
     114        // Always return a raw value; never call apply_filters('nonce_life', ...) from here
     115        $life = $this->get_nonce_life();
     116        return ( is_int( $life ) && $life > (int)self::ITS_FALSE ) ? $life : $default_nonce_life;
    110117    }
    111118   
     
    115122    public function plugins_loaded() {
    116123        // Load options only if not already set
    117         if (empty($this->options)) {
    118             $this->options = get_option('wp-autorefresh', array());
    119         }
    120 
    121         // Define text domain
     124        if ( empty( $this->options ) ) {
     125            $this->options = get_option('wp-autorefresh', []);
     126        }
     127
    122128        $domain = 'autorefresh';
    123129
    124         // First, attempt to load translations from the WordPress languages directory
    125         load_plugin_textdomain($domain, false, WP_LANG_DIR . "/plugins/");
    126 
    127         // Check if the text domain is already loaded
    128         if (!is_textdomain_loaded($domain)) {
    129             $locale = apply_filters("{$domain}_locale", get_locale(), $domain);
     130        // Preferred: relative path to /languages inside this plugin
     131        load_plugin_textdomain(
     132            $domain,
     133            false,
     134            dirname( plugin_basename( __FILE__ ) ) . '/languages'
     135        );
     136
     137        // Manual fallback (optional)
     138        if ( ! is_textdomain_loaded( $domain ) ) {
     139            $locale = apply_filters( "{$domain}_locale", get_locale(), $domain );
    130140            $domain_path = __DIR__ . '/languages';
    131 
    132             // Possible .mo file locations
    133141            $mo_files = [
    134142                "{$domain_path}/{$domain}-{$locale}.mo",
    135143                "{$domain_path}/{$locale}.mo"
    136144            ];
    137 
    138             // Try to load the translation file
    139             foreach ($mo_files as $mo_file) {
    140                 if (file_exists($mo_file) && load_textdomain($domain, $mo_file)) {
     145            foreach ( $mo_files as $mo_file ) {
     146                if ( file_exists( $mo_file ) && load_textdomain( $domain, $mo_file ) ) {
    141147                    break;
    142148                }
     
    152158            'wp-autorefresh', // Option group
    153159            'wp-autorefresh', // Option name
    154             [&$this, 'sanitize'] // Sanitize
     160            [$this, 'sanitize'] // Sanitize
    155161        );
    156162
     
    158164            'wp-autorefresh', // ID
    159165            esc_attr__('Auto-Refresh Settings','autorefresh'), // Title
    160             [&$this, 'print_section_info'], // Callback
     166            [$this, 'print_section_info'], // Callback
    161167            'wp-autorefresh' // Page
    162168        );
     
    165171            'global_refresh', // ID
    166172            esc_attr__('Auto-Refresh','autorefresh'), // Title
    167             [&$this, 'input_global_refresh__callback'], // Callback
     173            [$this, 'input_global_refresh__callback'], // Callback
    168174            'wp-autorefresh', // Page
    169175            'wp-autorefresh' // Section
     
    173179            'timeout', // ID
    174180            esc_attr__('Auto-Refresh Timeout','autorefresh'), // Title
    175             [&$this, 'input_timeout__callback'], // Callback
     181            [$this, 'input_timeout__callback'], // Callback
    176182            'wp-autorefresh', // Page
    177183            'wp-autorefresh' // Section
     
    181187            'clear_cache', // ID
    182188            esc_attr__('Browser Cache','autorefresh'), // Title
    183             [&$this, 'input_clear_cache__callback'], // Callback
     189            [$this, 'input_clear_cache__callback'], // Callback
    184190            'wp-autorefresh', // Page
    185191            'wp-autorefresh' // Section
     
    189195            'wp_admin', // ID
    190196            esc_attr__('WP Admin','autorefresh'), // Title
    191             [&$this, 'input_wp_admin__callback'], // Callback
     197            [$this, 'input_wp_admin__callback'], // Callback
    192198            'wp-autorefresh', // Page
    193199            'wp-autorefresh' // Section
     
    198204                'nonce_life', // ID
    199205                esc_attr__('Lifespan of nonces','autorefresh'), // Title
    200                 [&$this, 'input_nonce_life__callback'], // Callback
     206                [$this, 'input_nonce_life__callback'], // Callback
    201207                'wp-autorefresh', // Page
    202208                'wp-autorefresh' // Section
     
    207213            'post_type', // ID
    208214            esc_attr__('Allow custom refresh in page and post types','autorefresh'), // Title
    209             [&$this, 'input_post_types__callback'], // Callback
     215            [$this, 'input_post_types__callback'], // Callback
    210216            'wp-autorefresh', // Page
    211217            'wp-autorefresh' // Section
     
    221227            esc_attr__('Auto-Refresh','autorefresh'),
    222228            esc_attr__('Auto-Refresh','autorefresh'),
    223             'administrator',
     229            'manage_options', // <-- use capability, not role
    224230            'wp-autorefresh',
    225             [&$this, 'options_page'],
     231            [$this, 'options_page'],
    226232            6
    227233        );
     
    264270     */
    265271    public function options_page(){
    266         if(!empty($this->options)) {
     272        if(empty($this->options)) {
    267273            $this->options = get_option('wp-autorefresh', array());
    268274        }
     
    323329   
    324330    /*
    325      * Clear Browser cahce
     331     * Clear Browser cache
    326332     */
    327333    public function input_clear_cache__callback(){
     
    334340   
    335341    /*
    336      * Enable autoefresh inside WP Admin
     342     * Enable autorefresh inside WP Admin
    337343     */
    338344    public function input_wp_admin__callback(){
     
    340346            '<label for="wp_admin"><input type="checkbox" id="wp_admin" name="wp-autorefresh[wp_admin]" value="1"%s/>%s</label>',
    341347            ($this->enable_in_admin() ? ' checked' : ''),
    342             __('Enable autoefresh inside WP Admin.','autorefresh')
    343         );
    344     }
    345    
    346     /*
    347      * Enable autoefresh to Post Types
     348            __('Enable autorefresh inside WP Admin.','autorefresh')
     349        );
     350    }
     351   
     352    /*
     353     * Enable autorefresh to Post Types
    348354     */
    349355    public function input_post_types__callback(){
     
    374380   
    375381    /*
    376      * Place JavaScript code inside `wp_head` to prevent brakeing by any other script.
     382     * Place JavaScript code inside `wp_head` to prevent breaking by any other script.
    377383     * This must be placed inside document <head> area to working properly.
    378384     */
     
    489495                'easy-auto-reload',
    490496                __('Auto Reload','autorefresh'),
    491                 [&$this, 'meta_box__callback'],
     497                [$this, 'meta_box__callback'],
    492498                $post_type,
    493499                'side',
     
    589595        if ( isset( $_POST['_auto_reload_time'] ) ) {
    590596            $number_value = absint( $_POST['_auto_reload_time'] );
    591             if ( $number_value >= 1 ) {
     597            if ( $number_value >= self::ITS_TRUE ) {
    592598                update_post_meta( $post_id, '_easy_auto_reload_time', $number_value );
    593599            } else {
     
    599605   
    600606   
    601     /*
    602      * Get timeout option on the safe way
     607    /**
     608     * Returns the effective timeout (in minutes) for the current context, with static caching.
     609     * Order of precedence:
     610     * 1) Per-post custom setting (if enabled for the post type and mode is 'custom')
     611     * 2) Global plugin setting
     612     * 3) Provided $default
    603613     */
    604614    private function get_timeout(int $default = 5) {
     615
    605616        static $cached_timeout = null;
    606617
     
    628639            }
    629640
    630             if ($timeout < 1) {
     641            if ($timeout < self::ITS_TRUE) {
    631642                $timeout = $default;
    632643            }
     
    645656     */
    646657    private function get_nonce_life(){
    647         $wp_autorefresh = ( !empty($this->options) ? $this->options : get_option('wp-autorefresh', array()) );
    648 
    649         if(isset($wp_autorefresh['nonce_life']) && ($timeout=absint($wp_autorefresh['nonce_life']))){
    650            
    651             if( empty($timeout) || !is_numeric($timeout) ) {
    652                 $timeout = apply_filters( 'nonce_life', DAY_IN_SECONDS );
    653             }
    654            
    655             if($timeout < 1) {
    656                 $timeout = apply_filters( 'nonce_life', DAY_IN_SECONDS );
    657             }
    658            
    659             return absint($timeout);
    660         }
    661        
    662         return apply_filters( 'nonce_life', DAY_IN_SECONDS );
     658        $wp_autorefresh = ( !empty($this->options) ? $this->options : get_option('wp-autorefresh', []) );
     659
     660        if ( isset( $wp_autorefresh['nonce_life'] ) ) {
     661            $timeout = (int) $wp_autorefresh['nonce_life'];
     662            // If invalid, fall back to core default constant (no filters!)
     663            if ( $timeout < 1 ) {
     664                return DAY_IN_SECONDS;
     665            }
     666            return $timeout;
     667        }
     668
     669        // Default (no filter call here to avoid recursion)
     670        return DAY_IN_SECONDS;
    663671    }
    664672   
     
    672680   
    673681    /*
    674      * Enable autoefresh inside WP Admin
     682     * Enable autorefresh inside WP Admin
    675683     */
    676684    private function enable_in_admin(){
     
    680688   
    681689    /*
    682      * Enable autoefresh inside member types
     690     * Enable autorefresh inside member types
    683691     */
    684692    private function enable_post_type(){
     
    688696   
    689697    /*
    690      * Enable autoefresh inside member types
     698     * Enable autorefresh inside member types
    691699     */
    692700    private function enable_global_refresh(){
    693         $wp_autorefresh = ( !empty($this->options) ? $this->options : get_option('wp-autorefresh', array()) );
    694         return ((int)($wp_autorefresh['global_refresh']??0)) === (3/3);
     701        $wp_autorefresh = ( !empty($this->options) ? $this->options : get_option('wp-autorefresh', []) );
     702        return (int)( $wp_autorefresh['global_refresh'] ?? self::ITS_FALSE ) === (int)self::ITS_TRUE;
    695703    }
    696704   
  • easy-auto-reload/trunk/readme.txt

    r3285404 r3356585  
    66Tested up to: 6.8
    77Requires PHP: 7.0
    8 Stable tag: 2.0.2
     8Stable tag: 2.0.3
    99License: GPLv2 or later
    1010License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    6363
    6464== Changelog ==
     65
     66= 2.0.3 =
     67* Code improvement
     68* Fixed bugs with nonce
     69* Fixed permissions
    6570
    6671= 2.0.2 =
Note: See TracChangeset for help on using the changeset viewer.