NEU-Libraries / drs-toolkit-wp-plugin

This repository is for the layer of the CERES: Exhibit Toolkit that consumes data from the DRS API and presents it through a Wordpress front end. The primary plugin provides search, browse, and display of items from the DRS.
13 stars 11 forks source link

Refactor shortcodes.php #276

Open patrickmj opened 9 months ago

patrickmj commented 9 months ago

Estimated Time the initial estimate of the time required to completion

20+ hours

Description what the issue is about

That file contains a lot of the work to convert a shortcode into the query to the DRS. It needs to more modular, with particular emphasis on separation of responsibilities, with the URL for GET query being built up in distinct pieces.

The parameters for the query string are currently passed along in $_POST. The ability to continue using that will have to persist. However, for future CERES, we also need to be able to construct the same query string from data that does not arrive by POST. The same data will be present, just delivered via a different mechanism TBD.

For both situations, though, separations of responsibility will be essential.

Sub tasks briefly and descriptively add sub tasks, which may be converted to issues

drashti-NEU commented 9 months ago

Following are the subtasks/steps that can be followed to address the modularization and separation of responsibilities in the given code :-

  1. Refactor Query Building Logic:

    • Identify the logic responsible for building the query string. Refactor the logic into a separate function or class method.
  2. Separate Query Parameter Handling:

    • Separate the handling of query parameters from the main logic. Create a function or class method specifically for processing and validating query parameters which may just take the raw input (from $_POST or any other source) and return a structured set of parameters.
  3. Handle AJAX Request and Non-AJAX Request Separately:

    • Identify the parts of the code that handle AJAX requests and those that might handle non-AJAX requests. Create separate functions or methods to handle these two scenarios. For both cases, make use of the common query building and parameter handling logic to avoid redundancy.
  4. Ensure Data Sanitization and Validation:

    • Implement robust data sanitization and validation for all input parameters.
  5. Proper Commenting in the code:

    • Add inline comments to explain the purpose and functionality of each function of the refactored code.
  6. Test the Refactored Code:

    • Create test cases to validate that the refactored code performs as expected. Test both AJAX and non-AJAX scenarios with various input parameters. Ensure that the refactored code produces the correct query strings and handles errors gracefully.
  7. Update Dependencies and Libraries:

    • Check for updates to any third-party libraries or dependencies used in the code. Update these dependencies to their latest versions, ensuring compatibility and security.
drashti-NEU commented 8 months ago

The shortcodes.php file is successfully refactored and the changes are committed to shortcode-refactor branch. Following are the key changes applied :-

This refactoring makes the code more optimized, adaptable and easier to maintain, especially as the complexity of the application grows or as new features are added.