diff --git a/server.js b/server.js index b53c41b37df59..df9b5b3428044 100644 --- a/server.js +++ b/server.js @@ -2102,6 +2102,157 @@ cache(function(data, match, sendBadge, request) { }); })); +// wordpress plugin version integration. +// example: https://img.shields.io/wordpress/plugin/v/akismet.svg for https://wordpress.org/plugins/akismet +camp.route(/^\/wordpress\/plugin\/v\/(.*)\.(svg|png|gif|jpg|json)$/, +cache(function(data, match, sendBadge, request) { + var plugin = match[1]; // eg, `localeval`. + var format = match[2]; + var apiUrl = 'http://api.wordpress.org/plugins/info/1.0/' + plugin + '.json'; + var badgeData = getBadgeData('plugin', data); + request(apiUrl, function(err, res, buffer) { + if (err != null) { + badgeData.text[1] = 'inaccessible'; + sendBadge(format, badgeData); + } + try { + var data = JSON.parse(buffer); + var version = data.version; + badgeData.text[1] = 'v' + version; + if (version[0] === '0') { + badgeData.colorscheme = 'orange'; + } else { + badgeData.colorscheme = 'blue'; + } + sendBadge(format, badgeData); + } catch(e) { + badgeData.text[1] = 'invalid'; + sendBadge(format, badgeData); + } + }); +})); + +// wordpress plugin downloads integration. +// example: https://img.shields.io/wordpress/plugin/dt/akismet.svg for https://wordpress.org/plugins/akismet +camp.route(/^\/wordpress\/plugin\/dt\/(.*)\.(svg|png|gif|jpg|json)$/, +cache(function(data, match, sendBadge, request) { + var plugin = match[1]; // eg, `localeval`. + var format = match[2]; + var apiUrl = 'http://api.wordpress.org/plugins/info/1.0/' + plugin + '.json'; + var badgeData = getBadgeData('downloads', data); + request(apiUrl, function(err, res, buffer) { + if (err != null) { + badgeData.text[1] = 'inaccessible'; + sendBadge(format, badgeData); + return; + } + try { + var total = JSON.parse(buffer).downloaded; + } catch(e) { + badgeData.text[1] = 'invalid'; + sendBadge(format, badgeData); + return; + } + badgeData.text[1] = metric(total) + ' total'; + if (total === 0) { + badgeData.colorscheme = 'red'; + } else if (total < 100) { + badgeData.colorscheme = 'yellow'; + } else if (total < 1000) { + badgeData.colorscheme = 'yellowgreen'; + } else if (total < 10000) { + badgeData.colorscheme = 'green'; + } else { + badgeData.colorscheme = 'brightgreen'; + } + sendBadge(format, badgeData); + }); +})); + +// wordpress plugin rating integration. +// example: https://img.shields.io/wordpress/plugin/r/akismet.svg for https://wordpress.org/plugins/akismet +camp.route(/^\/wordpress\/plugin\/r\/(.*)\.(svg|png|gif|jpg|json)$/, +cache(function(data, match, sendBadge, request) { + var plugin = match[1]; // eg, `localeval`. + var format = match[2]; + var apiUrl = 'http://api.wordpress.org/plugins/info/1.0/' + plugin + '.json'; + var badgeData = getBadgeData('rating', data); + request(apiUrl, function(err, res, buffer) { + if (err != null) { + badgeData.text[1] = 'inaccessible'; + sendBadge(format, badgeData); + return; + } + try { + var rating = JSON.parse(buffer).rating; + rating = (rating/100)*5; + } catch(e) { + badgeData.text[1] = 'invalid'; + sendBadge(format, badgeData); + return; + } + badgeData.text[1] = metric(Math.round(rating * 10) / 10) + ' stars'; + if (rating === 0) { + badgeData.colorscheme = 'red'; + } else if (rating < 2) { + badgeData.colorscheme = 'yellow'; + } else if (rating < 3) { + badgeData.colorscheme = 'yellowgreen'; + } else if (rating < 4) { + badgeData.colorscheme = 'green'; + } else { + badgeData.colorscheme = 'brightgreen'; + } + sendBadge(format, badgeData); + }); +})); + +// wordpress version support integration. +// example: https://img.shields.io/wordpress/v/akismet.svg for https://wordpress.org/plugins/akismet +camp.route(/^\/wordpress\/v\/(.*)\.(svg|png|gif|jpg|json)$/, +cache(function(data, match, sendBadge, request) { + var plugin = match[1]; // eg, `localeval`. + var format = match[2]; + var apiUrl = 'http://api.wordpress.org/plugins/info/1.0/' + plugin + '.json'; + var badgeData = getBadgeData('wordpress', data); + request(apiUrl, function(err, res, buffer) { + if (err != null) { + badgeData.text[1] = 'inaccessible'; + sendBadge(format, badgeData); + } + try { + var data = JSON.parse(buffer); + var pluginVersion = data.version; + if (data.tested) { + var testedVersion = data.tested.replace(/[^0-9.]/g,''); + badgeData.text[1] = testedVersion + ' tested'; + var coreUrl = 'https://api.wordpress.org/core/version-check/1.7/'; + request(coreUrl, function(err, res, response) { + var versions = JSON.parse(response).offers.map(function(v) { + return v.version + }); + if (err != null) { sendBadge(format, badgeData); return; } + var svTestedVersion = testedVersion.split('.').length == 2 ? testedVersion += '.0' : testedVersion; + var svVersion = versions[0].split('.').length == 2 ? versions[0] += '.0' : versions[0]; + if (testedVersion == versions[0] || semver.gtr(svTestedVersion, svVersion)) { + badgeData.colorscheme = 'brightgreen'; + } else if (versions.indexOf(testedVersion) != -1) { + badgeData.colorscheme = 'orange'; + } else { + badgeData.colorscheme = 'yellow'; + } + sendBadge(format, badgeData); + }); + } else { + sendBadge(format, badgeData); + } + } catch(e) { + badgeData.text[1] = 'invalid'; + sendBadge(format, badgeData); + } + }); +})); + // Any badge. camp.route(/^\/(:|badge\/)(([^-]|--)+)-(([^-]|--)+)-(([^-]|--)+)\.(svg|png|gif|jpg)$/, function(data, match, end, ask) {