Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
root: true,
extends: [ 'plugin:@wordpress/eslint-plugin/recommended' ],
plugins: [ 'import' ],
globals: {
wp: 'off',
ajaxurl: 'readonly',
FormData: 'readonly',
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we revisit this, we need to import this correctly.

},
rules: {
'no-console': 'off',
'@wordpress/i18n-text-domain': [
'error',
{
allowedTextDomain: 'plugin-check',
},
],
},
};
34 changes: 34 additions & 0 deletions .github/workflows/js-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Lint JS

on:
push:
paths:
- '**.js'
- 'package-lock.json'
branches:
- trunk
pull_request:
paths:
- '**.js'
- 'package-lock.json'

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
cache: npm

- name: Install dependencies
run: npm ci

- name: JS Lint
run: npm run lint-js
173 changes: 74 additions & 99 deletions assets/js/plugin-check-admin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
( function ( pluginCheck ) {
const checkItButton = document.getElementById( 'plugin-check__submit' );
const pluginsList = document.getElementById( 'plugin-check__plugins-dropdown' );
const pluginsList = document.getElementById(
'plugin-check__plugins-dropdown'
);

// Return early if the elements cannot be found on the page.
if ( ! checkItButton || ! pluginsList ) {
Expand All @@ -12,24 +14,21 @@
e.preventDefault();

getChecksToRun()
.then( setUpEnvironment )
.then( runChecks )
.then( cleanUpEnvironment )
.then(
( data ) => {
.then( setUpEnvironment )
.then( runChecks )
.then( cleanUpEnvironment )
.then( ( data ) => {
console.log( data.message );
}
)
.catch(
( error ) => {
} )
.catch( ( error ) => {
console.error( error );
}
);
} );
} );

/**
* Setup the runtime environment if needed.
*
* @param {Object} data Data object with props passed to form data.
* @since n.e.x.t
*/
function setUpEnvironment( data ) {
Expand All @@ -38,75 +37,60 @@
pluginCheckData.append( 'plugin', data.plugin );
pluginCheckData.append( 'action', 'plugin_check_set_up_environment' );

for (var i = 0; i < data.checks.length; i++) {
for ( let i = 0; i < data.checks.length; i++ ) {
pluginCheckData.append( 'checks[]', data.checks[ i ] );
}

return fetch(
ajaxurl,
{
method: 'POST',
credentials: 'same-origin',
body: pluginCheckData
}
)
.then(
( response ) => {
return fetch( ajaxurl, {
method: 'POST',
credentials: 'same-origin',
body: pluginCheckData,
} )
.then( ( response ) => {
return response.json();
}
)
.then( handleDataErrors )
.then(
( data ) => {
if ( ! data.data || ! data.data.message ) {
} )
.then( handleDataErrors )
.then( ( responseData ) => {
if ( ! responseData.data || ! responseData.data.message ) {
throw new Error( 'Response contains no data.' );
}

console.log( data.data.message );
console.log( responseData.data.message );

return data.data;
}
);
return responseData.data;
} );
}

/**
* Cleanup the runtime environment.
*
* @since n.e.x.t
*/
function cleanUpEnvironment( data ) {
function cleanUpEnvironment() {
const pluginCheckData = new FormData();
pluginCheckData.append( 'nonce', pluginCheck.nonce );
pluginCheckData.append( 'action', 'plugin_check_clean_up_environment' );

return fetch(
ajaxurl,
{
method: 'POST',
credentials: 'same-origin',
body: pluginCheckData
}
)
.then(
( response ) => {
return fetch( ajaxurl, {
method: 'POST',
credentials: 'same-origin',
body: pluginCheckData,
} )
.then( ( response ) => {
return response.json();
}
)
.then( handleDataErrors )
.then(
( data ) => {
if ( ! data.data || ! data.data.message ) {
} )
.then( handleDataErrors )
.then( ( responseData ) => {
if ( ! responseData.data || ! responseData.data.message ) {
throw new Error( 'Response contains no data.' );
}

console.log( data.data.message );
console.log( responseData.data.message );

return data.data;
}
);
return responseData.data;
} );
}


/**
* Get the Checks to run.
*
Expand All @@ -118,35 +102,34 @@
pluginCheckData.append( 'plugin', pluginsList.value );
pluginCheckData.append( 'action', 'plugin_check_get_checks_to_run' );

return fetch(
ajaxurl,
{
method: 'POST',
credentials: 'same-origin',
body: pluginCheckData
}
)
.then(
( response ) => {
return fetch( ajaxurl, {
method: 'POST',
credentials: 'same-origin',
body: pluginCheckData,
} )
.then( ( response ) => {
return response.json();
}
)
.then( handleDataErrors )
.then(
( data ) => {
if ( ! data.data || ! data.data.plugin || ! data.data.checks ) {
throw new Error( 'Plugin and Checks are missing from the response.' );
} )
.then( handleDataErrors )
.then( ( responseData ) => {
if (
! responseData.data ||
! responseData.data.plugin ||
! responseData.data.checks
) {
throw new Error(
'Plugin and Checks are missing from the response.'
);
}

return data.data;
}
);
return responseData.data;
} );
}


/**
* Run Checks.
*
* @param {Object} data The response data.
* @since n.e.x.t
*/
function runChecks( data ) {
Expand All @@ -155,34 +138,27 @@
pluginCheckData.append( 'plugin', data.plugin );
pluginCheckData.append( 'action', 'plugin_check_run_checks' );

for (var i = 0; i < data.checks.length; i++) {
for ( let i = 0; i < data.checks.length; i++ ) {
pluginCheckData.append( 'checks[]', data.checks[ i ] );
}

return fetch(
ajaxurl,
{
method: 'POST',
credentials: 'same-origin',
body: pluginCheckData
}
)
.then(
( response ) => {
return fetch( ajaxurl, {
method: 'POST',
credentials: 'same-origin',
body: pluginCheckData,
} )
.then( ( response ) => {
return response.json();
}
)
.then( handleDataErrors )
.then(
( data ) => {
} )
.then( handleDataErrors )
.then( ( responseData ) => {
// If the response is successful and there is no message in the response.
if ( ! data.data || ! data.data.message ) {
if ( ! responseData.data || ! responseData.data.message ) {
throw new Error( 'Response contains no data' );
}

return data.data;
}
);
return responseData.data;
} );
}

/**
Expand All @@ -200,15 +176,14 @@

if ( ! data.success ) {
// If not successful and no message in the response.
if ( ! data.data || ! data.data[0].message ) {
if ( ! data.data || ! data.data[ 0 ].message ) {
throw new Error( 'Response contains no data' );
}

// If not successful and there is a message in the response.
throw new Error( data.data[0].message );
throw new Error( data.data[ 0 ].message );
}

return data;
}

} )( PLUGIN_CHECK ); /* global PLUGIN_CHECK */
Loading