import {useState} from '@wordpress/element'; import {__, sprintf} from '@wordpress/i18n'; import Button from '../components/Button'; import '../scss/_html5_upload.scss'; const Html5UploadField = ( {field, value, onChange} ) => { const [isExtracting, setIsExtracting] = useState( false ); const [error, setError] = useState( '' ); // Google ad server hosts the uploaded creative loads from; blockers stop them whatever the files are called. const [adServerHosts, setAdServerHosts] = useState( [] ); const openMediaLibrary = () => { const frame = wp.media( { title: __( 'Select HTML5 ZIP File', 'adpresso' ), button: {text: __( 'Use this ZIP', 'adpresso' )}, multiple: false, library: { type: 'application/zip' } } ); frame.on( 'select', () => { const attachment = frame.state().get( 'selection' ).first().toJSON(); setError( '' ); setAdServerHosts( [] ); setIsExtracting( true ); wp.apiFetch( { path: '/adpresso/v1/extract-html5', method: 'POST', data: {attachment_id: attachment.id} } ).then( response => { onChange( field.id, response.directory ); setAdServerHosts( response.external_hosts || [] ); } ).catch( err => { console.error( err ); // WP_Error responses (400/403/404/500) carry a translated message. setError( err?.message || __( 'The ZIP file could not be extracted.', 'adpresso' ) ); } ).finally( () => { setIsExtracting( false ); } ); } ); frame.open(); }; const getFileName = ( url ) => { if ( !url ) { return ''; } return url.split( '/' ).pop(); }; return (
{isExtracting ? ( ) : value ? (
{getFileName( value )}
) : (
{__( 'No ZIP file selected', 'adpresso' )}
)}
{value && !isExtracting && ( )}
{adServerHosts.length > 0 && (
{sprintf( /* translators: %s: host names such as s0.2mdn.net */ __( 'This creative loads files from Google ad servers (%s). Ad blockers block these requests, so visitors with an ad blocker will not see it. In Google Web Designer, publish it for the "Generic" environment instead of Google Ad Manager or Display & Video 360.', 'adpresso' ), adServerHosts.join( ', ' ) )}
)} {error && (

{error}

)}
); }; export default Html5UploadField;