Raydium Framework

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

$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
see
https://wordpress.stackexchange.com/questions/28342/is-there-a-way-to-use-the-wordpress-users-but-without-loading-the-entire-wordpre/28347#28347
see
https://core.trac.wordpress.org/ticket/55489
see
https://github.com/WordPress/wordpress-develop/blob/bcb3299a37712b61eb9b2a92c0b2fcc81e5d3d9d/src/wp-settings.php#L149
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
psalm-return

array{status: int, error?: 'Dispatcher not set', allowed?: mixed, handler?: mixed, vars?: mixed}

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>
Return values
void

Search results