API Reference

Complete method-by-method reference for all classes in WPZylos\Framework\Http.


Request

final class WPZylos\Framework\Http\Request

Wraps $_GET, $_POST, $_FILES, $_SERVER with sanitization helpers. Automatically removes WordPress-added slashes.

Constructor

new Request(
    array $query = [],
    array $request = [],
    array $files = [],
    array $server = [],
    ?ContextInterface $context = null
)

Static Methods

capture

public static function capture(?ContextInterface $context = null): self

Creates a request from PHP superglobals. Applies wp_unslash to $_GET and $_POST.

Input Methods

input

public function input(string $key, mixed $default = null): mixed

Get input value. Checks POST data first, then GET.

all

public function all(): array

Get all input data (merged GET + POST).

only

public function only(array $keys): array

Get only the specified keys from all input.

except

public function except(array $keys): array

Get all input except the specified keys.

has

public function has(string $key): bool

Check if a key exists in POST or GET data.

query

public function query(string $key, mixed $default = null): mixed

Get a query (GET) parameter only.

post

public function post(string $key, mixed $default = null): mixed

Get a POST parameter only.

Sanitized Input Helpers

All helpers call input() internally and apply a WordPress sanitization function.

text

public function text(string $key, string $default = ''): string

Returns sanitize_text_field() result. Strips tags, removes extra whitespace.

textarea

public function textarea(string $key, string $default = ''): string

Returns sanitize_textarea_field() result. Preserves newlines.

email

public function email(string $key, string $default = ''): string

Returns sanitize_email() result. Strips invalid email characters.

url

public function url(string $key, string $default = ''): string

Returns esc_url_raw() result. Safe for database storage.

int

public function int(string $key, int $default = 0): int

Casts to (int).

absint

public function absint(string $key, int $default = 0): int

Returns absint() result. Non-negative integer.

float

public function float(string $key, float $default = 0.0): float

Casts to (float).

bool

public function bool(string $key, bool $default = false): bool

Uses filter_var(FILTER_VALIDATE_BOOLEAN). Accepts true, 1, 'yes', 'on', etc.

html

public function html(string $key, string $default = ''): string

Returns wp_kses_post() result. Allows post-content level HTML tags.

array

public function array(string $key, array $default = []): array

Returns the value if it is an array, otherwise returns $default.

Request Info Methods

method

public function method(): string

Get the uppercase HTTP method (GET, POST, PUT, etc.).

isMethod

public function isMethod(string $method): bool

Check if the HTTP method matches. Comparison is case-insensitive.

isPost

public function isPost(): bool

Check if the request method is POST.

isGet

public function isGet(): bool

Check if the request method is GET.

isAjax

public function isAjax(): bool

Check if the request is AJAX. Checks X-Requested-With: XMLHttpRequest header OR wp_doing_ajax().

uri

public function uri(): string

Get the full request URI including query string.

path

public function path(): string

Get the request path without the query string.

public function header(string $name, ?string $default = null): ?string

Get a request header value. Header name is normalized (dashes to underscores, uppercased).

file

public function file(string $key): ?array

Get an uploaded file entry from $_FILES. Returns null if not present.

ip

public function ip(): string

Get the client IP address from REMOTE_ADDR. Falls back to 127.0.0.1.

context

public function context(): ?ContextInterface

Get the plugin context instance.


Response

final class WPZylos\Framework\Http\Response

Simple HTTP response object. WP-friendly: does not depend on PSR-7 or Symfony.

Constructor

new Response(
    string $content = '',
    int $statusCode = 200,
    array $headers = []
)

Static Factory Methods

html

public static function html(string $html, int $status = 200): self

Create an HTML response. Sets Content-Type: text/html; charset=utf-8.

json

public static function json(mixed $data, int $status = 200): self

Create a JSON response. Sets Content-Type: application/json; charset=utf-8. Throws \JsonException on encoding failure.

redirect

public static function redirect(string $url, int $status = 302): self

Create a redirect response. URL is sanitized via esc_url_raw().

empty

public static function empty(int $status = 204): self

Create an empty response with no body.

error

public static function error(string $message, int $status = 500): self

Create a JSON error response: {"error": "message"}. Wraps json() internally.

Instance Methods

getContent

public function getContent(): string

Get the response body.

setContent

public function setContent(string $content): self

Set the response body. Returns $this for chaining.

getStatusCode

public function getStatusCode(): int

Get the HTTP status code.

setStatusCode

public function setStatusCode(int $statusCode): self

Set the HTTP status code. Returns $this for chaining.

getHeaders

public function getHeaders(): array

Get all response headers as an associative array.

header

public function header(string $name, string $value): self

Set a response header. Returns $this for chaining.

send

public function send(): void

Send headers (if not already sent) and echo the response body.

sendAndExit

public function sendAndExit(): void

Call send() then exit.


Pipeline

class WPZylos\Framework\Http\Pipeline

Middleware pipeline. Executes middleware in sequence, passing a request through each.

Constructor

new Pipeline(ContainerInterface $container)

$container - PSR-11 container for resolving string middleware class names.

Methods

send

public function send(Request $request): static

Set the request to pipe through middleware.

through

public function through(array $middleware): static

Set the middleware stack. Accepts:

  • Class name strings (resolved from container)
  • MiddlewareInterface instances
  • Callables (closures or invokable objects)

then

public function then(callable $destination): Response

Run the pipeline with a final destination handler. The destination receives a Request and should return a Response.

Non-Response return values are automatically converted:

Return TypeConverted To
ResponseUsed as-is
stringResponse::html($string)
arrayResponse::json($array)
nullResponse::empty()

Throws RuntimeException if no request was set via send().


MiddlewareInterface

interface WPZylos\Framework\Http\Contracts\MiddlewareInterface

Methods

handle

public function handle(Request $request, callable $next): Response

Handle the request. Call $next($request) to pass to the next middleware in the pipeline.


HttpServiceProvider

class WPZylos\Framework\Http\HttpServiceProvider extends ServiceProvider

Registers HTTP components with the application container.

Methods

register

public function register(ApplicationInterface $app): void

Registers the following bindings:

BindingTypeDescription
Request::classSingletonCurrent HTTP request captured from superglobals
'request'SingletonAlias for Request::class
Pipeline::classFactoryNew instance each resolution