'; $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() && $amps['on_posts']) || (is_page() && $amps['on_pages']) || (is_category() && $amps['on_categories']) || (is_tag() && $amps['on_tags']) || (is_home() && $amps['on_home']) ) { 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 = str_replace('/', '', $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' || is_category()) { $return = $pat[sizeof($pat) - 1]; return 2; } if ($pat[sizeof($pat) - 2] == 'tag' || is_tag()) { $return = $pat[sizeof($pat) - 1]; return 4; } if ($realurl == $siteurl) { return 0; } return -1; } public static function replaceForAMP($content, $structuredData = false) { // If no content found return empty string if(empty($content)){ return ''; } $content = wpautop($content); $amps = get_option('amps'); $content = do_shortcode($content); $removeInputAttributes = array('onchange', 'onclick', 'onmouseover', 'onfocus', 'onblur', 'rel'); $attributesRemove = array('style', 'border', 'xml:lang', 'content', 'webkitallowfullscreen', 'mozallowfullscreen', 'onchange', 'onclick', 'onmouseover', 'onfocus', 'onblur', 'rel'); foreach($attributesRemove as $attribute){ $content = preg_replace('/(<[^>]+) '.$attribute.'=("|\').*?("|\')/i', '$1', $content); } $tagsRemove = array('script', 'style', 'object', 'param', 'object'); foreach($tagsRemove as $tag){ $content = preg_replace('#<'.$tag.'(.*?)>(.*?)#is', '', $content); } // Constructing HTML Dom $htmlDom = str_get_html($content); // Processing [image] foreach ($htmlDom->find('img') as $img) { if(!empty($img->src)){ list($width, $height) = self::aspectRatioImage($img->src); $img->width = str_replace('%', '', $img->width); if (!$img->width) { $img->setAttribute('width', $width); } $img->height = str_replace('%', '', $img->height); if (!$img->height) { $img->setAttribute('height', $height); } $operateOnSecureContent = self::decideNonSecureContent($img->src, $amps); if($img->width > 400){ $img->setAttribute('layout', 'responsive'); $img->class = $img->class.' img-in-text-lg'; } else { $img->class = $img->class.' img-in-text-sm'; $img->outertext = '
'.$img->outertext.'
'; } $img->outertext = str_replace('outertext); $img->outertext = str_replace('>', '>', $img->outertext); if($operateOnSecureContent['removeImg']){ $img->outertext = ''; } else if($operateOnSecureContent['placehold']){ $img->outertext = SUPREMACY_BANNER_IMAGE; } } else { $img->outertext = ''; } } foreach ($htmlDom->find('a') as $anchor) { if($anchor->target && $anchor->target == '_top'){ $anchor->removeAttribute('target'); } $anchor->removeAttribute('type'); $anchor->removeAttribute('rel'); if(substr_count($anchor->href, 'javascript')){ $anchor->href = '#'; } if(!empty($anchor->href)){ $pdfUrl = strpos($anchor->href, '.pdf'); $link_domain = parse_url($anchor->href); $link_domain = preg_replace('#^www\.(.+\.)#i', '$1', $link_domain['host']); $domain = parse_url(get_site_url()); $site_domain = preg_replace('#^www\.(.+\.)#i', '$1', $domain['host']); if($site_domain == $link_domain && !$pdfUrl){ $p = $anchor->href; $p = str_replace('\\','/',trim($p)); $anchor->href = (substr($p,-1)!='/') ? $p.='/' : $p; $anchor->href = $anchor->href.'amp'; } } foreach ($removeInputAttributes as $removeInputAttribute) { $anchor->removeAttribute($removeInputAttribute); } } foreach ($htmlDom->find('td') as $td) { $td->removeAttribute('nowrap'); } foreach ($htmlDom->find('video') as $video) { foreach ($video->find('source') as $videosource) { $operateOnSecureVdContent = self::decideNonSecureContent($videosource->src, $amps); if($operateOnSecureVdContent['removeImg']){ $video->outertext = ''; } else if($operateOnSecureVdContent['placehold']){ $video->outertext = SUPREMACY_BANNER_IMAGE; } } $video->outertext = str_replace('outertext); $video->outertext = str_replace('', '>', $img->outertext); } foreach ($htmlDom->find('th') as $th) { $th->removeAttribute('nowrap'); } foreach ($htmlDom->find('font') as $font) { $font->outertext = str_replace('outertext); $font->outertext = str_replace('', '', $font->outertext); } foreach ($htmlDom->find('article') as $article) { $article->removeAttribute('style'); } foreach ($htmlDom->find('style') as $style) { $style->outertext = ''; } foreach ($htmlDom->find('script') as $script) { $style->outertext = ''; } // Processing [iframes] foreach ($htmlDom->find('iframe') as $iframe) { if(!empty($iframe->src)){ if (!$iframe->width) { $iframe->setAttribute('width', 800); } else { $iframe->width = ($iframe->width > 800) ? 800 : $iframe->width; } $iframe->width = str_replace('%', '', $iframe->width); if (!$iframe->height) { $iframe->setAttribute('height', 500); } else { $iframe->height = ($iframe->height > 500) ? 500 : $iframe->height; } $iframe->height = str_replace('%', '', $iframe->height); $operateOnSecureContentIf = self::decideNonSecureContent($iframe->src, $amps); $iframe->setAttribute('layout', 'responsive'); $iframe->removeAttribute('webkitallowfullscreen'); $iframe->removeAttribute('mozallowfullscreen'); $iframe->setAttribute('sandbox', 'allow-scripts allow-same-origin'); $iframe->setAttribute('frameborder', 0); $iframe->outertext = str_replace('outertext); $iframe->outertext = str_replace('', '', $iframe->outertext); if($operateOnSecureContentIf['removeImg']){ $iframe->outertext = ''; } else if($operateOnSecureContentIf['placehold']){ $iframe->outertext = SUPREMACY_BANNER_IMAGE; } } else { $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->outertext = ''; } else { $operateOnSecureContentIf = self::decideNonSecureContent($form->action, $amps); if($operateOnSecureContentIf['removeImg']){ $form->outertext = ''; } else if($operateOnSecureContentIf['placehold']){ $form->outertext = SUPREMACY_BANNER_IMAGE; } } } foreach ($htmlDom->find('input') as $input) { foreach ($removeInputAttributes as $removeInputAttribute) { $input->removeAttribute($removeInputAttribute); } if($input->type == 'file'){ $input->outertext = ''; } } if($structuredData){ $htmlDom = strip_tags($htmlDom); $htmlDom = addcslashes($htmlDom, '"'); $htmlDom = str_replace(array("\r","\n"),"",$htmlDom); } 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) { $parsed_url = parse_url($url); if(!isset($parsed_url['scheme'])){ $url = 'http:'.$url; } list($width, $height) = getimagesize($url); $result = array( 'width' => $width, 'height' => $height ); return $result; } public static function decideNonSecureContent($source_path, $amps){ $url = parse_url($source_path); $removeImg = false; $placehold = false; if (!empty($amps['non_secure_content']) && !empty($url['scheme']) && $url['scheme'] == 'http') { $operation = $amps['non_secure_content_should_be']; if($operation == 'removed'){ $removeImg = true; } else if($operation == 'placeholded'){ $placehold = true; } } return array('removeImg' => $removeImg, 'placehold' => $placehold); } public static function isSlug($defaultValue = -1){ $result = $defaultValue; $realurl = $_SERVER['REQUEST_URI']; $parsedUrl = parse_url($realurl); if(!empty($parsedUrl['path'])){ $pathArray = explode('/', $parsedUrl['path']); $term = $pathArray[1]; $termCategory = term_exists($term, 'category'); if ( $termCategory !== 0 && $termCategory !== null ){ $result = array('term' => $term, 'type' => 2); } $termTag = term_exists($term, 'post_tag'); if ( $termTag !== 0 && $termTag !== null ){ $result = array('term' => $term, 'type' => 4); } } return $result; } public static function constructAMPmenu($content = ''){ $htmlDom = str_get_html($content); $isAMP = self::extractAMP($_SERVER['REQUEST_URI']); foreach ($htmlDom->find('a') as $anchor) { if(!empty($anchor->href) && !$isAMP){ $anchor->href = $anchor->href.'amp'; } $anchor->class .= ' menu-item'; } foreach ($htmlDom->find('amp-iframe') as $ampiframe) { $ampiframe->outertext = ''; } return $htmlDom; } public static function aspectRatioImage($imageSrc){ $parsed_url = parse_url($imageSrc); if(!isset($parsed_url['scheme'])){ $imageSrc = 'http:'.$imageSrc; } list($originalWidth, $originalHeight) = getimagesize($imageSrc); $ratio = $originalWidth / $originalHeight; $size = 450; $targetWidth = $targetHeight = min($size, max($originalWidth, $originalHeight)); if ($ratio < 1) { $targetWidth = $targetHeight * $ratio; } else { $targetHeight = $targetWidth / $ratio; } return array(ceil($targetWidth), ceil($targetHeight)); } } }