Created
February 2, 2017 12:49
-
-
Save bisko/c19c2283752af139cbaead9db6f14e88 to your computer and use it in GitHub Desktop.
Check if a site is a WordPress site. Does only basic checks and doesn't check the site endpoints.
This file contains hidden or 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 | |
| $list = [ | |
| 'https://ma.tt', | |
| 'https://google.com', | |
| 'https://wordpress.org' | |
| ]; | |
| function is_wp_site( $site ) { | |
| $content = file_get_contents( $site ); | |
| libxml_use_internal_errors( true ); | |
| $dom = new DOMDocument; | |
| $dom->loadHTML( $content ); | |
| $x_path = new DOMXPath( $dom ); | |
| // check links (exposing API) | |
| $query = $x_path->query( '//link[@rel="https://api.w.org/"]' ); | |
| if ( $query->length ) { | |
| return true; | |
| } | |
| // check head scripts | |
| $query = $x_path->query( '//head//script' ); | |
| foreach ( $query as $element ) { | |
| if ( url_matches_wp_url( $element->getAttribute( 'src' ) ) ) { | |
| return true; | |
| } | |
| } | |
| // check all scripts | |
| $query = $x_path->query( '//script' ); | |
| foreach ( $query as $element ) { | |
| if ( url_matches_wp_url( $element->getAttribute( 'src' ) ) ) { | |
| return true; | |
| } | |
| } | |
| // check all style | |
| $query = $x_path->query( '//link[@rel="stylesheet"]' ); | |
| foreach ( $query as $element ) { | |
| if ( url_matches_wp_url( $element->getAttribute( 'href' ) ) ) { | |
| return true; | |
| } | |
| } | |
| if ( fulltext_matches_wp( $content ) ) { | |
| return true; | |
| } | |
| return false; | |
| } | |
| function url_matches_wp_url( $url ) { | |
| $matches = [ | |
| '\/blog-content\/', | |
| '\/wp-content\/', | |
| '\/wp-includes\/', | |
| '\/js\/devicepx-jetpack\.js' // included Jetpack | |
| ]; | |
| foreach ( $matches as $check ) { | |
| if ( preg_match( '!' . $check . '!uis', $url ) ) { | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| function fulltext_matches_wp( $content ) { | |
| // check for WP Emoji settings | |
| if ( preg_match( '!window\._wpemojiSettings!uis', $content ) ) { | |
| return true; | |
| } | |
| // check for smileys CSS class | |
| if ( preg_match( '!img\.wp-smiley!uis', $content ) ) { | |
| return true; | |
| } | |
| return false; | |
| } | |
| foreach ( $list as $site ) { | |
| var_dump( 'Is ' . $site . ' a WordPress site?: ' . ( is_wp_site( $site ) ? 'YES' : 'NO' ) ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment