Reference
Moments
A moment names conditions about a visitor and what to do when they turn true: show a prompt, or run an action an extension registered. Moments are evaluated in the visitor's browser, so one firing costs the site nothing. Since WordSocket 0.31.
In code a moment is a rule: the API is WPS::rules() and WPS.rules,
the routes and hooks say rules. Everywhere a site owner looks, it is a moment.
1. A moment's shape
{
"id": "a1b2c3d4",
"name": "Reading for a while",
"enabled": true,
"frequency": "visit",
"rev": 3,
"attention": { "enabled": false, "title": "" },
"conditions": {
"all": [
{ "condition": "page_kind", "op": "is", "value": "post" },
{ "condition": "dwell", "op": "at_least", "value": 60 }
]
},
"actions": [
{
"action": "show",
"after": 0,
"presentation": { "type": "toast", "pattern": 0, "text": "Enjoying {page.title}?" }
}
]
}
| Field | Description |
|---|---|
| id | Letters, digits, _ and -, up to 64. The builder makes 8 hex characters; a moment an extension ships uses a slug. |
| name | Up to 120 characters. |
| enabled | Only enabled moments reach browsers. |
| frequency | always (every time the conditions turn true), page (at most once per page view), visit (once per journey, the default) or visitor (once per browser). |
| rev | Set by the server and bumped on every change except the enabled flag. Browsers key their state by id:rev, so an edit re-arms the moment for everyone and a pause does not. |
| attention | { enabled, title }. When on and a prompt opens into a hidden tab, the tab title flashes (title, up to 80 characters, "Still there?" when empty) and the icon is marked until the visitor returns. |
| conditions | A group, { all: [...] } or { any: [...] }, of conditions { condition, op, value } or nested groups. |
| actions | A list of { action, ...params }, the params flat beside the key. Every action takes after: seconds to wait from the firing, 0 to 3600, called off if the moment turns false meanwhile. |
| source | Optional: the extension that shipped it. |
Limits: 50 moments a site, 20 conditions and 10 actions a moment.
2. Built-in vocabulary
| Condition | In the builder | Operators | Value |
|---|---|---|---|
| page_kind | Page | is, is_not | select: a page kind |
| page_id | Page id | is, is_not | id |
| term_id | Term id | is, is_not | id |
| dwell | Time on page while visible (seconds) | at_least, at_most | number |
| time_hidden | Time on page while hidden (seconds) | at_least, at_most | number |
| pages_viewed | Pages so far | at_least, at_most | number |
| visited | Earlier in the visit | includes, excludes | kind |
| site_hour | Time of day (the site’s clock) | between | hours: { from, to }, 0 to 23 |
| hour | Visitor’s time of day | between | hours: { from, to }, 0 to 23 |
| weekday | Day of the week | in | weekdays: a list of 0 to 6 |
| visitor | Visitor is | is | select: new, returning |
| logged_in | Logged in | is | boolean |
| device | Device | is, is_not | select: desktop, tablet, mobile |
One action, show, with a presentation param: the content as a
modal, banner (top), bar (bottom), toast or
panel (slide-in), from a synced pattern or from text. Text may hold placeholders;
{page.title} and {site.name} are built in, and each registered
context adds its own.
Page kinds are the journey's kinds (see the journey format).
Extensions add to them through wpsignal_page_kinds,
wpsignal_taxonomy_kinds and wpsignal_archive_kinds.
3. Registering vocabulary in pairs
A condition, an action or a context is registered twice under one key
([a-z][a-z0-9_]*). PHP describes it, so the builder can offer it and a saved
moment can be validated. The browser does the work.
| PHP | Arguments | Browser |
|---|---|---|
| WPS::rules()->condition( $key, $args ) | label, description, operators (a list of { key, label }), value (a value type), options for a select, source. |
WPS.rules.condition( key, test ) |
| WPS::rules()->action( $key, $args ) | label, description, params ({ key: { type, label, options, default, when, prefix, suffix } }), source. when names a boolean param the builder shows this one under; do not register a param named after. |
WPS.rules.action( key, run ) |
| WPS::rules()->context( $key, $args ) | label, placeholders ({ 'cart.total': label }, the hints the builder offers), source. |
WPS.rules.context( key, read ) |
| Value type | What a saved value is |
|---|---|
| none | No value. |
| number | A number. |
| text | Text, up to 500 characters. |
| boolean | True or false. |
| select | One of the entry’s options ({ key: label }). |
| kind | A page kind the site has. |
| id | A post or term id above 0. |
| hours | { from, to }, each 0 to 23. |
| weekdays | A list of days, 0 to 6. |
| presentation | { type, pattern, text }: one of the param’s options, a published synced pattern id or 0, and the text shown when there is no pattern. |
use WPSignal\WPS;
// On init, so the labels can be translated.
add_action( 'init', function () {
if ( ! class_exists( WPS::class ) || ! method_exists( WPS::class, 'rules' ) ) {
return;
}
WPS::rules()->context( 'cart', array(
'label' => __( 'Cart', 'my-plugin' ),
'placeholders' => array( 'cart.total' => __( 'the cart total', 'my-plugin' ) ),
'source' => 'my-plugin',
) );
WPS::rules()->condition( 'cart_total', array(
'label' => __( 'Cart total', 'my-plugin' ),
'operators' => array(
array( 'key' => 'at_least', 'label' => __( 'at least', 'my-plugin' ) ),
array( 'key' => 'at_most', 'label' => __( 'at most', 'my-plugin' ) ),
),
'value' => 'number',
'source' => 'my-plugin',
) );
WPS::rules()->action( 'offer_help', array(
'label' => __( 'Offer help', 'my-plugin' ),
'params' => array(
'text' => array( 'type' => 'text', 'label' => __( 'Message', 'my-plugin' ), 'default' => '' ),
),
'source' => 'my-plugin',
) );
} );
let cart = { total: 0 };
const cartChanged = WPS.rules.context('cart', () => cart);
WPS.rules.condition('cart_total', (ctx, condition) =>
condition.op === 'at_least' ? ctx.cart.total >= condition.value : ctx.cart.total <= condition.value
);
WPS.rules.action('offer_help', (ctx, params, recovery, moment) => {
recovery.show({ text: params.text }, 'toast', {
rule: `${moment.id}:${moment.rev}`,
action: 'offer_help',
context: ctx,
});
});
// Whenever the value changes, so moments are evaluated again.
onCartUpdated((next) => {
cart = next;
cartChanged();
});
- Load the browser half on every page the client runs on, with
wpsignalas a script dependency. - A moment naming a key nobody registered right now (its extension is inactive) is kept and shown as unavailable. It survives a save of the others.
- For the Moments tab's replay to answer your condition over stored journeys, hand each one a context through the
wpsignal_journey_contextfilter. - The full browser API is under Moments and Prompts and attention on the JS API page.
4. Shipping a moment
An extension ships ready-made moments through the wpsignal_default_rules filter.
Each is a full moment with a slug for an id. It appears switched off until the site owner
saves it, and once deleted it stays deleted.
add_filter( 'wpsignal_default_rules', function ( array $moments ) {
$moments[] = array(
'id' => 'my-plugin-help',
'name' => 'Offer help on a large cart',
'frequency' => 'visit',
'source' => 'my-plugin',
'conditions' => array(
'all' => array(
array( 'condition' => 'cart_total', 'op' => 'at_least', 'value' => 200 ),
),
),
'actions' => array(
array( 'action' => 'offer_help', 'text' => 'Questions about your order? We are here.' ),
),
);
return $moments;
} );
5. Delivery and firing
- Moments are a feature a site switches on (the Moments card on the Settings tab, off by default). While it is off no moment reaches a browser and saved moments are kept.
- Only enabled moments leave wp-admin. Each page carries them, and every save broadcasts
rules.updatedwith{ version, rules }on theeventschannel, so open tabs take the change at once. A tab that reconnects fetches/rules/liveto catch up. - Evaluation is edge-triggered: a moment fires when its conditions turn from false to true. The first evaluation after a moment arrives only records where it stands.
- A moment that is edited, paused or deleted is taken back in every open tab: its prompts close and its waiting actions are called off.
- Moments run where the client script runs, and never for a crawler. By default the client loads for logged-in users only; return
truefromwpsignal_allow_clientto load it for every visitor. Until a plugin reserves a namespace, a visitor's token may read and write every channel of the site, so reserve one before opening the client to guests. - Each firing reaches
WPS.rules.onFire()in the browser. While History is on, or while something listens towpsignal_rule_fired, the browser also reports it toPOST /fired, along with what the visitor did with the prompt.
add_action( 'wpsignal_rule_fired', function ( array $report ) {
// array(
// 'visitor' => '0b1f...',
// 'journey_id' => '7c2e...',
// 'rule_id' => 'a1b2c3d4',
// 'action' => 'show',
// 'step' => 2, which step of the journey
// 's' => 15, whole seconds into that step
// 'kind' => 'fired', or returned, dismissed, clicked
// 'user_id' => 0,
// 'data' => array(),
// )
} );
returned means the tab a prompt opened into while hidden became visible,
dismissed that the visitor closed the prompt, and clicked that they
followed a link or button inside it. The report is the browser's word: it is accepted only
for an enabled moment with that action, and should never be trusted for more than a count.
6. Prompt styles
How the built-in prompts look is a site setting, edited from the Moments card on the Settings
tab or through GET and POST /wp-json/wpsignal/v1/prompt-style
(manage_options). A style is kept per scope: all, and one for each
of modal, banner, bar, toast and
panel.
| Field | Value | Scopes |
|---|---|---|
| background, text, border | A hex colour or a theme palette slug. | Every scope |
| radius | Pixels, 0 to 48. | Every scope |
| padding, fontSize | A number is pixels; a string is a theme preset slug. | Every scope |
| shadow, close | Boolean: the shadow, and the close button. | Every scope |
| motion | slide, fade or none. |
Every scope but modal |
| width | Pixels, 280 to 1200. | modal, panel |
| overlay | A colour, or none. |
modal |
GET answers { styles, fields, presets, sampleText, url, preview },
with presets the theme's palette, spacing sizes and font sizes.
POST takes { styles } keyed by scope and answers the same object as
saved; a field a scope does not have, or a value a field does not take, is
400 wpsignal_invalid_style naming the scope and field.
The styles are printed as --wordsocket-prompt-* custom properties on
#wordsocket-prompts and on .wordsocket-prompt--<type>, after
the client's stylesheet, so a theme can still set its own. On a site whose environment type is
not production, an administrator can see a sample with
WPS.recovery.preview( 'toast' ) or by opening any page with
?wordsocket-preview=toast.
7. REST routes
All under /wp-json/wpsignal/v1.
GET, POST /rules
For manage_options. GET answers everything the builder needs:
{ version, rules, removed, vocabulary, kinds, kindGroups }, where
rules is every moment (each with unavailable, the keys nobody
registered) and vocabulary is { conditions, actions, contexts }.
POST takes { rules: [...] }, the whole list, and answers the
same listing. An invalid moment is 400 wpsignal_invalid_rules whose
data names the rule and the field.
GET /rules/live
Public, no-store. { version, rules }: the enabled moments, exactly
what every page already carries.
GET /prompt/{id}
Public, cached for an hour. { id, html, version }: the rendered markup of a
published synced pattern, and only one an enabled moment names; anything else is
404 wpsignal_unknown_prompt. A pattern a live moment shows is therefore public to
anyone who asks, whatever the moment's conditions.
POST /fired
Public. Answers { "ok": true }, or 404 wpsignal_unknown_rule
unless rule is an enabled moment with that action.
| Field | Type | Description |
|---|---|---|
| v, n | string, required | The visitor id and the journey id, UUIDs. |
| rule, action | string, required | The moment's id and the action's key. |
| step | integer | Which step of the journey it happened in. |
| s | integer | Whole seconds into that step. |
| kind | string | fired (the default), returned, dismissed or clicked. |
| data | object | Scalar fields the action attached. |
The prompt styles route is described above; the
moments switch is moments_enabled on
POST /settings.
8. Abilities
On WordPress 6.9 and later WordSocket registers its moments, its journeys and publishing with
the Abilities API, under the wordsocket category. Each needs
manage_options and is public, so the /wp-json/wp-abilities/v1 routes
and the MCP adapter offer it. They call the same code as the routes above. Kind is the
annotation an agent reads before it runs one.
| Ability | Kind | What it does |
|---|---|---|
| wordsocket/describe-moments | Read only | The vocabulary a moment is built from: conditions and actions with their operators and value types, the page kinds, the synced patterns, the site’s timezone and the limits. |
| wordsocket/list-moments | Read only | Every moment, enabled or not, with live: whether moments are switched on for the site. |
| wordsocket/save-moment | Writes | Create or update one moment from { moment }. A new moment is saved switched off. |
| wordsocket/enable-moment | Destructive | Switch a moment on or off: { id, enabled }. |
| wordsocket/delete-moment | Destructive | Delete a moment: { id }. |
| wordsocket/publish-event | Writes | Publish { channel, event, data } on one of the site’s channels. |
| wordsocket/list-journeys | Read only | Stored journeys, newest first, as { count, currency, legend, journeys }. Filters: days, limit (100 by default, up to 1000), outcome, ref, user_id, visitor, rule. |
curl -s -u 'admin:APPLICATION_PASSWORD' \
'https://example.com/wp-json/wp-abilities/v1/abilities/wordsocket/list-moments/run'