Skip to content

Create AJAX function and triggers and handling of progress indicator for the user #34

@mehulkaklotar

Description

@mehulkaklotar

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:
    1. JS sends an AJAX POST request to admin-ajax.php with the plugin_check_run_check action.
    2. 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.
    3. 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.

Metadata

Metadata

Labels

WP AdminIssues for the WP Admin screen[Type] EnhancementA suggestion for improvement of an existing feature

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions