Skip to content

Commit

Permalink
Add apply_filter to rewrite srcset to the CDN
Browse files Browse the repository at this point in the history
Fixes #2812.
  • Loading branch information
vmanthos authored Mar 8, 2021
1 parent f9e34ec commit 310ad4b
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion inc/Engine/CDN/CDN.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function( $matches ) {
* @return string
*/
public function rewrite_srcset( $html ) {
$pattern = '#\s+(?:data-lazy-|data-)?srcset\s*=\s*["\']\s*(?<sources>[^"\',\s]+\.[^"\',\s]+(?:\s+\d+[wx])?(?:\s*,\s*[^"\',\s]+\.[^"\',\s]+\s+\d+[wx])*)\s*["\']#i';
$pattern = '#\s+(?:' . $this->get_srcset_attributes() . ')?\s*=\s*["\']\s*(?<sources>[^"\',\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;
Expand Down Expand Up @@ -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-srcset',
'data-srcset',
]
);
return implode( '|', $srcset_attributes );
}
}

0 comments on commit 310ad4b

Please sign in to comment.