From 6b7ffbc4ad7f5b7193196d2e69d6d18205d49b0c Mon Sep 17 00:00:00 2001 From: Jan Jakes Date: Fri, 6 Jan 2023 15:30:23 +0100 Subject: [PATCH] Allow plugins to add their own context data for automation editor [PREMIUM-215] --- .../js/src/automation/editor/store/initial_state.ts | 1 + .../js/src/automation/editor/store/selectors.ts | 7 +++++++ .../assets/js/src/automation/editor/store/types.ts | 4 ++++ mailpoet/lib/AdminPages/Pages/AutomationEditor.php | 9 +++++++++ mailpoet/lib/Automation/Engine/Registry.php | 12 ++++++++++++ mailpoet/views/automation/editor.html | 1 + 6 files changed, 34 insertions(+) diff --git a/mailpoet/assets/js/src/automation/editor/store/initial_state.ts b/mailpoet/assets/js/src/automation/editor/store/initial_state.ts index d6e6deddb2..b4efebf0ab 100644 --- a/mailpoet/assets/js/src/automation/editor/store/initial_state.ts +++ b/mailpoet/assets/js/src/automation/editor/store/initial_state.ts @@ -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, diff --git a/mailpoet/assets/js/src/automation/editor/store/selectors.ts b/mailpoet/assets/js/src/automation/editor/store/selectors.ts index 515d0626d3..553c13b33c 100644 --- a/mailpoet/assets/js/src/automation/editor/store/selectors.ts +++ b/mailpoet/assets/js/src/automation/editor/store/selectors.ts @@ -43,6 +43,13 @@ export function getRegistryStep( return state.registry.steps[key]; } +export function getContext( + state: State, + key: string, +): T | undefined { + return state.context[key] as T | undefined; +} + export function getSteps(state: State): StepType[] { return Object.values(state.stepTypes); } diff --git a/mailpoet/assets/js/src/automation/editor/store/types.ts b/mailpoet/assets/js/src/automation/editor/store/types.ts index 6c0f91ddb2..14d8e1e1e2 100644 --- a/mailpoet/assets/js/src/automation/editor/store/types.ts +++ b/mailpoet/assets/js/src/automation/editor/store/types.ts @@ -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; + 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; automationData: Automation; automationSaved: boolean; diff --git a/mailpoet/lib/AdminPages/Pages/AutomationEditor.php b/mailpoet/lib/AdminPages/Pages/AutomationEditor.php index 713d244bfd..506f2e42b2 100644 --- a/mailpoet/lib/AdminPages/Pages/AutomationEditor.php +++ b/mailpoet/lib/AdminPages/Pages/AutomationEditor.php @@ -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; + } } diff --git a/mailpoet/lib/Automation/Engine/Registry.php b/mailpoet/lib/Automation/Engine/Registry.php index 5943730690..9f169ce7fc 100644 --- a/mailpoet/lib/Automation/Engine/Registry.php +++ b/mailpoet/lib/Automation/Engine/Registry.php @@ -22,6 +22,9 @@ class Registry { /** @var array */ private $actions = []; + /** @var array */ + 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); } diff --git a/mailpoet/views/automation/editor.html b/mailpoet/views/automation/editor.html index e669f20f2a..a51e9e0ec8 100644 --- a/mailpoet/views/automation/editor.html +++ b/mailpoet/views/automation/editor.html @@ -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' %>; <% endblock %>