You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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_AJAX file should exist at includes/Admin
Take output of the add_management_page() function available in the add_page() method of the Admin_Page class in to $hook variable. (Admin_Page class introduced in Create Tools page #32)
Add hook on load-{$hook} and attach the initialize_page method to it in the Admin_Page class.
initialize_page() method will have the admin_enqueue_scripts hook and callback method will be enqueue_scripts.
Register new protected variable called $admin_ajax in the Admin_Page class.
Add constructor() method in the Admin_Page class, and instantiate Admin_AJAX class in that.
Add the get_nonce() method in the Admin_AJAX class. It will return the nonce, which can be used in the Admin_Page class.
Admin_Page class should have the enqueue_scripts() method, which will register a new custom JS with the name plugin-check-admin.js, and this JS file will be placed at the top level of the plugin's directory at plugin-check/assets.
The plugin-check-admin.js script should expose two PHP variables: the admin-ajax URL and nonce (which we get from Admin_AJAX->get_nonce()).
Admin_AJAX will have add_hooks() method.
Admin_AJAX will have run_check() method.
Admin_AJAX->add_hooks() method will hook the ajax call with the run_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 POST request to admin-ajax.php with the plugin_check_run_check action.
plugin_check_run_check action will call the run_check() method.
The run_check() method gets the variables as below:
nonce - Basically, it is nonce to verify it with the check_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.
JS will get the details of the success or error messages and display them in the console.
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.js script is enqueued only on the plugin-check page.
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
Admin_AJAXfile should exist atincludes/Adminadd_management_page()function available in theadd_page()method of theAdmin_Pageclass in to$hookvariable. (Admin_Page class introduced in Create Tools page #32)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.$admin_ajaxin theAdmin_Pageclass.constructor()method in theAdmin_Pageclass, and instantiateAdmin_AJAXclass in that.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.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.Check it!button (button details available at Create Tools page #32), the below process should start:POSTrequest toadmin-ajax.phpwith theplugin_check_run_checkaction.plugin_check_run_checkaction will call therun_check()method.run_check()method gets the variables as below:nonce- Basically, it is nonce to verify it with thecheck_ajax_referer()function.run_check()method will now respond to the success message if the nonce is valid else, return an error message in the JSON format.Test Coverage
Admin_AJAX class
Admin_Page->initialize_page()method to check JS script only loads on the plugin-check.php only.Admin_AJAX->add_hooks()method.Admin_AJAX->get_nonce()method.Admin_AJAX->run_check()method.Admin_Page class
plugin-check-admin.jsscript is enqueued only on theplugin-checkpage.