PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.10.3
Independent Analytics – WordPress Analytics Plugin v2.10.3
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / IAWP / Form_Submissions / Form.php
independent-analytics / IAWP / Form_Submissions Last commit date
Form.php 1 year ago Submission.php 1 year ago Submission_Listener.php 1 year ago
Form.php
187 lines
1 <?php
2
3 namespace IAWP\Form_Submissions;
4
5 use IAWP\Illuminate_Builder;
6 use IAWP\Query;
7 use IAWP\Utils\Plugin;
8 /**
9 * Form tracking requires dynamic columns. If you have 2 forms, there will be 6 new columns. 2 for
10 * a summary and 2 per form. This class makes it easy to get all the forms a given site has, while
11 * caching the result, so it doesn't need to re-fetch from the database.
12 * @internal
13 */
14 class Form
15 {
16 private $id;
17 private $title;
18 private $plugin_id;
19 private static $forms = null;
20 private static $plugins = [
21 ['id' => 1, 'name' => 'Fluent Forms', 'plugin_slugs' => ['fluentform/fluentform.php']],
22 ['id' => 2, 'name' => 'WPForms', 'plugin_slugs' => ['wpforms-lite/wpforms.php', 'wpforms/wpforms.php']],
23 ['id' => 3, 'name' => 'Contact Form 7', 'plugin_slugs' => ['contact-form-7/wp-contact-form-7.php']],
24 ['id' => 4, 'name' => 'Gravity Forms', 'plugin_slugs' => ['gravityforms/gravityforms.php']],
25 ['id' => 5, 'name' => 'Ninja Forms', 'plugin_slugs' => ['ninja-forms/ninja-forms.php']],
26 ['id' => 6, 'name' => 'MailOptin', 'plugin_slugs' => ['mailoptin/mailoptin.php']],
27 ['id' => 7, 'name' => 'Convert Pro', 'plugin_slugs' => ['convertpro/convertpro.php']],
28 ['id' => 8, 'name' => 'Elementor Pro', 'plugin_slugs' => ['elementor-pro/elementor-pro.php']],
29 ['id' => 9, 'name' => 'JetFormBuilder', 'plugin_slugs' => ['jetformbuilder/jet-form-builder.php']],
30 ['id' => 10, 'name' => 'Formidable Forms', 'plugin_slugs' => ['formidable/formidable.php']],
31 ['id' => 11, 'name' => 'WS Form', 'plugin_slugs' => ['ws-form/ws-form.php', 'ws-form-pro/ws-form.php']],
32 ['id' => 12, 'name' => 'Amelia', 'plugin_slugs' => ['ameliabooking/ameliabooking.php']],
33 ['id' => 13, 'name' => 'Bricks Builder', 'theme' => 'bricks'],
34 ['id' => 14, 'name' => 'ARForms', 'plugin_slugs' => ['arforms-form-builder/arforms-form-builder.php']],
35 ['id' => 15, 'name' => 'Custom form submissions'],
36 ['id' => 16, 'name' => 'Bit Form', 'plugin_slugs' => ['bit-form/bitforms.php']],
37 ['id' => 17, 'name' => 'Forminator', 'plugin_slugs' => ['forminator/forminator.php']],
38 ['id' => 18, 'name' => 'Hustle', 'plugin_slugs' => ['wordpress-popup/popover.php', 'hustle/opt-in.php']],
39 ['id' => 19, 'name' => 'Avada', 'plugin_slugs' => ['fusion-builder/fusion-builder.php', 'fusion-core/fusion-core.php']],
40 ['id' => 20, 'name' => 'WP Store Locator', 'plugin_slugs' => ['wp-store-locator/wp-store-locator.php']],
41 // [
42 // 'id' => 21,
43 // 'name' => 'Thrive Leads',
44 // 'plugin_slugs' => ['thrive-leads/thrive-leads.php'],
45 // ],
46 ['id' => 22, 'name' => 'SureForms', 'plugin_slugs' => ['sureforms/sureforms.php']],
47 ['id' => 23, 'name' => 'Kali Forms', 'plugin_slugs' => ['kali-forms/kali-forms.php']],
48 ];
49 /**
50 * @var array A key/value pair (plugin_id/bool) of plugin IDs
51 */
52 private static $has_any_tracked_submissions_cache = [];
53 private function __construct(int $id, string $title, int $plugin_id)
54 {
55 $this->id = $id;
56 $this->title = $title;
57 $this->plugin_id = $plugin_id;
58 }
59 public function id() : int
60 {
61 return $this->id;
62 }
63 public function title() : string
64 {
65 return $this->title;
66 }
67 public function icon() : string
68 {
69 $lowercase = \strtolower($this->plugin_name());
70 $hyphenated = \str_replace(' ', '_', $lowercase);
71 return $hyphenated;
72 }
73 public function plugin_name() : string
74 {
75 return \IAWP\Form_Submissions\Form::find_plugin_by_id($this->plugin_id)['name'];
76 }
77 public function is_plugin_active() : bool
78 {
79 return self::static_is_plugin_active($this->plugin_id);
80 }
81 public function submissions_column() : string
82 {
83 return "form_submissions_for_{$this->id}";
84 }
85 public function conversion_rate_column() : string
86 {
87 return "form_conversion_rate_for_{$this->id}";
88 }
89 public static function has_active_form_plugin() : bool
90 {
91 return \is_array(self::get_first_active_form_plugin());
92 }
93 public static function get_first_active_form_plugin_name() : ?string
94 {
95 $active_plugin = self::get_first_active_form_plugin();
96 return \is_array($active_plugin) ? $active_plugin['name'] : null;
97 }
98 public static function find_form_by_column_name(string $column_name) : ?\IAWP\Form_Submissions\Form
99 {
100 $id = \intval(\preg_match('/(\\d+)\\z/', $column_name));
101 $forms = self::get_forms();
102 foreach ($forms as $form) {
103 if ($id === $form->id()) {
104 return $form;
105 }
106 }
107 return null;
108 }
109 /**
110 * @return Form[]
111 */
112 public static function get_forms() : array
113 {
114 if (\is_array(self::$forms)) {
115 return self::$forms;
116 }
117 $forms_table = Query::get_table_name(Query::FORMS);
118 $query = Illuminate_Builder::new()->select(['form_id', 'cached_form_title', 'plugin_id'])->from($forms_table);
119 $forms = \array_filter(\array_map(function ($form) {
120 if (\is_null(self::find_plugin_by_id($form->plugin_id))) {
121 return null;
122 }
123 return new self($form->form_id, $form->cached_form_title, $form->plugin_id);
124 }, $query->get()->all()));
125 \usort($forms, function (\IAWP\Form_Submissions\Form $a, \IAWP\Form_Submissions\Form $b) {
126 // First level sort by plugin name
127 $plugin_name_comparison = \strcmp($a->plugin_name(), $b->plugin_name());
128 // If types are equal, sort by 'name'
129 if ($plugin_name_comparison === 0) {
130 return \strcmp($a->title(), $b->title());
131 }
132 return $plugin_name_comparison;
133 });
134 self::$forms = $forms;
135 return self::$forms;
136 }
137 private static function has_any_tracked_submissions(int $plugin_id) : bool
138 {
139 if (\array_key_exists($plugin_id, self::$has_any_tracked_submissions_cache)) {
140 return self::$has_any_tracked_submissions_cache[$plugin_id];
141 }
142 $forms_table = Query::get_table_name(Query::FORMS);
143 $form_submissions_table = Query::get_table_name(Query::FORM_SUBMISSIONS);
144 $has_submissions = Illuminate_Builder::new()->from($forms_table, 'forms')->join("{$form_submissions_table} AS form_submissions", 'forms.form_id', '=', 'form_submissions.form_id')->where('forms.plugin_id', '=', $plugin_id)->exists();
145 self::$has_any_tracked_submissions_cache[$plugin_id] = $has_submissions;
146 return $has_submissions;
147 }
148 private static function get_first_active_form_plugin() : ?array
149 {
150 foreach (\IAWP\Form_Submissions\Form::$plugins as $plugin) {
151 if (self::static_is_plugin_active($plugin['id'])) {
152 return $plugin;
153 }
154 }
155 return null;
156 }
157 private static function static_is_plugin_active(int $plugin_id) : bool
158 {
159 $plugin = \IAWP\Form_Submissions\Form::find_plugin_by_id($plugin_id);
160 if (!\array_key_exists('plugin_slugs', $plugin) && !\array_key_exists('theme', $plugin)) {
161 return self::has_any_tracked_submissions($plugin_id);
162 }
163 if (\array_key_exists('theme', $plugin)) {
164 if (\get_template() === $plugin['theme']) {
165 return \true;
166 }
167 }
168 if (\array_key_exists('plugin_slugs', $plugin)) {
169 foreach ($plugin['plugin_slugs'] as $slug) {
170 if (\is_plugin_active($slug)) {
171 return \true;
172 }
173 }
174 }
175 return \false;
176 }
177 private static function find_plugin_by_id(int $id) : ?array
178 {
179 foreach (self::$plugins as $plugin) {
180 if ($plugin['id'] === $id) {
181 return $plugin;
182 }
183 }
184 return null;
185 }
186 }
187