'; $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['SCRIPT_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 iframes $content = str_replace('', '-->', $content); // Replace imgs with amp-imgs $content = str_replace(']+) style=".*?"/i', '$1', $content); $content = preg_replace('/(<[^>]+) border=".*?"/i', '$1', $content); return $content; } // 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; } } }