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