Download the PHP package helsingborg-stad/wputilservice without Composer
On this page you can find all versions of the php package helsingborg-stad/wputilservice. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download helsingborg-stad/wputilservice
More information about helsingborg-stad/wputilservice
Files in helsingborg-stad/wputilservice
Package wputilservice
Short Description Simplifies WordPress development by providing a centralized WpUtilService that exposes a WordPress api wrapped in a streamlined manner. Simplify your development workflow and enhance WordPress integration with ease.
License MIT
Informations about the package wputilservice
WpUtilService
WpUtilService is a lightweight, developer-friendly service layer for WordPress that wraps WpService. It provides a clear, controlled interface to common WordPress operations such as enqueuing scripts, adding translations, and more — all while keeping the main service clean and testable.
Why This Design?
- Single Public Entrypoint per Feature: Each feature (like enqueueing scripts or translations) is encapsulated in a trait. Traits expose exactly one public method, preventing API pollution.
- Feature Managers: Each public method returns a manager object that handles the feature's operations (e.g.,
EnqueueManagerfor scripts). Managers can contain multiple private/protected methods, helpers, or additional classes (likeCacheBuster) without exposing internal logic. - Fluent API: Methods on the manager can be chained, allowing concise and readable code.
- Separation of Concerns: The main service (
WpUtilService) remains simple, while complex logic is handled by feature-specific classes. - Testable and Extensible: The service depends on
WpService, which can be swapped for a mock or custom implementation in tests. Additional helpers (like cache-busting, minifiers, etc.) can be injected into managers.
Installation
Add WpUtilService to your project using PSR-4 autoloading with composer:
Usage
Basic Setup
Enqueue Scripts
enqueue()returns anEnqueueManager.on()wraps the following functions inside a hook (eg. wp_enqueue_script). Only documented hooks are allowed.add()enqueues a script.with()may be chained with data or translation functions.and()is a synonym towith()but cannot be called beforewith().- You can chain multiple add calls fluently. There are no need to call multiple enqueue.
- Enqueue implements the singleton pattern. This means that when you call enqueue() multiple times in succession, it will reuse the previously stored configuration instead of creating a new instance.
Example 1
Example 2 (alternative sytax)
Example 3 (alternative sytax)
Example 4 (chaining)
Example 5
Adding New Features
To add a new feature:
- Create a trait with a single public method representing the entrypoint.
- Create a manager class for that feature with all operations and private helpers.
- Use the trait in
WpUtilService. - Consumers interact only through the public entrypoint and manager API.
Extending Managers with Helpers
Managers can leverage additional helper classes. For example, EnqueueManager uses the CacheBustManager helper:
The enqueue manager keeps this internal, so the main service API remains clean. Helpers may reside the features folder, but should not have any publicly avabile api:s.
Diagram of Service Structure
Explanation:
WpUtilServiceacts as the facade.- Each trait contributes one public entrypoint (
enqueue(),translation()). - Each public method returns a manager object, which handles the feature and contains private helpers.
- Managers can call helpers like
CacheBusterfor extra functionality. - Ultimately, the manager delegates to
WpServiceto perform the actual WordPress operations.
Advantages
- Clear, controlled interface.
- One public method per trait; easy to discover.
- Supports fluent API and chaining.
- Testable: swap
WpServicefor mocks. - Extensible: add helpers like cache-busting, minification, etc., without changing the service.