: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* BLOCK: extend image block
const { createHigherOrderComponent } = wp.compose,
{ Fragment } = wp.element,
{ InspectorControls } = wp.blockEditor,
{ PanelBody } = wp.components;
* Transform bytes to human readable format.
* @return {string} Readable size string.
function humanFileSize( bytes ) {
units = [ 'kB', 'MB', 'GB', 'TB' ];
if ( Math.abs( bytes ) < thresh ) {
} while ( Math.abs( bytes ) >= thresh && u < units.length - 1 );
return bytes.toFixed( 1 ) + ' ' + units[ u ];
* Generate Smush stats table.
* @return {*} Smush stats.
export function smushStats( id, stats ) {
if ( 'undefined' === typeof stats ) {
return window.smush_vars.strings.gb.select_image;
} else if ( 'string' === typeof stats ) {
className="sui-smush-media smush-stats-wrapper hidden"
style={ { display: 'block' } }
<table className="wp-smush-stats-holder">
<th className="smush-stats-header">
{ window.smush_vars.strings.gb.size }
<th className="smush-stats-header">
{ window.smush_vars.strings.gb.savings }
{ Object.keys( stats.sizes )
.filter( ( item ) => 0 < stats.sizes[ item ].percent )
<td>{ item.toUpperCase() }</td>
stats.sizes[ item ].bytes
( { stats.sizes[ item ].percent }% )
* Fetch image data. If image is Smushing, update in 3 seconds.
* TODO: this could be optimized not to query so much.
export function fetchProps( props ) {
const image = new wp.api.models.Media( { id: props.attributes.id } ),
smushData = props.attributes.smush;
image.fetch( { attribute: 'smush' } ).done( function( img ) {
if ( 'string' === typeof img.smush ) {
props.setAttributes( { smush: img.smush } );
//setTimeout( () => fetch( props ), 3000 );
'undefined' !== typeof img.smush &&
( 'undefined' === typeof smushData ||
JSON.stringify( smushData ) !== JSON.stringify( img.smush ) )
props.setAttributes( { smush: img.smush } );
* Modify the block’s edit component.
* Receives the original block BlockEdit component and returns a new wrapped component.
const smushStatsControl = createHigherOrderComponent( ( BlockEdit ) => {
// If not image block or not selected, return unmodified block.
'core/image' !== props.name ||
'undefined' === typeof props.attributes.id
<BlockEdit { ...props } />
const smushData = props.attributes.smush;
<BlockEdit { ...props } />
<PanelBody title={ window.smush_vars.strings.gb.stats }>
{ smushStats( props.attributes.id, smushData ) }
}, 'withInspectorControl' );
'wp-smush/smush-data-control',