Skip to content

Commit

Permalink
#4342 - Merged in 1.0.52
Browse files Browse the repository at this point in the history
  • Loading branch information
WasimM3 committed Jun 16, 2020
1 parent 01a615c commit f98ae93
Showing 1 changed file with 45 additions and 21 deletions.
66 changes: 45 additions & 21 deletions templates/features.php
Original file line number Diff line number Diff line change
Expand Up @@ -8229,7 +8229,7 @@ function ampforwp_add_fallback_element($content='',$tag=''){
$m_content = $matches[1][$i];
$m_content = ampforwp_imagify_webp_compatibility($m_content);
$m_content = ampforwp_ewww_webp_compatibility($m_content);
$m_content = ampforwp_wp_rocket_compatibility($m_content);

$m1_content = ampforwp_set_default_fallback_image($matches[1][$i]);
preg_match_all('/src="(.*?)"/', $m1_content,$fimgsrc);
preg_match_all('/width="(.*?)"/', $m1_content,$fimgwidth);
Expand Down Expand Up @@ -8578,29 +8578,53 @@ function ampforwp_extra_category_fields( $tag ) {
</tr>
<?php
}
if(function_exists('rocket_activation')){
add_filter("ampforwp_the_content_last_filter",'ampforwp_wp_rocket_compatibility',25);
}
function ampforwp_wp_rocket_compatibility($content){
if(function_exists('rocket_activation')){
$cdn_url = get_option('wp_rocket_settings');
if($cdn_url['cdn'] == 1){
$img_cdn_url = '';
if($cdn_url['cdn'] == 1){
$img_cdn_url = '';
$cnds_arr = array();
if(!empty($cdn_url['cdn_zone']) && !empty($cdn_url['cdn_cnames'])){
foreach ($cdn_url['cdn_zone'] as $key => $element) {
if($element == 'images'){
$img_cdn_url = $cdn_url['cdn_cnames'][$key];
break;
}elseif ($element == 'all') {
$img_cdn_url = $cdn_url['cdn_cnames'][$key];
$content = preg_replace('/src="(.*?)\/\/(.*?)wp-content(.*?)"(.*?)>/', 'src=$1//'.$img_cdn_url.'/wp-content$3$4>', $content);
$content = preg_replace('/srcset="(.*?)\/\/(.*?)wp-content(.*?),(.*?)\/\/(.*?)wp-content(.*?),(.*?)\/\/(.*?)wp-content(.*?)"/', 'srcset="$1//'.$img_cdn_url.'/wp-content$3,$4//'.$img_cdn_url.'/wp-content$6"', $content);
}
if(isset($cdn_url['cdn_cnames'][$key]) && $cdn_url['cdn_cnames'][$key]!=''){
$cnds_arr[$element] = $cdn_url['cdn_cnames'][$key];
}
}
}
if($img_cdn_url!=''){
$cdn_url = $img_cdn_url;
$content = preg_replace('/src="(.*?)\/\/(.*?)wp-content(.*?)"(.*?)>/', 'src="$1//'.$cdn_url.'/wp-content$3"$4>', $content);
$content = preg_replace('/srcset="(.*?)\/\/(.*?)wp-content(.*?),(.*?)\/\/(.*?)wp-content(.*?),(.*?)\/\/(.*?)wp-content(.*?)"/', 'srcset="$1//'.$cdn_url.'/wp-content$3,$4//'.$cdn_url.'/wp-content$6,$7//'.$cdn_url.'/wp-content$9"', $content);
}
}
}
return $content;
}
if(isset($cnds_arr['images'])){
$img_cdn_url = $cnds_arr['images'];
}else if(isset($cnds_arr['all'])){
$img_cdn_url = $cnds_arr['all'];
}
if($img_cdn_url!=''){
$parse_url = parse_url($img_cdn_url);
if(!isset($parse_url['scheme'])){
if(!preg_match('/\/\//', $img_cdn_url)){
$img_cdn_url = '//'.$img_cdn_url;
}
}
$comp_dom = new DOMDocument();
@$comp_dom->loadHTML($content);
$xpath = new DOMXPath( $comp_dom );
$nodes = $xpath->query('//amp-img[@src]');
$home_url = home_url();
foreach ($nodes as $node) {
$url = $node->getAttribute('src');
$srcset = $node->getAttribute('srcset');
$is_external = ampforwp_isexternal($url);
if(!$is_external && !$node->hasAttribute('fallback')){
$img_src = str_replace($home_url, $img_cdn_url, $url);
$img_srcset = str_replace($home_url, $img_cdn_url, $srcset);
$node->setAttribute('src',$img_src);
if($img_srcset!=''){
$node->setAttribute('srcset',$img_srcset);
}
}
}
$content = $comp_dom->saveHTML();
}
}
return $content;
}

0 comments on commit f98ae93

Please sign in to comment.