PluginProbe
Polylang / 2.9
Polylang v2.9
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 / integrations / cache / cache-compat.php

cache-compat.php in Polylang 2.9, at integrations/cache/cache-compat.php

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