'; $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 = 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') { $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, $structuredData = false) { // If no content found return empty string if(empty($content)){ return ''; } $amps = get_option('amps'); $content = do_shortcode($content); $attributesRemove = array('style', 'border', 'xml:lang', 'content', 'webkitallowfullscreen', 'mozallowfullscreen', ); 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) { 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 > 400) ? 400 : $height; $img->setAttribute('height', $imageHeight); } else { $img->height = ($img->height > 400) ? 400 : $img->height; } $operateOnSecureContent = self::decideNonSecureContent($img->src, $amps); $img->setAttribute('layout', 'responsive'); $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; } } foreach ($htmlDom->find('a') as $anchor) { if($anchor->target && $anchor->target == '_top'){ $anchor->removeAttribute('target'); } } foreach ($htmlDom->find('style') as $style) { $style->outertext = ''; } foreach ($htmlDom->find('script') as $style) { $style->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; } $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; } } // 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', ''); } } 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) { 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; } } }