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

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