0 && !headers_sent()) { header('Retry-After: ' . $seconds); } } /** * v2.9.182 — Does a fatal error page during maintenance have to be * replaced? Only for someone who would not see the site. * * Before WordPress knows who is logged in (error while the plugins are * loading), a login cookie keeps the WordPress error page: the cookie * cannot be checked yet, and that page is what everyone got before. * * @return bool */ private static function fatal_shows_maintenance() { if (!is_array(self::$settings) || empty(self::$settings['maintenance_enabled'])) { return false; } if (!class_exists('MGENIUS_Main') && function_exists('mgenius_load_classes')) { mgenius_load_classes(); } if (!class_exists('MGENIUS_Main')) { return true; } if (did_action('plugins_loaded') && function_exists('wp_get_current_user')) { return !MGENIUS_Main::visitor_can_see_site(self::$settings, true); } foreach (array_keys($_COOKIE) as $mgenius_cookie) { if (is_string($mgenius_cookie) && 0 === strpos($mgenius_cookie, 'wordpress_logged_in_')) { return false; } } return !MGENIUS_Main::request_has_visitor_access(self::$settings); } /** * Filter wp_php_error_args: 503 + Retry-After + no-store. * * @param array $args wp_die() arguments. * @param array $error Error details. * @return array */ public static function fatal_error_args($args, $error) { if (!self::fatal_shows_maintenance()) { return $args; } $args = is_array($args) ? $args : array(); $args['response'] = 503; self::send_retry_after(self::$settings); // wp_die() sends nocache_headers() after this filter. add_filter('nocache_headers', [__CLASS__, 'no_store_headers']); return $args; } /** * Filter wp_php_error_message: neutral text, no technical detail. * * @param string $message Error page HTML. * @param array $error Error details. * @return string */ public static function fatal_error_message($message, $error) { if (!self::fatal_shows_maintenance()) { return $message; } // Translations are loaded on demand from « init » on (WordPress 6.7+). $mgenius_text = did_action('init') ? __('This site is temporarily unavailable. Please come back later.', '1001web-maintenance-launch-pages') : 'This site is temporarily unavailable. Please come back later.'; return '

' . esc_html($mgenius_text) . '

'; } /** * Filter nocache_headers: no-store, like the maintenance page. * * @param array $headers Headers. * @return array */ public static function no_store_headers($headers) { $headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0'; return $headers; } /** * Appele une fois, au chargement du fichier principal du plugin. */ public static function run() { // WP-CLI ne doit jamais declencher la purge (« wp core update » // pendant une maintenance). if (defined('WP_CLI') && WP_CLI) { return; } // Admin screens, AJAX, cron and the login screen are never cached pages: // the guard leaves them alone. These tests read what WordPress has set up // for this request (constants defined by wp-admin/admin.php, // admin-ajax.php and wp-cron.php, and the script really being run), never // a part of the URL chosen by the visitor. REST requests cannot be told // apart this early: they simply get the same no-cache headers. if (is_admin() || wp_doing_ajax() || wp_doing_cron() || (!empty($_SERVER['SCRIPT_NAME']) && is_login())) return; global $wpdb; if (!isset($wpdb) || !$wpdb) return; // Lecture directe en base : un cache d'objets peut encore servir // l'ancien etat juste apres l'interrupteur ; une seule requete legere. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Must bypass a stale object cache right after maintenance is toggled. $row = $wpdb->get_var($wpdb->prepare("SELECT option_value FROM {$wpdb->options} WHERE option_name = %s LIMIT 1", 'mgenius_settings')); $settings = is_string($row) ? maybe_unserialize($row) : null; if (empty($settings) || !is_array($settings) || empty($settings['maintenance_enabled'])) return; // v2.9.182 — a fatal error in another plugin during maintenance: // visitors who cannot see the site get a 503 with Retry-After and a // neutral message instead of the WordPress error page (HTTP 500). self::$settings = $settings; add_filter('wp_php_error_args', [__CLASS__, 'fatal_error_args'], 10, 2); add_filter('wp_php_error_message', [__CLASS__, 'fatal_error_message'], 10, 2); // Constantes standard lues par les plugins de cache : leur nom est impose. // phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound if (!defined('DONOTCACHEPAGE')) define('DONOTCACHEPAGE', true); if (!defined('DONOTCACHEOBJECT')) define('DONOTCACHEOBJECT', true); if (!defined('DONOTCACHEDB')) define('DONOTCACHEDB', true); // phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound // Purge au plus une fois par minute (avant : a chaque visite, // y compris « X-LiteSpeed-Purge: * » qui videait tout le cache LiteSpeed). // Rien ne se remet en cache entre deux purges : DONOTCACHEPAGE est pose. $mgenius_purge_now = !get_transient('mgenius_guard_purged'); if ($mgenius_purge_now) { set_transient('mgenius_guard_purged', 1, 60); } if (!headers_sent()) { header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0'); header('Pragma: no-cache'); header('X-LiteSpeed-Cache-Control: no-cache'); if ($mgenius_purge_now) { header('X-LiteSpeed-Purge: *'); } } $cache_dirs = $mgenius_purge_now ? [ WP_CONTENT_DIR . '/cache/all/', WP_CONTENT_DIR . '/cache/wpfc-minified/', WP_CONTENT_DIR . '/cache/wpfc-mobile-cache/', WP_CONTENT_DIR . '/cache/wp-rocket/', WP_CONTENT_DIR . '/cache/supercache/', WP_CONTENT_DIR . '/cache/cache-enabler/', WP_CONTENT_DIR . '/cache/seraphinite-accelerator/', WP_CONTENT_DIR . '/cache/wpo-cache/', WP_CONTENT_DIR . '/litespeed/cssjs/', WP_CONTENT_DIR . '/litespeed/page/', ] : []; // W3 Total Cache (Disk: Enhanced) : sous-dossiers seulement, // le .htaccess propre a W3TC reste en place. // glob() peut renvoyer false : jamais de chemin vide ou « / ». $pe_dirs = $mgenius_purge_now ? glob(WP_CONTENT_DIR . '/cache/page_enhanced/*', GLOB_ONLYDIR) : []; if (is_array($pe_dirs)) { foreach ($pe_dirs as $pe_dir) { if (is_string($pe_dir) && '' !== $pe_dir && 0 === strpos($pe_dir, WP_CONTENT_DIR . '/cache/page_enhanced/')) { $cache_dirs[] = $pe_dir . '/'; } } } $mgenius_cache_root = wp_normalize_path(WP_CONTENT_DIR . '/'); foreach ($cache_dirs as $dir) { // Garde-fou : uniquement des dossiers de cache sous wp-content. if (!is_string($dir) || 0 !== strpos(wp_normalize_path($dir), $mgenius_cache_root) || wp_normalize_path($dir) === $mgenius_cache_root) { continue; } if (!is_dir($dir) || is_link(rtrim($dir, '/'))) { continue; } // Fichiers et liens seulement : un lien est supprime lui- // meme, jamais sa cible. Dossier supprime par une requete concurrente // ou illisible : on passe, sans erreur fatale. Les dossiers vides // restent en place : ils ne servent aucune page. try { $it = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST, RecursiveIteratorIterator::CATCH_GET_CHILD ); foreach ($it as $f) { if ($f->isLink() || !$f->isDir()) { wp_delete_file($f->getPathname()); } } } catch (Exception $e) { continue; } } // Le .htaccess et WP Fastest Cache sont geres par // MGENIUS_Main::init(), sous le verrou de transition et sur l'etat relu en // base : plus d'ecriture concurrente avec la sortie de maintenance. // Suspension reelle de WP Rocket. // add_filter() plutot qu'une ecriture en base : rien n'est modifie chez le // plugin tiers, donc rien n'est a restaurer et aucune configuration // utilisateur ne peut etre perdue si la maintenance est interrompue. add_filter('do_rocket_generate_caching_files', '__return_false'); if ($mgenius_purge_now && function_exists('wp_cache_clear_cache')) { wp_cache_clear_cache(); } // Aucun reglage d'un plugin de cache n'est ecrit ici : ceux que la // maintenance suspend sont sauvegardes et restaures par // MGENIUS_Main::disable_cache_plugins() et restore_cache_plugins(). } }