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

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