| 1 |
( function ( wp ) { |
| 2 |
var registerBlockType = wp.blocks.registerBlockType; |
| 3 |
var InspectorControls = wp.blockEditor.InspectorControls; |
| 4 |
var useBlockProps = wp.blockEditor.useBlockProps; |
| 5 |
var PanelBody = wp.components.PanelBody; |
| 6 |
var TextControl = wp.components.TextControl; |
| 7 |
var ToggleControl = wp.components.ToggleControl; |
| 8 |
var el = wp.element.createElement; |
| 9 |
var Fragment = wp.element.Fragment; |
| 10 |
var __ = wp.i18n.__; |
| 11 |
|
| 12 |
registerBlockType( 'shortcode-redirect/redirect', { |
| 13 |
edit: function ( props ) { |
| 14 |
var attributes = props.attributes; |
| 15 |
var setAttributes = props.setAttributes; |
| 16 |
var url = attributes.url || ''; |
| 17 |
var sec = typeof attributes.sec === 'number' ? attributes.sec : 0; |
| 18 |
var showMessage = attributes.showMessage !== false; |
| 19 |
|
| 20 |
var blockProps = useBlockProps( { className: 'scr-block' } ); |
| 21 |
|
| 22 |
var preview; |
| 23 |
if ( url ) { |
| 24 |
preview = el( |
| 25 |
'div', |
| 26 |
{ className: 'scr-block__body' }, |
| 27 |
el( 'span', { className: 'scr-block__arrow', 'aria-hidden': 'true' }, '\u2192' ), |
| 28 |
el( |
| 29 |
'div', |
| 30 |
{ className: 'scr-block__text' }, |
| 31 |
el( 'div', { className: 'scr-block__line' }, |
| 32 |
el( 'strong', null, __( 'Redirects to', 'shortcode-redirect' ) ), |
| 33 |
' ', |
| 34 |
el( 'code', null, url ) |
| 35 |
), |
| 36 |
el( 'div', { className: 'scr-block__meta' }, |
| 37 |
( sec > 0 |
| 38 |
? __( 'after', 'shortcode-redirect' ) + ' ' + sec + ' ' + ( sec === 1 ? __( 'second', 'shortcode-redirect' ) : __( 'seconds', 'shortcode-redirect' ) ) |
| 39 |
: __( 'immediately on page load', 'shortcode-redirect' ) |
| 40 |
) + ' · ' + ( showMessage |
| 41 |
? __( 'message shown', 'shortcode-redirect' ) |
| 42 |
: __( 'silent', 'shortcode-redirect' ) |
| 43 |
) |
| 44 |
) |
| 45 |
) |
| 46 |
); |
| 47 |
} else { |
| 48 |
preview = el( |
| 49 |
'div', |
| 50 |
{ className: 'scr-block__body scr-block__body--empty' }, |
| 51 |
el( 'span', { className: 'scr-block__arrow', 'aria-hidden': 'true' }, '\u2192' ), |
| 52 |
el( |
| 53 |
'div', |
| 54 |
{ className: 'scr-block__text' }, |
| 55 |
el( 'em', null, __( 'Set a destination URL in the block settings.', 'shortcode-redirect' ) ) |
| 56 |
) |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
return el( |
| 61 |
Fragment, |
| 62 |
null, |
| 63 |
el( |
| 64 |
InspectorControls, |
| 65 |
null, |
| 66 |
el( |
| 67 |
PanelBody, |
| 68 |
{ title: __( 'Redirect settings', 'shortcode-redirect' ), initialOpen: true }, |
| 69 |
el( TextControl, { |
| 70 |
label: __( 'Destination URL', 'shortcode-redirect' ), |
| 71 |
type: 'url', |
| 72 |
value: url, |
| 73 |
placeholder: 'https://example.com', |
| 74 |
onChange: function ( value ) { |
| 75 |
setAttributes( { url: value } ); |
| 76 |
}, |
| 77 |
} ), |
| 78 |
el( TextControl, { |
| 79 |
label: __( 'Seconds to wait', 'shortcode-redirect' ), |
| 80 |
type: 'number', |
| 81 |
min: 0, |
| 82 |
value: String( sec ), |
| 83 |
onChange: function ( value ) { |
| 84 |
var n = parseInt( value, 10 ); |
| 85 |
setAttributes( { sec: isNaN( n ) || n < 0 ? 0 : n } ); |
| 86 |
}, |
| 87 |
help: __( 'Leave at 0 to redirect immediately.', 'shortcode-redirect' ), |
| 88 |
} ), |
| 89 |
el( ToggleControl, { |
| 90 |
label: __( 'Show "redirecting" message', 'shortcode-redirect' ), |
| 91 |
checked: showMessage, |
| 92 |
onChange: function ( value ) { |
| 93 |
setAttributes( { showMessage: !! value } ); |
| 94 |
}, |
| 95 |
help: showMessage |
| 96 |
? __( 'Visitors see a short message with a manual link while they wait.', 'shortcode-redirect' ) |
| 97 |
: __( 'The page stays blank — the browser redirects silently.', 'shortcode-redirect' ), |
| 98 |
} ) |
| 99 |
) |
| 100 |
), |
| 101 |
el( 'div', blockProps, preview ) |
| 102 |
); |
| 103 |
}, |
| 104 |
save: function () { |
| 105 |
return null; |
| 106 |
}, |
| 107 |
} ); |
| 108 |
} )( window.wp ); |
| 109 |
|