class-deodar-script.php
File: lib/models/class-deodar-script.php
Package: Deodar
Since: 2.0.0
Author: Brock Cataldi
Overview
The Deodar_Script class extends Deodar_Enqueuable to provide JavaScript file management functionality. It handles the enqueuing of JavaScript files with WordPress's wp_enqueue_script function.
Class Declaration
class Deodar_Script extends Deodar_Enqueuable
Properties
$args
private array|bool $args = false;
Arguments for the script. Can be a boolean for 'in_footer' or an array of arguments.
Methods
Constructor
__construct($data)
public function __construct(array $data)
Since: 2.0.0
Parameters:
array $data- The script configuration data
Returns: void
Throws: InvalidArgumentException - If the script configuration is invalid
Inherited Fields from Deodar_Enqueuable:
handle(string, required) - The script handleurl(string, optional) - Absolute URL to the scriptfile(string, optional) - Relative file path to the scriptdependencies(array, optional) - Script dependenciesversion(string|bool|null, optional) - Script versiontemplate(string|array|null, optional) - Template restrictionsfrontend(bool, optional) - Load on frontend (default: true)backend(bool, optional) - Load on backend (default: false)
Additional Fields:
args(array|bool, optional) - Script arguments (in_footer or array of arguments)
Note: Either url or file must be provided.
Public Methods
enqueue($url_root, $end)
public function enqueue(string $url_root, bool $end): void
Since: 2.0.0
Parameters:
string $url_root- The base source URL (required if 'file' is used instead of 'url')bool $end- Which end is being loaded (true = frontend, false = backend)
Returns: void
Enqueues the script using WordPress's wp_enqueue_script function. The method:
- Checks if the script should be enqueued using
should_enqueue() - Calls
wp_enqueue_script()with the appropriate parameters - Uses the resolved URL from
get_url()
WordPress Integration
This class integrates with WordPress's script system by:
- Script Registration: Uses
wp_enqueue_script()to register and enqueue scripts - Dependency Management: Handles script dependencies automatically
- Version Control: Supports versioning for cache busting
- Footer Loading: Supports loading scripts in the footer
- Conditional Loading: Supports template-based and frontend/backend conditional loading
Configuration Validation
The constructor validates the args field and throws InvalidArgumentException for:
- Invalid
argstype (must be array, boolean, or removed)
Related Classes
Deodar_Enqueuable- Parent class providing base functionalityDeodar_Style- Sister class for stylesheet managementDeodar- Main class that manages script instances
Related Functions
wp_enqueue_script()- WordPress function used internally_deodar_get_template_name()- Used for template matching