PluginProbe
Polylang / 3.0
Polylang v3.0
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 3.0, at integrations/cache/cache-compat.php

74 lines 2.2 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 to 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 // Embeds should not set the cookie.
38 if ( is_embed() ) {
39 return;
40 }
41
42 $domain = ( 2 === PLL()->options['force_lang'] ) ? wp_parse_url( PLL()->links_model->home, PHP_URL_HOST ) : COOKIE_DOMAIN;
43 $samesite = ( 3 === PLL()->options['force_lang'] ) ? 'None' : 'Lax';
44
45 $js = sprintf(
46 '(function() {
47 var expirationDate = new Date();
48 expirationDate.setTime( expirationDate.getTime() + %d * 1000 );
49 document.cookie = "%s=%s; expires=" + expirationDate.toUTCString() + "; path=%s%s%s%s";
50 }());',
51 esc_js( apply_filters( 'pll_cookie_expiration', YEAR_IN_SECONDS ) ),
52 esc_js( PLL_COOKIE ),
53 esc_js( pll_current_language() ),
54 esc_js( COOKIEPATH ),
55 $domain ? '; domain=' . esc_js( $domain ) : '',
56 is_ssl() ? '; secure' : '',
57 '; SameSite=' . $samesite
58 );
59 echo '<script type="text/javascript">' . $js . '</script>'; // phpcs:ignore WordPress.Security.EscapeOutput
60 }
61
62 /**
63 * Informs cache plugins not to cache the home in the default language
64 * When the detection of the browser preferred language is active
65 *
66 * @since 2.3
67 */
68 public function do_not_cache_site_home() {
69 if ( ! defined( 'DONOTCACHEPAGE' ) && PLL()->options['browser'] && PLL()->options['hide_default'] && is_front_page() && pll_current_language() === pll_default_language() ) {
70 define( 'DONOTCACHEPAGE', true );
71 }
72 }
73 }
74