PHPackages                             helsingborg-stad/wputilservice - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. helsingborg-stad/wputilservice

ActiveLibrary

helsingborg-stad/wputilservice
==============================

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.

0.2.48(5mo ago)04.0k↓69.6%[3 PRs](https://github.com/helsingborg-stad/wputilservice/pulls)20MITPHPPHP ^8.2CI passing

Since Sep 2Pushed 3mo agoCompare

[ Source](https://github.com/helsingborg-stad/wputilservice)[ Packagist](https://packagist.org/packages/helsingborg-stad/wputilservice)[ RSS](/packages/helsingborg-stad-wputilservice/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (60)Used By (20)

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?
----------------

[](#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., `EnqueueManager` for scripts). Managers can contain multiple private/protected methods, helpers, or additional classes (like `CacheBuster`) 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
------------

[](#installation)

Add `WpUtilService` to your project using PSR-4 autoloading with composer:

```
composer require helsingborg-stad/wputilservice
```

---

Usage
-----

[](#usage)

### Basic Setup

[](#basic-setup)

```
use WpService\NativeWpService;
use WpUtilService\WpUtilService;

$wpService = new NativeWpService();
$wpUtilService = new WpUtilService($wpService);
```

---

### Enqueue Scripts

[](#enqueue-scripts)

- `enqueue()` returns an `EnqueueManager`.
- `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 to `with()` but cannot be called before `with()`.
- 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-1)

```
$wpUtilService
    ->enqueue(__DIR__)
    ->on('wp_enqueue_scripts', 20)
    ->add('main.js', ['jquery'])
    ->with()->translation(
        'objectName',
        ['localization_a' => __('Test', 'testdomain')]
    )->and()->data(
        'objectName',
        ['id' => 1]
    );
```

#### Example 2 (alternative sytax)

[](#example-2-alternative-sytax)

```
$wpUtilService
    ->enqueue(__DIR__)
    ->add('main.js', ['jquery'])
    ->with('translation', 'objectName', ['localization_a' => __('Test', 'testdomain')])
    ->and('data', 'objectName', ['id' => 1]);
```

---

#### Example 3 (alternative sytax)

[](#example-3-alternative-sytax)

```
$wpUtilService
    ->enqueue(__DIR__)
    ->add('main.js', ['jquery'])
    ->with('translation', 'objectName', ['localization_a' => __('Test', 'testdomain')])
    ->with('data', 'objectName', ['id' => 1]);
```

---

#### Example 4 (chaining)

[](#example-4-chaining)

```
$wpUtilService
    ->enqueue(__DIR__)
    ->add('main.js', ['jquery'])
    ->with('data', 'objectName', ['id' => 1]);
    ->add('main.css')
    ->add('styleguide.css');
```

---

#### Example 5

[](#example-5)

```
$enqueue = $wpUtilService->enqueue(__DIR__);
$enqueue->add('main.js', ['jquery']);
$enqueue->add('main.css');
```

---

### Adding New Features

[](#adding-new-features)

To add a new feature:

1. **Create a trait** with a single public method representing the entrypoint.
2. **Create a manager class** for that feature with all operations and private helpers.
3. **Use the trait** in `WpUtilService`.
4. Consumers interact only through the public entrypoint and manager API.

---

### Extending Managers with Helpers

[](#extending-managers-with-helpers)

Managers can leverage additional helper classes. For example, `EnqueueManager` uses the `CacheBustManager` helper:

```
$cacheBustManager = new CacheBustManager();
```

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
----------------------------

[](#diagram-of-service-structure)

 ```
flowchart TD
    A[WpUtilService] --> B[Enqueue Trait]
    A --> C[Translation Trait]
    B --> D[EnqueueManager]
    C --> E[TranslationManager]
    D --> F[CacheBuster Helper]
    D --> G[Other internal private helpers]
    E --> H[Internal private helpers]
    D --> I[WpService]
    E --> I
```

      Loading **Explanation:**

- `WpUtilService` acts 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 `CacheBuster` for extra functionality.
- Ultimately, the manager delegates to `WpService` to perform the actual WordPress operations.

---

### Advantages

[](#advantages)

- Clear, controlled interface.
- One public method per trait; easy to discover.
- Supports **fluent API** and chaining.
- Testable: swap `WpService` for mocks.
- Extensible: add helpers like cache-busting, minification, etc., without changing the service.

---

Folder Structure
----------------

[](#folder-structure)

```
src/
├─ WpUtilService.php          # Main service (facade)
├─ Traits/
│  ├─ Enqueue.php             # Trait for enqueue feature
│  ├─ Translation.php         # Trait for translation feature
├─ Features/
│  ├─ EnqueueManager.php      # Manager class for enqueue
│  ├─ TranslationManager.php  # Manager class for translation
│  ├─ CacheBuster.php         # Optional helper
├─ Contracts/
   ├─ Enqueue.php             # Interface
   ├─ Translation.php         # Interface

```

###  Health Score

45

—

FairBetter than 92% of packages

Maintenance77

Regular maintenance activity

Popularity23

Limited adoption so far

Community21

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 53.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

49

Last Release

164d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f46fe64973c3e89d15c745c0bf601c25983bafea81d30e64d4bf813a6f8c8c7c?d=identicon)[sebastianthulin](/maintainers/sebastianthulin)

---

Top Contributors

[![sebastianthulin](https://avatars.githubusercontent.com/u/797129?v=4)](https://github.com/sebastianthulin "sebastianthulin (7 commits)")[![Copilot](https://avatars.githubusercontent.com/in/1143301?v=4)](https://github.com/Copilot "Copilot (5 commits)")[![petter-a](https://avatars.githubusercontent.com/u/40427478?v=4)](https://github.com/petter-a "petter-a (1 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/helsingborg-stad-wputilservice/health.svg)

```
[![Health](https://phpackages.com/badges/helsingborg-stad-wputilservice/health.svg)](https://phpackages.com/packages/helsingborg-stad-wputilservice)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
