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

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