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
$core
private
mixed
$core
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
getCore()
Get core instance.
public
getCore() : PluginReceiver
Tags
Return values
PluginReceivergetExternalContainerUrl()
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
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
stringgetObfuscateOffset()
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
instance()
Get a new instance of Service.
public
static instance(PluginReceiver $core) : Service
Parameters
- $core : PluginReceiver
Tags
Return values
ServiceobfuscatePath()
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
orkeep-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
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
__construct()
C'tor.
private
__construct(PluginReceiver $core) : mixed
Parameters
- $core : PluginReceiver