| @@ -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. |
| @@ -41,23 +43,32 @@ | ||
| 41 | 43 | |
| 42 | 44 | $domain = ( 2 === PLL()->options['force_lang'] ) ? wp_parse_url( PLL()->links_model->home, PHP_URL_HOST ) : COOKIE_DOMAIN; |
| 43 | 45 | $samesite = ( 3 === PLL()->options['force_lang'] ) ? 'None' : 'Lax'; |
| 44 | 46 | |
| 47 | + /** This filter is documented in include/cookie.php */ | |
| 48 | + $expiration = apply_filters( 'pll_cookie_expiration', YEAR_IN_SECONDS ); | |
| 49 | + | |
| 50 | + if ( $expiration > 0 ) { | |
| 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 | + | |
| 45 | 58 | $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 ) ), | |
| 59 | + "(function() { | |
| 60 | + {$format} | |
| 61 | + }());\n", | |
| 52 | 62 | esc_js( PLL_COOKIE ), |
| 53 | 63 | esc_js( pll_current_language() ), |
| 54 | 64 | esc_js( COOKIEPATH ), |
| 55 | 65 | $domain ? '; domain=' . esc_js( $domain ) : '', |
| 56 | 66 | is_ssl() ? '; secure' : '', |
| 57 | - '; SameSite=' . $samesite | |
| 67 | + '; SameSite=' . $samesite, | |
| 68 | + esc_js( $expiration ) | |
| 58 | 69 | ); |
| 59 | - 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 | |
| 60 | 71 | } |
| 61 | 72 | |
| 62 | 73 | /** |
| 63 | 74 | * Informs cache plugins not to cache the home in the default language |
| @@ -67,7 +78,25 @@ | ||
| 67 | 78 | */ |
| 68 | 79 | public function do_not_cache_site_home() { |
| 69 | 80 | if ( ! defined( 'DONOTCACHEPAGE' ) && PLL()->options['browser'] && PLL()->options['hide_default'] && is_front_page() && pll_current_language() === pll_default_language() ) { |
| 70 | 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 ); | |
| 71 | 100 | } |
| 72 | 101 | } |
| 73 | 102 | } |