PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.15.0
Independent Analytics – WordPress Analytics Plugin v2.15.0
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 4 days ago Newsletter.php 6 months ago Submission.php 5 months ago Submission_Listener.php 4 days ago
Form.php
198 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 use IAWPSCOPED\Illuminate\Support\Str;
9 /**
10 * Form tracking requires dynamic columns. If you have 2 forms, there will be 6 new columns. 2 for
11 * a summary and 2 per form. This class makes it easy to get all the forms a given site has, while
12 * caching the result, so it doesn't need to re-fetch from the database.
13 * @internal
14 */
15 class Form
16 {
17 private $id;
18 private $title;
19 private $plugin_id;
20 private static $forms = null;
21 private static $is_plugin_active_cache = [];
22 private static $active_plugin_basenames = null;
23 private static $plugins = [['id' => 1, 'name' => 'Fluent Forms', 'plugin_slugs' => ['fluentform/fluentform.php']], ['id' => 2, 'name' => 'WPForms', 'plugin_slugs' => ['wpforms-lite/wpforms.php', 'wpforms/wpforms.php']], ['id' => 3, 'name' => 'Contact Form 7', 'plugin_slugs' => ['contact-form-7/wp-contact-form-7.php']], ['id' => 4, 'name' => 'Gravity Forms', 'plugin_slugs' => ['gravityforms/gravityforms.php']], ['id' => 5, 'name' => 'Ninja Forms', 'plugin_slugs' => ['ninja-forms/ninja-forms.php']], ['id' => 6, 'name' => 'MailOptin', 'plugin_slugs' => ['mailoptin/mailoptin.php']], ['id' => 7, 'name' => 'Convert Pro', 'plugin_slugs' => ['convertpro/convertpro.php']], ['id' => 8, 'name' => 'Elementor Pro', 'plugin_slugs' => ['elementor-pro/elementor-pro.php']], ['id' => 9, 'name' => 'JetFormBuilder', 'plugin_slugs' => ['jetformbuilder/jet-form-builder.php']], ['id' => 10, 'name' => 'Formidable Forms', 'plugin_slugs' => ['formidable/formidable.php']], ['id' => 11, 'name' => 'WS Form', 'plugin_slugs' => ['ws-form/ws-form.php', 'ws-form-pro/ws-form.php']], ['id' => 12, 'name' => 'Amelia', 'plugin_slugs' => ['ameliabooking/ameliabooking.php']], ['id' => 13, 'name' => 'Bricks Builder', 'theme' => 'bricks'], ['id' => 14, 'name' => 'ARForms', 'plugin_slugs' => ['arforms-form-builder/arforms-form-builder.php']], ['id' => 15, 'name' => 'Custom form submissions'], ['id' => 16, 'name' => 'Bit Form', 'plugin_slugs' => ['bit-form/bitforms.php']], ['id' => 17, 'name' => 'Forminator', 'plugin_slugs' => ['forminator/forminator.php']], ['id' => 18, 'name' => 'Hustle', 'plugin_slugs' => ['wordpress-popup/popover.php', 'hustle/opt-in.php']], ['id' => 19, 'name' => 'Avada', 'plugin_slugs' => ['fusion-builder/fusion-builder.php', 'fusion-core/fusion-core.php']], ['id' => 20, 'name' => 'WP Store Locator', 'plugin_slugs' => ['wp-store-locator/wp-store-locator.php']], ['id' => 21, 'name' => 'Thrive Leads', 'plugin_slugs' => ['thrive-leads/thrive-leads.php']], ['id' => 22, 'name' => 'SureForms', 'plugin_slugs' => ['sureforms/sureforms.php']], ['id' => 23, 'name' => 'Kali Forms', 'plugin_slugs' => ['kali-forms/kali-forms.php']], ['id' => 24, 'name' => 'Divi', 'theme' => 'Divi'], ['id' => 25, 'name' => 'MailPoet', 'plugin_slugs' => ['mailpoet/mailpoet.php']], ['id' => 26, 'name' => 'Mailchimp', 'plugin_slugs' => ['mailchimp-for-wp/mailchimp-for-wp.php']], ['id' => 27, 'name' => 'Kadence', 'plugin_slugs' => ['kadence-blocks/kadence-blocks.php']], ['id' => 29, 'name' => 'Newsletter', 'plugin_slugs' => ['newsletter/plugin.php']], ['id' => 30, 'name' => 'Everest Forms', 'plugin_slugs' => ['everest-forms/everest-forms.php']], ['id' => 31, 'name' => 'Request a Quote for WooCommerce', 'plugin_slugs' => ['*/class-addify-request-for-quote.php']], ['id' => 32, 'name' => 'Core Forms', 'plugin_slugs' => ['core-forms/core-forms.php']], ['id' => 33, 'name' => 'Superb Addons', 'plugin_slugs' => ['superb-blocks/plugin.php']], ['id' => 34, 'name' => 'Breakdance', 'plugin_slugs' => ['breakdance/plugin.php']]];
24 /**
25 * @var array A key/value pair (plugin_id/bool) of plugin IDs
26 */
27 private static $has_any_tracked_submissions_cache = [];
28 private function __construct(int $id, string $title, int $plugin_id)
29 {
30 $this->id = $id;
31 $this->title = $title;
32 $this->plugin_id = $plugin_id;
33 }
34 public function id() : int
35 {
36 return $this->id;
37 }
38 public function title() : string
39 {
40 return $this->title;
41 }
42 public function icon() : string
43 {
44 $lowercase = \strtolower($this->plugin_name());
45 $hyphenated = \str_replace(' ', '_', $lowercase);
46 return $hyphenated;
47 }
48 public function plugin_name() : string
49 {
50 return \IAWP\Form_Submissions\Form::find_plugin_by_id($this->plugin_id)['name'];
51 }
52 public function is_plugin_active() : bool
53 {
54 return self::static_is_plugin_active($this->plugin_id);
55 }
56 public function submissions_column() : string
57 {
58 return "form_submissions_for_{$this->id}";
59 }
60 public function conversion_rate_column() : string
61 {
62 return "form_conversion_rate_for_{$this->id}";
63 }
64 public static function has_active_form_plugin() : bool
65 {
66 return \is_array(self::get_first_active_form_plugin());
67 }
68 public static function get_first_active_form_plugin_name() : ?string
69 {
70 $active_plugin = self::get_first_active_form_plugin();
71 return \is_array($active_plugin) ? $active_plugin['name'] : null;
72 }
73 public static function find_form_by_column_name(string $column_name) : ?\IAWP\Form_Submissions\Form
74 {
75 $id = Str::match('/\\d+\\z/', $column_name);
76 if (!\ctype_digit($id)) {
77 return null;
78 }
79 $id = \intval($id);
80 $forms = self::get_forms();
81 foreach ($forms as $form) {
82 if ($id === $form->id()) {
83 return $form;
84 }
85 }
86 return null;
87 }
88 /**
89 * @return Form[]
90 */
91 public static function get_forms() : array
92 {
93 if (\is_array(self::$forms)) {
94 return self::$forms;
95 }
96 $forms_table = Query::get_table_name(Query::FORMS);
97 $query = Illuminate_Builder::new()->select(['form_id', 'cached_form_title', 'plugin_id'])->from($forms_table);
98 $forms = \array_filter(\array_map(function ($form) {
99 if (\is_null(self::find_plugin_by_id($form->plugin_id))) {
100 return null;
101 }
102 return new self($form->form_id, $form->cached_form_title, $form->plugin_id);
103 }, $query->get()->all()));
104 \usort($forms, function (\IAWP\Form_Submissions\Form $a, \IAWP\Form_Submissions\Form $b) {
105 // First level sort by plugin name
106 $plugin_name_comparison = \strcmp($a->plugin_name(), $b->plugin_name());
107 // If types are equal, sort by 'name'
108 if ($plugin_name_comparison === 0) {
109 return \strcmp($a->title(), $b->title());
110 }
111 return $plugin_name_comparison;
112 });
113 self::$forms = $forms;
114 return self::$forms;
115 }
116 public static function find_plugin_by_id(int $id) : ?array
117 {
118 foreach (self::$plugins as $plugin) {
119 if ($plugin['id'] === $id) {
120 return $plugin;
121 }
122 }
123 return null;
124 }
125 public static function is_plugin_slug_active(string $slug) : bool
126 {
127 if (Str::startsWith($slug, '*/')) {
128 $suffix = \strtolower(Str::after($slug, '*'));
129 foreach (self::active_plugin_basenames() as $active_plugin_basename) {
130 if (Str::endsWith(\strtolower($active_plugin_basename), $suffix)) {
131 return \true;
132 }
133 }
134 return \false;
135 }
136 return \is_plugin_active($slug);
137 }
138 private static function has_any_tracked_submissions(int $plugin_id) : bool
139 {
140 if (\array_key_exists($plugin_id, self::$has_any_tracked_submissions_cache)) {
141 return self::$has_any_tracked_submissions_cache[$plugin_id];
142 }
143 $forms_table = Query::get_table_name(Query::FORMS);
144 $form_submissions_table = Query::get_table_name(Query::FORM_SUBMISSIONS);
145 $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();
146 self::$has_any_tracked_submissions_cache[$plugin_id] = $has_submissions;
147 return $has_submissions;
148 }
149 private static function get_first_active_form_plugin() : ?array
150 {
151 foreach (\IAWP\Form_Submissions\Form::$plugins as $plugin) {
152 if (self::static_is_plugin_active($plugin['id'])) {
153 return $plugin;
154 }
155 }
156 return null;
157 }
158 private static function static_is_plugin_active(int $plugin_id) : bool
159 {
160 if (\array_key_exists($plugin_id, self::$is_plugin_active_cache)) {
161 return self::$is_plugin_active_cache[$plugin_id];
162 }
163 $plugin = \IAWP\Form_Submissions\Form::find_plugin_by_id($plugin_id);
164 if (!\array_key_exists('plugin_slugs', $plugin) && !\array_key_exists('theme', $plugin)) {
165 if (self::has_any_tracked_submissions($plugin_id)) {
166 self::$is_plugin_active_cache[$plugin_id] = \true;
167 return \true;
168 }
169 }
170 if (\array_key_exists('theme', $plugin)) {
171 if (\strtolower(\get_template()) === \strtolower($plugin['theme'])) {
172 self::$is_plugin_active_cache[$plugin_id] = \true;
173 return \true;
174 }
175 }
176 if (\array_key_exists('plugin_slugs', $plugin)) {
177 foreach ($plugin['plugin_slugs'] as $slug) {
178 if (self::is_plugin_slug_active($slug)) {
179 self::$is_plugin_active_cache[$plugin_id] = \true;
180 return \true;
181 }
182 }
183 }
184 self::$is_plugin_active_cache[$plugin_id] = \false;
185 return \false;
186 }
187 private static function active_plugin_basenames() : array
188 {
189 if (\is_array(self::$active_plugin_basenames)) {
190 return self::$active_plugin_basenames;
191 }
192 $site_active_plugins = (array) \get_option('active_plugins', []);
193 $network_active_plugins = \array_keys((array) \get_site_option('active_sitewide_plugins', []));
194 self::$active_plugin_basenames = \array_values(\array_unique(\array_merge($site_active_plugins, $network_active_plugins)));
195 return self::$active_plugin_basenames;
196 }
197 }
198