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 / Core / Util / Activation.php

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

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