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

171 lines 4.7 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\Hooks\Hooks;
14 use BitCode\BitForm\Core\Util\Activation;
15 use BitCode\BitForm\Core\Util\Deactivation;
16 use BitCode\BitForm\Core\Util\FrontendHelpers;
17 use BitCode\BitForm\Core\Util\Uninstallation;
18
19 final class Plugin {
20 /**
21 * Main instance of the plugin.
22 *
23 * @since 1.0.0-alpha
24 * @var Plugin|null
25 */
26 private static $instance = null;
27
28 /**
29 * Registers the plugin with WordPress.
30 *
31 * @since 1.0.0-alpha
32 */
33 public function register() {
34 add_action('plugins_loaded', [$this, 'init_plugin']);
35 add_action('init', [$this, 'checkIfFirstHttpRequest']);
36 (new Activation())->activate();
37 (new Deactivation())->register();
38 (new Uninstallation())->register();
39 }
40
41 public function checkIfFirstHttpRequest() {
42 $referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
43 $referrerQueryParams = FrontendHelpers::parseQueryParams($referrer);
44 $pageBuilderQueryParams = FrontendHelpers::$pageBuilderList;
45 $referrerMatchesWithPageBuilder = array_intersect_assoc($referrerQueryParams, $pageBuilderQueryParams);
46 if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($referrer) && 0 === count($referrerMatchesWithPageBuilder)) {
47 delete_transient('is_page_builder');
48 delete_transient('bf_frontend_form_ids');
49 $currentUrl = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
50 $currentUrlQueryParams = FrontendHelpers::parseQueryParams($currentUrl);
51 $currentUrlMatchesWithPageBuilder = array_intersect_assoc($currentUrlQueryParams, $pageBuilderQueryParams);
52 if (0 !== count($currentUrlMatchesWithPageBuilder)) {
53 set_transient('is_page_builder', true);
54 }
55 }
56 }
57
58 private function commaReplace($options, $lbl, $val) {
59 foreach ($options as $key => $option) {
60 if (!empty($option[$lbl]) && !empty($option[$val])) {
61 $options[$key][$val] = str_replace(',', '_', $option[$val]);
62 }
63 if (!empty($option[$lbl]) && empty($option[$val])) {
64 $options[$key][$val] = str_replace(',', '_', $option[$lbl]);
65 }
66 }
67 return $options;
68 }
69
70 private function replaceOptionSeparator($formContent) {
71 $updated = false;
72 foreach ($formContent['fields'] as $key => $field) {
73 if ('check' === $field['typ']) {
74 $formContent['fields'][$key]['opt'] = $this->commaReplace($field['opt'], 'lbl', 'val');
75 $updated = true;
76 }
77 if ('select' === $field['typ']) {
78 $formContent['fields'][$key]['opt'] = $this->commaReplace($field['opt'], 'label', 'value');
79 $updated = true;
80 }
81 }
82 return [$formContent, $updated];
83 }
84
85 public function optionFallBack() {
86 $formModel = new FormModel();
87 $forms = $formModel->get(
88 ['id', 'form_content']
89 );
90
91 foreach ($forms as $form) {
92 $formContent = json_decode($form->form_content, true);
93 $optReplaceComma = $this->replaceOptionSeparator($formContent);
94 if ($optReplaceComma[1]) {
95 $formModel->update(
96 [
97 'form_content' => wp_json_encode($optReplaceComma[0]),
98 ],
99 [
100 'id' => $form->id,
101 ]
102 );
103 }
104 }
105 }
106
107 /**
108 * Updates bit form content tables on wp db
109 *
110 * @return void
111 */
112 public function update_tables() {
113 if (!current_user_can('manage_options')) {
114 return;
115 }
116
117 global $bitforms_db_version;
118 $installed_db_version = get_site_option('bitforms_db_version');
119 if ($installed_db_version !== $bitforms_db_version) {
120 DB::migrate();
121 }
122 }
123
124 /**
125 * Plugin action links
126 *
127 * @param array $links
128 *
129 * @return array
130 */
131 public function init_plugin() {
132 Hooks::init_hooks();
133 $this->update_tables();
134 do_action('bitform_loaded');
135 }
136
137 /**
138 * Retrieves the main instance of the plugin.
139 *
140 * @since 1.0.0-alpha
141 *
142 * @return BITFORM Plugin main instance.
143 */
144 public static function instance() {
145 return static::$instance;
146 }
147
148 public function plugin_action_links($links) {
149 $links[] = '<a href="https://docs.form.bitapps.pro" 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 if (null !== static::$instance) {
163 return false;
164 }
165
166 static::$instance = new static($main_file);
167 static::$instance->register();
168 return true;
169 }
170 }
171