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

354 lines 11.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 BitCode\BitForm\Core\Form\FormManager;
6 use WP_Rewrite;
7
8 final class FrontendHelpers
9 {
10 public static $isPageBuilder = false;
11 public static $bfFrontendFormIds = [];
12 public static $bfFrontendViewIds = [];
13 public static $bfFormIdsFromPost = [];
14 public static $bfViewIdsFromPost = [];
15 private static $formsPermissions = [];
16
17 public static $pageBuilderQueryParamsList = [
18 'et_pb_preview' => 'true', // divi
19 'vc_editable' => 'true', // wp bakery
20 'action' => 'ct_render_shortcode' // oxygen
21 ];
22
23 public static $pageBuilderURLParamsList = [
24 'wp-json/bricks/v1/render', // bricks
25 ];
26
27 public static $pageBuilderRefererQueryParamsList = [
28 'breakdance' => 'builder', // breakdance
29 ];
30
31 public static function getFormIdsFromPost()
32 {
33 global $post;
34 global $wpdb;
35 if (empty($post)) {
36 self::$bfFormIdsFromPost = [];
37 return [];
38 }
39 $postId = $post->ID;
40 $shortcodeFormIds = [];
41 $bfMetaValues = $wpdb->get_results(
42 $wpdb->prepare(
43 'SELECT meta_value FROM `' . $wpdb->postmeta . '` WHERE `post_id`=%d',
44 $postId
45 )
46 );
47 $postContent = $post->post_content;
48 $bfMetaValues[] = (object) ['meta_value' => $postContent]; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
49 foreach ($bfMetaValues as $bfShortcut) {
50 $meta_value = (is_string($bfShortcut->meta_value) && !empty($bfShortcut->meta_value)) ? $bfShortcut->meta_value : '';
51 $shortcodeIds = self::getShortCodeIds($meta_value);
52 $shortcodeFormIds = array_merge($shortcodeFormIds, $shortcodeIds);
53 }
54
55 self::$bfFormIdsFromPost = $shortcodeFormIds;
56 return $shortcodeFormIds;
57 }
58
59 public static function getShortCodeIds($content = '')
60 {
61 $pattern = '/' . get_shortcode_regex(['bitform']) . '/';
62 \preg_match_all($pattern, $content, $short);
63
64 $formIds = [];
65 foreach ($short[3] as $attr_string) {
66 $attr = shortcode_parse_atts($attr_string);
67 if (!empty($attr['id'])) {
68 $formIds[] = $attr['id'];
69 }
70 }
71
72 // Regex handles:
73 // 1. [bitform ... id=... ]
74 // 2. id="123" or id='123' or id=123
75 // 3. Escaped quotes id=\"123\" or id=\'123\' (common in builder meta)
76 // \preg_match_all('/\[bitform\s+\b[^\]]*\bid\s*=\s*(?:\\\\?[\'"])?(\d+)(?:\\\\?[\'"])?[^\]]*\]/', $content, $shortCode);
77 // $ids = $shortCode[1];
78
79 return $formIds;
80 }
81
82 public static function getViewIdsFromPost()
83 {
84 global $post;
85 global $wpdb;
86 if (empty($post)) {
87 self::$bfViewIdsFromPost = [];
88 return [];
89 }
90 $postId = $post->ID;
91 $shortcodeViewIds = [];
92 $bfMetaValues = $wpdb->get_results($wpdb->prepare('SELECT meta_value FROM `' . $wpdb->postmeta . '` WHERE `post_id` = %d', $postId));
93 $postContent = $post->post_content;
94
95 $bfMetaValues[] = (object) ['meta_value' => $postContent]; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
96 foreach ($bfMetaValues as $bfShortcut) {
97 $meta_value = (is_string($bfShortcut->meta_value) && !empty($bfShortcut->meta_value)) ? $bfShortcut->meta_value : '';
98 $shortcodeIds = self::getViewShortCodeIds($meta_value);
99 $shortcodeViewIds = array_merge($shortcodeViewIds, $shortcodeIds);
100 }
101
102 self::$bfViewIdsFromPost = $shortcodeViewIds;
103 return $shortcodeViewIds;
104 }
105
106 public static function getViewShortCodeIds($content = '')
107 {
108 $pattern = '/' . get_shortcode_regex(['bitform-view']) . '/';
109 \preg_match_all($pattern, $content, $short);
110
111 $viewIds = [];
112
113 foreach ($short[3] as $attr_string) {
114 $attr = shortcode_parse_atts($attr_string);
115 if (!empty($attr['id'])) {
116 $viewIds[] = $attr['id'];
117 }
118 }
119
120 // \preg_match_all('/\[bitform-view\s+\b[^\]]*\bid\s*=\s*["\']?(\d+)["\']?[^\]]*\]/', $content, $shortCode);
121 // $ids = $shortCode[1];
122 return $viewIds;
123 }
124
125 public static function checkIsPageBuilder($srvr)
126 {
127 if (is_admin()) {
128 self::$isPageBuilder = true;
129 return true;
130 }
131 $current_url = $srvr['REQUEST_URI'];
132 $queryParams = self::parseQueryParams($current_url);
133 foreach (self::$pageBuilderQueryParamsList as $key => $value) {
134 if (isset($queryParams[$key]) && $queryParams[$key] === $value) {
135 self::$isPageBuilder = true;
136 return true;
137 }
138 }
139 foreach (self::$pageBuilderURLParamsList as $value) {
140 if (false !== strpos($current_url, $value)) {
141 self::$isPageBuilder = true;
142 return true;
143 }
144 }
145
146 $referrer = isset($srvr['HTTP_REFERER']) ? $srvr['HTTP_REFERER'] : '';
147 $referrerQueryParams = self::parseQueryParams($referrer);
148 foreach (self::$pageBuilderRefererQueryParamsList as $key => $value) {
149 if (isset($referrerQueryParams[$key]) && $referrerQueryParams[$key] === $value) {
150 self::$isPageBuilder = true;
151 return true;
152 }
153 }
154
155 return self::$isPageBuilder;
156 }
157
158 public static function parseQueryParams($url)
159 {
160 $url_components = wp_parse_url($url);
161 if (isset($url_components['query'])) {
162 parse_str($url_components['query'], $queryParams);
163 return $queryParams;
164 }
165 return [];
166 }
167
168 public static function isRestRequest()
169 {
170 $prefix = rest_get_url_prefix();
171 if (defined('REST_REQUEST') && REST_REQUEST
172 || (isset($_GET['rest_route'])
173 && 0 === strpos(trim(sanitize_text_field(wp_unslash($_GET['rest_route'])), '\\/'), $prefix, 0))) {
174 return true;
175 }
176 global $wp_rewrite;
177 if (null === $wp_rewrite) {
178 $wp_rewrite = new WP_Rewrite();
179 }
180 $rest_url = wp_parse_url(trailingslashit(rest_url()));
181 $current_url = wp_parse_url(add_query_arg([]));
182 return 0 === strpos($current_url['path'], $rest_url['path'], 0);
183 }
184
185 public static function isAjaxRequest()
186 {
187 if (function_exists('wp_doing_ajax') && wp_doing_ajax()) {
188 return true;
189 }
190 if (self::isRestRequest()) {
191 return true;
192 }
193
194 if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 'xmlhttprequest' === strtolower(sanitize_text_field(wp_unslash($_SERVER['HTTP_X_REQUESTED_WITH'])))) {
195 return true;
196 }
197 if (isset($_SERVER['HTTP_SEC_FETCH_MODE'], $_SERVER['HTTP_SEC_FETCH_DEST'])) {
198 $destination = strtolower(sanitize_text_field(wp_unslash($_SERVER['HTTP_SEC_FETCH_DEST'])));
199 $mode = strtolower(sanitize_text_field(wp_unslash($_SERVER['HTTP_SEC_FETCH_MODE'])));
200 if (('empty' === $destination && in_array($mode, ['cors', 'same-origin'], true))) {
201 return true;
202 }
203 }
204
205 return false;
206 }
207
208 public static function isAdminRequest()
209 {
210 $current_url = home_url(add_query_arg(null, null));
211 $admin_url = strtolower(admin_url());
212 $referrer = strtolower(wp_get_referer());
213
214 $requestFromBackend = self::isRestRequest() && strpos($admin_url, '/wp-admin/') > 0 && !strpos($admin_url, '/wp-admin/admin-ajax.php');
215
216 if ($requestFromBackend) {
217 return true;
218 }
219
220 if (0 === strpos($current_url, $admin_url)) {
221 if (0 === strpos($referrer, $admin_url)) {
222 return true;
223 } else {
224 if (function_exists('wp_doing_ajax')) {
225 return !wp_doing_ajax();
226 } else {
227 return !(defined('DOING_AJAX') && DOING_AJAX);
228 }
229 }
230 } else {
231 return false;
232 }
233 }
234
235 public static function parseUrlParams($url)
236 {
237 $url_components = wp_parse_url($url);
238 if (isset($url_components['path'])) {
239 $urlParams = explode('/', $url_components['path']);
240 return $urlParams;
241 }
242
243 return [];
244 }
245
246 public static function setBfFrontendFormIds($formId)
247 {
248 self::$bfFrontendFormIds[] = $formId;
249 }
250
251 public static function getAllFormIdsInPage()
252 {
253 $bfFrontendFormIds = self::$bfFrontendFormIds;
254 $bfFormIdsFromPost = self::getFormIdsFromPost();
255 $allFormIds = array_merge($bfFrontendFormIds, $bfFormIdsFromPost);
256 return $allFormIds;
257 }
258
259 public static function getAllViewIdsInPage()
260 {
261 $bfFrontendViewIds = self::$bfFrontendViewIds;
262 $bfViewIdsFromPost = self::getViewIdsFromPost();
263 $allViewIds = array_merge($bfFrontendViewIds, $bfViewIdsFromPost);
264 return $allViewIds;
265 }
266
267 public static function getAllUniqFormIdsInPage()
268 {
269 return array_unique(self::getAllFormIdsInPage());
270 }
271
272 public static function hasMultipleForms()
273 {
274 $bfUniqFormIds = self::getAllFormIdsInPage();
275 self::checkIsPageBuilder($_SERVER);
276 $isPageBuilder = self::$isPageBuilder;
277 $bfMultipleFormsExists = $isPageBuilder ? true : count($bfUniqFormIds) > 1;
278 return $bfMultipleFormsExists;
279 }
280
281 public static function getFormPermissions($formId)
282 {
283 if (!isset(self::$formsPermissions[$formId])) {
284 $formManager = FormManager::getInstance($formId);
285 self::$formsPermissions[$formId] = $formManager->getFormPermission();
286 }
287
288 return self::$formsPermissions[$formId];
289 }
290
291 public static function is_current_user_can_access($formId, $action = 'entryViewAccess', $scope = '', $entryUserId = '')
292 {
293 $formPermissions = self::getFormPermissions($formId);
294 $accessPermission = isset($formPermissions->{$action}) ? $formPermissions->{$action} : null;
295 if (empty($accessPermission)) {
296 return false;
297 }
298 if ('entryViewAccess' === $action && (!isset($accessPermission->preventPublicAccess) || !$accessPermission->preventPublicAccess)) {
299 return true;
300 }
301 if (is_user_logged_in()) {
302 $user = wp_get_current_user();
303 $userId = (string) $user->ID;
304 if (in_array('administrator', $user->roles) || current_user_can('manage_bitform')) {
305 return true;
306 }
307 if ('entryEditAccess' === $action && !(isset($accessPermission->allowEntriesEdit) && $accessPermission->allowEntriesEdit)) {
308 return false;
309 }
310 if (!empty($scope) && !empty($accessPermission->{$scope}) && is_string($accessPermission->{$scope})) {
311 $accessRolesArray = explode(',', $accessPermission->{$scope});
312 if (self::has_access_for_roles($user, $accessRolesArray) && empty($entryUserId)) {
313 return true;
314 }
315 if (!empty($entryUserId) && (('ownEntries' === $scope && $userId === $entryUserId) || ('othersEntries' === $scope && $userId !== $entryUserId))) {
316 return true;
317 }
318 }
319
320 if (empty($scope) && isset($accessPermission->ownEntries) && !empty($accessPermission->ownEntries) && is_string($accessPermission->ownEntries)) {
321 $accessRolesArray = explode(',', $accessPermission->ownEntries);
322 if (self::has_access_for_roles($user, $accessRolesArray) && !empty($entryUserId) && $userId === $entryUserId) {
323 return true;
324 }
325 if (self::has_access_for_roles($user, $accessRolesArray) && empty($entryUserId)) {
326 return true;
327 }
328 }
329
330 if (empty($scope) && isset($accessPermission->othersEntries) && !empty($accessPermission->othersEntries) && is_string($accessPermission->othersEntries)) {
331 $accessRolesArray = explode(',', $accessPermission->othersEntries);
332 if (self::has_access_for_roles($user, $accessRolesArray) && !empty($entryUserId) && $userId !== $entryUserId) {
333 return true;
334 }
335 if (self::has_access_for_roles($user, $accessRolesArray) && empty($entryUserId)) {
336 return true;
337 }
338 }
339 }
340 return false;
341 }
342
343 private static function has_access_for_roles($user, $accessRoles)
344 {
345 // If "all_logged_in_users" is in the allowed roles, grant access
346 if (in_array('all_logged_in_users', $accessRoles)) {
347 return true;
348 }
349 // Check if any of the user's roles match the allowed roles
350 $userRoles = array_intersect($user->roles, $accessRoles);
351 return !empty($userRoles);
352 }
353 }
354