Last active
February 17, 2021 19:02
-
-
Save westonruter/5bbf867ea1823c1f193b8adee246ca6b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Check domains for the status of AMP on the homepage. | |
* | |
* The input file needs to have one domain per line. | |
* | |
* USAGE: | |
* | |
* wp eval-file amp-domain-checker.php domains.txt | |
* cat domains.txt | wp eval-file amp-domain-checker.php - | |
*/ | |
use AmpProject\Dom\Document; | |
use AmpProject\Attribute; | |
global $argv; | |
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped | |
// phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited | |
if ( ! defined( 'WP_CLI' ) || ! isset( $argv[3] ) ) { | |
printf( "Usage: wp eval-file %s FILE\n", basename( __FILE__ ) ); | |
exit( 1 ); | |
} | |
if ( ! class_exists( 'AmpProject\Dom\Document' ) ) { | |
WP_CLI::error( 'The AMP plugin must be active.' ); | |
} | |
$input = $argv[3]; | |
if ( '-' === $input ) { | |
$input = 'php://stdin'; | |
} elseif ( ! file_exists( $input ) ) { | |
WP_CLI::error( "File does not exist: {$input}" ); | |
} | |
$domains = explode( "\n", trim( file_get_contents( $input ) ) ); | |
/** | |
* Examine a URL for AMP status. | |
* | |
* @param string $url URL. | |
* @param bool $follow_link Whether to follow the amphtml link. | |
* | |
* @return string[] Status. | |
* @throws Exception | |
*/ | |
function examine_url( $url, $follow_link = true ) { | |
$response = wp_remote_get( $url ); | |
if ( is_wp_error( $response ) ) { | |
throw new Exception( $response->get_error_code() ); | |
} elseif ( 200 !== wp_remote_retrieve_response_code( $response ) ) { | |
throw new Exception( sprintf( 'HTTP %s', wp_remote_retrieve_response_code( $response ) ) ); | |
} | |
$dom = new Document(); | |
$dom->loadHTML( wp_remote_retrieve_body( $response ) ); | |
$amphtml_link = $dom->xpath->query( '/html/head/link[ @rel = "amphtml" ]' )->item( 0 ); | |
if ( $follow_link && $amphtml_link instanceof DOMElement ) { | |
$amphtml_url = $amphtml_link->getAttribute( 'href' ); | |
return examine_url( $amphtml_url, false ); | |
} | |
$has_attribute = ( | |
$dom->documentElement->hasAttribute( Attribute::AMP ) | |
|| | |
$dom->documentElement->hasAttribute( Attribute::AMP4ADS_EMOJI ) | |
|| | |
$dom->documentElement->hasAttribute( Attribute::AMP4ADS_EMOJI_ALT ) | |
); | |
$has_runtime = $dom->xpath->query( '/html/head/script[ @src = "https://cdn.ampproject.org/v0.js" ]' )->length > 0; | |
$is_amp = $has_attribute || $has_runtime; | |
$amp_generator_meta = $dom->xpath->query( '/html/head/meta[ @name = "generator" and contains( @content, "AMP Plugin" ) ]' )->item( 0 ); | |
if ( ! $is_amp ) { | |
return [ 'NOT_AMP' ]; | |
} elseif ( ! $amp_generator_meta ) { | |
return [ 'AMP_BUT_NOT_OURS' ]; | |
} | |
$result = []; | |
if ( ! $has_attribute ) { | |
$result[] = 'DIRTY_AMP'; | |
} else { | |
$result[] = 'AMP'; | |
} | |
$result[] = preg_replace( '/^AMP Plugin .+?; /', '', $amp_generator_meta->getAttribute( 'content' ) ); | |
return $result; | |
} | |
fwrite( STDERR, sprintf( "Checking %d domains...\n", count( $domains ) ) ); | |
foreach ( $domains as $i => $domain ) { | |
echo "$i\t$domain\t"; | |
if ( preg_match( '#^https?://#', $domain ) ) { | |
$url = $domain; | |
} else { | |
$url = "https://$domain/"; | |
} | |
try { | |
echo implode( "\t", examine_url( $url ) ); | |
} catch ( Exception $e ) { | |
printf( "EXCEPTION\t%s", $e->getMessage() ); | |
} | |
echo "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment