'; $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'] && !post_password_required() && !$amps_disabled_for_this_post) || (is_page() && $amps['on_pages'] && !post_password_required()&& !$amps_disabled_for_this_post) || (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 && !post_password_required()) { status_header(200); require_once AMP_PATH . DIRECTORY_SEPARATOR . 'amp-template.php'; die(); } } // Extract the AMP url public 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, $menuContent = false) { global $wp, $global_content_specs; $current_url = str_replace('/amp', '', add_query_arg('', '', home_url($wp->request))); // 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('vocab', 'property', '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', 'spantimes'); foreach($tagsRemove as $tag){ $content = preg_replace('#<'.$tag.'(.*?)>(.*?)#is', '', $content); } // Constructing HTML Dom $htmlDom = str_get_html($content); // Processing [image] $imgCounter = 1; 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); } if($img->width > 740) $img->width = 740; $operateOnSecureContent = self::decideNonSecureContent($img->src, $amps); $lightBoxEnabled = (!isset($amps['disable_lightbox_on_images']) || empty($amps['disable_lightbox_on_images'])) ? 1 : 0; $global_content_specs['lightbox'] = boolval($lightBoxEnabled); if(!empty($lightBoxEnabled) && !$menuContent){ $img->setAttribute('on', "tap:lightbox{$imgCounter}"); $img->setAttribute('tabindex', $imgCounter); $img->setAttribute('role', "button"); } $img->outertext = preg_replace('#(.*?)#is', '', $img->outertext); 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); $img->outertext = str_replace('/>', '/>', $img->outertext); if(!empty($lightBoxEnabled) && !$menuContent){ $img->outertext .= ''; } if($operateOnSecureContent['removeImg']){ $img->outertext = ''; } else if($operateOnSecureContent['placehold']){ $img->outertext = SUPREMACY_BANNER_IMAGE; } } else { $img->outertext = ''; } $imgCounter++; } foreach ($htmlDom->find('a') as $anchor) { if($anchor->target && (in_array($anchor->target, array('_top', '_new')) || $anchor->target == '')){ $anchor->removeAttribute('target'); } $anchor->removeAttribute('type'); $anchor->removeAttribute('rel'); if(substr_count($anchor->href, 'javascript')){ $anchor->href = '#'; } $lightBoxEnabled = (!isset($amps['disable_lightbox_on_images']) || empty($amps['disable_lightbox_on_images'])) ? 1 : 0; if(!empty($anchor->href) && !empty($lightBoxEnabled)){ $imageUrlContainsImg = explode('.',$anchor->href); if(in_array($imageUrlContainsImg[1], array('jpg', 'png', 'jpeg', 'gif'))){ $anchor->href = '#'; } } if(!isset($amps['internal_links_non_amp']) || empty($amps['internal_links_non_amp'])){ if(!empty($anchor->href) && $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] $iFrameCounter = 0; foreach ($htmlDom->find('iframe') as $iframe) { if(!empty($iframe->src)){ $global_content_specs['iframe'][$iFrameCounter] = TRUE; 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(strpos($iframe->src, 'youtube') > 0 || strpos($iframe->src, 'youtu') > 0){ $global_content_specs['youtube'][$iFrameCounter] = TRUE; $global_content_specs['iframe'][$iFrameCounter] = FALSE; $videoId = self::getYouTubeVideoId($iframe->src); $iframe->outertext = ''; } if($operateOnSecureContentIf['removeImg']){ $global_content_specs['iframe'][$iFrameCounter] = FALSE; $iframe->outertext = ''; } else if($operateOnSecureContentIf['placehold']){ $global_content_specs['iframe'][$iFrameCounter] = FALSE; $iframe->outertext = SUPREMACY_BANNER_IMAGE; } } else { $global_content_specs['iframe'][$iFrameCounter] = FALSE; $iframe->outertext = ''; } $iFrameCounter++; } // Processing [forms] foreach ($htmlDom->find('form') as $form) { if (!$form->hasAttribute('target')) { $form->setAttribute('target', '_blank'); } else { $form->target = '_blank'; } if (!$form->hasAttribute('method') || $form->method == '') { $form->setAttribute('method', 'GET'); } if (!$form->hasAttribute('action') || $form->action == '') { $temp_current_url = $current_url; $temp_current_url = str_replace('/amp', '', $temp_current_url); $form->action = $temp_current_url; } else { $operateOnSecureContentIf = self::decideNonSecureContent($form->action, $amps); if($operateOnSecureContentIf['removeImg']){ $form->outertext = ''; } else if($operateOnSecureContentIf['placehold']){ $form->outertext = SUPREMACY_BANNER_IMAGE; } } if($form->method == 'POST'){ $form->setAttribute('action-xhr', $form->action); $form->removeAttribute('action'); } } 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); foreach ($htmlDom->find('a') as $anchor) { if(!empty($anchor->href)){ $isAMP = self::extractAMP($anchor->href); if(!$isAMP){ $anchor->href = str_replace('//amp', '/amp', $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); if($originalWidth != 0 && $originalHeight != 0){ $ratio = $originalWidth / $originalHeight; } else { $ratio = 0; } $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)); } private static function getYouTubeVideoId($youTubeVideoUrl){ $id = 0; if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $youTubeVideoUrl, $match)) { $id = $match[1]; } return $id; } public static function ampSupremacyLoadYoastOgData(){ if ( is_plugin_active( 'wordpress-seo/wp-seo.php' ) ) { $yoastOptions = WPSEO_Options::get_option( 'wpseo_social' ); if ( $yoastOptions['twitter'] === true ) { WPSEO_Twitter::get_instance(); } if ( $yoastOptions['opengraph'] === true ) { $GLOBALS['wpseo_og'] = new WPSEO_OpenGraph; } do_action( 'wpseo_opengraph' ); } } public static function amps_load_ps_meta_data($data){ $htmlDom = str_get_html($data); foreach ($htmlDom->find('link') as $link) { $link->outertext = ''; } foreach ($htmlDom->find('meta') as $meta) { if($meta->hasAttribute('name') && (!empty($meta->name) && $meta->name == 'description')){ $meta->content = htmlspecialchars($meta->content); } } echo $htmlDom; } public function amps_overwrite_with_custom_settings($amps, $post){ if(!empty($post)){ $custom_global_settings_relationship = array( 'on_post_date' => 'hide_post_date', 'on_author_name' => 'hide_post_author_name', 'on_post_tags' => 'hide_post_tags', 'on_post_categories' => 'hide_post_categories', 'use_seo_meta' => 'load_seo', ); $amps_use_custom_settings_option = get_post_meta($post->ID, AMP_CUSTOM_SETTINGS_SWITCH, TRUE); $amps_use_custom_settings = get_post_meta($post->ID, AMP_CUSTOM_SETTINGS, TRUE); if(isset($amps_use_custom_settings_option) && $amps_use_custom_settings_option == 1){ foreach($custom_global_settings_relationship as $this_key => $this_val){ $amps[$this_key] = (int) !$amps_use_custom_settings[$this_val]; } } } return $amps; } public static function amps_custom_amp_enabled($post){ $amps_disabled_for_this_post = 0; if(isset($post) && !empty($post->ID)){ $amps_use_custom_settings_option = get_post_meta($post->ID, AMP_CUSTOM_SETTINGS_SWITCH, TRUE); $amps_use_custom_settings = get_post_meta($post->ID, AMP_CUSTOM_SETTINGS, TRUE); if(isset($amps_use_custom_settings['enable']) && $amps_use_custom_settings['enable'] == 1){ $amps_disabled_for_this_post = 1; } } return $amps_disabled_for_this_post; } } }