';
$current_url = add_query_arg( $wp->query_string, '', home_url( $wp->request ) );
$parse = parse_url($current_url);
$current_url = $parse['scheme'] . '://' . $parse['host'] . $parse['path'] . '/amp/';
$current_url = sprintf($amp_tag, $current_url);
if (is_single() || is_page() || is_category() || is_tag()) {
echo $current_url;
}
}
// Determine if AMP is present on the current screen
public static function renderAMP() {
$isAMP = self::extractAMP( $_SERVER['REQUEST_URI'] );
if ($isAMP) {
status_header(200);
require_once AMP_PATH . DIRECTORY_SEPARATOR . 'amp-template.php';
die();
}
}
// Extract the AMP url
private static function extractAMP($url) {
$par = parse_url($url);
$pat = $par['path'];
$las = substr($pat, -4);
$las = rtrim($las, '/');
if (strtolower( $las ) == 'amp') {
return true;
} else {
return false;
}
}
// Get the AMP Types
public static function getAMPtype(&$return = null) {
$siteurl = get_site_url();
$realurl = $_SERVER['REQUEST_URI'];
$realurl = rtrim($realurl, '/');
$realurl = str_replace('/amp', '', $realurl);
$pat = explode('/', $realurl);
if ($pat[sizeof($pat) - 2] == 'category') {
$return = $pat[sizeof($pat) - 1];
return 2;
}
if ($pat[sizeof($pat) - 2] == 'tag') {
$return = $pat[sizeof($pat) - 1];
return 4;
}
if ($realurl == $siteurl) {
return 0;
}
return -1;
}
public static function replaceForAMP($content) {
// Remove 'style' attribute from HTML
$content = preg_replace('/(<[^>]+) style=".*?"/i', '$1', $content);
// Constructing HTML Dom
$htmlDom = str_get_html($content);
// Processing [image]
foreach($htmlDom->find('img') as $img){
list($width, $height) = getimagesize($img->src);
if(!$img->width){
$imageWidth = ($width > 800) ? 800 : $width;
$img->setAttribute('width', $imageWidth);
} else {
$img->width = ($img->width > 800) ? 800 : $img->width;
}
if(!$img->height){
$imageHeight = ($height > 300) ? 300 : $height;
$img->setAttribute('height', $imageHeight);
} else {
$img->height = ($img->height > 300) ? 300 : $img->height;
}
$img->outertext = str_replace('
outertext);
$img->outertext = str_replace('>', '>', $img->outertext);
}
// Processing [iframes]
foreach($htmlDom->find('iframe') as $iframe){
if(!$iframe->width){
$iframe->setAttribute('width', 800);
} else {
$iframe->width = ($iframe->width > 800) ? 800 : $iframe->width;
}
if(!$iframe->height){
$iframe->setAttribute('height', 500);
} else {
$iframe->height = ($iframe->height > 500) ? 500 : $iframe->height;
}
$iframe->setAttribute('layout', 'responsive');
$iframe->setAttribute('sandbox', 'allow-scripts allow-same-origin');
$iframe->setAttribute('frameborder', 0);
$iframe->outertext = str_replace('', '', $iframe->outertext);
}
// Processing [forms]
foreach($htmlDom->find('form') as $form){
if(!$form->hasAttribute('target')){
$form->setAttribute('target', '_blank');
} else {
$form->target = '_blank';
}
if(!$form->hasAttribute('action')){
$form->setAttribute('action', '');
}
}
return $htmlDom;
}
// Google Mobile Test
public static function mobileTest() {
$website = $_POST['website'];
if (filter_var($website, FILTER_VALIDATE_URL) === FALSE) {
wp_send_json( array(
'status'=>'ERROR',
'message'=>"URL that you provided is not a valid URL."
) );
wp_die();
}
$gurl = "https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady?key=%s&screenshot=true&snapshots=true&locale=en_US&url=%s&strategy=mobile&filter_third_party_resources=true";
$key = "AIzaSyAwlPiPJIkTejgqqH01v9DmtPoPeOPXDUQ";
$url = sprintf($gurl, $key, urlencode($website));
$response = wp_remote_get( $url, array('timeout' => 120));
if( is_array($response) ) {
$body = $response['body']; // use the content
echo $body;
} else {
wp_send_json( array(
'status'=>'ERROR',
'message'=>"There was a problem while testing this URL. Please try again later.",
'debug'=>$response
) );
}
}
public static function getImageDimensions($url){
list($width, $height) = getimagesize($url);
$result = array(
'width' => $width,
'height' => $height
);
return $result;
}
}
}