| @@ -23,33 +23,52 @@ | ||
| 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 | - * 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 | |
| 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. | |
| 33 | 35 | * |
| 34 | 36 | * @since 2.3 |
| 35 | 37 | */ |
| 36 | 38 | public function add_cookie_script() { |
| 37 | - $domain = ( 2 == PLL()->options['force_lang'] ) ? wp_parse_url( PLL()->links_model->home, PHP_URL_HOST ) : COOKIE_DOMAIN; | |
| 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 | + | |
| 38 | 58 | $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 ) ), | |
| 59 | + "(function() { | |
| 60 | + {$format} | |
| 61 | + }());\n", | |
| 45 | 62 | esc_js( PLL_COOKIE ), |
| 46 | 63 | esc_js( pll_current_language() ), |
| 47 | 64 | esc_js( COOKIEPATH ), |
| 48 | 65 | $domain ? '; domain=' . esc_js( $domain ) : '', |
| 49 | - is_ssl() ? '; secure' : '' | |
| 66 | + is_ssl() ? '; secure' : '', | |
| 67 | + '; SameSite=' . $samesite, | |
| 68 | + esc_js( $expiration ) | |
| 50 | 69 | ); |
| 51 | - echo '<script type="text/javascript">' . $js . '</script>'; // phpcs:ignore WordPress.Security.EscapeOutput | |
| 70 | + echo "<script type='text/javascript'>\n" . $js . "</script>\n"; // phpcs:ignore WordPress.Security.EscapeOutput | |
| 52 | 71 | } |
| 53 | 72 | |
| 54 | 73 | /** |
| 55 | 74 | * Informs cache plugins not to cache the home in the default language |
| @@ -59,7 +78,25 @@ | ||
| 59 | 78 | */ |
| 60 | 79 | public function do_not_cache_site_home() { |
| 61 | 80 | if ( ! defined( 'DONOTCACHEPAGE' ) && PLL()->options['browser'] && PLL()->options['hide_default'] && is_front_page() && pll_current_language() === pll_default_language() ) { |
| 62 | 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 ); | |
| 63 | 100 | } |
| 64 | 101 | } |
| 65 | 102 | } |