| 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 |
|
| 10 |
class StandaloneFormView |
| 11 |
{ |
| 12 |
public static function preview() |
| 13 |
{ |
| 14 |
if (!current_user_can('manage_options')) { |
| 15 |
auth_redirect(); |
| 16 |
return; |
| 17 |
} |
| 18 |
$requestUri = $_SERVER['REQUEST_URI']; |
| 19 |
$uri = explode('/', $requestUri); |
| 20 |
|
| 21 |
if (is_array($uri) && count($uri) > 0) { |
| 22 |
$formID = $uri[count($uri) - 1]; |
| 23 |
$attr = ['form_id' => $formID, 'form_preview' => true]; |
| 24 |
|
| 25 |
$frontendFormHandler = new FrontendFormHandler(); |
| 26 |
$formViewObject = $frontendFormHandler->handleFrontendRenderRequest($attr); |
| 27 |
if ('string' === gettype($formViewObject)) { |
| 28 |
Render::view('views/form-not-found'); |
| 29 |
exit; |
| 30 |
} |
| 31 |
$formHTML = $formViewObject->html; |
| 32 |
$font = $formViewObject->font; |
| 33 |
$bfGlobals = $formViewObject->bfGlobals; |
| 34 |
|
| 35 |
set_transient('bitform_form_preview', true); |
| 36 |
$frontendFormHandler->generateJs($formID); |
| 37 |
$title = 'BitForm Preview page'; |
| 38 |
Render::view('views/preview-page', compact('formID', 'title', 'formHTML', 'font', 'bfGlobals')); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
private static function getCustomUrlFormId() |
| 43 |
{ |
| 44 |
$currentUrl = get_site_url() . $_SERVER['REQUEST_URI']; |
| 45 |
$parsedCurrentUrl = wp_parse_url($currentUrl); |
| 46 |
$formModel = new FormModel(); |
| 47 |
$forms = $formModel->get( |
| 48 |
['id', 'form_content'] |
| 49 |
); |
| 50 |
|
| 51 |
if (!is_wp_error($forms)) { |
| 52 |
foreach ($forms as $form) { |
| 53 |
$formID = $form->id; |
| 54 |
$formContent = json_decode($form->form_content); |
| 55 |
$isStandaloneActive = !empty($formContent->formInfo->standaloneSettings->active); |
| 56 |
if ($isStandaloneActive) { |
| 57 |
$standaloneSettings = $formContent->formInfo->standaloneSettings; |
| 58 |
$hasCustomUrl = !empty($standaloneSettings->customUrl); |
| 59 |
if (!$hasCustomUrl) { |
| 60 |
continue; |
| 61 |
} |
| 62 |
$customUrl = get_site_url() . '/' . $standaloneSettings->customUrl; |
| 63 |
$parsedCustomUrl = wp_parse_url($customUrl); |
| 64 |
$pathMatched = $parsedCustomUrl['path'] === $parsedCurrentUrl['path']; |
| 65 |
if (!$pathMatched) { |
| 66 |
continue; |
| 67 |
} |
| 68 |
if (!isset($parsedCustomUrl['query'])) { |
| 69 |
return $formID; |
| 70 |
} |
| 71 |
parse_str($parsedCustomUrl['query'], $parsedCustomQueries); |
| 72 |
parse_str($parsedCurrentUrl['query'], $parsedCurrentQueries); |
| 73 |
$diff = array_diff_assoc($parsedCustomQueries, $parsedCurrentQueries); |
| 74 |
if (empty($diff)) { |
| 75 |
return $formID; |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
public static function standaloneFormView() |
| 83 |
{ |
| 84 |
$hasBitFormParam = isset($_GET['bit-form']); |
| 85 |
$formID = isset($_GET['bit-form']) ? $_GET['bit-form'] : ''; |
| 86 |
|
| 87 |
if (empty($formID)) { |
| 88 |
$formID = self::getCustomUrlFormId(); |
| 89 |
} |
| 90 |
|
| 91 |
if (empty($formID)) { |
| 92 |
return ''; |
| 93 |
} |
| 94 |
|
| 95 |
$formID = intval($formID); |
| 96 |
$attr = ['form_id' => $formID, 'form_preview' => true]; |
| 97 |
|
| 98 |
$FormManager = new FormManager($formID); |
| 99 |
$formContent = $FormManager->getFormContent(); |
| 100 |
if (empty($formContent->formInfo->standaloneSettings->active)) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
$standaloneSettings = $formContent->formInfo->standaloneSettings; |
| 105 |
|
| 106 |
$frontendFormHandler = new FrontendFormHandler(); |
| 107 |
$formViewObject = $frontendFormHandler->handleFrontendRenderRequest($attr); |
| 108 |
if ('string' === gettype($formViewObject)) { |
| 109 |
Render::view('views/form-not-found'); |
| 110 |
exit; |
| 111 |
} |
| 112 |
|
| 113 |
$formHTML = $formViewObject->html; |
| 114 |
$font = $formViewObject->font; |
| 115 |
$bfGlobals = $formViewObject->bfGlobals; |
| 116 |
|
| 117 |
set_transient('bitform_form_preview', true); |
| 118 |
$frontendFormHandler->generateJs($formID); |
| 119 |
$title = !empty($standaloneSettings->pageTitle) ? $standaloneSettings->pageTitle : 'Bit Form'; |
| 120 |
Render::view('views/standalone-form', compact('formID', 'title', 'formHTML', 'font', 'bfGlobals')); |
| 121 |
|
| 122 |
exit(200); |
| 123 |
} |
| 124 |
} |
| 125 |
|