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

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