PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0
Elementor Website Builder – more than just a page builder v4.3.0
4.3.1 4.3.0 4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 All 454 releases
elementor / modules / mcp / events / mcp-event-dispatcher.php

mcp-event-dispatcher.php in Elementor Website Builder – more than just a page builder 4.3.0, at modules/mcp/events/mcp-event-dispatcher.php

143 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\Mcp\Events;
4
5 use Elementor\Core\Common\Modules\EventsManager\Module as Events_Manager_Module;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit;
9 }
10
11 class Mcp_Event_Dispatcher {
12
13 const APP_TYPE = 'editor';
14 const WINDOW_NAME = 'MCP';
15 const INTERACTION_TYPE = 'MCP';
16 const TARGET_TYPE = 'MCP';
17 const TARGET_LOCATION = 'MCP';
18 const TARGET_NAME = 'MCP';
19 const EXECUTED_BY = 'mcp_tool';
20
21 const FEATURE_NAME_COMPONENTS = 'Components';
22
23 private const ENVELOPE_KEYS = [
24 'app_type',
25 'window_name',
26 'interaction_type',
27 'target_type',
28 'target_location',
29 'target_name',
30 'executed_by',
31 'interaction_result',
32 ];
33
34 /**
35 * Per ED-25569: keys copied into the metadata object for warehouse parsing.
36 * All other non-envelope fields from emit() remain top-level DB columns.
37 *
38 * @var array<string, string[]|null>
39 */
40 private const METADATA_KEYS_BY_RESULT = [
41 'class_created' => [ 'name' ],
42 'class_applied' => null,
43 'component_created' => [
44 'name',
45 'nested_components_count',
46 'nested_elements_count',
47 'top_element_type',
48 'feature_name',
49 ],
50 'component_instance_added' => [
51 'name',
52 'top_element_type',
53 'feature_name',
54 ],
55 'variable_created' => [ 'name', 'var_type' ],
56 'variable_updated' => [ 'name', 'var_type' ],
57 'variable_connected' => [ 'var_type', 'control_path' ],
58 'element_added' => [ 'element_name' ],
59 'interaction_created' => [ 'affected_element_type', 'interaction_trigger', 'interaction_effect' ],
60 'interaction_updated' => [ 'affected_element_type', 'interaction_trigger', 'interaction_effect' ],
61 'interactions_cleared' => [ 'affected_element_type', 'target_value' ],
62 ];
63
64 private const COMPONENT_RESULTS = [
65 'component_created',
66 'component_instance_added',
67 ];
68
69 /** @var callable|null Test-only interceptor. When set, replaces the Mixpanel dispatch. */
70 private static $interceptor = null;
71
72 public static function set_interceptor( ?callable $interceptor ): void {
73 self::$interceptor = $interceptor;
74 }
75
76 public static function emit( string $interaction_result, array $extra = [] ): void {
77 $payload = [
78 'app_type' => self::APP_TYPE,
79 'window_name' => self::WINDOW_NAME,
80 'interaction_type' => self::INTERACTION_TYPE,
81 'target_type' => self::TARGET_TYPE,
82 'target_location' => self::TARGET_LOCATION,
83 'target_name' => self::TARGET_NAME,
84 'executed_by' => self::EXECUTED_BY,
85 'interaction_result' => $interaction_result,
86 ];
87
88 $metadata_merge = [];
89
90 foreach ( $extra as $key => $value ) {
91 if ( in_array( $key, self::ENVELOPE_KEYS, true ) ) {
92 $payload[ $key ] = $value;
93 continue;
94 }
95
96 if ( 'metadata' === $key && is_array( $value ) ) {
97 $metadata_merge = array_merge( $metadata_merge, $value );
98 continue;
99 }
100
101 $payload[ $key ] = $value;
102 }
103
104 $metadata = self::build_metadata( $interaction_result, $extra, $metadata_merge );
105
106 if ( [] !== $metadata ) {
107 $payload['metadata'] = $metadata;
108 }
109
110 try {
111 if ( null !== self::$interceptor ) {
112 ( self::$interceptor )( $interaction_result, $payload );
113 return;
114 }
115
116 Events_Manager_Module::dispatch_event( $interaction_result, $payload );
117 } catch ( \Throwable $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
118 }
119 }
120
121 private static function build_metadata( string $interaction_result, array $extra, array $metadata_merge ): array {
122 $key_list = self::METADATA_KEYS_BY_RESULT[ $interaction_result ] ?? null;
123
124 if ( null === $key_list ) {
125 return [];
126 }
127
128 $metadata = [];
129
130 foreach ( $key_list as $key ) {
131 if ( array_key_exists( $key, $extra ) ) {
132 $metadata[ $key ] = $extra[ $key ];
133 }
134 }
135
136 if ( in_array( $interaction_result, self::COMPONENT_RESULTS, true ) ) {
137 $metadata['feature_name'] = self::FEATURE_NAME_COMPONENTS;
138 }
139
140 return array_merge( $metadata, $metadata_merge );
141 }
142 }
143