PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 1.5.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v1.5.2
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 / Plugin.php

Plugin.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 1.5.2, at includes/Plugin.php

606 lines 21.7 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;
4
5 /**
6 * Main class for the plugin.
7 *
8 * @since 1.0.0-alpha
9 */
10
11 use BitCode\BitForm\Admin\Admin_Bar;
12 use BitCode\BitForm\Core\Database\DB;
13 use BitCode\BitForm\Core\Util\Activation;
14 use BitCode\BitForm\Core\Form\FormHandler;
15 use BitCode\BitForm\Core\Ajax\AjaxService;
16 use BitCode\BitForm\Core\Util\Deactivation;
17 use BitCode\BitForm\Core\Util\Uninstallation;
18 use BitCode\BitForm\Core\Capability\Request;
19 use BitCode\BitForm\Core\Util\GutenBlockProvider;
20 use BitCode\BitForm\Core\Integration\Integrations;
21 use BitCode\BitForm\Core\Util\FileDownloadProvider;
22 use BitCode\BitForm\Core\Fallback\FormEntryMetaChange;
23 use BitCode\BitForm\Admin\Form\AdminFormHandler;
24 use BitCode\BitForm\Core\Database\FormModel;
25
26 final class Plugin
27 {
28
29 /**
30 * Main instance of the plugin.
31 *
32 * @since 1.0.0-alpha
33 * @var Plugin|null
34 */
35 private static $instance = null;
36
37
38 /**
39 * Registers the plugin with WordPress.
40 *
41 * @since 1.0.0-alpha
42 */
43 public function register()
44 {
45 add_action('plugins_loaded', array($this, 'init_plugin'));
46 (new Activation())->activate();
47 (new Deactivation())->register();
48 (new Uninstallation())->register();
49 }
50
51
52 /**
53 * Initialize the hooks
54 *
55 * @return void
56 */
57 public function init_hooks()
58 {
59 add_action('init', array($this, 'registerBitformsPostType'));
60 add_action('init', array($this, 'registerCustomPostType'));
61 add_action("bitforms_exec_integrations", array(Integrations::class, 'integrationExecutionHelper'), 1, 5);
62 // Localize our plugin
63 add_action('init', array($this, 'localization_setup'));
64
65 // initialize the classes
66 add_action('init', array($this, 'init_classes'));
67
68 add_action('init', array($this, 'entryMetaKeyUpdate'));
69
70 add_filter('plugin_action_links_' . plugin_basename(BITFORMS_PLUGIN_MAIN_FILE), array($this, 'plugin_action_links'));
71 }
72
73 public function entryMetaKeyUpdate()
74 {
75 $installed = get_option('bitforms_installed');
76 $oldversion = null;
77 if ($installed) {
78 $oldversion = get_option('bitforms_version');
79 }
80 if ($oldversion && version_compare($oldversion, BITFORMS_VERSION, '!=')) {
81 update_option('bitforms_version', BITFORMS_VERSION);
82 if (version_compare('1.0.4', $oldversion, '>=')) {
83 // if version is lower or equal than 1.0.4
84 $formEntryMetaChange = new FormEntryMetaChange();
85 $formEntryMetaChange->changeMeta();
86 }
87
88 if (version_compare('1.4.1', $oldversion, '>=')) {
89 // if version is lower or equal than 1.4.1
90 $this->addButtonToFieds();
91 }
92
93 if (version_compare('1.4.7', $oldversion, '>=')) {
94 // if version is equal or lower than 1.4.7
95 $this->validationErrMsgFallback();
96 }
97
98 if (version_compare('1.4.11', $oldversion, '>=')) {
99 // if version is equal or lower than 1.4.11
100 $this->buttonValidsObjectFallback();
101 }
102
103 if (version_compare('1.4.12.2', $oldversion, '>=')) {
104 // if version is equal or lower than 1.4.12.2
105 $this->validationEmailFallback();
106 }
107 }
108 }
109
110 public function validationErrMsgFallback()
111 {
112 $formModel = new FormModel();
113 $forms = $formModel->get(
114 ['id', 'form_content']
115 );
116
117 if (isset($forms) && !is_wp_error($forms)) {
118 foreach ($forms as $form) {
119 $formID = $form->id;
120 $formContent = json_decode($form->form_content);
121 $fields = $formContent->fields;
122
123 foreach ($fields as $fldKey => $fldData) {
124 if (isset($fldData->valid->req)) {
125 if (!isset($fldData->err)) {
126 $fldData->err = (object) [];
127 }
128 $fldData->err->req = (object) [
129 'show' => true,
130 'dflt' => 'This field is required'
131 ];
132 }
133
134 if ($fldData->typ === 'check') {
135 $reqOpts = array_map(function ($op) {
136 return $op->lbl;
137 }, array_filter($fldData->opt, function ($ot) {
138 return isset($ot->req);
139 }));
140 if (!empty($reqOpts)) {
141 if (!isset($fldData->err)) {
142 $fldData->err = (object) [];
143 }
144 $fldData->err->req = (object) [
145 'show' => true,
146 'dflt' => implode(', ', $reqOpts) . ' is required'
147 ];
148 }
149 }
150
151 if ($fldData->typ === 'number') {
152 if (!isset($fldData->err)) {
153 $fldData->err = (object) [];
154 }
155 $fldData->err->invalid = (object) [
156 'show' => true,
157 'dflt' => 'Number is invalid'
158 ];
159
160 if (isset($fldData->mn)) {
161 $fldData->err->mn = (object) [
162 'show' => true,
163 'dflt' => 'Minimum number is ' . $fldData->mn
164 ];
165 }
166
167 if (isset($fldData->mx)) {
168 $fldData->err->mx = (object) [
169 'show' => true,
170 'dflt' => 'Maximum number is ' . $fldData->mx
171 ];
172 }
173 }
174
175 if ($fldData->typ === 'email') {
176 $fldData->pattern = '^$_bf_$w+([.-]?$_bf_$w+)*@$_bf_$w+([.-]?$_bf_$w+)*($_bf_$.$_bf_$w{2,3})+$';
177 if (!isset($fldData->err)) {
178 $fldData->err = (object) [];
179 }
180 $fldData->err->invalid = (object) [
181 'show' => true,
182 'dflt' => 'Email is invalid'
183 ];
184 }
185
186 if ($fldData->typ === 'url') {
187 $fldData->attr->pattern = '^(?:(?:https?|ftp):$_bf_$/$_bf_$/)?(?:(?!(?:10|127)(?:$_bf_$.$_bf_$d{1,3}){3})(?!(?:169$_bf_$.254|192$_bf_$.168)(?:$_bf_$.$_bf_$d{1,3}){2})(?!172$_bf_$.(?:1[6-9]|2$_bf_$d|3[0-1])(?:$_bf_$.$_bf_$d{1,3}){2})(?:[1-9]$_bf_$d?|1$_bf_$d$_bf_$d|2[01]$_bf_$d|22[0-3])(?:$_bf_$.(?:1?$_bf_$d{1,2}|2[0-4]$_bf_$d|25[0-5])){2}(?:$_bf_$.(?:[1-9]$_bf_$d?|1$_bf_$d$_bf_$d|2[0-4]$_bf_$d|25[0-4]))|(?:(?:[a-z$_bf_$u00a1-$_bf_$uffff0-9]-*)*[a-z$_bf_$u00a1-$_bf_$uffff0-9]+)(?:$_bf_$.(?:[a-z$_bf_$u00a1-$_bf_$uffff0-9]-*)*[a-z$_bf_$u00a1-$_bf_$uffff0-9]+)*(?:$_bf_$.(?:[a-z$_bf_$u00a1-$_bf_$uffff]{2,})))(?::$_bf_$d{2,5})?(?:$_bf_$/$_bf_$S*)?$';
188 if (!isset($fldData->err)) {
189 $fldData->err = (object) [];
190 }
191 $fldData->err->invalid = [
192 'show' => true,
193 'dflt' => 'URL is invalid'
194 ];
195 }
196 $fields->{$fldKey} = $fldData;
197 }
198
199 $formContent->fields = $fields;
200
201 $formModel->update(
202 array(
203 "form_content" => wp_json_encode($formContent)
204 ),
205 array(
206 "id" => $formID,
207 )
208 );
209 }
210 }
211 }
212
213 public function validationEmailFallback()
214 {
215 $formModel = new FormModel();
216 $forms = $formModel->get(
217 ['id', 'form_content']
218 );
219
220 if (!is_wp_error($forms)) {
221 foreach ($forms as $form) {
222 $formID = $form->id;
223 $formContent = json_decode($form->form_content);
224 $fields = $formContent->fields;
225
226 foreach ($fields as $fldKey => $fldData) {
227 if ($fldData->typ === 'email') {
228 $fldData->pattern = '^$_bf_$w+([.-]?$_bf_$w+)*@$_bf_$w+([.-]?$_bf_$w+)*($_bf_$.$_bf_$w{1,24})+$';
229 }
230 $fields->{$fldKey} = $fldData;
231 }
232
233 $formContent->fields = $fields;
234
235 $formModel->update(
236 array(
237 "form_content" => wp_json_encode($formContent)
238 ),
239 array(
240 "id" => $formID,
241 )
242 );
243 }
244 }
245 }
246
247 public function buttonValidsObjectFallback()
248 {
249 $formModel = new FormModel();
250 $forms = $formModel->get(
251 ['id', 'form_content']
252 );
253
254 if (!is_wp_error($forms)) {
255 foreach ($forms as $form) {
256 $formID = $form->id;
257 $formContent = json_decode($form->form_content);
258 $fields = $formContent->fields;
259
260 foreach ($fields as $fldKey => $fldData) {
261 if ($fldData->typ === 'button' && !isset($fldData->valid)) {
262 $fldData->valid = (object) [];
263 }
264 $fields->{$fldKey} = $fldData;
265 }
266
267 $formContent->fields = $fields;
268
269 $formModel->update(
270 array(
271 "form_content" => wp_json_encode($formContent)
272 ),
273 array(
274 "id" => $formID,
275 )
276 );
277 }
278 }
279 }
280
281 /**
282 * Updates bit form content tables on wp db
283 *
284 * @return void
285 */
286 public function update_tables()
287 {
288 if (!current_user_can('manage_options')) {
289 return;
290 }
291
292 global $bitforms_db_version;
293 $installed_db_version = get_site_option("bitforms_db_version");
294 if ($installed_db_version != $bitforms_db_version) {
295 DB::migrate();
296 }
297 }
298
299 /**
300 * Register post type for bitforms
301 *
302 * @return void
303 */
304 public function registerBitformsPostType()
305 {
306 $args = array(
307 'label' => __('Bitform Pages', 'bit-form'),
308 'public' => true,
309 'show_ui' => true,
310 'show_in_menu' => false,
311 'capability_type' => 'page',
312 'hierarchical' => false,
313 'query_var' => false,
314 'supports' => array('title'),
315 'show_in_rest' => false
316 );
317 register_post_type('bitforms', $args);
318 }
319
320 public function registerCustomPostType()
321 {
322 flush_rewrite_rules(false);
323 $cpts = get_option('bitform_custom_post_types');
324 if (!empty($cpts)) {
325 foreach ($cpts as $cpt) {
326 $labels = array(
327 'name' => _x($cpt->name, 'Post type general name', 'bit-form'),
328 'singular_name' => _x($cpt->singular_label, 'Post type singular name', 'bit-form'),
329 'menu_name' => _x($cpt->menu_name, 'Admin Menu text', 'bit-form'),
330 );
331 $args = array(
332 'labels' => $labels,
333 'public' => $cpt->public == 1 ? true : false,
334 'publicly_queryable' => $cpt->public_queryable == 1 ? true : false,
335 'show_ui' => $cpt->show_ui == 1 ? true : false,
336 'show_in_menu' => $cpt->show_in_menu == 1 ? true : false,
337 'query_var' => true,
338 'rewrite' => array('slug' => $cpt->name),
339 'capability_type' => 'post',
340 'has_archive' => true,
341 'hierarchical' => false,
342 'show_in_rest' => isset($cpt->show_in_rest) == 1 ? true : false,
343 'menu_icon' => $cpt->menu_icon,
344 'supports' => array('title', 'editor', 'author', 'excerpt', 'comments'),
345 );
346 register_post_type($cpt->name, $args);
347 }
348 }
349 }
350
351 /**
352 * Initialize plugin for localization
353 *
354 * @uses load_plugin_textdomain()
355 */
356 public function localization_setup()
357 {
358 load_plugin_textdomain('bitform', false, dirname(BITFORMS_PLUGIN_BASENAME) . '/languages');
359 }
360
361 /**
362 * Instantiate the required classes
363 *
364 * @return void
365 */
366 public function init_classes()
367 {
368 if (Request::Check('admin')) {
369 (new Admin_Bar())->register();
370 }
371 if (Request::Check('ajax')) {
372 new AjaxService();
373 }
374 if (Request::Check('frontend')) {
375 $formHandler = new FormHandler();
376 $formHandler->frontend;
377 }
378 if (Request::isPluginPage()) {
379 (new FileDownloadProvider())->register();
380 }
381 if (current_user_can('edit_posts')) {
382 (new GutenBlockProvider())->register();
383 }
384 }
385
386 /**
387 * Plugin action links
388 *
389 * @param array $links
390 *
391 * @return array
392 */
393 public function plugin_action_links($links)
394 {
395 $links[] = '<a href="https://docs.form.bitapps.pro" target="_blank">' . __('Docs', 'bit-form') . '</a>';
396
397 return $links;
398 }
399
400
401 public function init_plugin()
402 {
403 $this->init_hooks();
404 $this->update_tables();
405 do_action('bitform_loaded');
406 }
407
408 /**
409 * Retrieves the main instance of the plugin.
410 *
411 * @since 1.0.0-alpha
412 *
413 * @return BITFORM Plugin main instance.
414 */
415 public static function instance()
416 {
417 return static::$instance;
418 }
419
420 /**
421 * Loads the plugin main instance and initializes it.
422 *
423 * @param string $main_file Absolute path to the plugin main file.
424 *
425 * @return bool True if the plugin main instance could be loaded, false otherwise.
426 */
427 public static function load($main_file)
428 {
429 if (null !== static::$instance) {
430 return false;
431 }
432
433 static::$instance = new static($main_file);
434 static::$instance->register();
435 return true;
436 }
437
438 public function addButtonToFieds()
439 {
440 // if version is lower or equal than 1.3.13
441 $formModel = new FormModel();
442 $forms = $formModel->get(
443 ['id', 'form_content'],
444 [
445 'form_content' => ['operator' => 'LIKE', 'value' => '%\"buttons\":%']
446 ]
447 );
448 if (!is_wp_error($forms)) {
449 $adminFormHandler = new AdminFormHandler();
450 foreach ($forms as $form) {
451 $formID = $form->id;
452 $formContent = json_decode($form->form_content);
453 $fields = json_encode($formContent->fields);
454 if (isset($formContent->buttons)) {
455 if (strpos($fields, '"btnTyp":"submit"') === false) {
456 $align = isset($formContent->buttons->align) ? $formContent->buttons->align : 'right';
457 $btnSiz = isset($formContent->buttons->btnSiz) ? $formContent->buttons->btnSiz : 'md';
458 $fulW = isset($formContent->buttons->fulW) ? $formContent->buttons->fulW : false;
459 $btnData = [
460 'typ' => 'button',
461 'btnSiz' => $btnSiz,
462 'fulW' => $fulW,
463 ];
464 $newID = 0;
465 foreach ($formContent->fields as $key => $value) {
466 $oldID = \preg_replace('/bf?\d+-(\d+)-?/', "$1", $key);
467 if ($oldID > $newID) {
468 $newID = $oldID;
469 }
470 }
471 $newID = $newID + 1;
472 $sBtnID = "bf${formID}-${newID}";
473 $lY = 0;
474 $mY = 0;
475 $sY = 0;
476 foreach ($formContent->layout->lg as $lg) {
477 if ($lg->y > $lY) {
478 $lY = $lg->y;
479 }
480 }
481 $lY = $lY + $lg->h + 1;
482 foreach ($formContent->layout->md as $md) {
483 if ($md->y > $mY) {
484 $mY = $md->y;
485 }
486 }
487 $mY = $mY + $md->h + 1;
488 foreach ($formContent->layout->sm as $sm) {
489 if ($sm->y > $sY) {
490 $sY = $sm->y;
491 }
492 }
493 $sY = $sY + $sm->h + 1;
494 if (isset($formContent->buttons->rstBtnTxt)) {
495 $newID += 1;
496 $rBtnID = "bf${formID}-${newID}";
497
498 if ($fulW) {
499 $btnData['btnTyp'] = 'submit';
500 $btnData['align'] = $align;
501 $btnData['txt'] = $formContent->buttons->subBtnTxt;
502 $formContent->fields->{$sBtnID} = (object) $btnData;
503 $rBtnData = $btnData;
504 $rBtnData['btnTyp'] = 'reset';
505 $rBtnData['align'] = $align;
506 $rBtnData['txt'] = $formContent->buttons->rstBtnTxt;
507 $formContent->fields->{$rBtnID} = (object) $rBtnData;
508 } else {
509 $btnData['btnTyp'] = 'submit';
510 $btnData['align'] = 'right';
511 $btnData['txt'] = $formContent->buttons->subBtnTxt;
512 $formContent->fields->{$sBtnID} = (object) $btnData;
513 $rBtnData = $btnData;
514 $rBtnData['btnTyp'] = 'reset';
515 $rBtnData['align'] = 'left';
516 $rBtnData['txt'] = $formContent->buttons->rstBtnTxt;
517 $formContent->fields->{$rBtnID} = (object) $rBtnData;
518 }
519 $subBtnLay = [
520 "h" => 2,
521 "i" => $sBtnID,
522 "minH" => 2,
523 "x" => 0,
524 "y" => 1000
525 ];
526
527 $subBtnLay["w"] = 3;
528 $subBtnLay["y"] = $lY;
529
530
531 $formContent->layout->lg[] = (object) $subBtnLay;
532
533 $subBtnLay["w"] = 2;
534 $subBtnLay["y"] = $mY;
535
536
537 $formContent->layout->md[] = (object) $subBtnLay;
538
539 $subBtnLay["w"] = 1;
540 $subBtnLay["y"] = $sY;
541
542
543 $formContent->layout->sm[] = (object) $subBtnLay;
544
545
546 $subBtnLay["i"] = $rBtnID;
547 $subBtnLay["x"] = 3;
548 $subBtnLay["w"] = 3;
549 $subBtnLay["y"] = $lY;
550
551 $formContent->layout->lg[] = (object) $subBtnLay;
552
553 $subBtnLay["x"] = 2;
554 $subBtnLay["w"] = 2;
555 $subBtnLay["y"] = $mY;
556
557 $formContent->layout->md[] = (object) $subBtnLay;
558
559 $subBtnLay["w"] = 1;
560 $subBtnLay["x"] = 1;
561 $subBtnLay["y"] = $sY;
562
563 $formContent->layout->sm[] = (object) $subBtnLay;
564 } else {
565 $btnData['btnTyp'] = 'submit';
566 $btnData['txt'] = $formContent->buttons->subBtnTxt;
567 $btnData['align'] = $align;
568 $formContent->fields->{$sBtnID} = (object) $btnData;
569
570 $subBtnLay = [
571 "h" => 2,
572 "i" => $sBtnID,
573 "minH" => 2,
574 "x" => 0,
575 "w" => 6,
576 ];
577
578 $subBtnLay["y"] = $lY;
579
580 $formContent->layout->lg[] = (object) $subBtnLay;
581
582 $subBtnLay["y"] = $mY;
583
584 $formContent->layout->md[] = (object) $subBtnLay;
585
586 $subBtnLay["y"] = $sY;
587
588 $formContent->layout->sm[] = (object) $subBtnLay;
589 }
590 }
591 $adminFormHandler->saveLayoutStyleSheet($formContent->layout, 'bitform-layout-' . $formID . '.css');
592 unset($formContent->buttons);
593 $formModel->update(
594 array(
595 "form_content" => wp_json_encode($formContent)
596 ),
597 array(
598 "id" => $formID,
599 )
600 );
601 }
602 }
603 }
604 }
605 }
606