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

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