From fe1d2635e6fae49e94c33b0cd4f3e1b8ca289e4f Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Thu, 28 Nov 2024 15:46:11 +0000 Subject: [PATCH] WP: Clean up Gravatar URLs --- packages/mg-wp-api/lib/processor.js | 15 +++++++++++---- packages/mg-wp-api/test/process.test.js | 18 ++++++++++++++++-- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/packages/mg-wp-api/lib/processor.js b/packages/mg-wp-api/lib/processor.js index 7f46bd015..4a5d2e89b 100644 --- a/packages/mg-wp-api/lib/processor.js +++ b/packages/mg-wp-api/lib/processor.js @@ -63,9 +63,6 @@ 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: { @@ -73,11 +70,21 @@ const processAuthor = (wpAuthor) => { 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); diff --git a/packages/mg-wp-api/test/process.test.js b/packages/mg-wp-api/test/process.test.js index 01fd8cc4e..e5acfbcef 100644 --- a/packages/mg-wp-api/test/process.test.js +++ b/packages/mg-wp-api/test/process.test.js @@ -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'); }); @@ -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); @@ -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'); });