Created
April 4, 2023 10:39
-
-
Save dovidezra/493952d010d74d5ead3d058fdb69cd7d to your computer and use it in GitHub Desktop.
Check the installed WooCommerce version, and compare it to the latest version available on WordPress.org. If the installed version is older, it will print "Newer Version Available." If the installed version is the same or WooCommerce is not found, it will print an appropriate message.
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 | |
///////////////////////////////////////////////////////////////////////////////////////////////////// | |
/// Run the following command in SSH to install the dependency (i.e. "fabpot/goutte"): ////////////// | |
/// composer require fabpot/goutte ////////////////////////////////////////////////////////////////// | |
///////////////////////////////////////////////////////////////////////////////////////////////////// | |
/// 1. Get the latest version of the WooCommerce plugin from the WordPress.org plugin repository. /// | |
/// 2. Check a list of websites for the installed version of the WooCommerce plugin. //////////////// | |
/// 3. Compare the installed version with the latest version available on WordPress.org. //////////// | |
/// 4. Output the appropriate message based on the comparison results. ////////////////////////////// | |
///////////////////////////////////////////////////////////////////////////////////////////////////// | |
require_once 'vendor/autoload.php'; | |
use Goutte\Client; | |
use GuzzleHttp\Client as GuzzleClient; | |
use SimpleXMLElement; | |
function get_latest_woocommerce_version() { | |
$url = 'https://api.wordpress.org/plugins/info/1.0/woocommerce'; | |
$client = new GuzzleClient(); | |
$response = $client->get($url); | |
$xml = new SimpleXMLElement($response->getBody()); | |
return (string) $xml->version; | |
} | |
function get_installed_woocommerce_version($url) { | |
$client = new Client(); | |
try { | |
$crawler = $client->request('GET', $url . '/wp-content/plugins/woocommerce/readme.txt'); | |
$text = $crawler->text(); | |
preg_match('/Stable tag: (\d+\.\d+\.\d+)/', $text, $matches); | |
return isset($matches[1]) ? $matches[1] : null; | |
} catch (Exception $e) { | |
return null; | |
} | |
} | |
$latest_version = get_latest_woocommerce_version(); | |
$websites = ['https://example1.com', 'https://example2.com']; | |
foreach ($websites as $website) { | |
$installed_version = get_installed_woocommerce_version($website); | |
if ($installed_version !== null && version_compare($installed_version, $latest_version, '<')) { | |
echo "Website: {$website} - Newer Version Available.\n"; | |
} elseif ($installed_version !== null) { | |
echo "Website: {$website} - WooCommerce version: {$installed_version}\n"; | |
} else { | |
echo "Website: {$website} - WooCommerce not found or version couldn't be determined\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment