Allow plugins to add their own context data for automation editor
[PREMIUM-215]
This commit is contained in:
@@ -4,6 +4,7 @@ declare let window: AutomationEditorWindow;
|
|||||||
|
|
||||||
export const getInitialState = (): State => ({
|
export const getInitialState = (): State => ({
|
||||||
registry: { ...window.mailpoet_automation_registry },
|
registry: { ...window.mailpoet_automation_registry },
|
||||||
|
context: { ...window.mailpoet_automation_context },
|
||||||
stepTypes: {},
|
stepTypes: {},
|
||||||
automationData: { ...window.mailpoet_automation },
|
automationData: { ...window.mailpoet_automation },
|
||||||
automationSaved: true,
|
automationSaved: true,
|
||||||
|
@@ -43,6 +43,13 @@ export function getRegistryStep(
|
|||||||
return state.registry.steps[key];
|
return state.registry.steps[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getContext<T = unknown>(
|
||||||
|
state: State,
|
||||||
|
key: string,
|
||||||
|
): T | undefined {
|
||||||
|
return state.context[key] as T | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
export function getSteps(state: State): StepType[] {
|
export function getSteps(state: State): StepType[] {
|
||||||
return Object.values(state.stepTypes);
|
return Object.values(state.stepTypes);
|
||||||
}
|
}
|
||||||
|
@@ -3,6 +3,7 @@ import { Step, Automation } from '../components/automation/types';
|
|||||||
|
|
||||||
export interface AutomationEditorWindow extends Window {
|
export interface AutomationEditorWindow extends Window {
|
||||||
mailpoet_automation_registry: Registry;
|
mailpoet_automation_registry: Registry;
|
||||||
|
mailpoet_automation_context: Context;
|
||||||
mailpoet_automation: Automation;
|
mailpoet_automation: Automation;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -20,6 +21,8 @@ export type Registry = {
|
|||||||
>;
|
>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type Context = Record<string, unknown>;
|
||||||
|
|
||||||
export type StepGroup = 'actions' | 'logical' | 'triggers';
|
export type StepGroup = 'actions' | 'logical' | 'triggers';
|
||||||
|
|
||||||
export type StepType = {
|
export type StepType = {
|
||||||
@@ -47,6 +50,7 @@ export type Errors = {
|
|||||||
|
|
||||||
export type State = {
|
export type State = {
|
||||||
registry: Registry;
|
registry: Registry;
|
||||||
|
context: Context;
|
||||||
stepTypes: Record<string, StepType>;
|
stepTypes: Record<string, StepType>;
|
||||||
automationData: Automation;
|
automationData: Automation;
|
||||||
automationSaved: boolean;
|
automationSaved: boolean;
|
||||||
|
@@ -83,6 +83,7 @@ class AutomationEditor {
|
|||||||
$roles = new \WP_Roles();
|
$roles = new \WP_Roles();
|
||||||
$this->pageRenderer->displayPage('automation/editor.html', [
|
$this->pageRenderer->displayPage('automation/editor.html', [
|
||||||
'registry' => $this->buildRegistry(),
|
'registry' => $this->buildRegistry(),
|
||||||
|
'context' => $this->buildContext(),
|
||||||
'automation' => $this->automationMapper->buildAutomation($automation),
|
'automation' => $this->automationMapper->buildAutomation($automation),
|
||||||
'sub_menu' => 'mailpoet-automation',
|
'sub_menu' => 'mailpoet-automation',
|
||||||
'api' => [
|
'api' => [
|
||||||
@@ -108,4 +109,12 @@ class AutomationEditor {
|
|||||||
}
|
}
|
||||||
return ['steps' => $steps];
|
return ['steps' => $steps];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function buildContext(): array {
|
||||||
|
$data = [];
|
||||||
|
foreach ($this->registry->getContextFactories() as $key => $factory) {
|
||||||
|
$data[$key] = $factory();
|
||||||
|
}
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -22,6 +22,9 @@ class Registry {
|
|||||||
/** @var array<string, Action> */
|
/** @var array<string, Action> */
|
||||||
private $actions = [];
|
private $actions = [];
|
||||||
|
|
||||||
|
/** @var array<string, callable> */
|
||||||
|
private $contextFactories = [];
|
||||||
|
|
||||||
/** @var WordPress */
|
/** @var WordPress */
|
||||||
private $wordPress;
|
private $wordPress;
|
||||||
|
|
||||||
@@ -107,6 +110,15 @@ class Registry {
|
|||||||
return $this->actions;
|
return $this->actions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function addContextFactory(string $key, callable $factory): void {
|
||||||
|
$this->contextFactories[$key] = $factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return callable[] */
|
||||||
|
public function getContextFactories(): array {
|
||||||
|
return $this->contextFactories;
|
||||||
|
}
|
||||||
|
|
||||||
public function onBeforeAutomationSave(callable $callback, int $priority = 10): void {
|
public function onBeforeAutomationSave(callable $callback, int $priority = 10): void {
|
||||||
$this->wordPress->addAction(Hooks::AUTOMATION_BEFORE_SAVE, $callback, $priority);
|
$this->wordPress->addAction(Hooks::AUTOMATION_BEFORE_SAVE, $callback, $priority);
|
||||||
}
|
}
|
||||||
|
@@ -9,6 +9,7 @@
|
|||||||
var mailpoet_user_roles = <%= json_encode(user_roles) %>;
|
var mailpoet_user_roles = <%= json_encode(user_roles) %>;
|
||||||
var mailpoet_segments = <%= json_encode(segments) %>;
|
var mailpoet_segments = <%= json_encode(segments) %>;
|
||||||
var mailpoet_automation_registry = <%= json_encode(registry) %>;
|
var mailpoet_automation_registry = <%= json_encode(registry) %>;
|
||||||
|
var mailpoet_automation_context = <%= json_encode(context) %>;
|
||||||
var mailpoet_automation = <%= automation ? json_encode(automation) : 'undefined' %>;
|
var mailpoet_automation = <%= automation ? json_encode(automation) : 'undefined' %>;
|
||||||
</script>
|
</script>
|
||||||
<% endblock %>
|
<% endblock %>
|
||||||
|
Reference in New Issue
Block a user