Allow plugins to add their own context data for automation editor

[PREMIUM-215]
This commit is contained in:
Jan Jakes
2023-01-06 15:30:23 +01:00
committed by Aschepikov
parent 4ddcc14eee
commit 6b7ffbc4ad
6 changed files with 34 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ declare let window: AutomationEditorWindow;
export const getInitialState = (): State => ({
registry: { ...window.mailpoet_automation_registry },
context: { ...window.mailpoet_automation_context },
stepTypes: {},
automationData: { ...window.mailpoet_automation },
automationSaved: true,

View File

@@ -43,6 +43,13 @@ export function getRegistryStep(
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[] {
return Object.values(state.stepTypes);
}

View File

@@ -3,6 +3,7 @@ import { Step, Automation } from '../components/automation/types';
export interface AutomationEditorWindow extends Window {
mailpoet_automation_registry: Registry;
mailpoet_automation_context: Context;
mailpoet_automation: Automation;
}
@@ -20,6 +21,8 @@ export type Registry = {
>;
};
export type Context = Record<string, unknown>;
export type StepGroup = 'actions' | 'logical' | 'triggers';
export type StepType = {
@@ -47,6 +50,7 @@ export type Errors = {
export type State = {
registry: Registry;
context: Context;
stepTypes: Record<string, StepType>;
automationData: Automation;
automationSaved: boolean;

View File

@@ -83,6 +83,7 @@ class AutomationEditor {
$roles = new \WP_Roles();
$this->pageRenderer->displayPage('automation/editor.html', [
'registry' => $this->buildRegistry(),
'context' => $this->buildContext(),
'automation' => $this->automationMapper->buildAutomation($automation),
'sub_menu' => 'mailpoet-automation',
'api' => [
@@ -108,4 +109,12 @@ class AutomationEditor {
}
return ['steps' => $steps];
}
private function buildContext(): array {
$data = [];
foreach ($this->registry->getContextFactories() as $key => $factory) {
$data[$key] = $factory();
}
return $data;
}
}

View File

@@ -22,6 +22,9 @@ class Registry {
/** @var array<string, Action> */
private $actions = [];
/** @var array<string, callable> */
private $contextFactories = [];
/** @var WordPress */
private $wordPress;
@@ -107,6 +110,15 @@ class Registry {
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 {
$this->wordPress->addAction(Hooks::AUTOMATION_BEFORE_SAVE, $callback, $priority);
}

View File

@@ -9,6 +9,7 @@
var mailpoet_user_roles = <%= json_encode(user_roles) %>;
var mailpoet_segments = <%= json_encode(segments) %>;
var mailpoet_automation_registry = <%= json_encode(registry) %>;
var mailpoet_automation_context = <%= json_encode(context) %>;
var mailpoet_automation = <%= automation ? json_encode(automation) : 'undefined' %>;
</script>
<% endblock %>