diff --git a/freifunk-versions.php b/freifunk-versions.php index d57b361..88ff49e 100644 --- a/freifunk-versions.php +++ b/freifunk-versions.php @@ -37,12 +37,41 @@ function ff_hh_getmanifest ($basedir) { return $manifest; } +/* gets latest version from first manifest line */ +function ff_hh_getlatest ($basedir) { + // Caching + if ( false === ( $sw_ver = get_transient( "ff_hh_latestversion" ) ) ) { + $sw_ver = 'unknown'; + $input = wp_remote_retrieve_body( wp_remote_get($basedir . 'sysupgrade/manifest') ); + foreach ( explode("\n", $input) as $line ) { + $ret = sscanf($line, '%s %s %s %s', $hw, $sw_ver, $hash, $filename); + if ($ret === 4) { + // break processing on first matching line + $cachetime = FF_HH_CACHETIME * MINUTE_IN_SECONDS; + set_transient( "ff_hh_latestversion", $sw_ver, $cachetime ); + break; + } + } + } + return $sw_ver; +} + +if ( ! shortcode_exists( 'ff_hh_latestversion' ) ) { + add_shortcode( 'ff_hh_latestversion', 'ff_hh_shortcode_latestversion'); +} +// Example: +// [ff_hh_latestversion] +function ff_hh_shortcode_latestversion( $atts, $content, $name ) { + $sw_ver = ff_hh_getlatest(FF_HH_STABLE_BASEDIR); + $outstr = "$sw_ver"; + return $outstr; +} if ( ! shortcode_exists( 'ff_hh_versions' ) ) { - add_shortcode( 'ff_hh_versions', 'ff_hh_shortcode_handler'); + add_shortcode( 'ff_hh_versions', 'ff_hh_shortcode_versions'); } // Example: // [ff_hh_versions] -function ff_hh_shortcode_handler( $atts, $content, $name ) { +function ff_hh_shortcode_versions( $atts, $content, $name ) { $manifest = ff_hh_getmanifest(FF_HH_STABLE_BASEDIR); $outstr = "
";