From dacdd9712471bf77145cabddf667bb456bfbbe72 Mon Sep 17 00:00:00 2001 From: Chris Christoff Date: Tue, 17 Sep 2024 16:10:40 -0700 Subject: [PATCH 1/2] Replace fetch with wp.ajax.post Fixes #125 This PR was written by AI and has not been reviewed yet for accuracy --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/WordPress/plugin-check/issues/125?shareId=XXXX-XXXX-XXXX-XXXX). --- assets/js/plugin-check-admin.js | 93 ++++++++++----------------------- 1 file changed, 29 insertions(+), 64 deletions(-) diff --git a/assets/js/plugin-check-admin.js b/assets/js/plugin-check-admin.js index e56abf341..d20f77faf 100644 --- a/assets/js/plugin-check-admin.js +++ b/assets/js/plugin-check-admin.js @@ -118,26 +118,14 @@ * @param {Object} data Data object with props passed to form data. */ function setUpEnvironment( data ) { - const pluginCheckData = new FormData(); - pluginCheckData.append( 'nonce', pluginCheck.nonce ); - pluginCheckData.append( 'plugin', data.plugin ); - pluginCheckData.append( - 'action', - pluginCheck.actionSetUpRuntimeEnvironment - ); - - 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 response.json(); - } ) + const pluginCheckData = { + nonce: pluginCheck.nonce, + plugin: data.plugin, + action: pluginCheck.actionSetUpRuntimeEnvironment, + checks: data.checks + }; + + return wp.ajax.post( pluginCheck.actionSetUpRuntimeEnvironment, pluginCheckData ) .then( handleDataErrors ) .then( ( responseData ) => { if ( ! responseData.data || ! responseData.data.message ) { @@ -158,21 +146,12 @@ * @return {Object} The response data. */ function cleanUpEnvironment() { - const pluginCheckData = new FormData(); - pluginCheckData.append( 'nonce', pluginCheck.nonce ); - pluginCheckData.append( - 'action', - pluginCheck.actionCleanUpRuntimeEnvironment - ); + const pluginCheckData = { + nonce: pluginCheck.nonce, + action: pluginCheck.actionCleanUpRuntimeEnvironment + }; - return fetch( ajaxurl, { - method: 'POST', - credentials: 'same-origin', - body: pluginCheckData, - } ) - .then( ( response ) => { - return response.json(); - } ) + return wp.ajax.post( pluginCheck.actionCleanUpRuntimeEnvironment, pluginCheckData ) .then( handleDataErrors ) .then( ( responseData ) => { if ( ! responseData.data || ! responseData.data.message ) { @@ -189,28 +168,20 @@ * @since 1.0.0 */ function getChecksToRun() { - const pluginCheckData = new FormData(); - pluginCheckData.append( 'nonce', pluginCheck.nonce ); - pluginCheckData.append( 'plugin', pluginsList.value ); - pluginCheckData.append( 'action', pluginCheck.actionGetChecksToRun ); + const pluginCheckData = { + nonce: pluginCheck.nonce, + plugin: pluginsList.value, + action: pluginCheck.actionGetChecksToRun, + categories: [] + }; for ( let i = 0; i < categoriesList.length; i++ ) { if ( categoriesList[ i ].checked ) { - pluginCheckData.append( - 'categories[]', - categoriesList[ i ].value - ); + pluginCheckData.categories.push( categoriesList[ i ].value ); } } - return fetch( ajaxurl, { - method: 'POST', - credentials: 'same-origin', - body: pluginCheckData, - } ) - .then( ( response ) => { - return response.json(); - } ) + return wp.ajax.post( pluginCheck.actionGetChecksToRun, pluginCheckData ) .then( handleDataErrors ) .then( ( responseData ) => { if ( @@ -286,20 +257,14 @@ * @return {Object} The check results. */ function runCheck( plugin, check ) { - const pluginCheckData = new FormData(); - pluginCheckData.append( 'nonce', pluginCheck.nonce ); - pluginCheckData.append( 'plugin', plugin ); - pluginCheckData.append( 'checks[]', check ); - pluginCheckData.append( 'action', pluginCheck.actionRunChecks ); - - return fetch( ajaxurl, { - method: 'POST', - credentials: 'same-origin', - body: pluginCheckData, - } ) - .then( ( response ) => { - return response.json(); - } ) + const pluginCheckData = { + nonce: pluginCheck.nonce, + plugin: plugin, + checks: [ check ], + action: pluginCheck.actionRunChecks + }; + + return wp.ajax.post( pluginCheck.actionRunChecks, pluginCheckData ) .then( handleDataErrors ) .then( ( responseData ) => { // If the response is successful and there is no message in the response. From e270291a6c0d476fd96592c9c50cefb2436921fe Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Wed, 18 Sep 2024 12:21:34 +0545 Subject: [PATCH 2/2] Update JS code with formatter --- assets/js/plugin-check-admin.js | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/assets/js/plugin-check-admin.js b/assets/js/plugin-check-admin.js index d20f77faf..81318eb3e 100644 --- a/assets/js/plugin-check-admin.js +++ b/assets/js/plugin-check-admin.js @@ -122,10 +122,11 @@ nonce: pluginCheck.nonce, plugin: data.plugin, action: pluginCheck.actionSetUpRuntimeEnvironment, - checks: data.checks + checks: data.checks, }; - return wp.ajax.post( pluginCheck.actionSetUpRuntimeEnvironment, pluginCheckData ) + return wp.ajax + .post( pluginCheck.actionSetUpRuntimeEnvironment, pluginCheckData ) .then( handleDataErrors ) .then( ( responseData ) => { if ( ! responseData.data || ! responseData.data.message ) { @@ -148,10 +149,14 @@ function cleanUpEnvironment() { const pluginCheckData = { nonce: pluginCheck.nonce, - action: pluginCheck.actionCleanUpRuntimeEnvironment + action: pluginCheck.actionCleanUpRuntimeEnvironment, }; - return wp.ajax.post( pluginCheck.actionCleanUpRuntimeEnvironment, pluginCheckData ) + return wp.ajax + .post( + pluginCheck.actionCleanUpRuntimeEnvironment, + pluginCheckData + ) .then( handleDataErrors ) .then( ( responseData ) => { if ( ! responseData.data || ! responseData.data.message ) { @@ -172,7 +177,7 @@ nonce: pluginCheck.nonce, plugin: pluginsList.value, action: pluginCheck.actionGetChecksToRun, - categories: [] + categories: [], }; for ( let i = 0; i < categoriesList.length; i++ ) { @@ -181,7 +186,8 @@ } } - return wp.ajax.post( pluginCheck.actionGetChecksToRun, pluginCheckData ) + return wp.ajax + .post( pluginCheck.actionGetChecksToRun, pluginCheckData ) .then( handleDataErrors ) .then( ( responseData ) => { if ( @@ -259,12 +265,13 @@ function runCheck( plugin, check ) { const pluginCheckData = { nonce: pluginCheck.nonce, - plugin: plugin, + plugin, checks: [ check ], - action: pluginCheck.actionRunChecks + action: pluginCheck.actionRunChecks, }; - return wp.ajax.post( pluginCheck.actionRunChecks, pluginCheckData ) + return wp.ajax + .post( pluginCheck.actionRunChecks, pluginCheckData ) .then( handleDataErrors ) .then( ( responseData ) => { // If the response is successful and there is no message in the response.