Documentation

Service
in package

Create a base REST Service needed for boilerplate development.

Table of Contents

Constants

NOTICE_CORRUPT_REST_API_ID  = 'notice-corrupt-rest-api'
SECURITY_PLUGINS_BLOCKING_REST_API  = ['better-wp-security', 'all-in-one-wp-security-and-firewall', 'sucuri-scanner', 'anti-spam', 'wp-cerber', 'wp-simple-firewall', 'wp-hide-security-enhancer', 'bulletproof-security', 'disable-json-api', 'ninjafirewall', 'hide-my-wp', 'perfmatters', 'swift-performance', 'clearfy', 'password-protected', 'wp-rest-api-controller']

Properties

$core  : mixed

Methods

admin_notices()  : mixed
Show a notice for `corruptRestApi.tsx`.
deObfuscatePath()  : mixed
A helper function which obfuscates a given path string with a given offset.
getCore()  : PluginReceiver
Get core instance.
getExternalContainerUrl()  : mixed
Get the (backend) API URL for the current running environment for another container.
getNamespace()  : string
Get the default namespace of this plugin generated from the slug.
getObfuscateOffset()  : mixed
Get the offset for the obfuscate mechanism. This can be a string or a number. This allows uniqueness of obfuscated strings between WordPress instances.
getSecurityPlugins()  : array<string|int, string>
Get all active security plugins which can limit the WP REST API.
getUrl()  : string
Get the wp-json URL for a defined REST service.
instance()  : Service
Get a new instance of Service.
obfuscatePath()  : mixed
A helper function which obfuscates a given path string with a given offset.
request()  : mixed
Enable obfuscate for REST API routes. This is useful when an ad-blocker blocks a route.
rest_api_init()  : mixed
Register endpoints.
routePlugin()  : mixed
Response for /plugin route.
sanitizePostMetaJson()  : mixed
When saving JSON in a registered post meta we need to convert the JavaScript `JSON.stringify`ied result to a valid PHP encoded JSON string with backslashes:
__construct()  : mixed
C'tor.

Constants

NOTICE_CORRUPT_REST_API_ID

public mixed NOTICE_CORRUPT_REST_API_ID = 'notice-corrupt-rest-api'

SECURITY_PLUGINS_BLOCKING_REST_API

public mixed SECURITY_PLUGINS_BLOCKING_REST_API = ['better-wp-security', 'all-in-one-wp-security-and-firewall', 'sucuri-scanner', 'anti-spam', 'wp-cerber', 'wp-simple-firewall', 'wp-hide-security-enhancer', 'bulletproof-security', 'disable-json-api', 'ninjafirewall', 'hide-my-wp', 'perfmatters', 'swift-performance', 'clearfy', 'password-protected', 'wp-rest-api-controller']

Properties

Methods

admin_notices()

Show a notice for `corruptRestApi.tsx`.

public admin_notices() : mixed

deObfuscatePath()

A helper function which obfuscates a given path string with a given offset.

public static deObfuscatePath(string $offset, string $str) : mixed
Parameters
$offset : string
$str : string

getExternalContainerUrl()

Get the (backend) API URL for the current running environment for another container.

public static getExternalContainerUrl(string $serviceName) : mixed
Parameters
$serviceName : string

E.g. commerce

Tags
codeCoverageIgnore

Ignored due to the fact that it depends on too much server variables

getNamespace()

Get the default namespace of this plugin generated from the slug.

public static getNamespace(mixed $instance[, string $version = 'v1' ]) : string
Parameters
$instance : mixed

The plugin class instance, so we can determine the slug from

$version : string = 'v1'

The version used for this namespace

Return values
string

getObfuscateOffset()

Get the offset for the obfuscate mechanism. This can be a string or a number. This allows uniqueness of obfuscated strings between WordPress instances.

public static getObfuscateOffset() : mixed

getSecurityPlugins()

Get all active security plugins which can limit the WP REST API.

public getSecurityPlugins() : array<string|int, string>
Return values
array<string|int, string>

getUrl()

Get the wp-json URL for a defined REST service.

public static getUrl(string $instance[, string $namespace = null ][, string $endpoint = '' ]) : string
Parameters
$instance : string

The plugin class instance, so we can determine the slug from

$namespace : string = null

The prefix for REST service

$endpoint : string = ''

The path appended to the prefix

Return values
string

Example: https://wordpress.org/wp-json

obfuscatePath()

A helper function which obfuscates a given path string with a given offset.

public static obfuscatePath(string $offset, string $str[, string $mode = 'keep-last-part' ]) : mixed
Parameters
$offset : string
$str : string
$mode : string = 'keep-last-part'

Can be full or keep-last-part

request()

Enable obfuscate for REST API routes. This is useful when an ad-blocker blocks a route.

public request(array<string|int, mixed> $queryVars) : mixed
Parameters
$queryVars : array<string|int, mixed>
Tags
see
https://regex101.com/r/fx1BfD/1

rest_api_init()

Register endpoints.

public rest_api_init() : mixed

routePlugin()

Response for /plugin route.

public routePlugin() : mixed

sanitizePostMetaJson()

When saving JSON in a registered post meta we need to convert the JavaScript `JSON.stringify`ied result to a valid PHP encoded JSON string with backslashes:

public static sanitizePostMetaJson(string $value) : mixed
json_encode("https://")      // -> "https:\/\/"
JSON.stringify("https://)    // -> "https://"

You only need to use this sanitize_callback when your meta value contains slashes /, otherwise you could get error messages like Could not update the meta value of %s in database..

Example usage:

register_meta('post', self::META_NAME_TECHNICAL_DEFINITIONS, [
    'object_subtype' => self::CPT_NAME,
    'type' => 'string',
    'single' => true,
    'sanitize_callback' => [Service::class, 'sanitizePostMetaJson'],
    'show_in_rest' => true
]);
Parameters
$value : string
Tags
see
https://github.com/WordPress/WordPress/blob/99366f31d23cdbad3296fa78d7813e2d3664790a/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php#L372-L379
see
https://app.clickup.com/t/861n4602e
deprecated

Do not use this, use register_meta with type=object|array instead!


        
On this page

Search results