From e971dd25bca95be47d56c38e466c6e97ffb829bd Mon Sep 17 00:00:00 2001 From: rvpatel Date: Wed, 14 Oct 2026 21:03:47 +0530 Subject: [PATCH 1/3] Administration: Create site icon sizes for exact-size images in General Settings. When an image that already has the required site icon dimensions (512x512) is selected in Settings > General, the crop step is skipped and the original image is saved as the site icon, so the site icon sizes (32, 64, 180, 192 and 270) are never generated. Send the image through the crop-image request with the `site-icon` context, as the Customizer already does, so the site icon and its sizes are created. See #66309. --- src/js/_enqueues/admin/site-icon.js | 36 ++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/js/_enqueues/admin/site-icon.js b/src/js/_enqueues/admin/site-icon.js index 59a098dc08aac..6e8c03f64ed8e 100644 --- a/src/js/_enqueues/admin/site-icon.js +++ b/src/js/_enqueues/admin/site-icon.js @@ -117,17 +117,37 @@ // When an image is selected, run a callback. frame.on( 'select', function () { // Grab the selected attachment. - var attachment = frame.state().get( 'selection' ).first(); + var attachment = frame.state().get( 'selection' ).first(), + size = $el.data( 'size' ); if ( - attachment.attributes.height === $el.data( 'size' ) && - $el.data( 'size' ) === attachment.attributes.width + attachment.attributes.height === size && + size === attachment.attributes.width ) { - switchToUpdate( attachment.attributes ); - frame.close(); - - // Set the value of the hidden input to the attachment id. - $hiddenDataField.val( attachment.id ); + /* + * The image doesn't need cropping, but it still goes through the crop + * request so that the site icon and its additional sizes are created. + */ + wp.ajax + .post( 'crop-image', { + nonce: attachment.get( 'nonces' ).edit, + id: attachment.id, + context: 'site-icon', + cropDetails: { + x1: 0, + y1: 0, + width: size, + height: size, + dst_width: size, + dst_height: size, + }, + } ) + .done( function ( croppedImage ) { + frame.trigger( 'cropped', croppedImage ); + } ) + .fail( function () { + frame.trigger( 'content:error:crop' ); + } ); } else { frame.setState( 'cropper' ); } From 418052efd307dbdab3c19849d60bfbbc0f4fcab8 Mon Sep 17 00:00:00 2001 From: rvpatel Date: Wed, 11 Nov 2026 19:03:39 +0530 Subject: [PATCH 2/3] Administration: Allow skipping the site icon crop for square images. In Settings > General, show the "Skip cropping" button for square images, as the Customizer does. Skipping still sends the whole image through the `crop-image` request with the `site-icon` context, so the site icon sizes are created. See #64020, #66309. --- src/js/_enqueues/admin/site-icon.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/js/_enqueues/admin/site-icon.js b/src/js/_enqueues/admin/site-icon.js index 6e8c03f64ed8e..33898175e5204 100644 --- a/src/js/_enqueues/admin/site-icon.js +++ b/src/js/_enqueues/admin/site-icon.js @@ -24,9 +24,10 @@ * @since 6.5.0 * * @param {Object} attachment The attachment object representing the image. + * @param {Object} controller The cropper controller. * @return {Object} The image selection options. */ - function calculateImageSelectOptions( attachment ) { + function calculateImageSelectOptions( attachment, controller ) { var realWidth = attachment.get( 'width' ), realHeight = attachment.get( 'height' ), xInit = 512, @@ -38,6 +39,20 @@ y1, imgSelectOptions; + /* + * Square images can skip cropping. "Skip Cropping" still sends the whole + * image through the crop request, so the site icon sizes are created. + */ + controller.set( 'hasRequiredAspectRatio', realWidth === realHeight ); + controller.set( 'suggestedCropSize', { + width: realWidth, + height: realHeight, + x1: 0, + y1: 0, + x2: xInit, + y2: yInit, + } ); + if ( realWidth / realHeight > ratio ) { yInit = realHeight; xInit = yInit * ratio; From 4c1329d6ebd7860dae510523ae6af4c5b7722135 Mon Sep 17 00:00:00 2001 From: rvpatel Date: Wed, 25 Nov 2026 13:32:00 +0530 Subject: [PATCH 3/3] Administration: Add tests and `@since` notes for the site icon changes. Adds end-to-end tests for the Site Icon in Settings > General: an image that is exactly 512x512 and a larger square image that skips cropping both create a cropped copy with all site icon sizes, and a non-square image is not offered "Skip cropping". Notes in `site-icon.js` that the change is since 7.3.0. See #66309, #64020. --- src/js/_enqueues/admin/site-icon.js | 5 +- tests/e2e/assets/site-icon-1024x1024.png | Bin 0 -> 4569 bytes tests/e2e/assets/site-icon-512x512.png | Bin 0 -> 1513 bytes tests/e2e/assets/site-icon-800x600.png | Bin 0 -> 2333 bytes tests/e2e/specs/site-icon.test.js | 146 +++++++++++++++++++++++ 5 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 tests/e2e/assets/site-icon-1024x1024.png create mode 100644 tests/e2e/assets/site-icon-512x512.png create mode 100644 tests/e2e/assets/site-icon-800x600.png create mode 100644 tests/e2e/specs/site-icon.test.js diff --git a/src/js/_enqueues/admin/site-icon.js b/src/js/_enqueues/admin/site-icon.js index 33898175e5204..a1c663cbb185a 100644 --- a/src/js/_enqueues/admin/site-icon.js +++ b/src/js/_enqueues/admin/site-icon.js @@ -22,6 +22,7 @@ * Calculate image selection options based on the attachment dimensions. * * @since 6.5.0 + * @since 7.3.0 Added the `controller` parameter, to let square images skip cropping. * * @param {Object} attachment The attachment object representing the image. * @param {Object} controller The cropper controller. @@ -140,8 +141,8 @@ size === attachment.attributes.width ) { /* - * The image doesn't need cropping, but it still goes through the crop - * request so that the site icon and its additional sizes are created. + * Since 7.3.0, an image that doesn't need cropping still goes through + * the crop request, so that the site icon and its additional sizes are created. */ wp.ajax .post( 'crop-image', { diff --git a/tests/e2e/assets/site-icon-1024x1024.png b/tests/e2e/assets/site-icon-1024x1024.png new file mode 100644 index 0000000000000000000000000000000000000000..6dc6a9ba6b02c5f19ae804bc249613f4fa453b5f GIT binary patch literal 4569 zcmeAS@N?(olHy`uVBq!ia0y~yU;#2&7&w@K)Q9>#SAY~}fk$L90|Va?5N4dJ%_q&k zAlU8c;uunK>+N+zMg{|p0~-uAEnKAdJ~DQ;)b*~ZV_-PI8xJ(X0I0oz5lHX=aRLxC zFaxm!5FY?y1~wo@QG#100i+&7Gm*x^HDg!^m%(l`hJ`4u#jbgfxrvnE2YKiK5MxG6 z0!S2hXyBH?8XB~A69Lx_2{#du(omFOPrz8c3Xfh=!wJPgA~cigCj73&Of_r{qh$=f z;uIc5G%oirV+McBA7B82A?_x4*y8spN});Xa3a-Bw9d~U^9P%oM#~r?GaktS1&nM` z64W4TGl28vAafI`dFKEy3P#HqU?32vTu4c#plm=w5f8SRe6S4|ppr^jBUj&+xs5lYxhUftld|1A_qrSi0*Z)=h^hlA&H|6fVg?4jBOuH;Rhv(m zfq~<^r;B4q#jUs33bB}!-0AFj0_D2nHUTr7#P^N z85o*b84@-yFz|?hxd##$86h zholH*J22Qlq68Hog$PJ1c9Td|1d23*CLt>V#w?P5Y>A3*C{i8GyU?VA z6az#>I8xx!Bra(a;Yj&}sInR { + const mediaIds = []; + + test.afterAll( async ( { requestUtils } ) => { + for ( const id of mediaIds ) { + await requestUtils.deleteMedia( id ); + } + } ); + + /** + * Uploads an image from the e2e assets folder. + * + * @param {Object} requestUtils The request utilities. + * @param {string} filename The image file name. + * @return {Promise} The attachment ID. + */ + async function uploadImage( requestUtils, filename ) { + const media = await requestUtils.uploadMedia( + path.join( __dirname, '../assets', filename ) + ); + mediaIds.push( media.id ); + return media.id; + } + + /** + * Selects an image for the Site Icon in Settings > General. + * + * @param {Object} page The page. + * @param {Object} admin The admin utilities. + * @param {number} mediaId The attachment ID. + * @return {Promise} The media modal locator. + */ + async function selectSiteIcon( page, admin, mediaId ) { + await admin.visitAdminPage( '/options-general.php' ); + await page.locator( '#choose-from-library-button' ).click(); + + const modal = page.locator( '.media-modal' ); + await modal.locator( `li.attachment[data-id="${ mediaId }"]` ).click(); + await modal.locator( '.media-button-select' ).click(); + + return modal; + } + + /** + * Checks that the selected Site Icon is a new cropped copy with all site icon sizes. + * + * @param {Object} page The page. + * @param {Object} requestUtils The request utilities. + * @param {number} originalId The ID of the image that was selected. + */ + async function expectCroppedSiteIcon( page, requestUtils, originalId ) { + const field = page.locator( '#site_icon_hidden_field' ); + await expect( field ).not.toHaveValue( '' ); + await expect( field ).not.toHaveValue( String( originalId ) ); + + const croppedId = Number( await field.inputValue() ); + mediaIds.push( croppedId ); + + const cropped = await requestUtils.rest( { + path: `/wp/v2/media/${ croppedId }`, + params: { context: 'edit' }, + } ); + + expect( cropped.media_details.width ).toBe( 512 ); + expect( cropped.media_details.height ).toBe( 512 ); + expect( Object.keys( cropped.media_details.sizes ) ).toEqual( + expect.arrayContaining( SITE_ICON_SIZES ) + ); + } + + test( 'creates the site icon sizes for an image that is exactly 512 by 512', async ( { + page, + admin, + requestUtils, + } ) => { + const mediaId = await uploadImage( + requestUtils, + 'site-icon-512x512.png' + ); + + const modal = await selectSiteIcon( page, admin, mediaId ); + + // The image does not need cropping, so the cropper is not shown. + await expect( modal ).toBeHidden(); + await expectCroppedSiteIcon( page, requestUtils, mediaId ); + } ); + + test( 'lets a larger square image skip cropping and creates the site icon sizes', async ( { + page, + admin, + requestUtils, + } ) => { + const mediaId = await uploadImage( + requestUtils, + 'site-icon-1024x1024.png' + ); + + const modal = await selectSiteIcon( page, admin, mediaId ); + + await expect( + modal.getByRole( 'button', { name: 'Crop image' } ) + ).toBeVisible(); + await modal.getByRole( 'button', { name: 'Skip cropping' } ).click(); + + await expect( modal ).toBeHidden(); + await expectCroppedSiteIcon( page, requestUtils, mediaId ); + } ); + + test( 'does not offer to skip cropping for an image that is not square', async ( { + page, + admin, + requestUtils, + } ) => { + const mediaId = await uploadImage( + requestUtils, + 'site-icon-800x600.png' + ); + + const modal = await selectSiteIcon( page, admin, mediaId ); + + await expect( + modal.getByRole( 'button', { name: 'Crop image' } ) + ).toBeVisible(); + await expect( + modal.getByRole( 'button', { name: 'Skip cropping' } ) + ).toHaveCount( 0 ); + } ); +} );