Created
February 28, 2022 16:20
-
-
Save titomus/9357705d778d0d075227fcd8d30ba1e8 to your computer and use it in GitHub Desktop.
Cache WordPress sur index.php directement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class Compress | |
*/ | |
class Compress { | |
// methode basique de compression utile pour html, css, js | |
public static function compress_inline($code) { | |
return preg_replace( | |
array( | |
'/ {2,}/', | |
'/<!--.*?-->|\t|(?:\r?\n[ \t]*)+/s' | |
), | |
array( | |
' ', | |
'' | |
), | |
$code | |
); | |
} | |
} | |
/** | |
* Classe de mise en cache de contenu | |
* | |
*/ | |
class Cache { | |
public $lifetime; | |
private $filename; | |
public function __construct($lifetime = 60) { | |
// expiration du fichier de cache, en minute | |
$this->lifetime = $lifetime; | |
// création d’un nom encodé, placé dans le folder spécifié | |
$this->filename = $this->makeCacheName($this->get_url()); | |
//verification du dossier cache | |
if (!is_dir(JA_CACHE_FOLDER)) { | |
@mkdir(JA_CACHE_FOLDER, 0777, true); | |
} | |
} | |
// Méthode privée qui construit le nom du fichier de cache | |
private function makeCacheName($path) { | |
return JA_CACHE_FOLDER . urlencode($path) . ".cache.gz"; | |
} | |
// retrouver l'url entière | |
public function get_url() { | |
return $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; | |
} | |
public function open($force = false) { | |
// vérification de l’existance ou de l’expiration du cache, seulement si pas forcé | |
if ( | |
file_exists($this->filename) | |
&& (time() - filemtime($this->filename)) < $this->lifetime * 60 | |
&& !$force | |
) { | |
// affichage puis arrêt | |
readgzfile($this->filename); | |
exit; | |
} | |
// on lance l'écoute du buffer | |
ob_start(); | |
} | |
public function close() { | |
// on intercepte le contenu du buffer… | |
$content = ob_get_clean(); | |
// on minifie le HTML de rendu sur une seule ligne. | |
$content = Compress::compress_inline($content); | |
// utilisation du cdn | |
$content = str_replace('jeremy-allard.com/wp-', 'titomus-1b96b.kxcdn.com/wp-', $content); | |
// …puis on l’écrit dans un fichier | |
$added_comment = chr(10).'<!-- cached by Ja Optimised pour URL: '.$this->get_url().' -->'; | |
file_put_contents($this->filename, gzencode($content . $added_comment, 9)); | |
// et enfin on l’affiche | |
echo $content; | |
} | |
public function clear($paths) { | |
if (!is_array($paths)) { | |
$paths = array($paths); | |
} | |
foreach ($paths as $path) { | |
// efface chaque chemin de page une fois encodé | |
@unlink($this->makeCacheName($this->get_url())); | |
} | |
} | |
} | |
// utilisation sur index.php d'un WordPress | |
/** | |
* Front to the WordPress application. This file doesn't do anything, but loads | |
* wp-blog-header.php which does and tells WordPress to load the theme. | |
* | |
* @package WordPress | |
*/ | |
/** | |
* Tells WordPress to load the WordPress theme and output it. | |
* | |
* @var bool | |
*/ | |
define('WP_USE_THEMES', true); | |
/** | |
* define cache folder. | |
* | |
* @var bool | |
*/ | |
define('JA_CACHE_FOLDER', "./ja_cache/cache/"); | |
/** | |
* Define site url | |
*/ | |
define('JA_SITE_URL', "https://site.com"); | |
// filtrage des requêtes GET à ne pas enregistrer | |
if ( | |
!preg_match('/(comment_author_|wordpress_logged_in_|wp-postpass_|woocommerce_)/', var_export($_COOKIE, true)) | |
&& !preg_match('/\/?(\/ja_cache|wp-content|wp-admin|robots\.txt|feed|sitemap\.xml)(.*)$/', $_SERVER["REQUEST_URI"]) | |
) { | |
$cache = new Cache(5 * 24 * 60); | |
$cache->open($_POST); | |
/** Loads the WordPress Environment and Template */ | |
require __DIR__ . '/wp-blog-header.php'; | |
$cache->close(); | |
} else { | |
/** Loads the WordPress Environment and Template */ | |
require __DIR__ . '/wp-blog-header.php'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment