From 9c06b1af9a302a50abd0c4ec7faa4d8fa478f591 Mon Sep 17 00:00:00 2001 From: Darin Kotter Date: Fri, 10 Mar 2023 12:58:53 -0700 Subject: [PATCH 01/10] Update linting workflow to use our wp-script lint command. Change a few other things in the Action, like updating various Actions and adding in caching --- .github/workflows/lint.yml | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 0e0cfd979..baecca442 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -19,16 +19,42 @@ jobs: 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 + cache: 'npm' + + - name: Install Node dependencies + if: steps.cache-node-modules.outputs.cache-hit != 'true' run: npm install - - name: eslint - uses: icrawl/action-eslint@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Generate linting report + run: npm run lint:js -- --output-file eslint-report.json --format json + continue-on-error: true + + - name: Annotate code linting results + uses: ataylorme/eslint-annotate-action@v2 + with: + repo-token: '${{ secrets.GITHUB_TOKEN }}' + report-json: 'eslint-report.json' + + - name: Update summary + run: | + npm_config_yes=true npx github:10up/eslint-json-to-md --path ./eslint-report.json --output ./eslint-report.md + cat eslint-report.md >> $GITHUB_STEP_SUMMARY + if: ${{ failure() }} + phpcs: name: phpcs runs-on: ubuntu-latest From 0e80cd612610bd94752c482ddaf918029ee38ca1 Mon Sep 17 00:00:00 2001 From: Darin Kotter Date: Fri, 10 Mar 2023 13:04:57 -0700 Subject: [PATCH 02/10] Fix spacing --- .github/workflows/lint.yml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index baecca442..9f6505fb7 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -21,13 +21,13 @@ jobs: - uses: actions/checkout@v3 - 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') }} + 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 From fb4c5e830144c2ab972bc9f61d8f9598a9b9366c Mon Sep 17 00:00:00 2001 From: Darin Kotter Date: Fri, 10 Mar 2023 13:17:51 -0700 Subject: [PATCH 03/10] Change annotate action version --- .github/workflows/lint.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 9f6505fb7..afc8c956f 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -44,10 +44,10 @@ jobs: continue-on-error: true - name: Annotate code linting results - uses: ataylorme/eslint-annotate-action@v2 + uses: ataylorme/eslint-annotate-action@1.2.0 with: - repo-token: '${{ secrets.GITHUB_TOKEN }}' - report-json: 'eslint-report.json' + repo-token: "${{ secrets.GITHUB_TOKEN }}" + report-json: "eslint-report.json" - name: Update summary run: | From 056386db29c03df87024bf6ef35e9b51ba62795f Mon Sep 17 00:00:00 2001 From: Darin Kotter Date: Fri, 10 Mar 2023 13:21:02 -0700 Subject: [PATCH 04/10] Start fixing eslint errors, leaving some in place to see if they are caught --- .github/workflows/lint.yml | 2 +- src/js/media.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index afc8c956f..b701a2a55 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -44,7 +44,7 @@ jobs: continue-on-error: true - name: Annotate code linting results - uses: ataylorme/eslint-annotate-action@1.2.0 + uses: ataylorme/eslint-annotate-action@v2 with: repo-token: "${{ secrets.GITHUB_TOKEN }}" report-json: "eslint-report.json" diff --git a/src/js/media.js b/src/js/media.js index 7f1136075..e1d3c91ea 100644 --- a/src/js/media.js +++ b/src/js/media.js @@ -2,7 +2,7 @@ import { handleClick } from './helpers'; -( function ( $ ) { +( function( $ ) { const { __ } = wp.i18n; /** From 0f796a7e3fe8f493b0343de855a07e0e7fe25b70 Mon Sep 17 00:00:00 2001 From: Darin Kotter Date: Fri, 10 Mar 2023 13:27:59 -0700 Subject: [PATCH 05/10] Update lint workflow a bit more to see if it catches existing issues better --- .github/workflows/lint.yml | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index b701a2a55..4937068df 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -17,6 +17,7 @@ jobs: eslint: name: eslint runs-on: ubuntu-latest + steps: - uses: actions/checkout@v3 @@ -37,24 +38,12 @@ jobs: - name: Install Node dependencies if: steps.cache-node-modules.outputs.cache-hit != 'true' - run: npm install + run: npm ci --no-optional - - name: Generate linting report - run: npm run lint:js -- --output-file eslint-report.json --format json + - name: Run JS linting + run: npm run lint:js continue-on-error: true - - name: Annotate code linting results - uses: ataylorme/eslint-annotate-action@v2 - with: - repo-token: "${{ secrets.GITHUB_TOKEN }}" - report-json: "eslint-report.json" - - - name: Update summary - run: | - npm_config_yes=true npx github:10up/eslint-json-to-md --path ./eslint-report.json --output ./eslint-report.md - cat eslint-report.md >> $GITHUB_STEP_SUMMARY - if: ${{ failure() }} - phpcs: name: phpcs runs-on: ubuntu-latest From 12c3b8d707b27d20b0700896d7b013063e9cb21b Mon Sep 17 00:00:00 2001 From: Darin Kotter Date: Fri, 10 Mar 2023 13:31:07 -0700 Subject: [PATCH 06/10] Don't continue on error --- .github/workflows/lint.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 4937068df..1337ec130 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -42,7 +42,6 @@ jobs: - name: Run JS linting run: npm run lint:js - continue-on-error: true phpcs: name: phpcs From 239c6407bcc551318b572d014394c8fa8c532f18 Mon Sep 17 00:00:00 2001 From: Darin Kotter Date: Fri, 10 Mar 2023 13:48:50 -0700 Subject: [PATCH 07/10] Fix all eslint errors. Update linting workflows to only run when needed. --- .github/workflows/lint.yml | 34 ++++++++++++++++--- .../recommended-content-block/frontend.js | 10 +++--- .../inspector-controls/taxonomy-controls.js | 24 ++++++++++--- .../Blocks/recommended-content-block/utils.js | 5 ++- src/js/admin.js | 4 ++- src/js/gutenberg-plugin.js | 12 +++---- src/js/language-processing.js | 18 ++++++---- src/js/media.js | 6 ++-- 8 files changed, 82 insertions(+), 31 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 1337ec130..afb47338b 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -40,8 +40,20 @@ jobs: 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: npm run lint:js + 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 @@ -49,14 +61,14 @@ jobs: 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') }} @@ -73,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 e1d3c91ea..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 ); } ); From 3afe4b935067cce9d75b15720a174112f4d1261b Mon Sep 17 00:00:00 2001 From: Darin Kotter Date: Fri, 10 Mar 2023 13:50:59 -0700 Subject: [PATCH 08/10] Fix spacing --- .github/workflows/lint.yml | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index afb47338b..b693ea875 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -41,11 +41,11 @@ jobs: run: npm ci --no-optional - name: Get updated JS files - id: changed-files - uses: tj-actions/changed-files@v35 - with: - files: | - **/*.js + id: changed-files + uses: tj-actions/changed-files@v35 + with: + files: | + **/*.js - name: Run JS linting run: | @@ -86,11 +86,11 @@ jobs: run: composer install - name: Get updated PHP files - id: changed-files - uses: tj-actions/changed-files@v35 - with: - files: | - **/*.php + id: changed-files + uses: tj-actions/changed-files@v35 + with: + files: | + **/*.php - name: PHPCS check run: | From 54b290e56ab82b60c262ddcce638c335067c00b6 Mon Sep 17 00:00:00 2001 From: Darin Kotter Date: Fri, 10 Mar 2023 13:54:47 -0700 Subject: [PATCH 09/10] Add issues to make sure checks work --- includes/Classifai/Plugin.php | 2 +- src/js/media.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/Classifai/Plugin.php b/includes/Classifai/Plugin.php index 6eee6605e..bee4b9549 100644 --- a/includes/Classifai/Plugin.php +++ b/includes/Classifai/Plugin.php @@ -121,7 +121,7 @@ public function init_services() { * @since 1.4.0 */ public function init_admin_helpers() { - if ( ! empty( $this->admin_helpers ) ) { + if (! empty( $this->admin_helpers ) ) { return; } diff --git a/src/js/media.js b/src/js/media.js index d13934b0e..687cbae40 100644 --- a/src/js/media.js +++ b/src/js/media.js @@ -168,7 +168,7 @@ import { handleClick } from './helpers'; $( 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 ); } ); From 31503bcb39b1519434efd08c92bb75d5b7b84f67 Mon Sep 17 00:00:00 2001 From: Darin Kotter Date: Fri, 10 Mar 2023 13:56:41 -0700 Subject: [PATCH 10/10] Revert errors --- includes/Classifai/Plugin.php | 2 +- src/js/media.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/Classifai/Plugin.php b/includes/Classifai/Plugin.php index bee4b9549..6eee6605e 100644 --- a/includes/Classifai/Plugin.php +++ b/includes/Classifai/Plugin.php @@ -121,7 +121,7 @@ public function init_services() { * @since 1.4.0 */ public function init_admin_helpers() { - if (! empty( $this->admin_helpers ) ) { + if ( ! empty( $this->admin_helpers ) ) { return; } diff --git a/src/js/media.js b/src/js/media.js index 687cbae40..d13934b0e 100644 --- a/src/js/media.js +++ b/src/js/media.js @@ -168,7 +168,7 @@ import { handleClick } from './helpers'; $( 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 ); } );