Conversation
…ved asset management
… plugin prefix parameters for enhanced flexibility
… functionality and maintainability
…proved flexibility in template management
… support for request body and content type resolution
… allowing for more flexible API interactions and improved handling of endpoint-specific body parameters.
… standardizing method documentation and ensuring consistent formatting across the class.
There was a problem hiding this comment.
Pull request overview
This PR modernizes several WPToolkit utilities by expanding HTTP request handling (separating query params vs request body), adding manual registration/enqueue controls for scripts/styles, and refactoring the view/template system to use a dedicated ViewHelper plus improved template override resolution.
Changes:
- Refactors
APIHelperrequest/make_request to accept a dedicated$bodypayload and introduces content-type-aware body preparation. - Extends
EnqueueManagerwith methods to register assets/groups without enqueuing and to enqueue already-registered handles. - Introduces
ViewHelperfor templates and updatesViewLoaderto support theme overrides; adjusts frontend page template/callback behavior.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Utils/ViewLoader.php | Adds overridable theme template resolution and switches template helper injection to a ViewHelper instance. |
| src/Utils/ViewHelper.php | New helper class intended to be injected into templates as $view for loading/sections. |
| src/Utils/Page.php | Alters frontend template_include behavior for callback handling and changes default template fallback behavior. |
| src/Utils/EnqueueManager.php | Adds registration-only APIs and enqueue-by-handle APIs for previously registered scripts/styles. |
| src/Utils/APIHelper.php | Splits query params from request body, adds body preparation helpers, and updates request/make_request signatures. |
Comments suppressed due to low confidence (3)
src/Utils/Page.php:1142
getFrontendTemplate()is documented/used as returning a template path, but the fallback now returns a plain human-readable message string. WordPress will treat that string as a filename and try to include it. Revert to returning a valid template path (e.g.,createDefaultFrontendTemplate(...)) or otherwise ensure a real file is returned for thetemplate_includehook.
}
// Create default template
return "This page has no template configured. Please create a template file named '{$slug}.php' in your plugin or theme directory.";
}
src/Utils/ViewLoader.php:633
handle_template_not_found()/handle_render_error()are documented as returningfalseand always returnfalse, but their return type was widened tobool. This can be tightened back tofalseto match the docblock and make intent clearer (and avoid implyingtrueis a meaningful return value).
This issue also appears on line 642 of the same file.
private static function handle_template_not_found(string $view, bool $echo): bool
{
$error_message = "Template not found: {$view}";
if (defined('WP_DEBUG') && WP_DEBUG) {
if ($echo) {
echo "<div class=\"wptoolkit-error\">{$error_message}</div>";
}
error_log("ViewLoader: {$error_message}");
}
return false;
}
src/Utils/ViewLoader.php:654
handle_render_error()is documented as returningfalseand always returnsfalse, but its return type isbool. Consider tightening the return type back tofalseto better reflect actual behavior and intent.
private static function handle_render_error(string $view, bool $echo): bool
{
$error_message = "Error rendering template: {$view}";
if (defined('WP_DEBUG') && WP_DEBUG) {
if ($echo) {
echo "<div class=\"wptoolkit-error\">{$error_message}</div>";
}
error_log("ViewLoader: {$error_message}");
}
return false;
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+26
to
+40
| public function load(string $view, array $data = [], ?string $base_path = null, bool $overridable = null, ?string $plugin_prefix = null): string | ||
| { | ||
| $overridable = $overridable === null ? $this->overridable : $overridable; | ||
| return ViewLoader::load( | ||
| $view, | ||
| $data, | ||
| false, | ||
| $base_path ?? $this->default_base_path, | ||
| $overridable, | ||
| $plugin_prefix ?? $this->default_plugin_prefix | ||
| ) ?: ''; | ||
| } | ||
|
|
||
| public function include(string $view, array $data = [], ?string $base_path = null, bool $overridable = null, ?string $plugin_prefix = null): void | ||
| { |
Comment on lines
+1088
to
+1095
| // if callback just echo the output of the callback and return | ||
| if (isset($config['callback']) && is_callable($config['callback'])) { | ||
| // Call the callback function and return its output | ||
| ob_start(); | ||
| call_user_func($config['callback'], $config); | ||
| $output = ob_get_clean(); | ||
| echo $output; | ||
| return ''; // Return empty to prevent further template processing |
| $request_args['body'] = $params; | ||
| if ($method !== 'GET' && !empty($body)) { | ||
| $content_type = static::resolve_content_type($request_args['headers']); | ||
| $request_args['body'] = static::prepare_request_body($body, $content_type); |
|
|
||
| // Force JSON encoding by default or when content type is JSON | ||
| if ($force_json_encode || str_contains($content_type, 'application/json')) { | ||
| return json_encode($body); |
| */ | ||
| protected static function resolve_content_type(array $headers): string | ||
| { | ||
| return $headers['Content-Type'] ?? static::get_default_content_type(); |
Comment on lines
+459
to
+476
| private static function resolve_template_path(string $view, ?string $base_path = null, bool $overridable = false, string $plugin_prefix = 'wptoolkit'): string|false | ||
| { | ||
| $view = ltrim($view, '/'); | ||
|
|
||
| // Check for WordPress theme override first if overridable | ||
| if ($overridable) { | ||
| $theme_template = locate_template("{$plugin_prefix}/{$view}"); | ||
| if ($theme_template) { | ||
| return $theme_template; | ||
| } | ||
|
|
||
| // Also check with .php extension if not present | ||
| if (!pathinfo($view, PATHINFO_EXTENSION)) { | ||
| $theme_template = locate_template("{$plugin_prefix}/{$view}.php"); | ||
| if ($theme_template) { | ||
| return $theme_template; | ||
| } | ||
| } |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This pull request introduces significant enhancements to the
APIHelperandEnqueueManagerutilities, as well as improvements to the view/template loading system. The main changes include a complete overhaul of how API request bodies are handled, new methods for registering and enqueuing scripts/styles, and the introduction of a dedicatedViewHelperclass for cleaner template usage. There are also improvements to frontend page handling and template fallback logic.API Request Handling Improvements:
APIHelperto support separate handling of query parameters and request bodies, including new logic for merging, filtering, and preparing the body based on content type. This includes new methods such asprepare_request_body,get_default_content_type, andresolve_content_type, and updates to therequestandmake_requestsignatures to accept a$bodyparameter. [1] [2] [3] [4] [5] [6] [7] [8]Script and Style Registration Enhancements:
EnqueueManagerfor registering scripts and styles (individually, in groups, or by handle) without enqueuing them, as well as methods to enqueue previously registered assets and check their registration status. This enables more flexible and performant asset management.View and Template Loading Improvements:
ViewHelperclass, providing a clean API for loading, including, and managing sections in views, and integrated it into theViewLoadersystem. The view helper is now passed to templates as the$viewvariable, replacing the previous anonymous class approach. [1] [2] [3] [4]ViewLoader::loadto support plugin prefix and overridable templates, improving theme override support.Frontend Page and Template Handling:
Pageto allow direct callback output and prevent further template processing when a callback is present. Also, improved the default template fallback message. [1] [2] [3]Codebase Cleanup:
Cache) fromViewLoader.php.