apply WP coding style

This commit is contained in:
Martin Schuette 2014-06-29 18:22:12 +00:00
parent 765dce0720
commit 7ed0325a9a
2 changed files with 130 additions and 122 deletions

View file

@ -9,143 +9,149 @@ Author URI: http://mschuette.name/
Licence: 2-clause BSD Licence: 2-clause BSD
*/ */
define('FF_HH_STABLE_BASEDIR', 'http://updates.hamburg.freifunk.net/stable/'); define( 'FF_HH_STABLE_BASEDIR', 'http://updates.hamburg.freifunk.net/stable/' );
define('FF_HH_CACHETIME', 15); define( 'FF_HH_CACHETIME', 15 );
/* gets metadata from URL, handles caching */ /* gets metadata from URL, handles caching */
function ff_hh_getmanifest ($basedir) { function ff_hh_getmanifest( $basedir ) {
// Caching // Caching
if ( WP_DEBUG || ( false === ( $manifest = get_transient( "ff_hh_manifest" ) ) ) ) { if ( WP_DEBUG || ( false === ( $manifest = get_transient( 'ff_hh_manifest' ) ) ) ) {
$manifest = array(); $manifest = array();
$url = $basedir . 'sysupgrade/manifest'; $url = $basedir . 'sysupgrade/manifest';
$http_response = wp_remote_get( $url ); // TODO: error handling $http_response = wp_remote_get( $url ); // TODO: error handling
$input = wp_remote_retrieve_body( $http_response ); $input = wp_remote_retrieve_body( $http_response );
foreach ( explode("\n", $input) as $line ) { foreach ( explode( "\n", $input ) as $line ) {
$ret = sscanf($line, '%s %s %s %s', $hw, $sw_ver, $hash, $filename); $ret = sscanf( $line, '%s %s %s %s', $hw, $sw_ver, $hash, $filename );
if ($ret === 4) { if ( $ret === 4 ) {
if (preg_match('/^(.*)-v(\d+)$/', $hw, $matches)) { if ( preg_match( '/^(.*)-v(\d+)$/', $hw, $matches ) ) {
$hw = $matches[1]; $hw = $matches[1];
$hw_ver = $matches[2]; $hw_ver = $matches[2];
} else { } else {
$hw_ver = '1'; $hw_ver = '1';
} }
$manifest[$hw][$hw_ver] = $filename; $manifest[$hw][$hw_ver] = $filename;
} }
} }
$cachetime = FF_HH_CACHETIME * MINUTE_IN_SECONDS; $cachetime = FF_HH_CACHETIME * MINUTE_IN_SECONDS;
set_transient( "ff_hh_manifest", $manifest, $cachetime ); set_transient( 'ff_hh_manifest', $manifest, $cachetime );
} }
return $manifest; return $manifest;
} }
/* gets latest version from first manifest line */ /* gets latest version from first manifest line */
function ff_hh_getlatest ($basedir) { function ff_hh_getlatest( $basedir ) {
// Caching // Caching
if ( false === ( $sw_ver = get_transient( "ff_hh_latestversion" ) ) ) { if ( false === ( $sw_ver = get_transient( 'ff_hh_latestversion' ) ) ) {
$sw_ver = 'unknown'; $sw_ver = 'unknown';
$input = wp_remote_retrieve_body( wp_remote_get($basedir . 'sysupgrade/manifest') ); $input = wp_remote_retrieve_body( wp_remote_get( $basedir . 'sysupgrade/manifest' ) );
foreach ( explode("\n", $input) as $line ) { foreach ( explode( "\n", $input ) as $line ) {
$ret = sscanf($line, '%s %s %s %s', $hw, $sw_ver, $hash, $filename); $ret = sscanf( $line, '%s %s %s %s', $hw, $sw_ver, $hash, $filename );
if ($ret === 4) { if ( $ret === 4 ) {
// break processing on first matching line // break processing on first matching line
$cachetime = FF_HH_CACHETIME * MINUTE_IN_SECONDS; $cachetime = FF_HH_CACHETIME * MINUTE_IN_SECONDS;
set_transient( "ff_hh_latestversion", $sw_ver, $cachetime ); set_transient( 'ff_hh_latestversion', $sw_ver, $cachetime );
break; break;
} }
} }
} }
return $sw_ver; return $sw_ver;
} }
if ( ! shortcode_exists( 'ff_hh_latestversion' ) ) { if ( ! shortcode_exists( 'ff_hh_latestversion' ) ) {
add_shortcode( 'ff_hh_latestversion', 'ff_hh_shortcode_latestversion'); add_shortcode( 'ff_hh_latestversion', 'ff_hh_shortcode_latestversion' );
} }
// Example: // Example:
// [ff_hh_latestversion] // [ff_hh_latestversion]
function ff_hh_shortcode_latestversion( $atts, $content, $name ) { function ff_hh_shortcode_latestversion( $atts, $content, $name ) {
$sw_ver = ff_hh_getlatest(FF_HH_STABLE_BASEDIR); $sw_ver = ff_hh_getlatest( FF_HH_STABLE_BASEDIR );
$outstr = "<span class=\"ff $name\">$sw_ver</span>"; $outstr = "<span class=\"ff $name\">$sw_ver</span>";
return $outstr; return $outstr;
} }
if ( ! shortcode_exists( 'ff_hh_versions' ) ) { if ( ! shortcode_exists( 'ff_hh_versions' ) ) {
add_shortcode( 'ff_hh_versions', 'ff_hh_shortcode_versions'); add_shortcode( 'ff_hh_versions', 'ff_hh_shortcode_versions' );
} }
// Example: // Example:
// [ff_hh_versions] // [ff_hh_versions]
// [ff_hh_versions grep="ubiquiti"] // [ff_hh_versions grep="ubiquiti"]
function ff_hh_shortcode_versions( $atts, $content, $name ) { function ff_hh_shortcode_versions( $atts, $content, $name ) {
$manifest = ff_hh_getmanifest(FF_HH_STABLE_BASEDIR); $manifest = ff_hh_getmanifest( FF_HH_STABLE_BASEDIR );
$outstr = "<div class=\"ff $name\">"; $outstr = "<div class=\"ff $name\">";
$outstr .= '<table><tr><th>Modell</th><th>Erstinstallation</th><th>Aktualisierung</th></tr>'; $outstr .= '<table><tr><th>Modell</th><th>Erstinstallation</th><th>Aktualisierung</th></tr>';
# optionally filter output by given substring # optionally filter output by given substring
if (is_array($atts) if ( is_array( $atts )
&& array_key_exists('grep', $atts) && array_key_exists( 'grep', $atts )
&& !empty($atts['grep'])) { && ! empty( $atts['grep'] ) ) {
$grep = $atts['grep']; $grep = $atts['grep'];
} else { } else {
$grep = false; $grep = false;
} }
foreach ($manifest as $hw => $versions) { foreach ( $manifest as $hw => $versions ) {
// filter // filter
if ($grep && (false === strpos($hw, $grep))) { if ( $grep && ( false === strpos( $hw, $grep ) ) ) {
continue; continue;
} }
$hw = ff_hh_beautify_hw_name($hw, $grep); $hw = ff_hh_beautify_hw_name( $hw, $grep );
$outstr .= sprintf("\n<tr><td>%s</td>", $hw); $outstr .= sprintf( "\n<tr><td>%s</td>", $hw );
// factory versions // factory versions
$hw_ver_links = array(); $hw_ver_links = array();
foreach ($versions as $hw_ver => $filename) { foreach ( $versions as $hw_ver => $filename ) {
$filename = str_replace('-sysupgrade', '', $filename); $filename = str_replace( '-sysupgrade', '', $filename );
$hw_ver_links[] = sprintf('<a href="%s%s">%s.x</a>', $hw_ver_links[] = sprintf(
FF_HH_STABLE_BASEDIR.'factory/', $filename, $hw_ver); '<a href="%s%s">%s.x</a>',
} FF_HH_STABLE_BASEDIR.'factory/',
$outstr .= '<td>Hardware Version ' . join(', ', $hw_ver_links) . '</td>'; $filename, $hw_ver
);
}
$outstr .= '<td>Hardware Version ' . join( ', ', $hw_ver_links ) . '</td>';
// sysupgrade versions // sysupgrade versions
$hw_ver_links = array(); $hw_ver_links = array();
foreach ($versions as $hw_ver => $filename) { foreach ( $versions as $hw_ver => $filename ) {
$hw_ver_links[] = sprintf('<a href="%s%s">%s.x</a>', $hw_ver_links[] = sprintf(
FF_HH_STABLE_BASEDIR.'sysupgrade/', $filename, $hw_ver); '<a href="%s%s">%s.x</a>',
} FF_HH_STABLE_BASEDIR.'sysupgrade/',
$outstr .= '<td>Hardware Version ' . join(', ', $hw_ver_links) . '</td>'; $filename, $hw_ver
);
}
$outstr .= '<td>Hardware Version ' . join( ', ', $hw_ver_links ) . '</td>';
$outstr .= '</tr>'; $outstr .= '</tr>';
} }
$outstr .= '</table>'; $outstr .= '</table>';
$outstr .= '</div>'; $outstr .= '</div>';
// $outstr .= '<pre>'.print_r($manifest, true).'</pre>'; // $outstr .= '<pre>'.print_r( $manifest, true ).'</pre>';
return $outstr; return $outstr;
} }
// some crude rules to add capitalization and whitespace to the // some crude rules to add capitalization and whitespace to the
// hardware model name. // hardware model name.
// set $discard_vendor to strip the vendor name // set $discard_vendor to strip the vendor name
// (used for single-vendor lists, e.g. $discard_vendor = 'tp-link') // (used for single-vendor lists, e.g. $discard_vendor = 'tp-link' )
function ff_hh_beautify_hw_name($hw, $discard_vendor = "") { function ff_hh_beautify_hw_name( $hw, $discard_vendor = '' ) {
if (!strncmp($hw, 'tp-link', 7)) { if ( ! strncmp( $hw, 'tp-link', 7 ) ) {
if ($discard_vendor) $hw = str_replace($discard_vendor, '', $hw); if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw );
$hw = strtoupper($hw); $hw = strtoupper( $hw );
$hw = str_replace('-', ' ', $hw); $hw = str_replace( '-', ' ', $hw );
$hw = str_replace('TP LINK ', 'TP-Link ', $hw); $hw = str_replace( 'TP LINK ', 'TP-Link ', $hw );
$hw = str_replace(' TL ', ' TL-', $hw); $hw = str_replace( ' TL ', ' TL-', $hw );
} elseif (!strncmp($hw, 'ubiquiti', 8)) { } elseif ( ! strncmp( $hw, 'ubiquiti', 8 ) ) {
if ($discard_vendor) $hw = str_replace($discard_vendor, '', $hw); if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw );
$hw = str_replace('bullet-m', 'bullet-m / nanostation-loco-m', $hw); $hw = str_replace( 'bullet-m', 'bullet-m / nanostation-loco-m', $hw );
$hw = str_replace('-m', ' M2', $hw); $hw = str_replace( '-m', ' M2', $hw );
$hw = str_replace('-', ' ', $hw); $hw = str_replace( '-', ' ', $hw );
$hw = ucwords($hw); $hw = ucwords( $hw );
} elseif (!strncmp($hw, 'd-link', 6)) { } elseif ( ! strncmp( $hw, 'd-link', 6 ) ) {
if ($discard_vendor) $hw = str_replace($discard_vendor, '', $hw); if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw );
$hw = str_replace('-', ' ', $hw); $hw = str_replace( '-', ' ', $hw );
$hw = str_replace('d link ', 'D-Link ', $hw); $hw = str_replace( 'd link ', 'D-Link ', $hw );
$hw = str_replace(' dir ', ' DIR-', $hw); $hw = str_replace( ' dir ', ' DIR-', $hw );
} }
return $hw; return $hw;
} }

View file

@ -1,21 +1,23 @@
<?php <?php
class SimpleTests extends WP_UnitTestCase { class SimpleTests extends WP_UnitTestCase {
function test_beautify() { function test_beautify() {
$cmplist = array( // examples with and without grep param $cmplist = array( // examples with and without grep param
array('tp-link-tl-wr740n-nd', '', 'TP-Link TL-WR740N ND'), array( 'tp-link-tl-wr740n-nd', '', 'TP-Link TL-WR740N ND' ),
array('tp-link-tl-wr740n-nd', 'tp-link', 'TL-WR740N ND'), array( 'tp-link-tl-wr740n-nd', 'tp-link', 'TL-WR740N ND' ),
array('tp-link-tl-wdr4300', '', 'TP-Link TL-WDR4300'), array( 'tp-link-tl-wdr4300', '', 'TP-Link TL-WDR4300' ),
array('tp-link-tl-wdr4300', 'tp-link', 'TL-WDR4300'), array( 'tp-link-tl-wdr4300', 'tp-link', 'TL-WDR4300' ),
array('ubiquiti-unifi', '', 'Ubiquiti Unifi'), array( 'ubiquiti-unifi', '', 'Ubiquiti Unifi' ),
array('ubiquiti-unifi', 'ubiquiti', 'Unifi'), array( 'ubiquiti-unifi', 'ubiquiti', 'Unifi' ),
array('ubiquiti-bullet-m', '', 'Ubiquiti Bullet M2 / Nanostation Loco M2'), array( 'ubiquiti-bullet-m', '', 'Ubiquiti Bullet M2 / Nanostation Loco M2' ),
array('ubiquiti-bullet-m', 'ubiquiti', 'Bullet M2 / Nanostation Loco M2'), array( 'ubiquiti-bullet-m', 'ubiquiti', 'Bullet M2 / Nanostation Loco M2' ),
array('d-link-dir-615-rev-e1', '', 'D-Link DIR-615 rev e1'), array( 'd-link-dir-615-rev-e1', '', 'D-Link DIR-615 rev e1' ),
array('d-link-dir-615-rev-e1', 'd-link', 'DIR-615 rev e1'), array( 'd-link-dir-615-rev-e1', 'd-link', 'DIR-615 rev e1' ),
); );
foreach ($cmplist as $item) { foreach ( $cmplist as $item ) {
$this->assertEquals($item[2], trim(ff_hh_beautify_hw_name($item[0], $item[1]))); $expect = $item[2];
$result = ff_hh_beautify_hw_name( $item[0], $item[1] );
$this->assertEquals( $expect, trim( $result ) );
}
} }
}
} }