PluginProbe
Polylang / 2.8.4
Polylang v2.8.4
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.8.4, at integrations/cache/cache-compat.php

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