Plugin Directory

Changeset 3492575


Ignore:
Timestamp:
03/27/2026 11:10:14 AM (30 hours ago)
Author:
leopardhost
Message:

v2.1.4: Proper auto-switching

Location:
tnc-toolbox
Files:
21 added
5 edited

Legend:

Unmodified
Added
Removed
  • tnc-toolbox/trunk/core/core.php

    r3488521 r3492575  
    5151
    5252    /**
    53      * Auto-detect web stack and switch if necessary
    54      *
    55      * Only runs once per day to avoid repeated checks.
     53     * Auto-detect web stack and correct if needed
     54     *
     55     * Only runs if the user has never explicitly saved a stack choice.
     56     * Once the user saves settings, their choice is always respected.
     57     *
     58     * On plugin update to v2.1.3+, runs immediately to correct sites
     59     * that were wrongly auto-switched to LiteSpeed by earlier detection.
    5660     */
    5761    public function auto_detect_web_stack() {
    58         // Only check once per day
    59         $last_check = get_transient('tnc_web_stack_check');
    60         if ($last_check) {
    61             return;
     62        // Never override an explicit user choice
     63        if (TNC_Detection::is_user_configured()) {
     64            return;
     65        }
     66
     67        // Run once immediately after update to fix wrongly-switched sites,
     68        // then check once per day after that
     69        $check_version = get_option('tnc_web_stack_check_version', '');
     70        if ($check_version !== TNCTOOLBOX_VERSION) {
     71            // Version changed — run detection now and update marker
     72            delete_transient('tnc_web_stack_check');
     73            update_option('tnc_web_stack_check_version', TNCTOOLBOX_VERSION);
     74        } else {
     75            $last_check = get_transient('tnc_web_stack_check');
     76            if ($last_check) {
     77                return;
     78            }
    6279        }
    6380
    6481        $result = TNC_Detection::auto_detect_and_switch_stack();
    6582        if ($result) {
    66             // Log the auto-switch
    6783            if (defined('WP_DEBUG') && WP_DEBUG) {
    6884                error_log(sprintf(
    69                     'TNC Toolbox: Auto-switched web stack from %s to %s (detected: %s)',
     85                    'TNC Toolbox: Auto-corrected web stack from %s to %s (detected: %s)',
    7086                    $result['previous'],
    7187                    $result['switched_to'],
  • tnc-toolbox/trunk/core/settings.php

    r3488521 r3492575  
    124124     */
    125125    private function save_settings() {
    126         // Web stack selection
     126        // Web stack selection - mark as explicit user choice
    127127        $web_stack = sanitize_text_field($_POST['tnc_web_stack'] ?? 'nginx');
    128         TNC_Detection::set_web_stack($web_stack);
     128        TNC_Detection::set_web_stack($web_stack, true);
    129129
    130130        // Sanitise inputs (always save these to preserve credentials)
     
    187187        $detected_server = TNC_Detection::detect_web_server();
    188188        $is_litespeed = TNC_Detection::is_litespeed_stack();
    189 
    190         // Check for auto-detection mismatch and notify
     189        $is_compatible = TNC_Detection::is_detection_compatible();
     190
     191        // Only auto-switch on first run if user has never saved settings
    191192        $auto_switch = TNC_Detection::auto_detect_and_switch_stack();
    192193        if ($auto_switch) {
    193             $web_stack = $auto_switch['switched_to']; // Update for display
     194            $web_stack = $auto_switch['switched_to'];
    194195            $is_litespeed = true;
     196            $is_compatible = true;
    195197        }
    196198        ?>
     
    200202                <p><strong>Configure your web stack settings. Supports ea-NGINX (cPanel) and LiteSpeed (OpenLiteSpeed/Enterprise).</strong><br>
    201203                <?php if ($detected_server): ?>
    202                     Detected server: <code><?php echo esc_html(ucfirst($detected_server)); ?></code>
     204                    Detected Server: <code><?php echo esc_html($detected_server); ?></code>
     205                    <?php if ($detected_server === 'apache' && $web_stack === 'nginx'): ?>
     206                        <em>(expected for ea-NGINX — PHP runs under Apache behind NGINX reverse proxy)</em>
     207                    <?php endif; ?>
    203208                <?php endif; ?>
    204209                </p>
     
    207212            <?php if ($auto_switch): ?>
    208213            <div class="notice notice-info is-dismissible">
    209                 <p><strong>Web stack auto-switched!</strong> Detected LiteSpeed server but was configured for NGINX. Automatically switched to LiteSpeed stack. Your cPanel credentials are preserved if you need to switch back.</p>
     214                <p><strong>Web stack auto-detected!</strong> LiteSpeed server detected — configured for LiteSpeed stack. You can change this below if needed.</p>
    210215            </div>
    211216            <?php endif; ?>
     
    232237                                    </option>
    233238                                </select>
    234                                 <?php if ($detected_server && $detected_server !== $web_stack): ?>
     239                                <?php if (!$is_compatible): ?>
    235240                                    <p class="description" style="color: #d63638;">
    236                                         ⚠ Detected <strong><?php echo esc_html(ucfirst($detected_server)); ?></strong> but configured for <strong><?php echo esc_html(ucfirst($web_stack)); ?></strong>
     241                                        ⚠ Detected <strong><?php echo esc_html($detected_server); ?></strong> but configured for <strong><?php echo esc_html(TNC_Detection::get_stack_name($web_stack)); ?></strong>. Please verify your selection.
    237242                                    </p>
    238243                                <?php endif; ?>
  • tnc-toolbox/trunk/readme.txt

    r3488521 r3492575  
    66Tags: NGINX, LiteSpeed, Cache Purge, Web Performance, Automatic Purge, Freeware
    77Tested up to: 6.9
    8 Stable tag: 2.1.3
     8Stable tag: 2.1.4
    99License: GPLv3
    1010License URI: https://www.gnu.org/licenses/gpl-3.0.html
     
    159159
    160160== Changelog ==
     161
     162= 2.1.4: Mar 27, 2026 =
     163* Auto-switch: Bidirectional, rather than "to LSWS only"
     164* Auto-switch: Don't switch if user has overridden stack
     165* Auto-switch: Refine detection to Server identifier only
    161166
    162167= 2.1.3: Mar 23, 2026 =
  • tnc-toolbox/trunk/tnc-toolbox.php

    r3488521 r3492575  
    66 * @author            The Network Crew Pty Ltd (Merlot Digital)
    77 * @license           gplv3
    8  * @version           2.1.3
     8 * @version           2.1.4
    99 *
    1010 * @wordpress-plugin
     
    1212 * Plugin URI:        https://merlot.digital
    1313 * Description:       Multi-stack caching for WordPress: ea-NGINX (cPanel) and LiteSpeed. Auto-detects web server!
    14  * Version:           2.1.3
     14 * Version:           2.1.4
    1515 * Author:            The Network Crew Pty Ltd (Merlot Digital)
    1616 * Author URI:        https://tnc.works
     
    3030
    3131// Plugin version
    32 define('TNCTOOLBOX_VERSION', '2.1.3');
     32define('TNCTOOLBOX_VERSION', '2.1.4');
    3333
    3434// Plugin Root File
  • tnc-toolbox/trunk/vendor/detection.php

    r3488521 r3492575  
    3131
    3232    /**
     33     * Option name to track if user has explicitly chosen a stack
     34     */
     35    const STACK_SET_BY_USER_KEY = 'tnc_web_stack_user_set';
     36
     37    /**
    3338     * Web stack constants
    3439     */
     
    5358     * Set the web stack type
    5459     *
    55      * @param string $stack Stack type ('nginx' or 'litespeed')
     60     * @param string $stack    Stack type ('nginx' or 'litespeed')
     61     * @param bool   $by_user  Whether this was explicitly set by the user via settings
    5662     * @return bool True on success
    5763     */
    58     public static function set_web_stack( $stack ) {
     64    public static function set_web_stack( $stack, $by_user = false ) {
    5965        $valid_stacks = array( self::STACK_NGINX, self::STACK_LITESPEED );
    6066        if ( ! in_array( $stack, $valid_stacks, true ) ) {
    6167            $stack = self::STACK_NGINX;
    6268        }
     69        if ( $by_user ) {
     70            update_option( self::STACK_SET_BY_USER_KEY, true );
     71        }
    6372        return update_option( self::WEB_STACK_KEY, $stack );
    6473    }
    6574
    6675    /**
     76     * Check whether the user has ever explicitly saved a stack choice
     77     *
     78     * @return bool True if the user has explicitly chosen a stack
     79     */
     80    public static function is_user_configured() {
     81        return (bool) get_option( self::STACK_SET_BY_USER_KEY, false );
     82    }
     83
     84    /**
    6785     * Detect current web server software
    6886     *
    69      * Checks multiple indicators for LiteSpeed since OpenLiteSpeed
    70      * may report as Apache in SERVER_SOFTWARE while using LSAPI for PHP.
     87     * Uses SERVER_SOFTWARE header only — this is a reliable indicator.
     88     *
     89     * - LiteSpeed/OpenLiteSpeed: SERVER_SOFTWARE contains "LiteSpeed"
     90     * - ea-NGINX (cPanel): SERVER_SOFTWARE is "Apache" (PHP runs under
     91     *   Apache behind the NGINX reverse proxy — this is expected)
    7192     *
    7293     * @return string|false Returns 'litespeed', 'nginx', 'apache', or false if unknown
     
    7596        $server_software = isset( $_SERVER['SERVER_SOFTWARE'] ) ? strtolower( $_SERVER['SERVER_SOFTWARE'] ) : '';
    7697
    77         // Check SERVER_SOFTWARE for litespeed first
     98        // LiteSpeed/OpenLiteSpeed identify themselves in SERVER_SOFTWARE
    7899        if ( strpos( $server_software, 'litespeed' ) !== false ) {
    79100            return 'litespeed';
    80101        }
    81102
    82         // LiteSpeed SAPI check - php_sapi_name() returns 'litespeed' when using LSPHP/LSAPI
    83         // This is the most reliable check for OpenLiteSpeed
    84         $sapi_name = strtolower( php_sapi_name() );
    85         if ( strpos( $sapi_name, 'litespeed' ) !== false ) {
    86             return 'litespeed';
    87         }
    88 
    89         // Check for LITESPEED constant (set by LiteSpeed Cache plugin or LSAPI)
    90         if ( defined( 'LITESPEED' ) || defined( 'LSCACHE_ADV' ) ) {
    91             return 'litespeed';
    92         }
    93 
    94         // Check for LiteSpeed-specific server variables
    95         if ( isset( $_SERVER['X_LSCACHE'] ) || isset( $_SERVER['HTTP_X_LSCACHE'] ) ) {
    96             return 'litespeed';
    97         }
    98 
    99         // Now check other servers
     103        // NGINX as primary server (not ea-NGINX, which proxies to Apache)
    100104        if ( strpos( $server_software, 'nginx' ) !== false ) {
    101105            return 'nginx';
    102106        }
     107
     108        // Apache — this also covers ea-NGINX (cPanel) where PHP runs
     109        // under Apache behind the NGINX reverse proxy
    103110        if ( strpos( $server_software, 'apache' ) !== false ) {
    104111            return 'apache';
     
    109116
    110117    /**
    111      * Auto-detect and update web stack if mismatched
    112      *
    113      * If server is LiteSpeed but config says nginx, auto-switch to litespeed.
    114      * Credentials are preserved - not deleted.
     118     * Auto-detect and correct web stack configuration
     119     *
     120     * Only switches automatically if the user has never explicitly saved
     121     * a stack choice. Once a user saves settings, their choice is respected
     122     * and auto-detection becomes advisory only (shown on the settings page).
     123     *
     124     * Handles both directions:
     125     * - LiteSpeed detected + configured nginx → switch to litespeed
     126     * - Non-LiteSpeed detected + configured litespeed → switch back to nginx
     127     *   (fixes sites wrongly auto-switched by earlier buggy detection)
    115128     *
    116129     * @return array|false Returns change info if stack was auto-switched, false otherwise
    117130     */
    118131    public static function auto_detect_and_switch_stack() {
     132        // Never override an explicit user choice
     133        if ( self::is_user_configured() ) {
     134            return false;
     135        }
     136
    119137        $detected   = self::detect_web_server();
    120138        $configured = self::get_web_stack();
    121139
    122         // If LiteSpeed detected but configured for nginx, auto-switch
     140        // LiteSpeed detected but configured for nginx → switch to litespeed
    123141        if ( $detected === 'litespeed' && $configured === self::STACK_NGINX ) {
    124             self::set_web_stack( self::STACK_LITESPEED );
     142            self::set_web_stack( self::STACK_LITESPEED, false );
    125143            return array(
    126144                'detected'    => $detected,
     
    130148        }
    131149
     150        // Not LiteSpeed but configured as litespeed → switch back to nginx
     151        // This corrects sites wrongly auto-switched by earlier detection logic
     152        if ( $detected !== 'litespeed' && $configured === self::STACK_LITESPEED ) {
     153            self::set_web_stack( self::STACK_NGINX, false );
     154            return array(
     155                'detected'    => $detected ? $detected : 'unknown',
     156                'previous'    => $configured,
     157                'switched_to' => self::STACK_NGINX,
     158            );
     159        }
     160
    132161        return false;
    133162    }
     
    149178    public static function is_nginx_stack() {
    150179        return self::get_web_stack() === self::STACK_NGINX;
     180    }
     181
     182    /**
     183     * Check if the detected server matches the configured stack
     184     *
     185     * On ea-NGINX (cPanel), PHP runs under Apache behind the NGINX reverse
     186     * proxy, so detection returns 'apache'. This is a known expected mismatch
     187     * and should NOT trigger a warning.
     188     *
     189     * @return bool True if detection and config are compatible
     190     */
     191    public static function is_detection_compatible() {
     192        $detected   = self::detect_web_server();
     193        $configured = self::get_web_stack();
     194
     195        // No detection = no mismatch to warn about
     196        if ( ! $detected ) {
     197            return true;
     198        }
     199
     200        // Direct match
     201        if ( $detected === $configured ) {
     202            return true;
     203        }
     204
     205        // ea-NGINX: PHP reports Apache because it runs behind NGINX reverse proxy
     206        // This is expected and not a mismatch
     207        if ( $detected === 'apache' && $configured === self::STACK_NGINX ) {
     208            return true;
     209        }
     210
     211        return false;
    151212    }
    152213
Note: See TracChangeset for help on using the changeset viewer.