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

103 lines 3.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 add_action( 'clean_post_cache', array( $this, 'clean_post_cache' ), 1 );
29 }
30
31 /**
32 * Currently all tested cache plugins don't send cookies with cached pages.
33 * This makes us impossible to know the language of the last browsed page.
34 * This functions allows to create the cookie in javascript as a workaround.
35 *
36 * @since 2.3
37 */
38 public function add_cookie_script() {
39 // Embeds should not set the cookie.
40 if ( is_embed() ) {
41 return;
42 }
43
44 $domain = ( 2 === PLL()->options['force_lang'] ) ? wp_parse_url( PLL()->links_model->home, PHP_URL_HOST ) : COOKIE_DOMAIN;
45 $samesite = ( 3 === PLL()->options['force_lang'] ) ? 'None' : 'Lax';
46
47 /** This filter is documented in include/cookie.php */
48 $expiration = (int) apply_filters( 'pll_cookie_expiration', YEAR_IN_SECONDS );
49
50 if ( 0 !== $expiration ) {
51 $format = 'var expirationDate = new Date();
52 expirationDate.setTime( expirationDate.getTime() + %7$d * 1000 );
53 document.cookie = "%1$s=%2$s; expires=" + expirationDate.toUTCString() + "; path=%3$s%4$s%5$s%6$s";';
54 } else {
55 $format = 'document.cookie = "%1$s=%2$s; path=%3$s%4$s%5$s%6$s";';
56 }
57
58 $js = sprintf(
59 "(function() {
60 {$format}
61 }());\n",
62 esc_js( PLL_COOKIE ),
63 esc_js( pll_current_language() ),
64 esc_js( COOKIEPATH ),
65 $domain ? '; domain=' . esc_js( $domain ) : '',
66 is_ssl() ? '; secure' : '',
67 '; SameSite=' . $samesite,
68 esc_js( $expiration )
69 );
70 echo "<script type='text/javascript'>\n" . $js . "</script>\n"; // phpcs:ignore WordPress.Security.EscapeOutput
71 }
72
73 /**
74 * Informs cache plugins not to cache the home in the default language
75 * When the detection of the browser preferred language is active
76 *
77 * @since 2.3
78 */
79 public function do_not_cache_site_home() {
80 if ( ! defined( 'DONOTCACHEPAGE' ) && PLL()->options['browser'] && PLL()->options['hide_default'] && is_front_page() && pll_current_language() === pll_default_language() ) {
81 define( 'DONOTCACHEPAGE', true );
82 }
83 }
84
85 /**
86 * Allows cache plugins to clean the right post type archive cache when cleaning a post cache.
87 *
88 * @since 3.0.5
89 *
90 * @param int $post_id Post id.
91 */
92 public function clean_post_cache( $post_id ) {
93 $lang = PLL()->model->post->get_language( $post_id );
94
95 if ( $lang ) {
96 $filter_callback = function ( $link, $post_type ) use ( $lang ) {
97 return pll_is_translated_post_type( $post_type ) && 'post' !== $post_type ? PLL()->links_model->switch_language_in_link( $link, $lang ) : $link;
98 };
99 add_filter( 'post_type_archive_link', $filter_callback, 99, 2 );
100 }
101 }
102 }
103