Developers

Adding Your Own Settings to Super Speedy Emails: Record Fields and Per-Step Fields

Published

When you write an add-on for an email plugin, the settings screen is usually the part that takes longest and adds least. Somebody has to draw the form, save it, validate it, migrate it, and make sure an import or an AI agent can’t store garbage in it.

Super Speedy Emails does that part for you. A Support Email type declares its options, and the engine draws them in the editor, stores them with the record, checks them on every write path and hands them back to your code resolved. This article shows both kinds of option using the real Follow-up Emails add-on, and explains when to use which.

Available since Super Speedy Emails 1.38.0 (per-step fields and a chosen number of steps); SSE_Support_Types::UNLIMITED_STAGES since 1.38.1.

Two kinds of option

Record fields (admin_fields)Per-step fields (stage_fields)
One value perSupport Email recordStep of that record
ShownIn the record’s editorInside each step’s editor, beside its subject and body
StoredWith the recordWith the step, as stages[i]['settings']
Read with$context->setting( $key, $fallback )$context->stage_setting( $key, $index )
Field typesselect, text, number, checkboxthe same

The rule of thumb: if the answer could differ between step 2 and step 4, it’s a per-step field. If it’s about the series as a whole, it’s a record field.

In the follow-up add-on, each step’s timing (“not sooner than” or “exactly on”) and its day after purchase differ per step, so they’re stage_fields entries. “Count days from first or most recent purchase”, “minimum days between steps”, “also email people who bought this product as part of a bundle” and “one follow-up per person per day” are about the whole series, so they’re admin_fields.

The real declaration

This is the add-on’s registration, trimmed to the options, with its constants and descriptions filled in or left out:

sse_register_support_email_type( 'follow_up_series', array(
	'label'        => __( 'Follow-up series (after purchase)', 'sse-addon-follow-up-emails' ),
	'mode'         => 'per_product',
	'stages'       => 5,    // a new record starts with 5 steps
	'max_stages'   => SSE_Support_Types::UNLIMITED_STAGES,   // -1: Add step / Remove this step, no maximum
	'stage_fields' => array(
		'timing'     => array(
			'type'    => 'select',
			'label'   => __( 'When this step goes', 'sse-addon-follow-up-emails' ),
			'default' => 'after',
			'options' => array(
				array( 'value' => 'after', 'label' => __( 'Not sooner than', 'sse-addon-follow-up-emails' ) ),
				array( 'value' => 'exact', 'label' => __( 'Exactly on', 'sse-addon-follow-up-emails' ) ),
			),
		),
		'delay_days' => array(
			'type'    => 'number',
			'label'   => __( 'Day (after purchase)', 'sse-addon-follow-up-emails' ),
			'default' => array( 1, 2, 3, 4, 5 ),   // one default per step
		),
	),
	'admin_fields' => array(
		'anchor' => array(
			'type'    => 'select',
			'label'   => __( 'Count days from', 'sse-addon-follow-up-emails' ),
			'default' => 'first',
			'options' => array(
				array( 'value' => 'first', 'label' => __( 'First purchase', 'sse-addon-follow-up-emails' ) ),
				array( 'value' => 'last', 'label' => __( 'Most recent purchase', 'sse-addon-follow-up-emails' ) ),
			),
		),
		'min_gap_days'       => array( 'type' => 'number', 'label' => __( 'Minimum days between steps', 'sse-addon-follow-up-emails' ), 'default' => 1 ),
		'include_bundle_owners' => array( 'type' => 'checkbox', 'label' => __( 'Also email people who bought this product as part of a bundle', 'sse-addon-follow-up-emails' ), 'default' => false ),
		'one_per_day_global' => array( 'type' => 'checkbox', 'label' => __( 'One follow-up per person per day', 'sse-addon-follow-up-emails' ), 'default' => true ),
	),
	'callbacks' => array(
		'next_stage'          => array( 'SSEFU_Series', 'next_stage' ),
		'should_send_to_user' => array( 'SSEFU_Series', 'should_send_to_user' ),
		'content'             => array( 'SSEFU_Series', 'content' ),
	),
) );

And reading them back, inside a callback:

$anchor = 'last' === (string) $context->setting( 'anchor', 'first' ) ? 'last' : 'first';
$gap    = max( 0, (int) $context->setting( 'min_gap_days', 1 ) );
$timing = 'exact' === (string) $context->stage_setting( 'timing', $index ) ? 'exact' : 'after';
$delay  = max( 0, (int) $context->stage_setting( 'delay_days', $index ) );

That’s all the settings code the add-on has. No admin page, no register_setting(), no sanitiser of its own.

Per-step defaults are a list

A per-step field’s default may be a list indexed by 0-based step. Step 1 defaults to the first entry, step 2 to the second, and any step past the end of the list takes the last entry. A scalar default applies to every step.

A list avoids a magic value. With one default for every step, the tempting shortcut is 0 meaning “use the step number”, which breaks the day someone wants a step to go on the purchase day and types 0. With a list, 0 means what it says.

Common mistake

Using a magic value (0, -1, an empty string) to mean “work it out for me”. It reads fine in your code and wrong in the admin’s head. Declare the real default.

A chosen number of steps

stages is how many steps a new record starts with. On its own it fixes the count: every record of that type has exactly that many steps. Add max_stages and the editor shows Add step (disabled at the maximum) and Remove this step (disabled at one step), and each new step shows the default for the position it lands in. max_stages => SSE_Support_Types::UNLIMITED_STAGES (-1) sets no maximum, so Add step is never disabled; the follow-up add-on uses it.

Super Speedy Emails’ browser test exercises exactly that: a new record with 3 default steps and delays 1, 2, 3, two steps added (4 and 5, with Add disabled at 5), step 3 removed, a delay edited, saved and reloaded as 4 steps with delays 1, 7, 4, 5. A changelog record in the same test shows no step controls at all.

What the engine guarantees

  • Only what you declared is stored, on every write path: the editor, a config import, or an AI agent through draft-support-rule and update-support-rule. Undeclared keys are dropped, and a value outside your options falls back to your default.
  • Values are always resolved. A step saved before you declared a field, or a step the record doesn’t have, returns that step’s default. An undeclared key returns null.
  • Preview uses unsaved values, so the admin sees the audience each choice reaches before saving.
  • Deactivating your add-on keeps the stored settings, waiting for the type to come back.
  • Agents can discover them. list-support-rules reports each type’s stage_fields (with per-step default lists as declared) and max_stages, and each record’s steps with their resolved settings.

Choosing what to expose

Every option you add is a decision you’re handing to someone who knows less about your code than you do. A few guidelines:

  • Name the option after its behaviour. “Not sooner than” and “Exactly on” say what happens when a day is missed. “Delay” doesn’t say whether a missed day loses the email.
  • Pick defaults you’d ship to a stranger. The follow-up add-on’s defaults (not sooner than days 1 to 5, a 1-day gap, first purchase, bundle-only owners left out, one a day) give a sensible series with nothing changed.
  • Write the description for the confused case. Under “Not sooner than” the follow-up editor says “Goes on day N, or on the first run after it if that day was missed. Customers who bought before this email existed still receive it.” Under “Exactly on” it says the opposite: “If that day’s run doesn’t happen (cron or the server was down), or the customer bought before this email existed, they never receive this step.” Each answers the support question before it arrives. A small editor script swaps the text when the choice changes; without it, the field’s own description shows.

Prompt to give your AI

Read the "admin_fields" and "Per-stage options and stage state" sections of the
Super Speedy Emails Automated Support Emails Developer Guide. Declare every option
my Support Email type needs as admin_fields (per record) or stage_fields (per step),
with real defaults (a list for per-step defaults) and a one-sentence description of
what the admin will see happen. Read them with $context->setting() and
$context->stage_setting(). Do not build a settings page or sanitise them yourself.

Further reading

admin_fields, stage_fields, max_stages in full
every type argument and context helper
how the record fields combine on launch day
proving the settings do what they say
setting stages[i].settings from an agent
every developer guide and reference in one place

Written for Super Speedy Emails 1.38.2.

Comments