diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 0e0cfd979..b693ea875 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -17,32 +17,58 @@ jobs: eslint: name: eslint runs-on: ubuntu-latest + steps: - uses: actions/checkout@v3 - - name: install node v16 + + - name: Cache node_modules + id: cache-node-modules + uses: actions/cache@v3 + env: + cache-name: cache-node-modules + with: + path: node_modules + key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} + + - name: Setup node v16 and npm cache uses: actions/setup-node@v3 with: node-version: 16 - - name: npm install - run: npm install - - name: eslint - uses: icrawl/action-eslint@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + cache: 'npm' + + - name: Install Node dependencies + if: steps.cache-node-modules.outputs.cache-hit != 'true' + run: npm ci --no-optional + + - name: Get updated JS files + id: changed-files + uses: tj-actions/changed-files@v35 + with: + files: | + **/*.js + + - name: Run JS linting + run: | + if [[ "${{ steps.changed-files.outputs.any_changed }}" == 'true' && "${{ github.event_name }}" == "pull_request" ]]; then + npx wp-scripts lint-js ${{ steps.changed-files.outputs.all_changed_files }} + elif [[ "${{ github.event_name }}" == "push" ]]; then + npm run lint-js + fi + phpcs: name: phpcs runs-on: ubuntu-latest steps: - name: Checkout - uses: actions/checkout@v2.4.0 + uses: actions/checkout@v3 - name: Set standard 10up cache directories run: | composer config -g cache-dir "${{ env.COMPOSER_CACHE }}" - name: Prepare composer cache - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: ${{ env.COMPOSER_CACHE }} key: composer-${{ env.COMPOSER_VERSION }}-${{ hashFiles('**/composer.lock') }} @@ -59,15 +85,29 @@ jobs: - name: Install dependencies run: composer install + - name: Get updated PHP files + id: changed-files + uses: tj-actions/changed-files@v35 + with: + files: | + **/*.php + - name: PHPCS check - run: vendor/bin/phpcs . --runtime-set testVersion 7.4- + run: | + if [[ "${{ steps.changed-files.outputs.any_changed }}" == 'true' && "${{ github.event_name }}" == "pull_request" ]]; then + ./vendor/bin/phpcs ${{ steps.changed-files.outputs.all_changed_files }} --runtime-set testVersion 7.4- + elif [[ "${{ github.event_name }}" == "push" ]]; then + ./vendor/bin/phpcs . --runtime-set testVersion 7.4- + fi vipcs: name: vipcs runs-on: ubuntu-latest + steps: - name: Checkout uses: actions/checkout@v2 + - name: VIPCS check uses: 10up/wpcs-action@stable with: diff --git a/includes/Classifai/Blocks/recommended-content-block/frontend.js b/includes/Classifai/Blocks/recommended-content-block/frontend.js index d0bdfe451..52b8b20ba 100644 --- a/includes/Classifai/Blocks/recommended-content-block/frontend.js +++ b/includes/Classifai/Blocks/recommended-content-block/frontend.js @@ -6,8 +6,8 @@ function setupRewardCall( blockId ) { const contentLinks = document.querySelectorAll( `#${ blockId } .classifai-send-reward` ); - contentLinks.forEach( function ( contentLink ) { - contentLink.addEventListener( 'click', function ( event ) { + contentLinks.forEach( function( contentLink ) { + contentLink.addEventListener( 'click', function( event ) { event.preventDefault(); const eventId = this.getAttribute( 'data-eventid' ); const isRewarded = this.getAttribute( 'data-rewarded' ); @@ -44,18 +44,18 @@ function classifaiSessionSet( key, value, expirationInMin ) { key, JSON.stringify( { expiresAt: new Date( - new Date().getTime() + 60000 * expirationInMin + new Date().getTime() + ( 60000 * expirationInMin ) ), value, } ) ); } -document.addEventListener( 'DOMContentLoaded', function () { +document.addEventListener( 'DOMContentLoaded', function() { const classifaiBlocks = document.querySelectorAll( '.classifai-recommended-block-wrap' ); - classifaiBlocks.forEach( function ( classifaiBlock ) { + classifaiBlocks.forEach( function( classifaiBlock ) { const blockId = classifaiBlock.getAttribute( 'id' ); const cached = classifaiSessionGet( blockId ); if ( cached !== null ) { diff --git a/includes/Classifai/Blocks/recommended-content-block/inspector-controls/taxonomy-controls.js b/includes/Classifai/Blocks/recommended-content-block/inspector-controls/taxonomy-controls.js index 2c353b9a7..b1c983d41 100644 --- a/includes/Classifai/Blocks/recommended-content-block/inspector-controls/taxonomy-controls.js +++ b/includes/Classifai/Blocks/recommended-content-block/inspector-controls/taxonomy-controls.js @@ -16,7 +16,11 @@ const termsPerPage = 100; const getTermIdByTermValue = ( termsMappedByName, termValue ) => { // First we check for exact match by `term.id` or case sensitive `term.name` match. const termId = termValue?.id || termsMappedByName[ termValue ]?.id; - if ( termId ) return termId; + + if ( termId ) { + return termId; + } + /** * Here we make an extra check for entered terms in a non case sensitive way, * to match user expectations, due to `FormTokenField` behaviour that shows @@ -57,14 +61,22 @@ const TaxonomyControls = ( { onChange, query } ) => { const taxonomyInfo = taxonomiesInfo.find( ( { slug } ) => slug === taxonomySlug ); - if ( ! taxonomyInfo ) return; + + if ( ! taxonomyInfo ) { + return; + } + const termIds = Array.from( newTermValues.reduce( ( accumulator, termValue ) => { const termId = getTermIdByTermValue( taxonomyInfo.terms.mapByName, termValue ); - if ( termId ) accumulator.add( termId ); + + if ( termId ) { + accumulator.add( termId ); + } + return accumulator; }, new Set() ) ); @@ -81,7 +93,11 @@ const TaxonomyControls = ( { onChange, query } ) => { const taxonomyInfo = taxonomiesInfo.find( ( { slug } ) => slug === taxonomySlug ); - if ( ! taxonomyInfo ) return []; + + if ( ! taxonomyInfo ) { + return []; + } + return ( query.taxQuery?.[ taxonomySlug ] || [] ).reduce( ( accumulator, termId ) => { const term = taxonomyInfo.terms.mapById[ termId ]; diff --git a/includes/Classifai/Blocks/recommended-content-block/utils.js b/includes/Classifai/Blocks/recommended-content-block/utils.js index e1ac018ee..c78d67f41 100644 --- a/includes/Classifai/Blocks/recommended-content-block/utils.js +++ b/includes/Classifai/Blocks/recommended-content-block/utils.js @@ -44,7 +44,10 @@ export const usePostTypes = () => { return filteredPostTypes; }, [] ); const postTypesTaxonomiesMap = useMemo( () => { - if ( ! postTypes?.length ) return; + if ( ! postTypes?.length ) { + return; + } + // eslint-disable-next-line consistent-return return postTypes.reduce( ( accumulator, type ) => { accumulator[ type.slug ] = type.taxonomies; diff --git a/src/js/admin.js b/src/js/admin.js index daff91736..1c8115035 100644 --- a/src/js/admin.js +++ b/src/js/admin.js @@ -6,7 +6,9 @@ 'classifai-settings-watson_username' ); - if ( $toggler === null || $userField === null ) return; + if ( $toggler === null || $userField === null ) { + return; + } const $userFieldWrapper = $userField.closest( 'tr' ); const [ $passwordFieldTitle ] = document diff --git a/src/js/gutenberg-plugin.js b/src/js/gutenberg-plugin.js index 0e5dfa392..500032b1a 100644 --- a/src/js/gutenberg-plugin.js +++ b/src/js/gutenberg-plugin.js @@ -39,13 +39,13 @@ const ClassifAIToggle = () => { help={ 'yes' === enabled ? __( - 'ClassifAI language processing is enabled', - 'classifai' - ) + 'ClassifAI language processing is enabled', + 'classifai' + ) : __( - 'ClassifAI language processing is disabled', - 'classifai' - ) + 'ClassifAI language processing is disabled', + 'classifai' + ) } checked={ 'yes' === enabled } onChange={ ( value ) => { diff --git a/src/js/language-processing.js b/src/js/language-processing.js index 2a10c622c..db84ca463 100644 --- a/src/js/language-processing.js +++ b/src/js/language-processing.js @@ -219,7 +219,7 @@ import '../scss/language-processing.scss'; name = item.label; } - const width = 300 + 300 * rating; + const width = 300 + ( 300 * rating ); rating = ( rating * 100 ).toFixed( 2 ); name = name .split( '/' ) @@ -325,30 +325,34 @@ import '../scss/language-processing.scss'; function debounce( func, wait, immediate ) { let timeout; - return function () { + return function() { const context = this, args = arguments; /** Debounced function. */ - const later = function () { + const later = function() { timeout = null; - if ( ! immediate ) func.apply( context, args ); + if ( ! immediate ) { + func.apply( context, args ); + } }; const callNow = immediate && ! timeout; clearTimeout( timeout ); timeout = setTimeout( later, wait ); - if ( callNow ) func.apply( context, args ); + if ( callNow ) { + func.apply( context, args ); + } }; } } )(); // Display "Classify Post" button only when "Process content on update" is unchecked (Classic Editor). -document.addEventListener( 'DOMContentLoaded', function () { +document.addEventListener( 'DOMContentLoaded', function() { const classifaiNLUCheckbox = document.getElementById( '_classifai_process_content' ); if ( classifaiNLUCheckbox ) { - classifaiNLUCheckbox.addEventListener( 'change', function () { + classifaiNLUCheckbox.addEventListener( 'change', function() { const classifyButton = document.querySelector( '.classifai-clasify-post-wrapper' ); diff --git a/src/js/media.js b/src/js/media.js index 7f1136075..d13934b0e 100644 --- a/src/js/media.js +++ b/src/js/media.js @@ -2,7 +2,7 @@ import { handleClick } from './helpers'; -( function ( $ ) { +( function( $ ) { // eslint-disable-line wrap-iife const { __ } = wp.i18n; /** @@ -166,9 +166,9 @@ import { handleClick } from './helpers'; } ); }; - $( document ).ready( function () { + $( document ).ready( function() { if ( wp.media ) { - wp.media.view.Modal.prototype.on( 'open', function () { + wp.media.view.Modal.prototype.on( 'open', function() { wp.media.frame.on( 'selection:toggle', handleButtonsClick ); wp.media.frame.on( 'selection:toggle', checkPdfReadStatus ); } );