-
Notifications
You must be signed in to change notification settings - Fork 98
Add plugin check ajax check request #91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
1a0bee1
Working on ajax fetch api
6e3698b
Add plugin check ajax check request
54bc9db
Fix phplint issue
ce20901
Fix phpunit test
d79db4e
Address feedback
2ab6968
Merge trunk and resolve conflicts
8f86b45
Fix phpunit test
556e18d
update action and method names to plural
jjgrainger 49857e4
Merge branch 'trunk' into feature/add-ajax-check-trigger
jjgrainger f08f33e
fix typo
jjgrainger aaf5847
use constant for nonce key
jjgrainger 9799130
use WP_PLUGIN_CHECK_VERSION for asset version
jjgrainger d4e4042
return page hook for the admin page
jjgrainger 9a737bd
update AJAX response and form element ids
jjgrainger af87e52
Merge branch 'trunk' into feature/add-ajax-check-trigger
jjgrainger 640b101
update AJAX response error handling
jjgrainger 0fa60eb
update response error checking
jjgrainger 1d28427
add button class for correct styles
jjgrainger 59e9724
update admin page elements
jjgrainger 0672b99
update error handling
jjgrainger 8e5261d
Merge branch 'trunk' into feature/add-ajax-check-trigger
jjgrainger 33900ed
fix lint issues
jjgrainger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| ( function ( data ) { | ||
| const checkItButton = document.getElementById( 'plugin-check__submit' ); | ||
| const pluginsList = document.getElementById( 'plugin-check__plugins-dropdown' ); | ||
|
|
||
| // Return early if the elements cannot be found on the page. | ||
| if ( ! checkItButton || ! pluginsList ) { | ||
| console.error( 'Missing form elements on page' ); | ||
| return; | ||
| } | ||
|
|
||
| checkItButton.addEventListener( 'click', (e) => { | ||
| e.preventDefault(); | ||
|
|
||
| const pluginCheckData = new FormData(); | ||
|
|
||
| // Collect the data to pass along for generating a check results. | ||
| pluginCheckData.append( 'action', 'plugin_check_run_checks' ); | ||
| pluginCheckData.append( 'nonce', data.nonce ); | ||
| pluginCheckData.append( 'plugin', pluginsList.value ); | ||
|
|
||
| fetch( | ||
| ajaxurl, | ||
| { | ||
| method: 'POST', | ||
| credentials: 'same-origin', | ||
| body: pluginCheckData | ||
| } | ||
| ) | ||
| .then( | ||
| ( response ) => { | ||
| return response.json(); | ||
| } | ||
| ) | ||
| .then( | ||
| ( data ) => { | ||
| if ( ! data ) { | ||
| throw new Error( 'Response contains no data' ); | ||
| } | ||
|
|
||
| if ( ! data.success ) { | ||
| // // If not successful and no message in the response. | ||
| 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 ); | ||
| } | ||
|
|
||
| // If the response is successful and there is no message in the response. | ||
| if ( ! data.data || ! data.data.message ) { | ||
| throw new Error( 'Response contains no data' ); | ||
| } | ||
|
|
||
| // If the response is successful and there is a message in the response. | ||
| console.log( data.data.message ); | ||
| } | ||
| ) | ||
| .catch( | ||
| ( error ) => { console.error( error ); } | ||
| ); | ||
|
|
||
| } ); | ||
|
|
||
| } )( PLUGIN_CHECK ); /* global PLUGIN_CHECK */ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| <?php | ||
| /** | ||
| * Class WordPress\Plugin_Check\Admin\Admin_AJAX | ||
| * | ||
| * @package plugin-check | ||
| */ | ||
|
|
||
| namespace WordPress\Plugin_Check\Admin; | ||
|
|
||
| use WP_Error; | ||
|
|
||
| /** | ||
| * Class to handle the Admin AJAX requests. | ||
| * | ||
| * @since n.e.x.t | ||
| */ | ||
| class Admin_AJAX { | ||
|
|
||
| /** | ||
| * Nonce key. | ||
| * | ||
| * @since n.e.x.t | ||
| * @var string | ||
| */ | ||
| const NONCE_KEY = 'plugin-check-run-checks'; | ||
|
|
||
| /** | ||
| * Registers WordPress hooks for the Admin AJAX. | ||
| * | ||
| * @since n.e.x.t | ||
| */ | ||
| public function add_hooks() { | ||
| add_action( 'wp_ajax_plugin_check_run_checks', array( $this, 'run_checks' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Creates and returns the nonce. | ||
| * | ||
| * @since n.e.x.t | ||
| */ | ||
| public function get_nonce() { | ||
| return wp_create_nonce( self::NONCE_KEY ); | ||
| } | ||
|
|
||
| /** | ||
| * Runs checks. | ||
| * | ||
| * @since n.e.x.t | ||
| */ | ||
| public function run_checks() { | ||
| $nonce = filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_STRING ); | ||
|
|
||
| if ( ! wp_verify_nonce( $nonce, self::NONCE_KEY ) ) { | ||
| wp_send_json_error( | ||
| new WP_Error( 'invalid-nonce', __( 'Invalid nonce', 'plugin-check' ) ), | ||
| 403 | ||
| ); | ||
| } | ||
|
|
||
| wp_send_json_success( | ||
| array( | ||
| 'message' => __( 'Verified!', 'plugin-check' ), | ||
| ) | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php | ||
| /** | ||
| * Tests for the Admin_AJAX class. | ||
| * | ||
| * @package plugin-check | ||
| */ | ||
|
|
||
| namespace Admin; | ||
|
|
||
| use WordPress\Plugin_Check\Admin\Admin_AJAX; | ||
| use WP_UnitTestCase; | ||
|
|
||
| class Admin_AJAX_Tests extends WP_UnitTestCase { | ||
|
|
||
| protected $admin_ajax; | ||
|
|
||
| public function set_up() { | ||
| parent::set_up(); | ||
| $this->admin_ajax = new Admin_AJAX(); | ||
| } | ||
|
|
||
| public function test_add_hooks() { | ||
| $this->admin_ajax->add_hooks(); | ||
| $this->assertEquals( 10, has_action( 'wp_ajax_plugin_check_run_checks', array( $this->admin_ajax, 'run_checks' ) ) ); | ||
| } | ||
|
|
||
| public function test_get_nonce() { | ||
| $this->assertNotFalse( | ||
| wp_verify_nonce( $this->admin_ajax->get_nonce(), Admin_AJAX::NONCE_KEY ) | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before checking this, we should check whether
dataeven is an object, particularly when removing the aboveresponse.okcheck. If the response is empty, then this line here would cause a JS error.