PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.19.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.19.2
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.19.2, at includes/Plugin.php

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