Wp plugin presenter

From Lonsdale201/wp-agent-skills · View interactive version

Wp plugin presenter is an AI agent skill from the GitHub repository Lonsdale201/wp-agent-skills. Design and review native presenter classes in WordPress plugins without requiring better-data - converting DTOs or domain objects into REST arrays, admin table rows, JS config payloads, email variables, and public view models with allowlisted fields, context methods, redaction by default, locale/dat

How to use this skill

Skill file

WordPress plugin: native presenters

For plugin code that turns DTOs / domain objects into output shapes. A

presenter chooses fields, computes labels, formats dates/numbers, redacts

sensitive values, and returns arrays that controllers can send to REST, AJAX,

admin tables, JS config, email templates, exports, or views.

This skill is intentionally better-data-free. If the project already uses

better-data, run bd-presenter; otherwise use this native pattern.

When to load references

Misconception this skill corrects

"The DTO already has to_array(), so the controller can return that everywhere."

Wrong. to_array() is usually the DTO's canonical data snapshot. REST output,

admin table rows, export rows, JS config, and email variables have different

audiences and redaction rules. A presenter makes those contexts explicit

instead of letting every controller hand-edit arrays.

When to use this skill

Trigger when ANY of the following is true:

Layer boundaries

| Layer | Responsibility |

|---|---|

| DTO | Normalized data. No audience-specific output. |

| Presenter | Context-specific arrays and computed fields. No DB writes. |

| Controller | Permission check, nonce/REST validation, calls presenter, sends response. |

| View/template | Escapes and echoes HTML. |

Presenter output for REST/JSON should be raw JSON-safe primitives, not

pre-escaped HTML. Presenter output for an HTML-only view may include already

escaped markup, but the method name must make that clear, e.g.

render_badge_html().

Minimal class shape

namespace MyPlugin\Presenter;

use MyPlugin\Dto\ProductDto;

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

final class ProductPresenter {
    private ProductDto $product;

    public function __construct( ProductDto $product ) {
        $this->product = $product;
    }

    /** @return array<string,mixed> */
    public function for_rest(): array {
        return array(
            'id'      => $this->product->id(),
            'title'   => $this->product->title(),
            'enabled' => $this->product->enabled(),
        );
    }

    /** @return array<string,string|int> */
    public function for_admin_table(): array {
        return array(
            'id'     => $this->product->id(),
            'title'  => $this->product->title(),
            'status' => $this->product->enabled() ? __( 'Enabled', 'my-plugin' ) : __( 'Disabled', 'my-plugin' ),
        );
    }
}

Use explicit context methods instead of a generic to_array( $context ) until

the contexts genuinely share most of the same shape. Method names make reviews

easier: for_rest(), for_admin_table(), for_export(), for_email(),

for_js_config().

Output rules by context

See references/presenter-context-patterns.md

for complete examples.

Redaction by default

Presenter methods should be public-safe by default. Sensitive values require

explicit opt-in:

public function for_admin_table(): array {
    return array(
        'name'    => $this->credential->name(),
        'api_key' => '***',
    );
}

public function for_private_admin( bool $can_reveal_secret ): array {
    if ( ! $can_reveal_secret ) {
        return $this->for_admin_table();
    }

    $api_key = $this->credential->api_key();

    return array(
        'name'    => $this->credential->name(),
        'api_key' => $api_key ? $api_key->reveal() : '',
    );
}

The controller passes $can_reveal_secret = current_user_can( 'manage_options' );

the presenter applies that already-made authorization decision. Do not create

convenience methods like reveal_all() or include secrets in a generic

for_rest() response.

Critical rules

Common mistakes

// WRONG - exposes every public property and misses redaction.
return get_object_vars( $dto );

// WRONG - REST payload contains HTML from an admin use case.
return array( 'status' => '<span class="badge">Enabled</span>' );

// WRONG - escaping too early for JSON.
return array( 'title' => esc_html( $dto->title() ) );

// WRONG - side effect in presenter.
update_option( 'myplugin_last_presented', time() );

Cross-references

What this skill does NOT cover

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.