Skip to content

Commit

Permalink
Merge branch 'develop' into fix/4508-clear-all-cpt-files-correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
wordpressfan committed Feb 14, 2025
2 parents e90a4f0 + a0ceecd commit 8334eab
Show file tree
Hide file tree
Showing 14 changed files with 251 additions and 54 deletions.
2 changes: 1 addition & 1 deletion dynamic-lists-delayjs.json

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion inc/Engine/Cache/TaxonomySubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ private function is_not_valid_taxonomy_page() {
$term_link = trailingslashit( $term_link ) . 'page/' . get_query_var( 'paged' );
}

return urldecode( untrailingslashit( $term_link ) ) !== urldecode( untrailingslashit( $current_link ) );
$term_link = urldecode( untrailingslashit( $term_link ) );

if ( urldecode( untrailingslashit( $current_link ) ) !== $term_link && ! empty( $_SERVER['REQUEST_URI'] ) ) {
$current_link = home_url( add_query_arg( [], wp_unslash( $_SERVER['REQUEST_URI'] ) ) );// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
}

return urldecode( untrailingslashit( $current_link ) ) !== $term_link;
}
}
2 changes: 1 addition & 1 deletion inc/Engine/Common/PerformanceHints/Frontend/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static function get_subscribed_events(): array {
* @return string
*/
public function maybe_apply_optimizations( $html ): string {
if ( ! isset( $_GET['wpr_imagedimensions'] ) && isset( $_GET['wpr_lazyrendercontent'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( empty( $html ) || ( ! isset( $_GET['wpr_imagedimensions'] ) && isset( $_GET['wpr_lazyrendercontent'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return $html;
}

Expand Down
44 changes: 34 additions & 10 deletions inc/Engine/Media/AboveTheFold/Frontend/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ private function preload_lcp( $html, $row ) {
$title = $matches[0];
$preload = $title;

if ( ! $this->is_valid_data( $row->lcp ) ) {
return $html;
}

$lcp = json_decode( $row->lcp );

$preload .= $this->preload_tag( $lcp );
Expand Down Expand Up @@ -198,13 +202,13 @@ public function add_exclusions( $exclusions ): array {
return $exclusions;
}

if ( $row->lcp && 'not found' !== $row->lcp ) {
if ( $row->lcp && 'not found' !== $row->lcp && $this->is_valid_data( $row->lcp ) ) {
$lcp = $this->generate_lcp_link_tag_with_sources( json_decode( $row->lcp ) );
$lcp = $lcp['sources'];
$lcp = $this->get_path_for_exclusion( $lcp );
}

if ( $row->viewport && 'not found' !== $row->viewport ) {
if ( $row->viewport && 'not found' !== $row->viewport && $this->is_valid_data( $row->viewport ) ) {
$atf = $this->get_atf_sources( json_decode( $row->viewport ) );
$atf = $this->get_path_for_exclusion( $atf );
}
Expand All @@ -217,22 +221,42 @@ public function add_exclusions( $exclusions ): array {
return $exclusions;
}

/**
* Check if lcp/viewport is valid
*
* @param string $data The lcp/viewport data.
*
* @return bool
*/
private function is_valid_data( string $data ): bool {
return ! empty( json_decode( $data, true ) );
}

/**
* Get only the url path to exclude.
*
* @param array $exclusions Array of exclusions.
* @return array
*/
private function get_path_for_exclusion( array $exclusions ): array {
$exclusions = array_map(
function ( $exclusion ) {
$exclusion = wp_parse_url( $exclusion );
return ltrim( $exclusion['path'], '/' );
},
$exclusions
);
$sanitized_exclusions = [];

return $exclusions;
foreach ( $exclusions as $exclusion ) {
if ( empty( $exclusion ) ) {
continue;
}

$path = wp_parse_url( $exclusion, PHP_URL_PATH );
$path = ! empty( $path ) ? ltrim( $path, '/' ) : '';

if ( empty( $path ) ) {
continue;
}

$sanitized_exclusions[] = $path;
}

return $sanitized_exclusions;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion inc/Engine/Optimization/RegexTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ protected function replace_html_comment( $match ) { // phpcs:ignore Universal.Na
* @return string
*/
protected function restore_html_comments( $html ) {
if ( empty( $this->html_replace ) ) {
if ( ! is_string( $html ) || empty( $this->html_replace ) ) { // @phpstan-ignore-line For some reason, html could be null for some users, and we can't find a way to reproduce it.
return $html;
}

Expand Down
63 changes: 28 additions & 35 deletions inc/Engine/Support/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,52 +93,45 @@ public function add_meta_generator( $html ): string {
* @return string
*/
private function get_meta_tag( array $features = [] ): string {
if ( $this->options->get( 'do_caching_mobile_files', 0 ) ) {
$options = $this->options;

// Feature mapping for meta tags.
$features_to_check = [
'wpr_preload_links' => 'preload_links',
'wpr_host_fonts_locally' => 'host_fonts_locally',
];

foreach ( $features_to_check as $meta_name => $option_name ) {
if ( $options->get( $option_name, false ) ) {
$features[] = $meta_name;
}
}

// Mobile/Desktop caching.
if ( $options->get( 'do_caching_mobile_files', false ) ) {
$features[] = $this->mobile_detect->isMobile() ? 'wpr_mobile' : 'wpr_desktop';
}

// CDN & DNS prefetch check.
$dns_prefetch = rocket_get_dns_prefetch_domains();

if (
(
! $this->options->get( 'cdn', 0 )
&&
! empty( $dns_prefetch )
)
||
(
$this->options->get( 'cdn', 0 )
&&
count( $dns_prefetch ) > 1
)
) {
if ( $dns_prefetch && ( ! $options->get( 'cdn', false ) || count( $dns_prefetch ) > 1 ) ) {
$features[] = 'wpr_dns_prefetch';
}

if ( (bool) $this->options->get( 'preload_links', 0 ) ) {
$features[] = 'wpr_preload_links';
}

if ( empty( $features ) ) {
if ( ! $features ) {
return '';
}

$version = '';

/**
* Filters the display of WP Rocket version in the content attribute of the meta generator tag.
*
* @since 3.17.2
*
* @param bool $display True to display, false otherwise.
*/
if ( wpm_apply_filters_typed( 'boolean', 'rocket_display_meta_generator_content_version', true ) ) {
$version = ' ' . rocket_get_constant( 'WP_ROCKET_VERSION', '' );
}

$meta = '<meta name="generator" content="WP Rocket' . $version . '" data-wpr-features="' . implode( ' ', $features ) . '" />';
// Check if WP Rocket version should be included.
$version = wpm_apply_filters_typed( 'boolean', 'rocket_display_meta_generator_content_version', true )
? ' ' . rocket_get_constant( 'WP_ROCKET_VERSION', '' )
: '';

return $meta;
return sprintf(
'<meta name="generator" content="WP Rocket%s" data-wpr-features="%s" />',
$version,
implode( ' ', $features )
);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions inc/common/purge.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,10 @@ function rocket_clean_post( $post_id, $post = null ) {
// Purge all files.
rocket_clean_files( $purge_urls );

// Never forget to purge homepage and their pagination.
rocket_clean_home( $lang );
if ( wpm_apply_filters_typed( 'boolean', 'rocket_clean_home_after_clean_post', true, $post_id ) ) {
// Never forget to purge homepage and their pagination.
rocket_clean_home( $lang );
}

// Purge home feeds (blog & comments).
if ( has_filter( 'rocket_cache_reject_uri', 'wp_rocket_cache_feed' ) !== false ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,123 @@
'foobar.jpg',
],
],
'testShouldReturnEmptyStringWhenUrlNotValid' => [
'config' => [
'filter' => true,
'wp' => (object) [
'request' => '',
],
'url' => 'http://example.org',
'is_mobile' => false,
'row' => (object) [
'lcp' => json_encode( (object) [
'type' => 'img',
'src' => ':',
] ),
'viewport' => json_encode( [
0 => (object) [
'type' => 'img',
'src' => 'https://example.com/foobar.jpg',
],
] ),
],
'cache_mobile' => 0,
'do_caching_mobile_files' => 0,
'wp_is_mobile' => false,
],
'exclusions' => [
'foo',
],
'expected' => [
'foo',
'foobar.jpg',
],
],
'testShouldReturnEmptyLcpNotValid' => [
'config' => [
'filter' => true,
'wp' => (object) [
'request' => '',
],
'url' => 'http://example.org',
'is_mobile' => false,
'row' => (object) [
'lcp' => '[]',
'viewport' => json_encode( [
0 => (object) [
'type' => 'img',
'src' => 'https://example.com/foobar.jpg',
],
] ),
],
'cache_mobile' => 0,
'do_caching_mobile_files' => 0,
'wp_is_mobile' => false,
],
'exclusions' => [
'foo',
],
'expected' => [
'foo',
'foobar.jpg',
],
],
'testShouldReturnEmptyLcpNotValid2' => [
'config' => [
'filter' => true,
'wp' => (object) [
'request' => '',
],
'url' => 'http://example.org',
'is_mobile' => false,
'row' => (object) [
'lcp' => 'null',
'viewport' => json_encode( [
0 => (object) [
'type' => 'img',
'src' => 'https://example.com/foobar.jpg',
],
] ),
],
'cache_mobile' => 0,
'do_caching_mobile_files' => 0,
'wp_is_mobile' => false,
],
'exclusions' => [
'foo',
],
'expected' => [
'foo',
'foobar.jpg',
],
],
'testShouldReturnEmptyLcpNotValid3' => [
'config' => [
'filter' => true,
'wp' => (object) [
'request' => '',
],
'url' => 'http://example.org',
'is_mobile' => false,
'row' => (object) [
'lcp' => '{}',
'viewport' => json_encode( [
0 => (object) [
'type' => 'img',
'src' => 'https://example.com/foobar.jpg',
],
] ),
],
'cache_mobile' => 0,
'do_caching_mobile_files' => 0,
'wp_is_mobile' => false,
],
'exclusions' => [
'foo',
],
'expected' => [
'foo',
'foobar.jpg',
],
],
];
4 changes: 3 additions & 1 deletion tests/Fixtures/inc/Engine/Support/Meta/addMetaGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
'cdn' => 0,
'do_caching_mobile_files' => 0,
'preload_links' => 0,
'host_fonts_locally' => 0,
],
'html' => '<html><head></head><body></body></html>',
'expected' => '<html><head></head><body></body></html>',
Expand All @@ -53,8 +54,9 @@
'do_caching_mobile_files' => 1,
'preload_links' => 1,
'is_mobile' => true,
'host_fonts_locally' => 1
],
'html' => '<html><head></head><body></body></html><!-- wpr_remove_unused_css -->',
'expected' => '<html><head><meta name="generator" content="WP Rocket 3.17" data-wpr-features="wpr_remove_unused_css wpr_mobile wpr_preload_links" /></head><body></body></html>',
'expected' => '<html><head><meta name="generator" content="WP Rocket 3.17" data-wpr-features="wpr_remove_unused_css wpr_preload_links wpr_host_fonts_locally wpr_mobile" /></head><body></body></html>',
],
];
20 changes: 20 additions & 0 deletions tests/Fixtures/inc/common/doAdminPostRocketPurgeCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@
],
'config' => [
'type' => 'post',
'rocket_clean_home_after_clean_post' => true,
'post_id' => 123, // Auto populated in integration tests.
'lang' => 'en',
'file' => 'lorem-ipsum-dolor',
'post_data' => [
'post_name' => 'lorem-ipsum-dolor',
'post_title' => 'Lorem ipsum dolor',
],
],
],
[
'$_GET' => [
'type' => 'post-123',
'_wpnonce' => 'post-123',
'lang' => 'en',
],
'config' => [
'type' => 'post',
'rocket_clean_home_after_clean_post' => false,
'post_id' => 123, // Auto populated in integration tests.
'lang' => 'en',
'file' => 'lorem-ipsum-dolor',
Expand All @@ -63,6 +82,7 @@
],
'config' => [
'type' => 'all',
'rocket_clean_home_after_clean_post' => true,
'lang' => 'en',
],
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function set_up() {
add_filter( 'pre_get_rocket_option_host_fonts_locally', [ $this, 'host_fonts_locally' ] );
add_filter( 'rocket_host_fonts_locally_inline_css', [ $this, 'locally_inline_css' ] );
add_filter('rocket_exclude_locally_host_fonts', [ $this, 'exclude_locally_host_fonts' ] );
add_filter('rocket_disable_meta_generator', '__return_true');
$this->setup_http();

}
Expand All @@ -32,6 +33,7 @@ public function tear_down() {
remove_filter('pre_get_rocket_option_host_fonts_locally', [$this, 'host_fonts_locally']);
remove_filter('rocket_host_fonts_locally_inline_css', [$this, 'locally_inline_css']);
remove_filter('rocket_exclude_locally_host_fonts', [ $this, 'exclude_locally_host_fonts' ] );
remove_filter('rocket_disable_meta_generator', '__return_true');

$this->restoreWpHook('rocket_buffer');
$this->tear_down_http();
Expand Down
Loading

0 comments on commit 8334eab

Please sign in to comment.