Skip to content

Instantly share code, notes, and snippets.

@clifgriffin
Created February 15, 2023 13:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save clifgriffin/16ac0e111fc9a35180bc45c7cef18820 to your computer and use it in GitHub Desktop.
Save clifgriffin/16ac0e111fc9a35180bc45c7cef18820 to your computer and use it in GitHub Desktop.
Check URLs to see if they are on Shopify or WooCommerce
<?php
/**
* Instructions: cd into site directory
*
* Run `wp eval-file ./path/to/script.php`
*/
global $wpdb;
// Query
$sites = $wpdb->get_col( "SELECT DISTINCT (site_name) FROM wp_edd_license_activations WHERE is_local = 0 AND license_id IN( SELECT id FROM wp_edd_licenses WHERE status = 'expired' )" );
$shopify_sites = array();
$non_shopify_sites = array();
$woocommerce_sites = array();
$err_sites = array();
$dead_sites = array();
foreach ( $sites as $site ) {
$url = $site;
if ( false === strpos( $site, 'http://' ) && false === strpos( $site, 'https://' ) ) {
$url = "https://{$site}";
}
// If dev or staging site skip it
if ( cg_is_local_url( $url ) ) {
echo "Skipping {$url} because it is a dev or staging site.\n";
continue;
}
// Check if URL has A record
$dns = checkdnsrr( rtrim( $site, '/' ), 'A' );
if ( ! $dns ) {
echo "Skipping {$url} because it has no A record.\n";
$dead_sites[] = $url;
continue;
}
stream_context_set_default(
array(
'http' => array(
'method' => 'GET',
'timeout' => 5,
),
)
);
// Get the response headers for the URL
$headers = get_headers( $url, true );
if ( ! $headers ) {
echo "Erred on site {$url}\n";
$err_sites[] = $url;
continue;
}
// print_r( $headers );
$headers = array_change_key_case( $headers );
// Check if the "X-ShopId" header is set
if ( isset( $headers['x-shopid'] ) || isset( $headers['x-sorting-hat-shopid'] ) ) {
echo "{$url} is on Shopify!\n";
$shopify_sites[] = $url;
continue;
}
// Check if this file exists wp-content/plugins/woocommerce/license.txt
$license_file = wp_remote_get( $url . '/wp-content/plugins/woocommerce/assets/js/frontend/add-to-cart.min.js' );
if ( ! is_wp_error( $license_file ) && 200 === wp_remote_retrieve_response_code( $license_file ) ) {
echo "{$url} is on WooCommerce!\n";
$woocommerce_sites[] = $url;
continue;
}
echo "{$url} is not on Shopify or Woo.\n";
$non_shopify_sites[] = $url;
}
echo 'Shopify sites: ' . count( $shopify_sites ) . "\n";
echo 'WooCommerce sites: ' . count( $woocommerce_sites ) . "\n";
echo 'Non-Shopify and Non-Woo sites: ' . count( $non_shopify_sites ) . "\n";
echo 'Erred sites: ' . count( $err_sites ) . "\n";
echo 'Dead sites: ' . count( $dead_sites ) . "\n";
function cg_is_local_url( $url = '' ) {
$is_local_url = false;
// Trim it up
$url = strtolower( trim( $url ) );
// Need to get the host...so let's add the scheme so we can use parse_url
if ( false === strpos( $url, 'http://' ) && false === strpos( $url, 'https://' ) ) {
$url = 'http://' . $url;
}
$url_parts = parse_url( $url );
$host = ! empty( $url_parts['host'] ) ? $url_parts['host'] : false;
if ( empty( $url ) || empty( $host ) ) {
return false;
}
if ( false !== ip2long( $host ) ) {
if ( ! filter_var( $host, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ) ) {
$is_local_url = true;
}
} elseif ( 'localhost' === $host ) {
$is_local_url = true;
}
$tlds_to_check = array(
'.dev',
'.local',
'.test',
);
foreach ( $tlds_to_check as $tld ) {
if ( false !== strpos( $host, $tld ) ) {
$is_local_url = true;
break;
}
}
if ( substr_count( $host, '.' ) > 1 ) {
$subdomains_to_check = array(
'dev.',
'*.staging.',
'*.test.',
'staging-*.',
'*.wpengine.com',
'*cloudways*',
);
foreach ( $subdomains_to_check as $subdomain ) {
$subdomain = str_replace( '.', '(.)', $subdomain );
$subdomain = str_replace( array( '*', '(.)' ), '(.*)', $subdomain );
if ( preg_match( '/^(' . $subdomain . ')/', $host ) ) {
$is_local_url = true;
break;
}
}
}
// If url has more than 3 forward slashes, it's probably a local url
if ( substr_count( $url, '/' ) > 3 ) {
$is_local_url = true;
}
return $is_local_url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment