Skip to content

Commit

Permalink
#2771 Carousel for AMP to nonAMP
Browse files Browse the repository at this point in the history
  • Loading branch information
MARQAS committed Jan 4, 2019
1 parent 7293348 commit a279c00
Showing 1 changed file with 133 additions and 8 deletions.
141 changes: 133 additions & 8 deletions includes/thirdparty-compatibility.php
Expand Up @@ -42,6 +42,7 @@ function ampforwp_amp_nonamp_convert($ampData, $type=""){
switch($type){
case 'filter':
$returnData = str_replace(array(
"</amp-img>",
"amp-img",
"<style amp-custom>",
"<amp-sidebar ",
Expand All @@ -53,6 +54,7 @@ function ampforwp_amp_nonamp_convert($ampData, $type=""){

),
array(
"",
"img",
"<style type=\"text/css\">",
"<sidebar ",
Expand All @@ -74,6 +76,123 @@ function ampforwp_gdrp_set(){document.getElementById(\'ampforwpConsent\').remove
)
, $ampData);

$returnData = preg_replace(
'/<amp-youtube\sdata-videoid="(.*?)"(.*?)><\/amp-youtube>/',
'<iframe src="https://www.youtube.com/embed/$1" style="width:100%;height:360px;" ></iframe>', $returnData);
$returnData = preg_replace(
'/<amp-iframe\ssrc="(.*?)"(.*?)><\/amp-iframe>/',
'<iframe src="$1" style="width:100%;height:400px;" ></iframe>', $returnData);
// CSS

if( false !== strpos($returnData, 'amp-carousel') ) {
$galleryCss = '* {box-sizing: border-box}.mySlides{display: none}
/* Slideshow container */
.slideshow-container {
max-width: 1000px;
position: relative;
margin: auto;
}
/* Next & previous buttons */
.prev, .next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 16px;
margin-top: -22px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
user-select: none;
}
/* Position the "next button" to the right */
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
background-color: rgba(0,0,0,0.8);
}
/* Caption text */
.text {
color: #f2f2f2;
font-size: 15px;
padding: 8px 12px;
position: absolute;
bottom: 8px;
width: 100%;
text-align: center;
}
/* Number text (1/3 etc) */
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 0;
}
/* The dots/bullets/indicators */
.dot {
cursor: pointer;
height: 15px;
width: 15px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
.active, .dot:hover {
background-color: #717171;
}
/* Fading animation */
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}
@-webkit-keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
@keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
/* On smaller screens, decrease text size */
@media only screen and (max-width: 300px) {
.prev, .next,.text {font-size: 11px}
}';
$galleryJs = '<script>
var slideIndex = 1;
showSlides(slideIndex);
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
}
</script>';
}
$nonampCss = '
.cntr img{height:auto !important;}
img{height:auto;}
Expand All @@ -82,22 +201,28 @@ function ampforwp_gdrp_set(){document.getElementById(\'ampforwpConsent\').remove
.image-mod img{width:100%;}
';
$re = '/<style\s*type="text\/css">(.*?)<\/style>/si';
$subst = "<style type=\"text/css\">$1 ".$nonampCss."</style>";
$subst = "<style type=\"text/css\">$1 ".$nonampCss.$galleryCss."</style>";
$returnData = preg_replace($re, $subst, $returnData);


$returnData = preg_replace(
'/<amp-youtube\sdata-videoid="(.*?)"(.*?)><\/amp-youtube>/',
'<iframe src="https://www.youtube.com/embed/$1" style="width:100%;height:360px;" ></iframe>', $returnData);
$returnData = preg_replace(
'/<amp-iframe\ssrc="(.*?)"(.*?)><\/amp-iframe>/',
'<iframe src="$1" style="width:100%;height:400px;" ></iframe>', $returnData);
$returnData = preg_replace_callback('/<amp-carousel\s(.*?)>(.*?)<\/amp-carousel>/s', 'ampforwp_non_amp_gallery', $returnData );
$returnData = str_replace('</footer>', '</footer>'.$galleryJs, $returnData);
break;
}
return $returnData;
}
}

//
function ampforwp_non_amp_gallery($matches){
$images = $matches[2];
$images = preg_replace_callback("/<img(.*?)>/", function($m){
return '<li class="mySlides fade">'.$m[0].'</li>';
}, $images);
$imagesHTML = '<ul class="slideshow-container">'.$images.'<a class="prev" onclick="plusSlides(-1)">&#10094;</a>
<a class="next" onclick="plusSlides(1)">&#10095;</a></ul>';
return $imagesHTML;
}

//Facility to create child theme For AMP
add_filter( 'amp_post_template_file', 'ampforwp_child_custom_header_file', 20, 3 );
add_filter( 'amp_post_template_file', 'ampforwp_child_designing_custom_template', 20, 3 );
Expand Down

0 comments on commit a279c00

Please sign in to comment.