-
Notifications
You must be signed in to change notification settings - Fork 83
Description
When the user triggers the checks by clicking on the “Check it” button, an admin AJAX request should be triggered to initiate the checks, and each check will run sequentially with each request in limited parameters. The response to the AJAX request will be to output checks with their respective results into the table format below the page options.
The AJAX callback function should always check for the nonces to verify the requests and should go ahead according to the verification.
There should be a visible indication of requests being made on the page for the user to see that the checks are in progress. After the successful completion of all checks, there should be an indication for the user that checks are complete and results are populated into the table. When the request has an error when running checks, it should indicate to the user that there has been some error while running the checks.
Acceptance Criteria
- Class
Admin_AJAXfile should exist atincludes/Admin - Take output of the
add_management_page()function available in theadd_page()method of theAdmin_Pageclass in to$hookvariable. (Admin_Page class introduced in Create Tools page #32) - Add hook on
load-{$hook}and attach theinitialize_pagemethod to it in theAdmin_Pageclass. initialize_page()method will have theadmin_enqueue_scriptshook and callback method will beenqueue_scripts.- Register new protected variable called
$admin_ajaxin theAdmin_Pageclass. - Add
constructor()method in theAdmin_Pageclass, and instantiateAdmin_AJAXclass in that. - Add the
get_nonce()method in theAdmin_AJAXclass. It will return the nonce, which can be used in theAdmin_Pageclass. Admin_Pageclass should have theenqueue_scripts()method, which will register a new custom JS with the nameplugin-check-admin.js, and this JS file will be placed at the top level of the plugin's directory atplugin-check/assets.- The
plugin-check-admin.jsscript should expose two PHP variables: theadmin-ajaxURL andnonce(which we get fromAdmin_AJAX->get_nonce()). Admin_AJAXwill haveadd_hooks()method.Admin_AJAXwill haverun_check()method.Admin_AJAX->add_hooks()method will hook the ajax call with therun_check()method for only logged-in users.- When the admin clicks on the
Check it!button (button details available at Create Tools page #32), the below process should start:- JS sends an AJAX
POSTrequest toadmin-ajax.phpwith theplugin_check_run_checkaction. plugin_check_run_checkaction will call therun_check()method.- The
run_check()method gets the variables as below:nonce- Basically, it is nonce to verify it with thecheck_ajax_referer()function.
- The
run_check()method will now respond to the success message if the nonce is valid else, return an error message in the JSON format.
- The
- JS will get the details of the success or error messages and display them in the console.
- JS sends an AJAX
Test Coverage
Admin_AJAX class
- Add a test for the
Admin_Page->initialize_page()method to check JS script only loads on the plugin-check.php only. - Add a test for the
Admin_AJAX->add_hooks()method. - Add a test case for the
Admin_AJAX->get_nonce()method. - Add a test for the
Admin_AJAX->run_check()method.
Admin_Page class
- Verified that the
plugin-check-admin.jsscript is enqueued only on theplugin-checkpage.