PluginProbe
Polylang / 3.7.7
Polylang v3.7.7
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
← All changes | integrations/cache/cache-compat.php +44 -9 3.03.7.7 View file →
@@ -16,9 +16,9 @@
16 16 * @since 2.3
17 17 */
18 18 public function init() {
19 19 if ( PLL_COOKIE ) {
20 - add_action( 'wp_print_footer_scripts', array( $this, 'add_cookie_script' ) );
20 + add_action( 'wp_enqueue_scripts', array( $this, 'add_cookie_script' ) );
21 21 }
22 22
23 23 // Since version 3.0.5, WP Rocket does not serve the cached page if our cookie is not set
24 24 if ( ! defined( 'WP_ROCKET_VERSION' ) || version_compare( WP_ROCKET_VERSION, '3.0.5', '<' ) ) {
@@ -23,8 +23,10 @@
23 23 // Since version 3.0.5, WP Rocket does not serve the cached page if our cookie is not set
24 24 if ( ! defined( 'WP_ROCKET_VERSION' ) || version_compare( WP_ROCKET_VERSION, '3.0.5', '<' ) ) {
25 25 add_action( 'wp', array( $this, 'do_not_cache_site_home' ) );
26 26 }
27 +
28 + add_action( 'clean_post_cache', array( $this, 'clean_post_cache' ), 1 );
27 29 }
28 30
29 31 /**
30 32 * Currently all tested cache plugins don't send cookies with cached pages.
@@ -31,8 +33,10 @@
31 33 * This makes us impossible to know the language of the last browsed page.
32 34 * This functions allows to create the cookie in javascript as a workaround.
33 35 *
34 36 * @since 2.3
37 + *
38 + * @return void
35 39 */
36 40 public function add_cookie_script() {
37 41 // Embeds should not set the cookie.
38 42 if ( is_embed() ) {
@@ -41,23 +45,36 @@
41 45
42 46 $domain = ( 2 === PLL()->options['force_lang'] ) ? wp_parse_url( PLL()->links_model->home, PHP_URL_HOST ) : COOKIE_DOMAIN;
43 47 $samesite = ( 3 === PLL()->options['force_lang'] ) ? 'None' : 'Lax';
44 48
49 + /** This filter is documented in include/cookie.php */
50 + $expiration = (int) apply_filters( 'pll_cookie_expiration', YEAR_IN_SECONDS );
51 +
52 + if ( 0 !== $expiration ) {
53 + $format = 'var expirationDate = new Date();
54 + expirationDate.setTime( expirationDate.getTime() + %7$d * 1000 );
55 + document.cookie = "%1$s=%2$s; expires=" + expirationDate.toUTCString() + "; path=%3$s%4$s%5$s%6$s";';
56 + } else {
57 + $format = 'document.cookie = "%1$s=%2$s; path=%3$s%4$s%5$s%6$s";';
58 + }
59 +
45 60 $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 ) ),
61 + "(function() {
62 + {$format}
63 + }());\n",
52 64 esc_js( PLL_COOKIE ),
53 65 esc_js( pll_current_language() ),
54 66 esc_js( COOKIEPATH ),
55 67 $domain ? '; domain=' . esc_js( $domain ) : '',
56 68 is_ssl() ? '; secure' : '',
57 - '; SameSite=' . $samesite
69 + '; SameSite=' . $samesite,
70 + esc_js( $expiration )
58 71 );
59 - echo '<script type="text/javascript">' . $js . '</script>'; // phpcs:ignore WordPress.Security.EscapeOutput
72 +
73 + // Need to register prior to enqueue empty script and add extra code to it.
74 + wp_register_script( 'pll_cookie_script', '', array(), POLYLANG_VERSION, true );
75 + wp_enqueue_script( 'pll_cookie_script' );
76 + wp_add_inline_script( 'pll_cookie_script', $js );
60 77 }
61 78
62 79 /**
63 80 * Informs cache plugins not to cache the home in the default language
@@ -67,7 +84,25 @@
67 84 */
68 85 public function do_not_cache_site_home() {
69 86 if ( ! defined( 'DONOTCACHEPAGE' ) && PLL()->options['browser'] && PLL()->options['hide_default'] && is_front_page() && pll_current_language() === pll_default_language() ) {
70 87 define( 'DONOTCACHEPAGE', true );
88 + }
89 + }
90 +
91 + /**
92 + * Allows cache plugins to clean the right post type archive cache when cleaning a post cache.
93 + *
94 + * @since 3.0.5
95 + *
96 + * @param int $post_id Post id.
97 + */
98 + public function clean_post_cache( $post_id ) {
99 + $lang = PLL()->model->post->get_language( $post_id );
100 +
101 + if ( $lang ) {
102 + $filter_callback = function ( $link, $post_type ) use ( $lang ) {
103 + return pll_is_translated_post_type( $post_type ) && 'post' !== $post_type ? PLL()->links_model->switch_language_in_link( $link, $lang ) : $link;
104 + };
105 + add_filter( 'post_type_archive_link', $filter_callback, 99, 2 );
71 106 }
72 107 }
73 108 }