PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.21.4
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.21.4
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 / Core / Util / Activation.php

Activation.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.21.4, at includes/Core/Util/Activation.php

266 lines 7.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\Core\Util;
4
5 use BitCode\BitForm\Admin\Form\Helpers;
6 use BitCode\BitForm\Core\Database\DB;
7 use BitCode\BitForm\Core\Database\FormModel;
8 use WP_Error;
9
10 /**
11 * Class handling plugin activation.
12 *
13 * @since 1.0.0
14 */
15 final class Activation
16 {
17 private static $formModel;
18
19 public function __construct()
20 {
21 static::$formModel = new FormModel();
22 }
23
24 public function activate()
25 {
26 add_action('bitforms_activation', [$this, 'install']);
27 }
28
29 public function install()
30 {
31 $installed = get_option('bitforms_installed');
32
33 if (!$installed) {
34 DB::migrate();
35 $this->createUploadDir();
36 update_option('bitforms_installed', time());
37 }
38
39 update_option('bitforms_migrated_to_v2', true);
40 $appConfig = get_option('bitform_app_config');
41 if (!$appConfig) {
42 $config = (object) [];
43 $config->delete_table = true;
44 update_option('bitform_app_config', $config);
45 }
46 $appSettings = get_option('bitform_app_settings');
47 if (!$appSettings) {
48 $appSettings = (object) [];
49 $appSettings->globalMessages = Helpers::getDefaultGlobalMessages();
50 update_option('bitform_app_settings', $appSettings);
51 }
52
53 update_option('bitforms_version', BITFORMS_VERSION);
54 $this->layoutUpdate();
55 $this->createUploadDir();
56 $this->createFrontendPages();
57 }
58
59 private function createUploadDir()
60 {
61 if (!file_exists(BITFORMS_UPLOAD_DIR)) {
62 wp_mkdir_p(BITFORMS_UPLOAD_DIR);
63 }
64 if (!file_exists(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles')) {
65 wp_mkdir_p(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles');
66 }
67 if (file_exists(BITFORMS_UPLOAD_DIR) && !file_exists(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . '.htaccess')) {
68 $htaccessFile = fopen(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . '.htaccess', 'w');
69 $rules = '
70 <IfDefine php_flag>
71 php_flag engine off
72 </IfDefine>
73 Options -Indexes
74 Order allow,deny
75 Deny from all
76 Require all denied
77 ';
78 fwrite($htaccessFile, $rules);
79 fclose($htaccessFile);
80 }
81 if (file_exists(BITFORMS_UPLOAD_DIR) && !file_exists(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . 'index.php')) {
82 $indexFile = fopen(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . 'index.php', 'w');
83 $code = "<?php\n";
84 fwrite($indexFile, $code);
85 fclose($indexFile);
86 }
87 if (file_exists(BITFORMS_CONTENT_DIR) && !file_exists(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'index.php')) {
88 $indexFile = fopen(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'index.php', 'w');
89 $code = "<?php\n";
90 fwrite($indexFile, $code);
91 fclose($indexFile);
92 }
93 }
94
95 private function getForms()
96 {
97 $getForms = static::$formModel->get(
98 [
99 'id',
100 'status',
101 'form_content',
102 ],
103 [
104 'status' => [
105 'operator' => '!=',
106 'value' => 2,
107 ],
108 ]
109 );
110 return $getForms;
111 }
112
113 private function layoutUpdate()
114 {
115 $getForms = $this->getForms();
116
117 if (is_wp_error($getForms)) {
118 return $getForms;
119 }
120
121 global $wpdb;
122
123 foreach ($getForms as $index => $form) {
124 $content = json_decode($form->form_content);
125 $layout = $content->layout;
126 $updated = false;
127
128 if (is_array($layout)) {
129 continue;
130 }
131 foreach ($layout->lg as $lyIndex => $lg) {
132 if ($lg->w < 10) {
133 $updated = true;
134 $layout->lg[$lyIndex]->h = $lg->h * 20;
135 $layout->lg[$lyIndex]->w = $lg->w * 10;
136 $layout->lg[$lyIndex]->x = $lg->x * 10;
137 $layout->lg[$lyIndex]->y = $lg->y * 20;
138
139 $layout->md[$lyIndex]->h = $layout->md[$lyIndex]->h * 20;
140 $layout->md[$lyIndex]->w = $layout->md[$lyIndex]->w * 10;
141 $layout->md[$lyIndex]->x = $layout->md[$lyIndex]->x * 10;
142 $layout->md[$lyIndex]->y = $layout->md[$lyIndex]->y * 20;
143
144 $layout->sm[$lyIndex]->h = $layout->sm[$lyIndex]->h * 20;
145 $layout->sm[$lyIndex]->w = $layout->sm[$lyIndex]->w * 10;
146 $layout->sm[$lyIndex]->x = $layout->sm[$lyIndex]->x * 10;
147 $layout->sm[$lyIndex]->y = $layout->sm[$lyIndex]->y * 20;
148
149 if (isset($layout->lg[$lyIndex]->maxW)) {
150 $layout->lg[$lyIndex]->maxW = $lg->maxW * 10;
151 }
152 if (isset($layout->lg[$lyIndex]->maxH)) {
153 $layout->lg[$lyIndex]->maxH = $lg->maxH * 20;
154 }
155 if (isset($layout->lg[$lyIndex]->minW)) {
156 $layout->lg[$lyIndex]->minW = $lg->minW * 10;
157 }
158 if (isset($layout->lg[$lyIndex]->minH)) {
159 $layout->lg[$lyIndex]->minH = $lg->minH * 20;
160 }
161
162 if (isset($layout->md[$lyIndex]->maxW)) {
163 $layout->md[$lyIndex]->maxW = $layout->md[$lyIndex]->maxW * 10;
164 }
165 if (isset($layout->md[$lyIndex]->maxH)) {
166 $layout->md[$lyIndex]->maxH = $layout->md[$lyIndex]->maxH * 20;
167 }
168 if (isset($layout->md[$lyIndex]->minW)) {
169 $layout->md[$lyIndex]->minW = $layout->md[$lyIndex]->minW * 10;
170 }
171 if (isset($layout->md[$lyIndex]->minH)) {
172 $layout->md[$lyIndex]->minH = $layout->md[$lyIndex]->minH * 20;
173 }
174
175 if (isset($layout->sm[$lyIndex]->maxW)) {
176 $layout->sm[$lyIndex]->maxW = $layout->sm[$lyIndex]->maxW * 10;
177 }
178 if (isset($layout->sm[$lyIndex]->maxH)) {
179 $layout->sm[$lyIndex]->maxH = $layout->sm[$lyIndex]->maxH * 20;
180 }
181 if (isset($layout->sm[$lyIndex]->minW)) {
182 $layout->sm[$lyIndex]->minW = $layout->md[$lyIndex]->minW * 10;
183 }
184 if (isset($layout->sm[$lyIndex]->minH)) {
185 $layout->sm[$lyIndex]->minH = $layout->sm[$lyIndex]->minH * 20;
186 }
187 }
188 }
189
190 $getForms[$index]->form_content = wp_json_encode($content);
191
192 $wpdb->query('START TRANSACTION');
193
194 if ($updated) {
195 $status = static::$formModel->update(
196 [
197 'form_content' => $getForms[$index]->form_content,
198 ],
199 [
200 'id' => $form->id,
201 ]
202 );
203
204 if (is_wp_error($status)) {
205 $wpdb->query('ROLLBACK');
206 return new WP_Error('update_error', __('Sorry, Error occured in updated form', 'bit-form'));
207 }
208 }
209
210 $wpdb->query('COMMIT');
211 }
212 }
213
214 private function createFrontendPages()
215 {
216 $args = [
217 'label' => __('Bitforms Pages', 'bit-form'),
218 'public' => true,
219 'show_ui' => true,
220 'show_in_menu' => false,
221 'capability_type' => 'page',
222 'hierarchical' => false,
223 'query_var' => false,
224 'supports' => ['title'],
225 'show_in_rest' => false
226 ];
227 register_post_type('bitforms', $args);
228 $routes = get_option('bitforms_routes');
229 $route_value = [];
230 if (!$routes) {
231 $file_route_id = $this->insertPage();
232 if (!is_wp_error($file_route_id)) {
233 $route_value['file'] = $file_route_id;
234 }
235 } elseif (isset($routes['file'])) {
236 if (empty(get_post($routes['file']))) {
237 $file_route_id = $this->insertPage();
238 if (!is_wp_error($file_route_id)) {
239 $route_value['file'] = $file_route_id;
240 }
241 } else {
242 $file_page = ['ID' => $routes['file'], 'post_status' => 'publish'];
243 wp_update_post($file_page);
244 }
245 }
246 if (!empty($route_value)) {
247 update_option('bitforms_routes', esc_sql($route_value));
248 }
249 flush_rewrite_rules();
250 }
251
252 private function insertPage()
253 {
254 return wp_insert_post(
255 [
256 'post_name' => 'bitforms-file',
257 'comment_status' => 'closed',
258 'ping_status' => 'closed',
259 'post_content' => '<!-- wp:shortcode -->[bitforms-frontend-file /]<!-- /wp:shortcode -->',
260 'post_status' => 'publish',
261 'post_type' => 'bitforms'
262 ]
263 );
264 }
265 }
266