: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Handle the site icon setting in options-general.php.
* @output wp-admin/js/site-icon.js
var $chooseButton = $( '#choose-from-library-button' ),
$iconPreview = $( '#site-icon-preview' ),
$browserIconPreview = $( '#browser-icon-preview' ),
$appIconPreview = $( '#app-icon-preview' ),
$hiddenDataField = $( '#site_icon_hidden_field' ),
$removeButton = $( '#js-remove-site-icon' ),
* Calculate image selection options based on the attachment dimensions.
* @param {Object} attachment The attachment object representing the image.
* @return {Object} The image selection options.
function calculateImageSelectOptions( attachment ) {
var realWidth = attachment.get( 'width' ),
realHeight = attachment.get( 'height' ),
if ( realWidth / realHeight > ratio ) {
x1 = ( realWidth - xInit ) / 2;
y1 = ( realHeight - yInit ) / 2;
aspectRatio: xInit + ':' + yInit,
minWidth: xImg > xInit ? xInit : xImg,
minHeight: yImg > yInit ? yInit : yImg,
* Initializes the media frame for selecting or cropping an image.
$chooseButton.on( 'click', function () {
// Create the media frame.
// Set the text of the button.
text: $el.data( 'update' ),
// Don't close, we might need to crop.
new wp.media.controller.Library( {
title: $el.data( 'choose-text' ),
library: wp.media.query( { type: 'image' } ),
suggestedWidth: $el.data( 'size' ),
suggestedHeight: $el.data( 'size' ),
new wp.media.controller.SiteIconCropper( {
width: $el.data( 'size' ),
height: $el.data( 'size' ),
imgSelectOptions: calculateImageSelectOptions,
frame.on( 'cropped', function ( attachment ) {
$hiddenDataField.val( attachment.id );
switchToUpdate( attachment );
// Start over with a frame that is so fresh and so clean clean.
// When an image is selected, run a callback.
frame.on( 'select', function () {
// Grab the selected attachment.
var attachment = frame.state().get( 'selection' ).first();
attachment.attributes.height === $el.data( 'size' ) &&
$el.data( 'size' ) === attachment.attributes.width
switchToUpdate( attachment.attributes );
// Set the value of the hidden input to the attachment id.
$hiddenDataField.val( attachment.id );
frame.setState( 'cropper' );
* Update the UI when a site icon is selected.
* @param {array} attributes The attributes for the attachment.
function switchToUpdate( attributes ) {
var i18nAppAlternativeString, i18nBrowserAlternativeString;
i18nAppAlternativeString = wp.i18n.sprintf(
/* translators: %s: The selected image alt text. */
wp.i18n.__( 'App icon preview: Current image: %s' ),
i18nBrowserAlternativeString = wp.i18n.sprintf(
/* translators: %s: The selected image alt text. */
wp.i18n.__( 'Browser icon preview: Current image: %s' ),
i18nAppAlternativeString = wp.i18n.sprintf(
/* translators: %s: The selected image filename. */
'App icon preview: The current image has no alternative text. The file name is: %s'
i18nBrowserAlternativeString = wp.i18n.sprintf(
/* translators: %s: The selected image filename. */
'Browser icon preview: The current image has no alternative text. The file name is: %s'
// Set site-icon-img src and alternative text to app icon preview.
alt: i18nAppAlternativeString,
// Set site-icon-img src and alternative text to browser preview.
$browserIconPreview.attr( {
alt: i18nBrowserAlternativeString,
// Remove hidden class from icon preview div and remove button.
$iconPreview.removeClass( 'hidden' );
$removeButton.removeClass( 'hidden' );
// If the choose button is not in the update state, swap the classes.
if ( $chooseButton.attr( 'data-state' ) !== '1' ) {
class: $chooseButton.attr( 'data-alt-classes' ),
'data-alt-classes': $chooseButton.attr( 'class' ),
// Swap the text of the choose button.
$chooseButton.text( $chooseButton.attr( 'data-update-text' ) );
* Handles the click event of the remove button.
$removeButton.on( 'click', function () {
$hiddenDataField.val( 'false' );
$( this ).toggleClass( 'hidden' );
$iconPreview.toggleClass( 'hidden' );
$browserIconPreview.attr( {
* Resets state to the button, for correct visual style and state.
* Updates the text of the button.
* Sets focus state to the button.
class: $chooseButton.attr( 'data-alt-classes' ),
'data-alt-classes': $chooseButton.attr( 'class' ),
.text( $chooseButton.attr( 'data-choose-text' ) )