| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Fallback; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\Database\FormModel; |
| 6 |
use BitCode\BitForm\Core\Util\Utilities; |
| 7 |
use BitCode\BitForm\Jcof\Jcof; |
| 8 |
|
| 9 |
class StylesFallback |
| 10 |
{ |
| 11 |
public function signatureFldIframeStyle() |
| 12 |
{ |
| 13 |
$formModel = new FormModel(); |
| 14 |
$forms = $formModel->get( |
| 15 |
['id', 'form_content', 'builder_helper_state'] |
| 16 |
); |
| 17 |
|
| 18 |
if (is_wp_error($forms)) { |
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
$signatureIframeStyle = [ |
| 23 |
'position' => 'absolute', |
| 24 |
'top' => 0, |
| 25 |
'left' => 0, |
| 26 |
'width' => '100%', |
| 27 |
'height' => '100%', |
| 28 |
'opacity' => 0, |
| 29 |
'pointer-events'=> 'none', |
| 30 |
'border' => 'none', |
| 31 |
]; |
| 32 |
|
| 33 |
foreach ($forms as $form) { |
| 34 |
$hasStyleChanged = false; |
| 35 |
$formId = $form->id; |
| 36 |
$builderHelperState = json_decode($form->builder_helper_state); |
| 37 |
if (empty($builderHelperState->staticStyles)) { |
| 38 |
continue; |
| 39 |
} |
| 40 |
$staticStyleState = $builderHelperState->staticStyles; |
| 41 |
$staticStyleState = Jcof::parse($staticStyleState); |
| 42 |
|
| 43 |
$formContent = json_decode($form->form_content); |
| 44 |
$fields = $formContent->fields; |
| 45 |
|
| 46 |
foreach ($fields as $fldKey => $fldData) { |
| 47 |
if ('signature' !== $fldData->typ) { |
| 48 |
continue; |
| 49 |
} |
| 50 |
if (!isset($staticStyleState['staticStyles'])) { |
| 51 |
$staticStyleState['staticStyles'] = []; |
| 52 |
} |
| 53 |
$iframeKey = ".{$fldKey}-signature-iframe"; |
| 54 |
$staticStyleState['staticStyles'][$iframeKey] = $signatureIframeStyle; |
| 55 |
$generatedStyleCSS = Utilities::styleGenerator([$iframeKey => $signatureIframeStyle]); |
| 56 |
Utilities::appendCSS($formId, $generatedStyleCSS); |
| 57 |
$hasStyleChanged = true; |
| 58 |
} |
| 59 |
|
| 60 |
if (!$hasStyleChanged) { |
| 61 |
continue; |
| 62 |
} |
| 63 |
|
| 64 |
$staticStyleState = Jcof::stringify($staticStyleState); |
| 65 |
$builderHelperState->staticStyles = $staticStyleState; |
| 66 |
$formModel->update( |
| 67 |
[ |
| 68 |
'builder_helper_state' => wp_json_encode($builderHelperState), |
| 69 |
], |
| 70 |
[ |
| 71 |
'id' => $formId, |
| 72 |
] |
| 73 |
); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
public function stripeFldButtonDisabled() |
| 78 |
{ |
| 79 |
$stylesArray = [ |
| 80 |
'.__fk__-stripe-btn:disabled' => [ |
| 81 |
'background' => '#7ca5e7', |
| 82 |
'transform' => 'none', |
| 83 |
], |
| 84 |
'.__fk__-stripe-wrp .stripe-pay-btn:disabled'=> [ |
| 85 |
'background' => '#7ca5e7', |
| 86 |
'transform' => 'none', |
| 87 |
] |
| 88 |
]; |
| 89 |
|
| 90 |
$this->addStyles('stripe', $stylesArray); |
| 91 |
} |
| 92 |
|
| 93 |
public function stripeFldErrorMessage() |
| 94 |
{ |
| 95 |
$stylesArray = [ |
| 96 |
'.__fk__-err-inner' => [ |
| 97 |
'overflow'=> 'hidden', |
| 98 |
], |
| 99 |
]; |
| 100 |
|
| 101 |
$this->addStyles('stripe', $stylesArray); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* for adding fallback styles to field in form staticStyles object |
| 106 |
* |
| 107 |
* @param string $fieldType |
| 108 |
* @param array $stylesArray |
| 109 |
* @return void |
| 110 |
*/ |
| 111 |
private function addStyles(string $fieldType, array $stylesArray): void |
| 112 |
{ |
| 113 |
$formModel = new FormModel(); |
| 114 |
$forms = $formModel->get( |
| 115 |
['id', 'form_content', 'builder_helper_state'] |
| 116 |
); |
| 117 |
|
| 118 |
if (is_wp_error($forms)) { |
| 119 |
return; |
| 120 |
} |
| 121 |
foreach ($forms as $form) { |
| 122 |
$hasStyleChanged = false; |
| 123 |
$formId = $form->id; |
| 124 |
$builderHelperState = json_decode($form->builder_helper_state); |
| 125 |
if (empty($builderHelperState->staticStyles)) { |
| 126 |
continue; |
| 127 |
} |
| 128 |
$staticStyleState = $builderHelperState->staticStyles; |
| 129 |
$staticStyleState = Jcof::parse($staticStyleState); |
| 130 |
|
| 131 |
$formContent = json_decode($form->form_content); |
| 132 |
$fields = $formContent->fields; |
| 133 |
|
| 134 |
foreach ($fields as $fldKey => $fldData) { |
| 135 |
if ($fieldType !== $fldData->typ) { |
| 136 |
continue; |
| 137 |
} |
| 138 |
if (!isset($staticStyleState['staticStyles'])) { |
| 139 |
$staticStyleState['staticStyles'] = []; |
| 140 |
} |
| 141 |
|
| 142 |
$newStyles = $this->replaceFldKey($fldKey, $stylesArray); |
| 143 |
foreach ($newStyles as $newStyleKey => $newStyleValue) { |
| 144 |
$staticStyleState['staticStyles'][$newStyleKey] = $newStyleValue; |
| 145 |
} |
| 146 |
|
| 147 |
$generatedStyleCSS = Utilities::styleGenerator($newStyles); |
| 148 |
Utilities::appendCSS($formId, $generatedStyleCSS); |
| 149 |
$hasStyleChanged = true; |
| 150 |
} |
| 151 |
|
| 152 |
if (!$hasStyleChanged) { |
| 153 |
continue; |
| 154 |
} |
| 155 |
$staticStyleState = Jcof::stringify($staticStyleState); |
| 156 |
$builderHelperState->staticStyles = $staticStyleState; |
| 157 |
$formModel->update( |
| 158 |
[ |
| 159 |
'builder_helper_state' => wp_json_encode($builderHelperState), |
| 160 |
], |
| 161 |
[ |
| 162 |
'id' => $formId, |
| 163 |
] |
| 164 |
); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* @method for replacing __fk__ with field key in styles array keys |
| 170 |
* @example: .__fk__-stripe-btn:disabled to .bk-1-stripe-btn:disabled |
| 171 |
* |
| 172 |
* @param string $fldKey |
| 173 |
* @param array $stylesArray |
| 174 |
* @return array $newStyleArray |
| 175 |
*/ |
| 176 |
private function replaceFldKey(string $fldKey, array $stylesArray): array |
| 177 |
{ |
| 178 |
$newStyleArray = []; |
| 179 |
foreach ($stylesArray as $key => $value) { |
| 180 |
$key = str_replace('__fk__', $fldKey, $key); |
| 181 |
$newStyleArray[$key] = $value; |
| 182 |
} |
| 183 |
return $newStyleArray; |
| 184 |
} |
| 185 |
|
| 186 |
// public function signatureFldIframeStyle() |
| 187 |
// { |
| 188 |
// $signatureIframeStyle = [ |
| 189 |
// '.__fk__-signature-iframe' => [ |
| 190 |
// 'position' => 'absolute', |
| 191 |
// 'top' => 0, |
| 192 |
// 'left' => 0, |
| 193 |
// 'width' => '100%', |
| 194 |
// 'height' => '100%', |
| 195 |
// 'opacity' => 0, |
| 196 |
// 'pointer-events'=> 'none', |
| 197 |
// 'border' => 'none', |
| 198 |
// ] |
| 199 |
// ]; |
| 200 |
|
| 201 |
// $this->addStyles('signature', $signatureIframeStyle); |
| 202 |
// } |
| 203 |
} |
| 204 |
|