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 / gravityplugin.php

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

156 lines 4.3 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 include_once(ABSPATH . 'wp-admin/includes/plugin.php');
5
6 class GravityPlugin implements Form {
7 /**
8 * @var String
9 */
10 private const GRAVITY_FORMS_PLUGIN_NAME = 'gravityforms';
11
12 /**
13 * @var String
14 */
15 private const GRAVITY_FORM_SUBMIT_EVENT = 'gform_after_submission';
16
17 /**
18 * @var GravityPlugin
19 */
20 private static $instance;
21
22 /**
23 * @return GravityPlugin
24 */
25 public static function instance()
26 {
27 if (is_null(self::$instance)) {
28 self::$instance = new self();
29 }
30
31 return self::$instance;
32 }
33
34 /**
35 * Gravity Forms can be loaded as a regular plugin, an mu-plugin, or via a
36 * Composer-managed install (e.g. Bedrock). The presence of \GFForms means
37 * the plugin's code is loaded — which is what our hooks and API calls
38 * actually depend on — regardless of how WordPress was told to load it.
39 *
40 * @return Boolean
41 */
42 public function is_form_plugin_active() {
43 return class_exists( '\GFForms' );
44 }
45
46 /**
47 * @return String|null
48 */
49 public function get_plugin_version() {
50 if (!$this->is_form_plugin_active()) {
51 return null;
52 }
53 if (class_exists( '\GFForms' ) && isset( \GFForms::$version )) {
54 return \GFForms::$version;
55 }
56 $path = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . self::GRAVITY_FORMS_PLUGIN_NAME . DIRECTORY_SEPARATOR . 'gravityforms.php';
57 return file_exists( $path ) ? get_plugin_data( $path )["Version"] : null;
58 }
59
60
61 /**
62 * @return String
63 */
64 public function get_plugin_name() {
65 return self::GRAVITY_FORMS_PLUGIN_NAME;
66 }
67
68 public function get_all_forms() {
69 if (!$this->is_form_plugin_active()) {
70 return [
71 'errors' => [[
72 error => 'required_form_plugin_not_active',
73 message => 'Required form plugin not active',
74 context => 'form_source',
75 ]],
76 ];
77 }
78
79 $forms = \GFAPI::get_forms(true, false);
80
81 return [
82 'data' => $forms,
83 'formPluginVersion' => $this->get_plugin_version(),
84 'formPluginName' => $this->get_plugin_name(),
85 'wpVersion' => WORDPRESS_VERSION,
86 'ofPluginVersion' => OUTFUNNEL_VERSION
87 ];
88 }
89
90 /**
91 * @return void
92 */
93 public function run_webhook($entry, $form) {
94 Logger::info("Starting to process Gravity webhook");
95
96 $outfunnel_settings = get_option('outfunnel_settings');
97
98 if (!$outfunnel_settings) {
99 Logger::warning("Failed to load Outfunnel settings");
100
101 return;
102 }
103
104 $of_api_key = $outfunnel_settings['of_api_key'];
105 $of_account_email = $outfunnel_settings['of_account_email'];
106 $of_user_id = $outfunnel_settings['of_id'];
107
108 if (!$of_api_key || !$of_account_email) {
109 Logger::warning("Api key or account email missing", $of_user_id);
110
111 return false;
112 }
113
114 $site_url = get_site_url();
115
116 $webhook_data = [
117 'data' => [
118 'form' => $form,
119 'submissionData' => $entry,
120 ],
121 'siteUrl' => $site_url,
122 'eventType' => self::GRAVITY_FORM_SUBMIT_EVENT,
123 'formPluginVersion' => $this->get_plugin_version(),
124 'formPluginName' => $this->get_plugin_name(),
125 'wpVersion' => WORDPRESS_VERSION,
126 'ofPluginVersion' => OUTFUNNEL_VERSION
127 ];
128
129 $response = wp_remote_post(OF_WEBHOOK_URL . '/webhooks/gravity', [
130 'headers' => [
131 'Content-Type' => 'application/json',
132 'x-api-key' => $of_api_key,
133 'x-account-email' => $of_account_email,
134 ],
135 'body' => wp_json_encode($webhook_data),
136 ]);
137
138 Logger::info("Webhook response", $of_user_id, ['response_code' => $response['response']['code']]);
139 }
140
141 /**
142 * @return void
143 */
144 private function setup_hooks() {
145 add_action(self::GRAVITY_FORM_SUBMIT_EVENT, [$this, 'run_webhook'], 10, 2);
146 }
147
148 private function __construct() {
149 if ($this->is_form_plugin_active()) {
150 $this->setup_hooks();
151 }
152 }
153
154
155 }
156