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

[Wordpress] Theme & Plugin Last Updated badge #5722

Merged
merged 10 commits into from
Oct 20, 2020
6 changes: 6 additions & 0 deletions services/wordpress/wordpress-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const { nonNegativeInteger } = require('../validators')
const { BaseJsonService, NotFound } = require('..')

const stringOrFalse = Joi.alternatives(Joi.string(), Joi.bool())
const pluginDatePattern =
'[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{1,2}:[0-5][0-9](am|pm) [A-Z]{3}'
const themeDatePattern = '[0-9]{4}-[0-9]{2}-[0-9]{2}'

const themeSchema = Joi.object()
.keys({
Expand All @@ -13,6 +16,7 @@ const themeSchema = Joi.object()
num_ratings: nonNegativeInteger,
downloaded: nonNegativeInteger,
active_installs: nonNegativeInteger,
last_updated: Joi.string().required().regex(RegExp(themeDatePattern)),
requires_php: stringOrFalse.required(),
})
.required()
Expand All @@ -26,6 +30,7 @@ const pluginSchema = Joi.object()
active_installs: nonNegativeInteger,
requires: stringOrFalse.required(),
tested: Joi.string().required(),
last_updated: Joi.string().required().regex(RegExp(pluginDatePattern)),
requires_php: stringOrFalse.required(),
})
.required()
Expand Down Expand Up @@ -63,6 +68,7 @@ module.exports = class BaseWordpress extends BaseJsonService {
tags: 0,
screenshot_url: 0,
downloaded: 1,
last_updated: 1,
requires_php: 1,
},
},
Expand Down
80 changes: 80 additions & 0 deletions services/wordpress/wordpress-last-update.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
'use strict'

const { formatDate } = require('../text-formatters')
const { age: ageColor } = require('../color-formatters')
const BaseWordpress = require('./wordpress-base')

const extensionData = {
plugin: {
capt: 'Plugin',
exampleSlug: 'bbpress',
},
theme: {
capt: 'Theme',
exampleSlug: 'twentyseventeen',
},
}

function LastUpdateForType(extensionType) {
const { capt, exampleSlug } = extensionData[extensionType]

return class WordpressLastUpdate extends BaseWordpress {
static name = `Wordpress${capt}LastUpdated`

static category = 'activity'

static route = {
base: `wordpress/${extensionType}/last-updated`,
pattern: ':slug',
}

static examples = [
{
title: `WordPress ${capt} Last Updated`,
namedParams: { slug: exampleSlug },
staticPreview: this.render({ last_updated: '2020-08-11' }),
},
]

static defaultBadgeData = { label: 'last updated' }

static render({ last_updated }) {
return {
label: 'last updated',
message: formatDate(last_updated),
color: ageColor(last_updated),
}
}

transform(date, extensionType) {
let d
if (extensionType === 'plugin') {
d = date.split(' ')
d = d[0]
} else {
d = date
}

const ymd = d.split('-')

const out = ymd[0] + ymd[1] + ymd[2]
return out
}

async handle({ slug }) {
const { last_updated } = await this.fetch({
extensionType,
slug,
})

const newDate = await this.transform(last_updated, extensionType)

return this.constructor.render({
last_updated: newDate,
})
}
}
}

const lastupdate = ['plugin', 'theme'].map(LastUpdateForType)
module.exports = [...lastupdate]
38 changes: 38 additions & 0 deletions services/wordpress/wordpress-last-update.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict'

const { ServiceTester } = require('../tester')
const { isFormattedDate } = require('../test-validators')

const t = new ServiceTester({
id: 'wordpress',
title: 'WordPress Last Update',
})
module.exports = t

t.create('Plugin Last Update')
.get('/plugin/last-updated/akismet.json')
.expectBadge({
label: 'last updated',
message: isFormattedDate,
})

t.create('Plugin (Not Found)')
.get('/plugin/last-updated/pleasenevermakethisplugin.json')
.expectBadge({
label: 'last updated',
message: 'not found',
})

t.create('Theme Last Update')
.get('/theme/last-updated/twentytwenty.json')
.expectBadge({
label: 'last updated',
message: isFormattedDate,
})

t.create('Theme (Not Found)')
.get('/theme/last-updated/pleasenevermakethistheme.json')
.expectBadge({
label: 'last updated',
message: 'not found',
})
3 changes: 3 additions & 0 deletions services/wordpress/wordpress-platform.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ t.create('Plugin Tested WP Version - current')
active_installs: 100,
requires: '4.9',
tested: '4.9.8',
last_updated: '2020-01-01 7:21am GMT',
requires_php: '5.5',
})
.get('/core/version-check/1.7/')
Expand All @@ -88,6 +89,7 @@ t.create('Plugin Tested WP Version - old')
active_installs: 100,
requires: '4.9',
tested: '4.9.6',
last_updated: '2020-01-01 7:21am GMT',
requires_php: '5.5',
})
.get('/core/version-check/1.7/')
Expand All @@ -113,6 +115,7 @@ t.create('Plugin Tested WP Version - non-exsistant or unsupported')
active_installs: 100,
requires: '4.0',
tested: '4.0.0',
last_updated: '2020-01-01 7:21am GMT',
requires_php: '5.5',
})
.get('/core/version-check/1.7/')
Expand Down