PluginProbe
Outfunnel: Web Visitor Tracking & CRM Integration / trunk
Outfunnel: Web Visitor Tracking & CRM Integration vtrunk
trunk 1.0.0 1.1.0 1.1.1 1.2.0 1.2.3 1.3.0 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 2.0.0 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.3.0 2.4.0 2.5.0 2.6.0 All 38 releases
outfunnel / forms / elementor.php

elementor.php in Outfunnel: Web Visitor Tracking & CRM Integration trunk, at forms/elementor.php

193 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Outfunnel\Forms;
3
4 use ElementorPro\Modules\Forms\Classes\Ajax_Handler;
5 use ElementorPro\Modules\Forms\Classes\Form_Record;
6 use ElementorPro\Modules\Forms\Submissions\Database\Repositories\Form_Snapshot_Repository;
7 use DateTime;
8
9 include_once(ABSPATH . 'wp-admin/includes/plugin.php');
10
11 class Elementor implements Form {
12 /**
13 * @var String
14 */
15 private const ELEMENTOR_PRO_PLUGIN_NAME = 'elementor-pro';
16
17 /**
18 * @var String
19 */
20 private const ELEMENTOR_PRO_FORM_SUBMIT_EVENT = 'elementor_pro/forms/new_record';
21
22
23 /**
24 * @var Elementor
25 */
26 private static $instance;
27
28 /**
29 * @return Elementor
30 */
31 public static function instance()
32 {
33 if (is_null(self::$instance)) {
34 self::$instance = new self();
35 }
36
37 return self::$instance;
38 }
39
40 /**
41 * Elementor Pro can be loaded as a regular plugin, an mu-plugin, or via a
42 * Composer-managed install (e.g. Bedrock). The class checks are what our
43 * hooks and API calls actually depend on — their presence is enough,
44 * regardless of how WordPress was told to load the plugin.
45 *
46 * @return Boolean
47 */
48 public function is_form_plugin_active() {
49 return class_exists( Form_Snapshot_Repository::class )
50 && class_exists( Ajax_Handler::class )
51 && class_exists( Form_Record::class );
52 }
53
54 /**
55 * @return String|null
56 */
57 public function get_plugin_version() {
58 if (!$this->is_form_plugin_active()) {
59 return null;
60 }
61 if (defined( 'ELEMENTOR_PRO_VERSION' )) {
62 return ELEMENTOR_PRO_VERSION;
63 }
64 $path = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . self::ELEMENTOR_PRO_PLUGIN_NAME . DIRECTORY_SEPARATOR . 'elementor-pro.php';
65 return file_exists( $path ) ? get_plugin_data( $path )["Version"] : null;
66 }
67
68 /**
69 * @return String
70 */
71 public function get_plugin_name() {
72 return self::ELEMENTOR_PRO_PLUGIN_NAME;
73 }
74
75 public function get_all_forms() {
76 if (!$this->is_form_plugin_active()) {
77 return [
78 'errors' => [[
79 error => 'required_form_plugin_not_active',
80 message => 'Required form plugin not active',
81 context => 'form_source',
82 ]],
83 ];
84 }
85
86 $forms = Form_Snapshot_Repository::instance()->all();
87
88 return [
89 'data' => $forms->map(function ($form) {
90 return [
91 'label' => $form->get_label(),
92 'id' => $form->id,
93 'fields' => $form->fields
94 ];
95 })->values(),
96 'formPluginVersion' => $this->get_plugin_version(),
97 'formPluginName' => $this->get_plugin_name(),
98 'wpVersion' => WORDPRESS_VERSION,
99 'ofPluginVersion' => OUTFUNNEL_VERSION
100 ];
101 }
102
103 /**
104 * @param Form_Record
105 * @param Ajax_Handler
106 * @return void
107 */
108 public function run_webhook($record, $handler) {
109 Logger::info("Starting to process Elementor webhook");
110
111 $outfunnel_settings = get_option('outfunnel_settings');
112
113 if (!$outfunnel_settings) {
114 Logger::warning("Failed to load Outfunnel settings");
115
116 return;
117 }
118
119 $of_api_key = $outfunnel_settings['of_api_key'];
120 $of_account_email = $outfunnel_settings['of_account_email'];
121 $of_user_id = $outfunnel_settings['of_id'];
122
123 if (!$of_api_key || !$of_account_email) {
124 Logger::warning("Api key or account email missing", $of_user_id);
125
126 return false;
127 }
128
129 $form_name = $record->get_form_settings('form_name');
130 // Currently we do not support custom form ids
131 $form_id = $record->get_form_settings('id');
132
133 $raw_fields = $record->get('fields');
134
135 $site_url = get_site_url();
136
137 $datetime = new DateTime();
138 $submission_time = $datetime->format('c');
139 $form_url = get_permalink(get_the_ID());
140
141 Logger::info(
142 "Starting to send a request",
143 $of_user_id,
144 [
145 'form_name' => $form_name,
146 'form_id' => $form_id
147 ]
148 );
149
150 $webhook_data = [
151 'data' => [
152 'formId' => $form_id,
153 'formName' => $form_name,
154 'submissionTime' => $submission_time,
155 'submissionData' => $raw_fields,
156 'formUrl' => $form_url
157 ],
158 'siteUrl' => $site_url,
159 'eventType' => self::ELEMENTOR_PRO_FORM_SUBMIT_EVENT,
160 'formPluginVersion' => $this->get_plugin_version(),
161 'formPluginName' => $this->get_plugin_name(),
162 'wpVersion' => WORDPRESS_VERSION,
163 'ofPluginVersion' => OUTFUNNEL_VERSION
164 ];
165
166 Logger::info("Sending out webhook", $of_user_id);
167
168 $response = wp_remote_post(OF_WEBHOOK_URL . '/webhooks/elementor', [
169 'headers' => [
170 'Content-Type' => 'application/json',
171 'x-api-key' => $of_api_key,
172 'x-account-email' => $of_account_email,
173 ],
174 'body' => wp_json_encode($webhook_data),
175 ]);
176
177 Logger::info("Webhook response", $of_user_id, ['response_code' => $response['response']['code']]);
178 }
179
180 /**
181 * @return void
182 */
183 private function setup_hooks() {
184 add_action(self::ELEMENTOR_PRO_FORM_SUBMIT_EVENT, [$this, 'run_webhook'], 10, 2);
185 }
186
187 private function __construct() {
188 if ($this->is_form_plugin_active()) {
189 $this->setup_hooks();
190 }
191 }
192 }
193