Skip to content

Commit

Permalink
WP: Clean up Gravatar URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulAdamDavis committed Nov 28, 2024
1 parent c1b591b commit fe1d263
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
15 changes: 11 additions & 4 deletions packages/mg-wp-api/lib/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,28 @@ const largerSrc = (imageSrc) => {
};

const processAuthor = (wpAuthor) => {
let profileImage = wpAuthor.avatar_urls && wpAuthor.avatar_urls['96'];
profileImage = profileImage ? profileImage.replace(/s=96/, 's=3000') : undefined;

let authorObject = {
url: wpAuthor.link,
data: {
id: wpAuthor.id && wpAuthor.id,
slug: wpAuthor.slug,
name: wpAuthor.name,
bio: wpAuthor.description,
profile_image: profileImage,
email: wpAuthor.email && wpAuthor.email
}
};

let profileImage = wpAuthor.avatar_urls && wpAuthor.avatar_urls['96'];
if (profileImage) {
const imgUrl = new URL(profileImage);
const params = new URLSearchParams(imgUrl.search);
params.set('d', 'blank');
params.set('r', 'g');
params.set('s', '500');
imgUrl.search = params.toString();
authorObject.data.profile_image = imgUrl.href;
}

if (wpAuthor.url) {
try {
new URL(wpAuthor.url);
Expand Down
18 changes: 16 additions & 2 deletions packages/mg-wp-api/test/process.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('Process WordPress REST API JSON', function () {
expect(data.slug).toEqual('example');
expect(data.name).toEqual('Example User');
expect(data.bio).toEqual('Lorem ipsum small bio.\r\n\r\nAnd emoji 🤓 on the second line.');
expect(data.profile_image).toEqual('https://secure.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=3000&d=mm&r=g');
expect(data.profile_image).toEqual('https://secure.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=500&d=blank&r=g');
expect(data.website).toEqual('https://example.com');
});

Expand All @@ -72,6 +72,20 @@ describe('Process WordPress REST API JSON', function () {
expect(user.data).toHaveProperty('name');
expect(user.data).not.toHaveProperty('website');
});

test('Will scale user avatars', function () {
const user = processor.processAuthor({
id: 29,
name: 'Example User',
avatar_urls: {
24: 'https://secure.gravatar.com/avatar/cb8419c1d471d55fbca0d63d1fb2b6ac?s=24&d=wp_user_avatar&r=g',
48: 'https://secure.gravatar.com/avatar/cb8419c1d471d55fbca0d63d1fb2b6ac?s=48&d=wp_user_avatar&r=g',
96: 'https://secure.gravatar.com/avatar/cb8419c1d471d55fbca0d63d1fb2b6ac?s=96&d=wp_user_avatar&r=g'
}
});

expect(user.data.profile_image).toEqual('https://secure.gravatar.com/avatar/cb8419c1d471d55fbca0d63d1fb2b6ac?s=500&d=blank&r=g');
});

test('Can convert a multiple users', function () {
const users = processor.processAuthors(multipleUsersfixture);
Expand All @@ -95,7 +109,7 @@ describe('Process WordPress REST API JSON', function () {
expect(data.slug).toEqual('another-user');
expect(data.name).toEqual('Another User');
expect(data.bio).toEqual('A different user bio');
expect(data.profile_image).toEqual('https://secure.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=3000&d=mm&r=g');
expect(data.profile_image).toEqual('https://secure.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=500&d=blank&r=g');
expect(data.website).toEqual('https://anothersite.com');
});

Expand Down

0 comments on commit fe1d263

Please sign in to comment.