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('
') - .append(jQuery.parseHTML(jQuery('#content').val())) - .find('.js-icw-is-insecure'); + $insecure = jQuery( '
' ) + .append( jQuery.parseHTML( jQuery( '#content' ).val() ) ) + .find( '.js-icw-is-insecure' ); } - $insecure.removeClass('js-icw-is-insecure'); + $insecure.removeClass( 'js-icw-is-insecure' ); - jQuery('.js-icw-is-insecure').removeClass('js-icw-is-insecure'); + jQuery( '.js-icw-is-insecure' ).removeClass( 'js-icw-is-insecure' ); }; export default blurInsecure; diff --git a/src/js/utils/check-content.js b/src/js/utils/check-content.js index 3b05cdc..0488203 100644 --- a/src/js/utils/check-content.js +++ b/src/js/utils/check-content.js @@ -2,43 +2,43 @@ import { __, _nx, sprintf } from '@wordpress/i18n'; import blurInsecure from './blur-insecure'; import { scanElements } from './scan-elements'; -const checkContent = (event) => { +const checkContent = ( event ) => { blurInsecure(); const $visualEditorWrap = jQuery( - document.getElementById('wp-content-wrap') + document.getElementById( 'wp-content-wrap' ) ); let $elements; // Enable the publish button - jQuery('#publish').removeClass('disabled'); + jQuery( '#publish' ).removeClass( 'disabled' ); if ( - $visualEditorWrap.hasClass('tmce-active') || - $visualEditorWrap.hasClass('tinymce-active') + $visualEditorWrap.hasClass( 'tmce-active' ) || + $visualEditorWrap.hasClass( 'tinymce-active' ) ) { - $elements = jQuery('#content_ifr').contents().find('*'); + $elements = jQuery( '#content_ifr' ).contents().find( '*' ); } else { - $elements = jQuery('
') - .append(jQuery.parseHTML(jQuery('#content').val())) - .find('*'); + $elements = jQuery( '
' ) + .append( jQuery.parseHTML( jQuery( '#content' ).val() ) ) + .find( '*' ); } - const scanResults = scanElements($elements); + const scanResults = scanElements( $elements ); const { insecure } = scanResults; const { insecureElementURLs } = scanResults; - const $hr = jQuery('#major-publishing-actions'); + const $hr = jQuery( '#major-publishing-actions' ); - if (insecure > 0) { + if ( insecure > 0 ) { event.preventDefault(); // Disable the publish button - jQuery('#publish').addClass('disabled'); + jQuery( '#publish' ).addClass( 'disabled' ); $hr.next().remove(); - const $errorContainer = jQuery('
', { + const $errorContainer = jQuery( '
', { class: 'error js-icw-error', text: sprintf( // translators: %d: Single insecure element found, %d: Multiple insecure elements found. @@ -49,88 +49,88 @@ const checkContent = (event) => { 'number of insecure elements', 'insecure-content-warning' ), - parseInt(insecure, 10) + parseInt( insecure, 10 ) ), - }); + } ); - const $ol = jQuery('
    '); + const $ol = jQuery( '
      ' ); - for (let i = 0, { length } = insecureElementURLs; i < length; i++) { - const $li = jQuery('
    1. ', { + for ( let i = 0, { length } = insecureElementURLs; i < length; i++ ) { + const $li = jQuery( '
    2. ', { class: 'icw-list-item', - }); - const $br = jQuery('
      '); - const $view = jQuery('', { + } ); + const $br = jQuery( '
      ' ); + const $view = jQuery( '
      ', { class: 'js-icw-view', - 'data-check': insecureElementURLs[i], + 'data-check': insecureElementURLs[ i ], href: '', - text: __('View element', 'insecure-content-warning'), - }); - const $a = jQuery('', { + text: __( 'View element', 'insecure-content-warning' ), + } ); + const $a = jQuery( '', { class: 'js-icw-check', - 'data-check': insecureElementURLs[i], + 'data-check': insecureElementURLs[ i ], href: '', - text: __('Fix this', 'insecure-content-warning'), - }); - const $spinner = jQuery('', { + text: __( 'Fix this', 'insecure-content-warning' ), + } ); + const $spinner = jQuery( '', { src: insecureContentAdmin.spinner, class: 'js-icw-spinner', style: 'display: none', - }); - const $url = 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('

      ' ); + const $label = jQuery( '

      ') - .append(jQuery.parseHTML(jQuery('#content').val())) - .find('*'); + $from = jQuery( '
      ' ) + .append( jQuery.parseHTML( jQuery( '#content' ).val() ) ) + .find( '*' ); } - const $found = $from.filter((index, el) => { - if (get(el, 'dataset.mceObject') === 'object') { + const $found = $from.filter( ( index, el ) => { + if ( get( el, 'dataset.mceObject' ) === 'object' ) { if ( el.dataset.mcePData && - el.dataset.mcePData.substr(0, url.length) === url + el.dataset.mcePData.substr( 0, url.length ) === url ) { return true; } } - if (el.src && el.src.substr(0, url.length) === url) { + if ( el.src && el.src.substr( 0, url.length ) === url ) { return true; } - if (el.srcset && el.srcset.substr(0, url.length) === url) { + if ( el.srcset && el.srcset.substr( 0, url.length ) === url ) { return true; } if ( el.nodeName.toLowerCase() === 'object' && el.data && - el.data.substr(0, url.length) === url + el.data.substr( 0, url.length ) === url ) { return true; } return false; - }); + } ); return $found; }; diff --git a/src/js/utils/gutenberg-check.js b/src/js/utils/gutenberg-check.js index 0eb273b..242eb31 100644 --- a/src/js/utils/gutenberg-check.js +++ b/src/js/utils/gutenberg-check.js @@ -7,27 +7,27 @@ import { registerInsecureContentPlugin } from './gutenberg-status'; const SECURE_CONTENT_WARNING_ID = 'secure-content-warning'; -export const gutenbergCheck = (event) => { +export const gutenbergCheck = ( event ) => { blurInsecure(); // Scan content, add warnings const scanResults = gutenbergScan(); const { insecure, insecureElementURLs } = scanResults; const proceedCheckBoxChecked = jQuery( '.js-icw-force-checkbox input[type=checkbox]' - ).is(':checked'); + ).is( ':checked' ); // Remove any previous notice. - dispatch('core/notices').removeNotice(SECURE_CONTENT_WARNING_ID); + dispatch( 'core/notices' ).removeNotice( SECURE_CONTENT_WARNING_ID ); // Remove previous warning panel - if (getPlugin('insecure-warnings')) { - unregisterPlugin('insecure-warnings'); + if ( getPlugin( 'insecure-warnings' ) ) { + unregisterPlugin( 'insecure-warnings' ); } // Unlock post saving - dispatch('core/editor').unlockPostSaving('insecureContentWarning'); + dispatch( 'core/editor' ).unlockPostSaving( 'insecureContentWarning' ); - if (insecure > 0 && !proceedCheckBoxChecked) { + if ( insecure > 0 && ! proceedCheckBoxChecked ) { event.preventDefault(); event.stopPropagation(); @@ -41,38 +41,41 @@ export const gutenbergCheck = (event) => { 'number of insecure elements', 'insecure-content-warning' ), - parseInt(insecure, 10) + parseInt( insecure, 10 ) ); // Display notice - dispatch('core/notices').createErrorNotice(message, { + dispatch( 'core/notices' ).createErrorNotice( message, { id: SECURE_CONTENT_WARNING_ID, - }); + } ); // Display detailed message in post status panel - registerInsecureContentPlugin(insecureElementURLs); + registerInsecureContentPlugin( insecureElementURLs ); // Switch back to the main panel. - setTimeout(() => dispatch('core/edit-post').closePublishSidebar(), 0); + setTimeout( + () => dispatch( 'core/edit-post' ).closePublishSidebar(), + 0 + ); setTimeout( () => - dispatch('core/edit-post').openGeneralSidebar( + dispatch( 'core/edit-post' ).openGeneralSidebar( 'edit-post/document' ), 0 ); - setTimeout(() => { + setTimeout( () => { const insecureWarningsPanelEl = document.querySelector( '.insecure-warnings-panel' ); - if (insecureWarningsPanelEl) { + if ( insecureWarningsPanelEl ) { insecureWarningsPanelEl.scrollIntoView(); } - }, 0); + }, 0 ); // Lock post saving - dispatch('core/editor').lockPostSaving('insecureContentWarning'); + dispatch( 'core/editor' ).lockPostSaving( 'insecureContentWarning' ); return false; } diff --git a/src/js/utils/gutenberg-scan.js b/src/js/utils/gutenberg-scan.js index ad7695b..c497ab9 100644 --- a/src/js/utils/gutenberg-scan.js +++ b/src/js/utils/gutenberg-scan.js @@ -2,12 +2,12 @@ import { select } from '@wordpress/data'; import { scanElements } from './scan-elements'; export const gutenbergScan = () => { - const content = select('core/editor').getEditedPostAttribute('content'); - const $elements = jQuery.parseHTML(content); - const scanResults = scanElements([ - ...jQuery($elements).toArray(), - ...jQuery($elements).find('*').toArray(), - ]); + const content = select( 'core/editor' ).getEditedPostAttribute( 'content' ); + const $elements = jQuery.parseHTML( content ); + const scanResults = scanElements( [ + ...jQuery( $elements ).toArray(), + ...jQuery( $elements ).find( '*' ).toArray(), + ] ); return scanResults; }; diff --git a/src/js/utils/gutenberg-status.js b/src/js/utils/gutenberg-status.js index 8ae6f53..e261c31 100644 --- a/src/js/utils/gutenberg-status.js +++ b/src/js/utils/gutenberg-status.js @@ -5,81 +5,87 @@ import { useState } from '@wordpress/element'; import { PluginPostStatusInfo } from '@wordpress/edit-post'; import { registerPlugin } from '@wordpress/plugins'; -export const registerInsecureContentPlugin = (insecureElementURLs) => { +export const registerInsecureContentPlugin = ( insecureElementURLs ) => { const renderInsecureContentWarnings = () => { - const [isChecked, setChecked] = useState(false); // eslint-disable-line react-hooks/rules-of-hooks + const [ isChecked, setChecked ] = useState( false ); // eslint-disable-line react-hooks/rules-of-hooks return ( -

      {__('Insecure Warnings', 'insecure-content-warning')}

      +

      { __( 'Insecure Warnings', 'insecure-content-warning' ) }

        - {insecureElementURLs.map((element, i) => { + { insecureElementURLs.map( ( element, i ) => { return ( -
      1. - {element} +
      2. + { element } Loading - {__('Success!', 'insecure-content-warning')} + { __( + 'Success!', + 'insecure-content-warning' + ) } - {__( + { __( 'Unable to find https:// equivalent. Please replace manually.', 'insecure-content-warning' - )} + ) }
      3. ); - })} + } ) }
      { - setChecked(checked); + ) } + checked={ isChecked } + onChange={ ( checked ) => { + setChecked( checked ); // Lock and unlock saving - if (checked) { - dispatch('core/editor').unlockPostSaving( + if ( checked ) { + dispatch( 'core/editor' ).unlockPostSaving( 'insecureContentWarning' ); } else { - dispatch('core/editor').lockPostSaving( + dispatch( 'core/editor' ).lockPostSaving( 'insecureContentWarning' ); } - }} + } } />
      ); }; - registerPlugin('insecure-warnings', { + registerPlugin( 'insecure-warnings', { render: renderInsecureContentWarnings, - }); + } ); }; diff --git a/src/js/utils/replace.js b/src/js/utils/replace.js index 2924b99..8db7bcc 100644 --- a/src/js/utils/replace.js +++ b/src/js/utils/replace.js @@ -3,37 +3,37 @@ * * @param {string} url URL to replace */ -const replaceContent = (url = '') => { - const replace = url.replace('http://', 'https://'); - const regex = new RegExp(url, 'g'); +const replaceContent = ( url = '' ) => { + const replace = url.replace( 'http://', 'https://' ); + const regex = new RegExp( url, 'g' ); - if (jQuery('#wp-content-wrap').hasClass('html-active')) { - const editor = document.getElementById('content'); + if ( jQuery( '#wp-content-wrap' ).hasClass( 'html-active' ) ) { + const editor = document.getElementById( 'content' ); const content = editor.value; // update the textarea value - editor.value = content.replace(regex, replace); - } else if (typeof tinyMCE === 'object') { - if (!tinyMCE.activeEditor) { + editor.value = content.replace( regex, replace ); + } else if ( typeof tinyMCE === 'object' ) { + if ( ! tinyMCE.activeEditor ) { // Update the block editor's content const content = wp.data - .select('core/editor') - .getEditedPostAttribute('content'); - const newContent = content.replace(regex, replace); + .select( 'core/editor' ) + .getEditedPostAttribute( 'content' ); + const newContent = content.replace( regex, replace ); wp.data - .dispatch('core/block-editor') - .resetBlocks(wp.blocks.parse(newContent)); + .dispatch( 'core/block-editor' ) + .resetBlocks( wp.blocks.parse( newContent ) ); - setTimeout(() => { - jQuery(document).trigger('recheck-contents'); - }, 1000); + setTimeout( () => { + jQuery( document ).trigger( 'recheck-contents' ); + }, 1000 ); } else { const content = tinyMCE.activeEditor.getContent(); - const newContent = content.replace(regex, replace); + const newContent = content.replace( regex, replace ); // Update tinyMCE's content - tinyMCE.activeEditor.setContent(newContent); + tinyMCE.activeEditor.setContent( newContent ); } } }; diff --git a/src/js/utils/scan-elements.js b/src/js/utils/scan-elements.js index c785fcf..8ff3a4b 100644 --- a/src/js/utils/scan-elements.js +++ b/src/js/utils/scan-elements.js @@ -1,57 +1,57 @@ import { each, get } from 'underscore'; -export const scanElements = ($elements) => { +export const scanElements = ( $elements ) => { const insecureElementURLs = []; let insecure = 0; - each($elements, (el) => { + each( $elements, ( el ) => { // Handle object elements that have been converted for the classic editor - if (get(el, 'dataset.mceObject') === 'object') { + if ( get( el, 'dataset.mceObject' ) === 'object' ) { if ( el.dataset.mcePData && - el.dataset.mcePData.substr(0, 7) === 'http://' + el.dataset.mcePData.substr( 0, 7 ) === 'http://' ) { insecure += 1; // remove query parameters for display. - const url = el.dataset.mcePData.split('?')[0]; - insecureElementURLs.push(url); + const url = el.dataset.mcePData.split( '?' )[ 0 ]; + insecureElementURLs.push( url ); } return; } // Handle elements with a src attribute, like img - if (el.src && el.src.substr(0, 8) !== 'https://') { + if ( el.src && el.src.substr( 0, 8 ) !== 'https://' ) { insecure += 1; // remove query parameters for display. - const url = el.src.split('?')[0]; - insecureElementURLs.push(url); + const url = el.src.split( '?' )[ 0 ]; + insecureElementURLs.push( url ); } // Handle elements with a srcset attribute, like img or source - if (el.srcset && el.srcset.substr(0, 8) !== 'https://') { + if ( el.srcset && el.srcset.substr( 0, 8 ) !== 'https://' ) { insecure += 1; // remove query parameters for display. - const url = el.srcset.split('?')[0]; - insecureElementURLs.push(url); + const url = el.srcset.split( '?' )[ 0 ]; + insecureElementURLs.push( url ); } // Handle object elements with a data attribute if ( el.nodeName.toLowerCase() === 'object' && el.data && - el.data.substr(0, 7) === 'http://' + el.data.substr( 0, 7 ) === 'http://' ) { insecure += 1; // remove query parameters for display. - const url = el.data.split('?')[0]; - insecureElementURLs.push(url); + const url = el.data.split( '?' )[ 0 ]; + insecureElementURLs.push( url ); } - }); + } ); return { insecureElementURLs,