From 12afc43024c130c29e72d6e1343985d7ca678bde Mon Sep 17 00:00:00 2001 From: Vasilis Manthos Date: Tue, 9 Mar 2021 16:47:29 +0200 Subject: [PATCH] Closes #2812 Add new filter to rewrite srcset to the CDN (PR #3648) --- inc/Engine/CDN/CDN.php | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/inc/Engine/CDN/CDN.php b/inc/Engine/CDN/CDN.php index add70f19f0..7af9e53cb7 100644 --- a/inc/Engine/CDN/CDN.php +++ b/inc/Engine/CDN/CDN.php @@ -60,7 +60,7 @@ function( $matches ) { * @return string */ public function rewrite_srcset( $html ) { - $pattern = '#\s+(?:data-lazy-|data-)?srcset\s*=\s*["\']\s*(?[^"\',\s]+\.[^"\',\s]+(?:\s+\d+[wx])?(?:\s*,\s*[^"\',\s]+\.[^"\',\s]+\s+\d+[wx])*)\s*["\']#i'; + $pattern = '#\s+(?:' . $this->get_srcset_attributes() . ')?srcset\s*=\s*["\']\s*(?[^"\',\s]+\.[^"\',\s]+(?:\s+\d+[wx])?(?:\s*,\s*[^"\',\s]+\.[^"\',\s]+\s+\d+[wx])*)\s*["\']#i'; if ( ! preg_match_all( $pattern, $html, $srcsets, PREG_SET_ORDER ) ) { return $html; @@ -368,4 +368,29 @@ function ( $file ) use ( $delimiter ) { return implode( '|', $files ); } + + /** + * Get srcset attributes to rewrite to the CDN. + * + * @since 3.8.7 + * + * @return string A pipe-separated list of srcset attributes. + */ + private function get_srcset_attributes() { + /** + * Filter the srcset attributes. + * + * @since 3.8.7 + * + * @param array $srcset_attributes List of srcset attributes. + */ + $srcset_attributes = (array) apply_filters( + 'rocket_cdn_srcset_attributes', + [ + 'data-lazy-', + 'data-', + ] + ); + return implode( '|', $srcset_attributes ); + } }