Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for wordpress plugin version, total downloads, rating and supported wordpress version. #300

Closed
wants to merge 7 commits into from
151 changes: 151 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you give a functional example in the comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: see commit d3f8cdb

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) {
Expand Down