PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.9.1
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.9.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 2.10.2 All 137 releases
bit-form / includes / Core / Util / FrontendHelpers.php

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

183 lines 5.5 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 WP_Rewrite;
6
7 final class FrontendHelpers
8 {
9 public static $isPageBuilder = false;
10 public static $bfFrontendFormIds = [];
11 public static $bfFormIdsFromPost = [];
12
13 public static $pageBuilderQueryParamsList = [
14 'et_pb_preview' => 'true', // divi
15 'vc_editable' => 'true', // wp bakery
16 'action' => 'ct_render_shortcode' // oxygen
17 ];
18
19 public static $pageBuilderURLParamsList = [
20 'wp-json/bricks/v1/render', // bricks
21 ];
22
23 public static $pageBuilderRefererQueryParamsList = [
24 'breakdance' => 'builder', // breakdance
25 ];
26
27 public static function getFormIdsFromPost()
28 {
29 global $post;
30 global $wpdb;
31 if (empty($post)) {
32 self::$bfFormIdsFromPost = [];
33 return [];
34 }
35 $postId = $post->ID;
36 $shortcodeFormIds = [];
37 // $bfMetaValues = $wpdb->get_results("SELECT pmt1.meta_value FROM wp_postmeta pmt1 LEFT OUTER JOIN wp_postmeta pmt2 ON (pmt1.meta_id < pmt2.meta_id AND pmt1.meta_key = pmt2.meta_key) WHERE pmt2.meta_id IS NULL AND pmt1.post_id = {$postId} AND pmt1.meta_value LIKE '%[bitform%' ORDER BY pmt1.meta_id DESC");
38 $bfMetaValues = $wpdb->get_results('SELECT meta_value FROM `' . $wpdb->postmeta . "` WHERE `post_id`={$postId}");
39 $postContent = $post->post_content;
40 $bfMetaValues[] = (object) ['meta_value' => $postContent];
41 foreach ($bfMetaValues as $bfShortcut) {
42 $meta_value = (is_string($bfShortcut->meta_value) && !empty($bfShortcut->meta_value)) ? $bfShortcut->meta_value : '';
43 $shortcodeIds = self::getShortCodeIds($meta_value);
44 $shortcodeFormIds = array_merge($shortcodeFormIds, $shortcodeIds);
45 }
46
47 self::$bfFormIdsFromPost = $shortcodeFormIds;
48 return $shortcodeFormIds;
49 }
50
51 public static function getShortCodeIds($content = '')
52 {
53 \preg_match_all("/\[bitform\s+id\s*=\s*('|\")\s*(\d+)\s*('|\")\]/", $content, $shortCode);
54 $ids = $shortCode[2];
55
56 return $ids;
57 }
58
59 public static function checkIsPageBuilder($srvr)
60 {
61 if (is_admin()) {
62 self::$isPageBuilder = true;
63 return true;
64 }
65 $current_url = $srvr['REQUEST_URI'];
66 $queryParams = self::parseQueryParams($current_url);
67 foreach (self::$pageBuilderQueryParamsList as $key => $value) {
68 if (isset($queryParams[$key]) && $queryParams[$key] === $value) {
69 self::$isPageBuilder = true;
70 return true;
71 }
72 }
73 foreach (self::$pageBuilderURLParamsList as $value) {
74 if (false !== strpos($current_url, $value)) {
75 self::$isPageBuilder = true;
76 return true;
77 }
78 }
79
80 $referrer = isset($srvr['HTTP_REFERER']) ? $srvr['HTTP_REFERER'] : '';
81 $referrerQueryParams = self::parseQueryParams($referrer);
82 foreach (self::$pageBuilderRefererQueryParamsList as $key => $value) {
83 if (isset($referrerQueryParams[$key]) && $referrerQueryParams[$key] === $value) {
84 self::$isPageBuilder = true;
85 return true;
86 }
87 }
88
89 return self::$isPageBuilder;
90 }
91
92 public static function parseQueryParams($url)
93 {
94 $url_components = parse_url($url);
95 if (isset($url_components['query'])) {
96 parse_str($url_components['query'], $queryParams);
97 return $queryParams;
98 }
99 return [];
100 }
101
102 public static function isRestRequest()
103 {
104 $prefix = rest_get_url_prefix();
105 if (defined('REST_REQUEST') && REST_REQUEST
106 || isset($_GET['rest_route'])
107 && 0 === strpos(trim($_GET['rest_route'], '\\/'), $prefix, 0)) {
108 return true;
109 }
110 global $wp_rewrite;
111 if (null === $wp_rewrite) {
112 $wp_rewrite = new WP_Rewrite();
113 }
114 $rest_url = wp_parse_url(trailingslashit(rest_url()));
115 $current_url = wp_parse_url(add_query_arg([]));
116 return 0 === strpos($current_url['path'], $rest_url['path'], 0);
117 }
118
119 public static function isAdminRequest()
120 {
121 $current_url = home_url(add_query_arg(null, null));
122 $admin_url = strtolower(admin_url());
123 $referrer = strtolower(wp_get_referer());
124
125 $requestFromBackend = self::isRestRequest() && strpos($admin_url, '/wp-admin/') > 0 && !strpos($admin_url, '/wp-admin/admin-ajax.php');
126
127 if ($requestFromBackend) {
128 return true;
129 }
130
131 if (0 === strpos($current_url, $admin_url)) {
132 if (0 === strpos($referrer, $admin_url)) {
133 return true;
134 } else {
135 if (function_exists('wp_doing_ajax')) {
136 return !wp_doing_ajax();
137 } else {
138 return !(defined('DOING_AJAX') && DOING_AJAX);
139 }
140 }
141 } else {
142 return false;
143 }
144 }
145
146 public static function parseUrlParams($url)
147 {
148 $url_components = parse_url($url);
149 if (isset($url_components['path'])) {
150 $urlParams = explode('/', $url_components['path']);
151 return $urlParams;
152 }
153
154 return [];
155 }
156
157 public static function setBfFrontendFormIds($formId)
158 {
159 self::$bfFrontendFormIds[] = $formId;
160 }
161
162 public static function getAllFormIdsInPage()
163 {
164 $bfFrontendFormIds = self::$bfFrontendFormIds;
165 $bfFormIdsFromPost = self::getFormIdsFromPost();
166 $allFormIds = array_merge($bfFrontendFormIds, $bfFormIdsFromPost);
167 return $allFormIds;
168 }
169
170 public static function getAllUniqFormIdsInPage()
171 {
172 return array_unique(self::getAllFormIdsInPage());
173 }
174
175 public static function hasMultipleForms()
176 {
177 $bfUniqFormIds = self::getAllFormIdsInPage();
178 $isPageBuilder = self::$isPageBuilder;
179 $bfMultipleFormsExists = $isPageBuilder ? true : count($bfUniqFormIds) > 1;
180 return $bfMultipleFormsExists;
181 }
182 }
183