diff --git a/src/js/admin.js b/src/js/admin.js index d2db3c2..e0a3099 100644 --- a/src/js/admin.js +++ b/src/js/admin.js @@ -1,57 +1,57 @@ import apiFetch from '@wordpress/api-fetch'; import { __ } from '@wordpress/i18n'; -const hideElement = (hide, element) => { - if (hide) { - element.classList.add('hidden'); - element.setAttribute('aria-hidden', 'true'); +const hideElement = ( hide, element ) => { + if ( hide ) { + element.classList.add( 'hidden' ); + element.setAttribute( 'aria-hidden', 'true' ); } else { - element.classList.remove('hidden'); - element.setAttribute('aria-hidden', 'false'); + element.classList.remove( 'hidden' ); + element.setAttribute( 'aria-hidden', 'false' ); } }; document - .querySelector('#icw-fix-content-btn') - .addEventListener('click', async (event) => { + .querySelector( '#icw-fix-content-btn' ) + .addEventListener( 'click', async ( event ) => { event.preventDefault(); const postSelection = document.querySelector( '#icw-post-selection' ).value; - const postIds = document.querySelector('#icw-post-ids').value; - const postType = document.querySelector('#icw-post-type').value; - const batchSize = +document.querySelector('#icw-batch-size').value; - const dryRun = document.querySelector('#icw-dry-run').checked; - const log = document.querySelector('#icw-fix-log'); - const loadingSpinner = document.querySelector('#icw-loading-spinner'); - const btn = document.querySelector('#icw-fix-content-btn'); + const postIds = document.querySelector( '#icw-post-ids' ).value; + const postType = document.querySelector( '#icw-post-type' ).value; + const batchSize = +document.querySelector( '#icw-batch-size' ).value; + const dryRun = document.querySelector( '#icw-dry-run' ).checked; + const log = document.querySelector( '#icw-fix-log' ); + const loadingSpinner = document.querySelector( '#icw-loading-spinner' ); + const btn = document.querySelector( '#icw-fix-content-btn' ); const toggleSpinnerVisibility = () => { - loadingSpinner.classList.toggle('hidden'); - loadingSpinner.setAttribute('aria-hidden', 'true'); + loadingSpinner.classList.toggle( 'hidden' ); + loadingSpinner.setAttribute( 'aria-hidden', 'true' ); }; - const disableButton = (disable) => { - if (disable) { - btn.setAttribute('disabled', ''); + const disableButton = ( disable ) => { + if ( disable ) { + btn.setAttribute( 'disabled', '' ); } else { - btn.removeAttribute('disabled'); + btn.removeAttribute( 'disabled' ); } }; - const setLogHTML = (html) => { + const setLogHTML = ( html ) => { log.innerHTML = html; log.scrollTop = log.scrollHeight; }; toggleSpinnerVisibility(); - disableButton(true); - setLogHTML(''); - hideElement(true, log); + disableButton( true ); + setLogHTML( '' ); + hideElement( true, log ); - const count = await apiFetch({ + const count = await apiFetch( { path: `/icw/v1/count-for-fix`, method: 'POST', data: { @@ -61,30 +61,31 @@ document postSelection === 'all_from_post_type' ? postType : '', batchSize, }, - }); + } ); - const times = Math.ceil(count / batchSize); + const times = Math.ceil( count / batchSize ); - hideElement(false, log); + hideElement( false, log ); - const getProgressBar = (time, __times, __batchSize, __count) => { + const getProgressBar = ( time, __times, __batchSize, __count ) => { const progressBarMax = 50; - const percentage = time > 0 ? Math.ceil((100 / __times) * time) : 0; - const value = (percentage * progressBarMax) / 100; + const percentage = + time > 0 ? Math.ceil( ( 100 / __times ) * time ) : 0; + const value = ( percentage * progressBarMax ) / 100; const completed = time !== __times ? __batchSize * time : __count; - return `[${'#'.repeat(value)}${'-'.repeat( + return `[${ '#'.repeat( value ) }${ '-'.repeat( progressBarMax - value - )}] | ${percentage}% || ${completed}/${__count} ${__( + ) }] | ${ percentage }% || ${ completed }/${ __count } ${ __( 'posts', 'insecure-content-warning' - )}`; + ) }`; }; - setLogHTML(getProgressBar(0, times, batchSize, count)); + setLogHTML( getProgressBar( 0, times, batchSize, count ) ); let innerHTML = ''; - for (let i = 1; i <= times; i++) { + for ( let i = 1; i <= times; i++ ) { // eslint-disable-next-line no-await-in-loop - const data = await apiFetch({ + const data = await apiFetch( { path: `/icw/v1/fix`, method: 'POST', data: { @@ -93,46 +94,52 @@ document postType: postSelection === 'all_from_post_type' ? postType : '', batchSize, - offset: (i - 1) * batchSize, + offset: ( i - 1 ) * batchSize, dryRun, }, - }); + } ); innerHTML += data; - setLogHTML(innerHTML + getProgressBar(i, times, batchSize, count)); + setLogHTML( + innerHTML + getProgressBar( i, times, batchSize, count ) + ); } toggleSpinnerVisibility(); - disableButton(false); - }); - -document.querySelector('#icw-post-selection').addEventListener('change', () => { - const postSelection = document.querySelector('#icw-post-selection').value; - - const hidePostIdsRow = (hide) => { - const postIdsRow = document.querySelector('#icw-post-ids-row'); - hideElement(hide, postIdsRow); - }; - - const hidePostTypeRow = (hide) => { - const postTypeRow = document.querySelector('#icw-post-type-row'); - hideElement(hide, postTypeRow); - }; - - switch (postSelection) { - case 'all': - hidePostIdsRow(true); - hidePostTypeRow(true); - break; - case 'posts': - hidePostIdsRow(false); - hidePostTypeRow(true); - break; - case 'all_from_post_type': - hidePostTypeRow(false); - hidePostIdsRow(true); - break; - default: - break; - } -}); + disableButton( false ); + } ); + +document + .querySelector( '#icw-post-selection' ) + .addEventListener( 'change', () => { + const postSelection = document.querySelector( + '#icw-post-selection' + ).value; + + const hidePostIdsRow = ( hide ) => { + const postIdsRow = document.querySelector( '#icw-post-ids-row' ); + hideElement( hide, postIdsRow ); + }; + + const hidePostTypeRow = ( hide ) => { + const postTypeRow = document.querySelector( '#icw-post-type-row' ); + hideElement( hide, postTypeRow ); + }; + + switch ( postSelection ) { + case 'all': + hidePostIdsRow( true ); + hidePostTypeRow( true ); + break; + case 'posts': + hidePostIdsRow( false ); + hidePostTypeRow( true ); + break; + case 'all_from_post_type': + hidePostTypeRow( false ); + hidePostIdsRow( true ); + break; + default: + break; + } + } ); diff --git a/src/js/classic-editor.js b/src/js/classic-editor.js index c69bc2c..a162893 100644 --- a/src/js/classic-editor.js +++ b/src/js/classic-editor.js @@ -5,26 +5,26 @@ import replaceContent from './utils/replace'; import '../css/admin.scss'; // Listen for clicks on the publish button -jQuery(document).on('click', '#publish', (event) => { +jQuery( document ).on( 'click', '#publish', ( event ) => { blurInsecure(); if ( - !jQuery(event.target).hasClass('disabled') && - jQuery('.js-icw-force-checkbox').prop('checked') !== true + ! jQuery( event.target ).hasClass( 'disabled' ) && + jQuery( '.js-icw-force-checkbox' ).prop( 'checked' ) !== true ) { - checkContent(event); + checkContent( event ); } -}); +} ); // Listen for clicks on the force publish checkbox -jQuery(document).on('change', '#icw-force-checkbox', function () { +jQuery( document ).on( 'change', '#icw-force-checkbox', function () { // Enable or disable the publish button as needed blurInsecure(); - if (jQuery(this).is(':checked')) { - jQuery('#publish').removeClass('disabled'); + if ( jQuery( this ).is( ':checked' ) ) { + jQuery( '#publish' ).removeClass( 'disabled' ); } else { - jQuery('#publish').addClass('disabled'); + jQuery( '#publish' ).addClass( 'disabled' ); } -}); +} ); /** * If the preview button is clicked after @@ -34,72 +34,74 @@ jQuery(document).on('change', '#icw-force-checkbox', function () { * the update button and then the preview button, * as it expects a page refresh after updates */ -jQuery(document).on('click', '#post-preview', () => { +jQuery( document ).on( 'click', '#post-preview', () => { blurInsecure(); - if (document.querySelector('.js-icw-error')) { - if (wp.autosave) { + if ( document.querySelector( '.js-icw-error' ) ) { + if ( wp.autosave ) { wp.autosave.server.resume(); wp.autosave.enableButtons(); } else { - jQuery(document).trigger('autosave-enable-buttons'); + jQuery( document ).trigger( 'autosave-enable-buttons' ); } - jQuery('#major-publishing-actions .spinner').removeClass('is-active'); + jQuery( '#major-publishing-actions .spinner' ).removeClass( + 'is-active' + ); } -}); +} ); // Listen for clicks on the fix asset links -jQuery(document).on('click', '.js-icw-check', function (e) { +jQuery( document ).on( 'click', '.js-icw-check', function ( e ) { e.preventDefault(); blurInsecure(); - const spinner = jQuery(this).next('.js-icw-spinner'); - const url = jQuery(this).data('check'); + const spinner = jQuery( this ).next( '.js-icw-spinner' ); + const url = jQuery( this ).data( 'check' ); spinner.show(); - wp.apiRequest({ path: `/icw/v1/check?url=${url}` }).then( - (data) => { + wp.apiRequest( { path: `/icw/v1/check?url=${ url }` } ).then( + ( data ) => { spinner.hide(); // Attempt to replace if https equivalent found. - if (data === true) { - jQuery(this).nextAll('.js-icw-fixed').show(); - replaceContent(url); + if ( data === true ) { + jQuery( this ).nextAll( '.js-icw-fixed' ).show(); + replaceContent( url ); } else { // show the error - jQuery(this).nextAll('.js-icw-error').show(); - throw new Error('No https equivalent found.'); + jQuery( this ).nextAll( '.js-icw-error' ).show(); + throw new Error( 'No https equivalent found.' ); } - setTimeout(function () { - checkContent(e); - }, 1000); + setTimeout( function () { + checkContent( e ); + }, 1000 ); }, - (err) => { + ( err ) => { // Don't recheck if replace unsuccessful. return err; } ); -}); +} ); -jQuery(document).on('click', '.js-icw-view', function (e) { +jQuery( document ).on( 'click', '.js-icw-view', function ( e ) { e.preventDefault(); blurInsecure(); - const url = jQuery(this).data('check'); - const elements = findElements(url); + const url = jQuery( this ).data( 'check' ); + const elements = findElements( url ); - if (elements.length) { - const $element = jQuery(elements[0]); + if ( elements.length ) { + const $element = jQuery( elements[ 0 ] ); - jQuery('html, body').animate( + jQuery( 'html, body' ).animate( { scrollTop: $element.offset().top, }, 0 ); - $element.addClass('js-icw-is-insecure'); + $element.addClass( 'js-icw-is-insecure' ); } -}); +} ); diff --git a/src/js/gutenberg.js b/src/js/gutenberg.js index e454771..5b2363e 100644 --- a/src/js/gutenberg.js +++ b/src/js/gutenberg.js @@ -29,155 +29,156 @@ const RedefineSaveShortcut = () => { const { unregisterShortcut, registerShortcut } = useDispatch( 'core/keyboard-shortcuts' ); - const { savePost } = useDispatch('core/editor'); - const { isEditedPostDirty, isPostSavingLocked } = useSelect('core/editor'); + const { savePost } = useDispatch( 'core/editor' ); + const { isEditedPostDirty, isPostSavingLocked } = + useSelect( 'core/editor' ); - useEffect(() => { - unregisterShortcut(saveShortcutId); - registerShortcut({ + useEffect( () => { + unregisterShortcut( saveShortcutId ); + registerShortcut( { name: newSaveShortcutId, category: 'global', - description: __('Save your changes.', 'insecure-content-warning'), + description: __( 'Save your changes.', 'insecure-content-warning' ), keyCombination: { modifier: 'primary', character: 's', }, - }); - }, []); + } ); + }, [] ); - useShortcut(newSaveShortcutId, (event) => { + useShortcut( newSaveShortcutId, ( event ) => { event.preventDefault(); - const isSecure = gutenbergCheck(event); + const isSecure = gutenbergCheck( event ); - if (!isSecure) { + if ( ! isSecure ) { return; } - if (isPostSavingLocked()) { + if ( isPostSavingLocked() ) { return; } - if (!isEditedPostDirty()) { + if ( ! isEditedPostDirty() ) { return; } savePost(); - }); + } ); return null; }; -registerPlugin('insecure-content-warning-redefine-save-shortcut', { +registerPlugin( 'insecure-content-warning-redefine-save-shortcut', { render: RedefineSaveShortcut, -}); +} ); -domReady(() => { - let content = select('core/editor').getEditedPostContent(); +domReady( () => { + let content = select( 'core/editor' ).getEditedPostContent(); let publishBtn = document.querySelector( '.editor-post-publish-button, .editor-post-publish-panel__toggle' ); - if (publishBtn) { - publishBtn.addEventListener('click', gutenbergCheck); + if ( publishBtn ) { + publishBtn.addEventListener( 'click', gutenbergCheck ); } else { - const interval = setInterval(() => { + const interval = setInterval( () => { publishBtn = document.querySelector( '.editor-post-publish-button, .editor-post-publish-panel__toggle' ); - if (publishBtn) { - publishBtn.addEventListener('click', gutenbergCheck); - clearInterval(interval); + if ( publishBtn ) { + publishBtn.addEventListener( 'click', gutenbergCheck ); + clearInterval( interval ); } - }, 500); + }, 500 ); } // Unlock post saving when content changes subscribe( - debounce(() => { - const newContent = select('core/editor').getEditedPostContent(); - const isLocked = select('core/editor').isPostSavingLocked(); - if (content !== newContent && isLocked) { + debounce( () => { + const newContent = select( 'core/editor' ).getEditedPostContent(); + const isLocked = select( 'core/editor' ).isPostSavingLocked(); + if ( content !== newContent && isLocked ) { blurInsecure(); - dispatch('core/editor').unlockPostSaving( + dispatch( 'core/editor' ).unlockPostSaving( 'insecureContentWarning' ); content = newContent; } - }, 1000) + }, 1000 ) ); - jQuery(document).on('click', '.gutenberg-js-icw-check', function (e) { + jQuery( document ).on( 'click', '.gutenberg-js-icw-check', function ( e ) { e.preventDefault(); blurInsecure(); - const spinner = jQuery(this).next('.js-icw-spinner'); - const url = jQuery(this).data('check'); + const spinner = jQuery( this ).next( '.js-icw-spinner' ); + const url = jQuery( this ).data( 'check' ); spinner.show(); - apiRequest({ path: `/icw/v1/check?url=${url}` }).then( - (data) => { + apiRequest( { path: `/icw/v1/check?url=${ url }` } ).then( + ( data ) => { spinner.hide(); // Attempt to replace if https equivalent found. - if (data === true) { - jQuery(this).nextAll('.js-icw-fixed').show(); - replaceContent(url); + if ( data === true ) { + jQuery( this ).nextAll( '.js-icw-fixed' ).show(); + replaceContent( url ); // The "check" data has to be removed because otherwise it will always return the URL for the first replaced URL. // Alternatively "const url = jQuery(clickedButton).data('check');" can be changed to "const url = clickedButton.dataset.check;". - jQuery(this).removeData('check'); + jQuery( this ).removeData( 'check' ); } else { // show the error - jQuery(this).nextAll('.js-icw-error').show(); - throw new Error('No https equivalent found.'); + jQuery( this ).nextAll( '.js-icw-error' ).show(); + throw new Error( 'No https equivalent found.' ); } // The instance of the clicked button will have been lost when the timeout happens, so an instance needs to be kept. const clickedButton = e.currentTarget; - setTimeout(function () { - jQuery(clickedButton).nextAll('.js-icw-fixed').hide(); - gutenbergCheck(e); - }, 1000); + setTimeout( function () { + jQuery( clickedButton ).nextAll( '.js-icw-fixed' ).hide(); + gutenbergCheck( e ); + }, 1000 ); }, - (err) => { + ( err ) => { // Don't recheck if replace unsuccessful. return err; } ); - }); + } ); - jQuery(document).on('click', '.gutenberg-js-icw-view', function (e) { + jQuery( document ).on( 'click', '.gutenberg-js-icw-view', function ( e ) { e.preventDefault(); blurInsecure(); - const url = jQuery(this).data('check'); - const blockEditor = select('core/block-editor'); + const url = jQuery( this ).data( 'check' ); + const blockEditor = select( 'core/block-editor' ); - const insecureBlocks = blockEditor.getBlocks().filter((block) => { + const insecureBlocks = blockEditor.getBlocks().filter( ( block ) => { const found = block.attributes?.url === url || block.attributes?.mediaUrl === url || block.attributes?.src === url; return found; - }); + } ); - if (insecureBlocks.length > 0) { + if ( insecureBlocks.length > 0 ) { const insecureBlock = document.querySelector( - `[data-block="${insecureBlocks[0].clientId}"]` + `[data-block="${ insecureBlocks[ 0 ].clientId }"]` ); const container = insecureBlock - ? getScrollContainer(insecureBlock) + ? getScrollContainer( insecureBlock ) : null; - if (insecureBlock && container) { + if ( insecureBlock && container ) { insecureBlock.scrollIntoView(); - jQuery(`[data-block="${insecureBlocks[0].clientId}"]`).addClass( - 'js-icw-is-insecure' - ); + jQuery( + `[data-block="${ insecureBlocks[ 0 ].clientId }"]` + ).addClass( 'js-icw-is-insecure' ); } // The "check" data has to be removed because otherwise it will always return the URL for the first highlighted URL. - jQuery(this).removeData('check'); + jQuery( this ).removeData( 'check' ); } - }); -}); + } ); +} ); diff --git a/src/js/utils/blur-insecure.js b/src/js/utils/blur-insecure.js index ddc913f..19eb2fe 100644 --- a/src/js/utils/blur-insecure.js +++ b/src/js/utils/blur-insecure.js @@ -3,26 +3,26 @@ */ const blurInsecure = () => { const $visualEditorWrap = jQuery( - document.getElementById('wp-content-wrap') + document.getElementById( 'wp-content-wrap' ) ); let $insecure; if ( - $visualEditorWrap.hasClass('tmce-active') || - $visualEditorWrap.hasClass('tinymce-active') + $visualEditorWrap.hasClass( 'tmce-active' ) || + $visualEditorWrap.hasClass( 'tinymce-active' ) ) { - $insecure = jQuery('#content_ifr') + $insecure = jQuery( '#content_ifr' ) .contents() - .find('.js-icw-is-insecure'); + .find( '.js-icw-is-insecure' ); } else { - $insecure = jQuery('
', {
+ } );
+ const $url = jQuery( '', {
class: 'icw-list-item-description',
- text: insecureElementURLs[i],
- });
- const $success = jQuery('', {
+ text: insecureElementURLs[ i ],
+ } );
+ const $success = jQuery( '', {
class: 'js-icw-fixed',
style: 'display: none; color: forestgreen; font-weight: bolder',
- text: __('Success!', 'insecure-content-warning'),
- });
- const $error = jQuery('', {
+ text: __( 'Success!', 'insecure-content-warning' ),
+ } );
+ const $error = jQuery( '', {
class: 'error js-icw-error',
style: 'display: none; color: #950e0d; font-weight: bolder',
text: __(
'Unable to find https:// equivalent. Please replace manually.',
'insecure-content-warning'
),
- });
-
- $li.append($url);
- $li.append($br);
- $li.append($view);
- $li.append($a);
- $li.append($spinner);
- $li.append($success);
- $li.append($error);
- $ol.append($li);
+ } );
+
+ $li.append( $url );
+ $li.append( $br );
+ $li.append( $view );
+ $li.append( $a );
+ $li.append( $spinner );
+ $li.append( $success );
+ $li.append( $error );
+ $ol.append( $li );
}
- const $p = jQuery('');
- const $label = jQuery('