PluginProbe
Polylang / 2.5
Polylang v2.5
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / modules / plugins / cache-compat.php

cache-compat.php in Polylang 2.5, at modules/plugins/cache-compat.php

62 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * A class to manage specific compatibility issue with cache plugins
5 * Tested with WP Rocket 2.10.7
6 *
7 * @since 2.3
8 */
9 class PLL_Cache_Compat {
10 /**
11 * Setups actions
12 *
13 * @since 2.3
14 */
15 public function init() {
16 if ( PLL_COOKIE ) {
17 add_action( 'wp_print_footer_scripts', array( $this, 'add_cookie_script' ) );
18 }
19
20 // Since version 3.0.5, WP Rocket does not serve the cached page if our cookie is not set
21 if ( ! defined( 'WP_ROCKET_VERSION' ) || version_compare( WP_ROCKET_VERSION, '3.0.5', '<' ) ) {
22 add_action( 'wp', array( $this, 'do_not_cache_site_home' ) );
23 }
24 }
25
26 /**
27 * Currently all tested cache plugins don't send cookies with cached pages
28 * This makes us impossible know the language of the last browsed page
29 * This functions allows to create the cookie in javascript as a workaround
30 *
31 * @since 2.3
32 */
33 public function add_cookie_script() {
34 $domain = ( 2 == PLL()->options['force_lang'] ) ? parse_url( PLL()->links_model->home, PHP_URL_HOST ) : COOKIE_DOMAIN;
35 $js = sprintf(
36 '(function() {
37 var expirationDate = new Date();
38 expirationDate.setTime( expirationDate.getTime() + %d * 1000 );
39 document.cookie = "%s=%s; expires=" + expirationDate.toUTCString() + "; path=%s%s";
40 }());',
41 esc_js( apply_filters( 'pll_cookie_expiration', YEAR_IN_SECONDS ) ),
42 esc_js( PLL_COOKIE ),
43 esc_js( pll_current_language() ),
44 esc_js( COOKIEPATH ),
45 $domain ? '; domain=' . esc_js( $domain ) : ''
46 );
47 echo '<script type="text/javascript">' . $js . '</script>';
48 }
49
50 /**
51 * Informs cache plugins not to cache the home in the default language
52 * When the detection of the browser preferred language is active
53 *
54 * @since 2.3
55 */
56 public function do_not_cache_site_home() {
57 if ( ! defined( 'DONOTCACHEPAGE' ) && PLL()->options['browser'] && PLL()->options['hide_default'] && is_front_page() && pll_current_language() === pll_default_language() ) {
58 define( 'DONOTCACHEPAGE', true );
59 }
60 }
61 }
62