| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Frontend; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\Database\FormModel; |
| 6 |
use BitCode\BitForm\Core\Form\FormManager; |
| 7 |
use BitCode\BitForm\Core\Util\Render; |
| 8 |
use BitCode\BitForm\Frontend\Form\FrontendFormHandler; |
| 9 |
use BitCode\BitForm\Frontend\Form\View\Conversational\ConversationalHelpers; |
| 10 |
|
| 11 |
class ConversationalFormView |
| 12 |
{ |
| 13 |
public static function preview() |
| 14 |
{ |
| 15 |
// Read-only: REQUEST_URI used to extract form ID from URL path for view dispatch. No state mutation. |
| 16 |
$requestUri = isset($_SERVER['REQUEST_URI']) ? esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'])) : ''; |
| 17 |
$uri = explode('/', $requestUri); |
| 18 |
|
| 19 |
if (is_array($uri) && count($uri) > 0) { |
| 20 |
$formID = $uri[count($uri) - 1]; |
| 21 |
$attr = ['form_id' => $formID, 'form_preview' => true]; |
| 22 |
|
| 23 |
$frontendFormHandler = new FrontendFormHandler(); |
| 24 |
$formViewObject = $frontendFormHandler->handleFrontendRenderRequest($attr); |
| 25 |
if ('string' === gettype($formViewObject)) { |
| 26 |
Render::view('views/form-not-found'); |
| 27 |
exit; |
| 28 |
} |
| 29 |
|
| 30 |
$formHTML = $formViewObject->html; |
| 31 |
$font = $formViewObject->font; |
| 32 |
$bfGlobals = $formViewObject->bfGlobals; |
| 33 |
$formContent = isset($formViewObject->formContent) ? $formViewObject->formContent : null; |
| 34 |
|
| 35 |
if (defined('BITAPPS_DEV') && BITAPPS_DEV) { |
| 36 |
set_transient('bitform_form_preview', true); |
| 37 |
$frontendFormHandler->generateJs($formID); |
| 38 |
$title = 'BitForm Preview page'; |
| 39 |
Render::view('views/preview-page', compact('formID', 'title', 'formHTML', 'font', 'bfGlobals', 'formContent')); |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
private static function getCustomUrlFormId() |
| 45 |
{ |
| 46 |
$urlMap = get_transient('bitform_conversational_url_map'); |
| 47 |
if (false === $urlMap) { |
| 48 |
$urlMap = self::buildConversationalUrlMap(); |
| 49 |
set_transient('bitform_conversational_url_map', $urlMap, DAY_IN_SECONDS); |
| 50 |
} |
| 51 |
|
| 52 |
if (empty($urlMap)) { |
| 53 |
return null; |
| 54 |
} |
| 55 |
|
| 56 |
$currentUrl = get_site_url() . (isset($_SERVER['REQUEST_URI']) ? esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'])) : ''); |
| 57 |
$parsedCurrentUrl = wp_parse_url($currentUrl); |
| 58 |
|
| 59 |
foreach ($urlMap as $formID => $customUrlSlug) { |
| 60 |
$customUrl = get_site_url() . '/' . $customUrlSlug; |
| 61 |
$parsedCustomUrl = wp_parse_url($customUrl); |
| 62 |
$pathMatched = isset($parsedCustomUrl['path']) && $parsedCustomUrl['path'] === ($parsedCurrentUrl['path'] ?? ''); |
| 63 |
if (!$pathMatched) { |
| 64 |
continue; |
| 65 |
} |
| 66 |
if (!isset($parsedCustomUrl['query'])) { |
| 67 |
return $formID; |
| 68 |
} |
| 69 |
parse_str($parsedCustomUrl['query'], $parsedCustomQueries); |
| 70 |
parse_str($parsedCurrentUrl['query'] ?? '', $parsedCurrentQueries); |
| 71 |
if (empty(array_diff_assoc($parsedCustomQueries, $parsedCurrentQueries))) { |
| 72 |
return $formID; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
return null; |
| 77 |
} |
| 78 |
|
| 79 |
private static function buildConversationalUrlMap() |
| 80 |
{ |
| 81 |
$formModel = new FormModel(); |
| 82 |
$forms = $formModel->get(['id', 'form_content']); |
| 83 |
$urlMap = []; |
| 84 |
if (is_wp_error($forms) || !is_array($forms)) { |
| 85 |
return $urlMap; |
| 86 |
} |
| 87 |
foreach ($forms as $form) { |
| 88 |
$formContent = json_decode($form->form_content); |
| 89 |
if (empty($formContent->formInfo->standaloneSettings->active)) { |
| 90 |
continue; |
| 91 |
} |
| 92 |
if (empty($formContent->formInfo->conversationalSettings->enable)) { |
| 93 |
continue; |
| 94 |
} |
| 95 |
$standaloneSettings = $formContent->formInfo->standaloneSettings; |
| 96 |
if (empty($standaloneSettings->customUrl)) { |
| 97 |
continue; |
| 98 |
} |
| 99 |
$urlMap[$form->id] = $standaloneSettings->customUrl; |
| 100 |
} |
| 101 |
return $urlMap; |
| 102 |
} |
| 103 |
|
| 104 |
public static function conversationalFormView() |
| 105 |
{ |
| 106 |
if (is_admin()) { |
| 107 |
return; |
| 108 |
} |
| 109 |
// Read-only: form ID and preview flag from query string for conversational form dispatch. No state mutation. |
| 110 |
$formID = isset($_GET['bit-conversational-form']) ? sanitize_text_field(wp_unslash($_GET['bit-conversational-form'])) : ''; |
| 111 |
$formType = 'conversational'; |
| 112 |
if (empty($formID)) { |
| 113 |
$formID = self::getCustomUrlFormId(); |
| 114 |
} |
| 115 |
|
| 116 |
if (empty($formID)) { |
| 117 |
return ''; |
| 118 |
} |
| 119 |
|
| 120 |
$formID = intval($formID); |
| 121 |
|
| 122 |
$isPreview = isset($_GET['bf_preview']); |
| 123 |
|
| 124 |
ConversationalHelpers::setPreviewMode($isPreview); |
| 125 |
|
| 126 |
$attr = ['type'=> $formType, 'form_id' => $formID, 'form_preview' => true]; |
| 127 |
|
| 128 |
$FormManager = FormManager::getInstance($formID); |
| 129 |
$formContent = $FormManager->getFormContent(); |
| 130 |
if (empty($formContent->formInfo->conversationalSettings->enable)) { |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
$frontendFormHandler = new FrontendFormHandler(); |
| 135 |
$formViewObject = $frontendFormHandler->handleFrontendRenderRequest($attr); |
| 136 |
if ('string' === gettype($formViewObject)) { |
| 137 |
Render::view('views/form-not-found'); |
| 138 |
exit; |
| 139 |
} |
| 140 |
|
| 141 |
$formHTML = $formViewObject->html; |
| 142 |
$font = $formViewObject->font; |
| 143 |
$bfGlobals = $formViewObject->bfGlobals; |
| 144 |
$formContent = isset($formViewObject->formContent) ? $formViewObject->formContent : null; |
| 145 |
|
| 146 |
set_transient('bitform_form_preview', true); |
| 147 |
$frontendFormHandler->generateJs($formID, null, $formType); |
| 148 |
$title = 'Bit Form'; |
| 149 |
Render::view('views/conversational-form', compact('formID', 'title', 'formHTML', 'font', 'bfGlobals', 'isPreview', 'formContent')); |
| 150 |
|
| 151 |
exit(200); |
| 152 |
} |
| 153 |
} |
| 154 |
|