#!/usr/bin/env php
<?php

if($argc != 2) {
        print "usage: check_wp_versions.php <path to wp installation>\n";
        exit(1);
}

chdir($argv[1]);

require_once('./wp-load.php');

global $wp_version;
$core_updates = 0;
$plugin_updates = 0;
$theme_updates = 0;

wp_version_check();
wp_update_plugins();
wp_update_themes();

// For WP-installations before 2.9.0
if (function_exists('get_transient')) {
        $core = get_transient('update_core');
        $plugins = get_transient('update_plugins');
        $themes = get_transient('update_themes');

        if ($core == false){
                $core = get_site_transient('update_core');
                $plugins = get_site_transient('update_plugins');
                $themes = get_site_transient('update_themes');
        }
    
// For WP-installations after 2.9.0
} else {
        $core = get_site_transient('update_core');
        $plugins = get_site_transient('update_plugins');
        $themes = get_site_transient('update_themes');
}

foreach($core->updates as $update) {
        if($update->current != $wp_version) {
                $core_updates = 1;
        }
}

foreach($plugins->response as $plgupd) {
        $plugin_updates = 1;
}

foreach($themes->response as $thupd) {
        $theme_updates = 1;
}

if($core_updates) {
        $text = "core updates available!";
} else {
        $text = "core ok";
}

if($plugin_updates) {
        $text .= " ; plugins updates are available!";
} else {
        $text .= " ; plugins ok";
}

if($theme_updates) {
        $text .= " ; theme updates are available!";
} else {
        $text .= " ; themes ok";
}

if($core_updates || $plugin_updates || $theme_updates) {
        print("CRITICAL - " . $text."\n");
        exit(2);
} else {
        print("OK - " . $text."\n");
        exit(0);
}

print("CRITICAL - Error in check_wp_versions.php");
exit(2);

?>