PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.15.3
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.15.3
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.15.3, at includes/Core/Util/FrontendHelpers.php

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