PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.21.13
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.21.13
V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 2.10.2 All 137 releases
bit-form / includes / Plugin.php

Plugin.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.21.13, at includes/Plugin.php

206 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BitCode\BitForm;
4
5 if (!defined('ABSPATH')) {
6 exit;
7 }
8
9 /**
10 * Main class for the plugin.
11 *
12 * @since 1.0.0-alpha
13 */
14
15 use BitCode\BitForm\BitApps\WPTelemetry\Telemetry\Telemetry;
16 use BitCode\BitForm\BitApps\WPTelemetry\Telemetry\TelemetryConfig;
17 use BitCode\BitForm\Core\Database\DB;
18 use BitCode\BitForm\Core\Database\FormModel;
19 use BitCode\BitForm\Core\Fallback\FormFallback;
20 use BitCode\BitForm\Core\Hooks\Hooks;
21 use BitCode\BitForm\Core\Util\Activation;
22 use BitCode\BitForm\Core\Util\Deactivation;
23 use BitCode\BitForm\Core\Util\Uninstallation;
24 use BitCode\BitForm\Widgets\RegisterBitFormElementorWidget;
25
26 final class Plugin
27 {
28 /**
29 * Main instance of the plugin.
30 *
31 * @since 1.0.0-alpha
32 * @var Plugin|null
33 */
34 private static $instance = null;
35
36 /**
37 * Registers the plugin with WordPress.
38 *
39 * @since 1.0.0-alpha
40 */
41 public function register()
42 {
43 add_action('plugins_loaded', [$this, 'init_plugin']);
44 add_action('init', [$this, 'localization_setup']);
45 (new Activation())->activate();
46 (new Deactivation())->register();
47 (new Uninstallation())->register();
48
49 $this->initWpTelemetry();
50 }
51
52 private function commaReplace($options, $lbl, $val)
53 {
54 foreach ($options as $key => $option) {
55 if (!empty($option[$lbl]) && !empty($option[$val])) {
56 $options[$key][$val] = str_replace(',', '_', $option[$val]);
57 }
58 if (!empty($option[$lbl]) && empty($option[$val])) {
59 $options[$key][$val] = str_replace(',', '_', $option[$lbl]);
60 }
61 }
62 return $options;
63 }
64
65 private function replaceOptionSeparator($formContent)
66 {
67 $updated = false;
68 foreach ($formContent['fields'] as $key => $field) {
69 if ('check' === $field['typ']) {
70 $formContent['fields'][$key]['opt'] = $this->commaReplace($field['opt'], 'lbl', 'val');
71 $updated = true;
72 }
73 if ('select' === $field['typ']) {
74 $formContent['fields'][$key]['opt'] = $this->commaReplace($field['opt'], 'label', 'value');
75 $updated = true;
76 }
77 }
78 return [$formContent, $updated];
79 }
80
81 public function optionFallBack()
82 {
83 $formModel = new FormModel();
84 $forms = $formModel->get(
85 ['id', 'form_content']
86 );
87
88 foreach ($forms as $form) {
89 $formContent = json_decode($form->form_content, true);
90 $optReplaceComma = $this->replaceOptionSeparator($formContent);
91 if ($optReplaceComma[1]) {
92 $formModel->update(
93 [
94 'form_content' => wp_json_encode($optReplaceComma[0]),
95 ],
96 [
97 'id' => $form->id,
98 ]
99 );
100 }
101 }
102 }
103
104 /**
105 * Updates bit form content tables on wp db
106 *
107 * @return void
108 */
109 public function update_tables()
110 {
111 if (!current_user_can('manage_options')) {
112 return;
113 }
114
115 global $bitforms_db_version;
116 $installed_db_version = get_site_option('bitforms_db_version');
117 if ($installed_db_version !== $bitforms_db_version) {
118 DB::migrate();
119 }
120 }
121
122 /**
123 * Plugin action links
124 *
125 * @param array $links
126 *
127 * @return array
128 */
129 public function init_plugin()
130 {
131 $currentBitFormVersion = BITFORMS_VERSION;
132 $installedBitFormVersion = get_option('bitforms_version');
133 if ($currentBitFormVersion !== $installedBitFormVersion) {
134 (new FormFallback())->resetJsGeneratedPageIds();
135 }
136 Hooks::init_hooks();
137 $this->update_tables();
138 $this->initWidgets();
139 do_action('bitform_loaded');
140 }
141
142 /**
143 * Retrieves the main instance of the plugin.
144 *
145 * @since 1.0.0-alpha
146 *
147 * @return BITFORM Plugin main instance.
148 */
149 public static function instance()
150 {
151 return static::$instance;
152 }
153
154 public function plugin_action_links($links)
155 {
156 $links[] = '<a href="https://bitapps.pro/docs/bit-form/" target="_blank">' . __('Docs', 'bit-form') . '</a>';
157
158 return $links;
159 }
160
161 /**
162 * Loads the plugin main instance and initializes it.
163 *
164 * @param string $main_file Absolute path to the plugin main file.
165 *
166 * @return bool True if the plugin main instance could be loaded, false otherwise.
167 */
168 public static function load($main_file)
169 {
170 if (null !== static::$instance) {
171 return false;
172 }
173
174 static::$instance = new static($main_file);
175 static::$instance->register();
176 return true;
177 }
178
179 private function initWidgets()
180 {
181 if (defined('ELEMENTOR_VERSION')) {
182 new RegisterBitFormElementorWidget();
183 }
184 }
185
186 public function localization_setup()
187 {
188 load_plugin_textdomain('bit-form', false, dirname(BITFORMS_PLUGIN_BASENAME) . '/languages'); // phpcs:ignore PluginCheck.CodeAnalysis.DiscouragedFunctions.load_plugin_textdomainFound
189 }
190
191 private function initWpTelemetry()
192 {
193 TelemetryConfig::setSlug(Config::SLUG);
194 TelemetryConfig::setTitle(Config::TITLE);
195 TelemetryConfig::setVersion(Config::VERSION);
196 TelemetryConfig::setPrefix(Config::VAR_PREFIX);
197
198 TelemetryConfig::setServerBaseUrl('https://wp-api.bitapps.pro/public/');
199 TelemetryConfig::setTermsUrl('https://bitapps.pro/terms-of-service/');
200 TelemetryConfig::setPolicyUrl('https://bitapps.pro/privacy-policy/');
201
202 Telemetry::report()->addPluginData()->init();
203 Telemetry::feedback()->init();
204 }
205 }
206