Skip to content

Dev#4

Open
codad5 wants to merge 9 commits into
mainfrom
dev
Open

Dev#4
codad5 wants to merge 9 commits into
mainfrom
dev

Conversation

@codad5
Copy link
Copy Markdown
Owner

@codad5 codad5 commented May 24, 2026

This pull request introduces significant enhancements to the APIHelper and EnqueueManager utilities, 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 dedicated ViewHelper class for cleaner template usage. There are also improvements to frontend page handling and template fallback logic.

API Request Handling Improvements:

  • Refactored APIHelper to 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 as prepare_request_body, get_default_content_type, and resolve_content_type, and updates to the request and make_request signatures to accept a $body parameter. [1] [2] [3] [4] [5] [6] [7] [8]

Script and Style Registration Enhancements:

  • Added a suite of new methods to EnqueueManager for 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:

  • Introduced a new ViewHelper class, providing a clean API for loading, including, and managing sections in views, and integrated it into the ViewLoader system. The view helper is now passed to templates as the $view variable, replacing the previous anonymous class approach. [1] [2] [3] [4]
  • Updated ViewLoader::load to support plugin prefix and overridable templates, improving theme override support.

Frontend Page and Template Handling:

  • Improved frontend page callback logic in Page to 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:

  • Removed an unused import (Cache) from ViewLoader.php.

codad5 added 9 commits August 18, 2025 11:49
… plugin prefix parameters for enhanced flexibility
… 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.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 APIHelper request/make_request to accept a dedicated $body payload and introduces content-type-aware body preparation.
  • Extends EnqueueManager with methods to register assets/groups without enqueuing and to enqueue already-registered handles.
  • Introduces ViewHelper for templates and updates ViewLoader to 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 the template_include hook.
		}

		// 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 returning false and always return false, but their return type was widened to bool. This can be tightened back to false to match the docblock and make intent clearer (and avoid implying true is 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 returning false and always returns false, but its return type is bool. Consider tightening the return type back to false to 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 thread src/Utils/ViewHelper.php
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 thread src/Utils/Page.php
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
Comment thread src/Utils/APIHelper.php
$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);
Comment thread src/Utils/APIHelper.php

// 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);
Comment thread src/Utils/APIHelper.php
*/
protected static function resolve_content_type(array $headers): string
{
return $headers['Content-Type'] ?? static::get_default_content_type();
Comment thread src/Utils/ViewLoader.php
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants