TinyQuery
in package
A minimal PSR-friendly class that: Boots a short-init version of WordPress, Offers a minimal DB query method (`query`), Provides a simple router mechanism via FastRoute, Lets you define routes and dispatch them,
Table of Contents
- $booted : bool
- $dispatcher : null|Dispatcher
- $queryResults : array<string|int, mixed>
- __construct() : mixed
- boot() : void
- A tiny bootloader when using SHORTINIT.
- dispatch() : array<string|int, int|mixed|string>
- Dispatch a given HTTP method and URI to the appropriate route using the FastRoute dispatcher.
- is404() : bool
- A minimal is_404() check. We treat "404" as "no posts found.".
- query() : array<string|int, mixed>
- Minimal function to query posts from wp_posts using $wpdb and prepared statements.
- setRoutes() : void
- Set route definitions for FastRoute.
Properties
$booted
private
bool
$booted
$dispatcher
private
null|Dispatcher
$dispatcher
$queryResults
private
array<string|int, mixed>
$queryResults
Methods
__construct()
public
__construct() : mixed
Return values
mixed —boot()
A tiny bootloader when using SHORTINIT.
public
boot([callable|null $callback = null ]) : void
Checks if the SHORTINIT
constant is defined and set to true
to enable a minimal WordPress bootstrap.
Boots a minimal environment (shortinit).
SHORTINIT
is a predefined constant in WordPress that allows for a lightweight initialization of the environment.
By defining SHORTINIT
as true
before loading WordPress, themes, plugins, and many optional features
are bypassed, leading to significant performance improvements in specific use cases.
Key Characteristics of SHORTINIT
:
- Performance Optimization: By skipping non-essential components, the initialization process is faster.
- Resource Efficiency: Helps scripts that only require core WordPress features operate smoothly in environments with limited resources.
- Specific Use Cases: Suitable for tasks requiring limited WordPress functionality, such as direct database interactions or basic user handling, without the overhead of a full WordPress load.
When SHORTINIT
is set to true
:
-
Skipped Features:
- Themes and plugins
- Widgets and shortcodes
- REST API
- Localization and translation functions
-
Retained Features:
- Core settings
- The
$wpdb
object for database interactions - Basic functionality for a minimal WordPress bootstrap
Example Use Cases:
- Direct database queries using
$wpdb
- Custom scripts requiring authentication without loading the full WordPress stack
Parameters
- $callback : callable|null = null
Tags
Return values
void —dispatch()
Dispatch a given HTTP method and URI to the appropriate route using the FastRoute dispatcher.
public
dispatch(string $httpMethod, string $uri) : array<string|int, int|mixed|string>
Returns an array with the shape: [ 'status' => int, 'handler' => mixed, 'vars' => array ] or [ 'status' => 404 ] [ 'status' => 405, 'allowed' => [ ... ] ]
Parameters
- $httpMethod : string
- $uri : string
Tags
Return values
array<string|int, int|mixed|string> —is404()
A minimal is_404() check. We treat "404" as "no posts found.".
public
is404() : bool
Return values
bool —query()
Minimal function to query posts from wp_posts using $wpdb and prepared statements.
public
query([array<string|int, mixed> $args = [] ][, string $output = ARRAY_A ]) : array<string|int, mixed>
Parameters
- $args : array<string|int, mixed> = []
- $output : string = ARRAY_A
-
Optional. Any of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants. With one of the first three, return an array of rows indexed from 0 by SQL result row number. Each row is an associative array (column => value, ...), a numerically indexed array (0 => value, ...), or an object ( ->column = value ), respectively. With OBJECT_K, return an associative array of row objects keyed by the value of each row's first column's value. Duplicate keys are discarded. Default OBJECT.
Return values
array<string|int, mixed> —setRoutes()
Set route definitions for FastRoute.
public
setRoutes(array<string|int, mixed> $routes) : void
Parameters
- $routes : array<string|int, mixed>