| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Fallback; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\Database\FormModel; |
| 6 |
use BitCode\BitForm\Core\Util\Log; |
| 7 |
use BitCode\BitForm\Core\Util\Utilities; |
| 8 |
use BitCode\BitForm\Jcof\Jcof; |
| 9 |
|
| 10 |
class StylesFallback |
| 11 |
{ |
| 12 |
public function signatureFldIframeStyle() |
| 13 |
{ |
| 14 |
$formModel = new FormModel(); |
| 15 |
$forms = $formModel->get( |
| 16 |
['id', 'form_content', 'builder_helper_state'] |
| 17 |
); |
| 18 |
|
| 19 |
if (is_wp_error($forms)) { |
| 20 |
return; |
| 21 |
} |
| 22 |
|
| 23 |
$signatureIframeStyle = [ |
| 24 |
'position' => 'absolute', |
| 25 |
'top' => 0, |
| 26 |
'left' => 0, |
| 27 |
'width' => '100%', |
| 28 |
'height' => '100%', |
| 29 |
'opacity' => 0, |
| 30 |
'pointer-events'=> 'none', |
| 31 |
'border' => 'none', |
| 32 |
]; |
| 33 |
|
| 34 |
foreach ($forms as $form) { |
| 35 |
$hasStyleChanged = false; |
| 36 |
$formId = $form->id; |
| 37 |
$builderHelperState = json_decode($form->builder_helper_state); |
| 38 |
if (empty($builderHelperState->staticStyles)) { |
| 39 |
continue; |
| 40 |
} |
| 41 |
$staticStyleState = $builderHelperState->staticStyles; |
| 42 |
$staticStyleState = Jcof::parse($staticStyleState); |
| 43 |
|
| 44 |
$formContent = json_decode($form->form_content); |
| 45 |
$fields = $formContent->fields; |
| 46 |
|
| 47 |
foreach ($fields as $fldKey => $fldData) { |
| 48 |
if ('signature' !== $fldData->typ) { |
| 49 |
continue; |
| 50 |
} |
| 51 |
if (!isset($staticStyleState['staticStyles'])) { |
| 52 |
$staticStyleState['staticStyles'] = []; |
| 53 |
} |
| 54 |
$iframeKey = ".{$fldKey}-signature-iframe"; |
| 55 |
$staticStyleState['staticStyles'][$iframeKey] = $signatureIframeStyle; |
| 56 |
$generatedStyleCSS = Utilities::styleGenerator([$iframeKey => $signatureIframeStyle]); |
| 57 |
Utilities::appendCSS($formId, $generatedStyleCSS); |
| 58 |
$hasStyleChanged = true; |
| 59 |
} |
| 60 |
|
| 61 |
if (!$hasStyleChanged) { |
| 62 |
continue; |
| 63 |
} |
| 64 |
|
| 65 |
$staticStyleState = Jcof::stringify($staticStyleState); |
| 66 |
$builderHelperState->staticStyles = $staticStyleState; |
| 67 |
$formModel->update( |
| 68 |
[ |
| 69 |
'builder_helper_state' => wp_json_encode($builderHelperState), |
| 70 |
], |
| 71 |
[ |
| 72 |
'id' => $formId, |
| 73 |
] |
| 74 |
); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
public function stripeFldButtonDisabled() |
| 79 |
{ |
| 80 |
$stylesArray = [ |
| 81 |
'.__fk__-stripe-btn:disabled' => [ |
| 82 |
'background' => '#7ca5e7', |
| 83 |
'transform' => 'none', |
| 84 |
], |
| 85 |
'.__fk__-stripe-wrp .stripe-pay-btn:disabled'=> [ |
| 86 |
'background' => '#7ca5e7', |
| 87 |
'transform' => 'none', |
| 88 |
] |
| 89 |
]; |
| 90 |
|
| 91 |
$this->addStyles('stripe', $stylesArray); |
| 92 |
} |
| 93 |
|
| 94 |
public function stripeFldErrorMessage() |
| 95 |
{ |
| 96 |
$stylesArray = [ |
| 97 |
'.__fk__-err-inner' => [ |
| 98 |
'overflow'=> 'hidden', |
| 99 |
], |
| 100 |
]; |
| 101 |
|
| 102 |
$this->addStyles('stripe', $stylesArray); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* for adding fallback styles to field in form staticStyles object |
| 107 |
* |
| 108 |
* @param string $fieldType |
| 109 |
* @param array $stylesArray |
| 110 |
* @return void |
| 111 |
*/ |
| 112 |
private function addStyles(string $fieldType, array $stylesArray): void |
| 113 |
{ |
| 114 |
$formModel = new FormModel(); |
| 115 |
$forms = $formModel->get( |
| 116 |
['id', 'form_content', 'builder_helper_state'] |
| 117 |
); |
| 118 |
|
| 119 |
if (is_wp_error($forms)) { |
| 120 |
return; |
| 121 |
} |
| 122 |
foreach ($forms as $form) { |
| 123 |
$hasStyleChanged = false; |
| 124 |
$formId = $form->id; |
| 125 |
$builderHelperState = json_decode($form->builder_helper_state); |
| 126 |
if (empty($builderHelperState->staticStyles)) { |
| 127 |
continue; |
| 128 |
} |
| 129 |
$staticStyleState = $builderHelperState->staticStyles; |
| 130 |
$staticStyleState = Jcof::parse($staticStyleState); |
| 131 |
|
| 132 |
$formContent = json_decode($form->form_content); |
| 133 |
$fields = $formContent->fields; |
| 134 |
|
| 135 |
foreach ($fields as $fldKey => $fldData) { |
| 136 |
if ($fieldType !== $fldData->typ) { |
| 137 |
continue; |
| 138 |
} |
| 139 |
if (!isset($staticStyleState['staticStyles'])) { |
| 140 |
$staticStyleState['staticStyles'] = []; |
| 141 |
} |
| 142 |
|
| 143 |
$newStyles = $this->replaceFldKey($fldKey, $stylesArray); |
| 144 |
foreach ($newStyles as $newStyleKey => $newStyleValue) { |
| 145 |
$staticStyleState['staticStyles'][$newStyleKey] = $newStyleValue; |
| 146 |
} |
| 147 |
|
| 148 |
$generatedStyleCSS = Utilities::styleGenerator($newStyles); |
| 149 |
Utilities::appendCSS($formId, $generatedStyleCSS); |
| 150 |
$hasStyleChanged = true; |
| 151 |
} |
| 152 |
|
| 153 |
if (!$hasStyleChanged) { |
| 154 |
continue; |
| 155 |
} |
| 156 |
$staticStyleState = Jcof::stringify($staticStyleState); |
| 157 |
$builderHelperState->staticStyles = $staticStyleState; |
| 158 |
$formModel->update( |
| 159 |
[ |
| 160 |
'builder_helper_state' => wp_json_encode($builderHelperState), |
| 161 |
], |
| 162 |
[ |
| 163 |
'id' => $formId, |
| 164 |
] |
| 165 |
); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* for adding fallback styles in form staticStyles object |
| 171 |
* |
| 172 |
* @return void |
| 173 |
*/ |
| 174 |
public function addStaticStyleForMultiStepForm(): void |
| 175 |
{ |
| 176 |
$newStylesArray = [ |
| 177 |
'._frm-__fk__-stp-hdr-wrpr' => [ |
| 178 |
'flex-wrap' => 'wrap', |
| 179 |
], |
| 180 |
'@media (max-width: 576px)' => [ |
| 181 |
'._frm-__fk__-stp-hdr-lbl' => [ |
| 182 |
'font-size' => '12px', |
| 183 |
], |
| 184 |
'._frm-__fk__-stp-hdr-sub-titl' => [ |
| 185 |
'display' => 'none', |
| 186 |
], |
| 187 |
'._frm-__fk__-stp-progress-bar' => [ |
| 188 |
'font-size'=> '.65rem', |
| 189 |
'height' => '.8rem' |
| 190 |
], |
| 191 |
], |
| 192 |
]; |
| 193 |
$this->overrideAllFormsStaticStyles($newStylesArray); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* for adding fallback styles in form staticStyles object |
| 198 |
* |
| 199 |
* @return void |
| 200 |
*/ |
| 201 |
public function addStaticStyleForMultiStepContentFld(): void |
| 202 |
{ |
| 203 |
$newStylesArray = [ |
| 204 |
'._frm-b-stp-cntnt .btcd-fld-itm' => [ |
| 205 |
'height' => 'fit-content', |
| 206 |
] |
| 207 |
]; |
| 208 |
$this->overrideAllFormsStaticStyles($newStylesArray); |
| 209 |
} |
| 210 |
|
| 211 |
private function overrideAllFormsStaticStyles($newStylesArray) |
| 212 |
{ |
| 213 |
$formModel = new FormModel(); |
| 214 |
$forms = $formModel->get( |
| 215 |
['id', 'form_content', 'builder_helper_state'] |
| 216 |
); |
| 217 |
|
| 218 |
if (is_wp_error($forms)) { |
| 219 |
Log::debug_log('Error fetching forms for static styles override' . $forms->get_error_message()); |
| 220 |
return; |
| 221 |
} |
| 222 |
foreach ($forms as $form) { |
| 223 |
$hasStyleChanged = false; |
| 224 |
$formId = $form->id; |
| 225 |
$builderHelperState = json_decode($form->builder_helper_state); |
| 226 |
if (!is_object($builderHelperState) || empty($builderHelperState->staticStyles)) { |
| 227 |
continue; |
| 228 |
} |
| 229 |
|
| 230 |
// Decode and validate form_content |
| 231 |
$formContent = json_decode($form->form_content); |
| 232 |
if (!is_object($formContent) || !isset($formContent->layout) || !is_array($formContent->layout)) { |
| 233 |
continue; |
| 234 |
} |
| 235 |
// Parse staticStyles safely |
| 236 |
try { |
| 237 |
$staticStyleState = Jcof::parse($builderHelperState->staticStyles); |
| 238 |
} catch (Exception $e) { |
| 239 |
Log::debug_log("Jcof::parse failed for form ID {$formId}: " . $e->getMessage()); |
| 240 |
continue; |
| 241 |
} |
| 242 |
|
| 243 |
// Ensure staticStyles array exists |
| 244 |
if (!isset($staticStyleState['staticStyles']) || !is_array($staticStyleState['staticStyles'])) { |
| 245 |
$staticStyleState['staticStyles'] = []; |
| 246 |
} |
| 247 |
|
| 248 |
$newStyles = self::replaceKeys($newStylesArray, '__fk__', "b{$formId}"); |
| 249 |
$staticStyleState['staticStyles'] = array_merge($staticStyleState['staticStyles'], $newStyles); |
| 250 |
|
| 251 |
// Convert and append CSS safely |
| 252 |
try { |
| 253 |
$generatedStyleCSS = Utilities::convertToCSS($newStyles); |
| 254 |
Utilities::appendCSS($formId, $generatedStyleCSS); |
| 255 |
} catch (Exception $e) { |
| 256 |
Log::debug_log("CSS generation/append failed for form ID {$formId}: " . $e->getMessage()); |
| 257 |
continue; |
| 258 |
} |
| 259 |
|
| 260 |
// Stringify safely |
| 261 |
try { |
| 262 |
$builderHelperState->staticStyles = Jcof::stringify($staticStyleState); |
| 263 |
} catch (Exception $e) { |
| 264 |
Log::debug_log("Jcof::stringify failed for form ID {$formId}: " . $e->getMessage()); |
| 265 |
continue; |
| 266 |
} |
| 267 |
|
| 268 |
// Save the updated builder_helper_state |
| 269 |
$updateResult = $formModel->update( |
| 270 |
[ |
| 271 |
'builder_helper_state' => wp_json_encode($builderHelperState), |
| 272 |
], |
| 273 |
[ |
| 274 |
'id' => $formId, |
| 275 |
] |
| 276 |
); |
| 277 |
|
| 278 |
if (false === $updateResult || is_wp_error($updateResult)) { |
| 279 |
Log::debug_log("Form update failed for form ID {$formId}"); |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* @method for replacing __fk__ with field key in styles array keys |
| 286 |
* @example: .__fk__-stripe-btn:disabled to .bk-1-stripe-btn:disabled |
| 287 |
* |
| 288 |
* @param string $fldKey |
| 289 |
* @param array $stylesArray |
| 290 |
* @return array $newStyleArray |
| 291 |
*/ |
| 292 |
private function replaceFldKey(string $fldKey, array $stylesArray): array |
| 293 |
{ |
| 294 |
$newStyleArray = []; |
| 295 |
foreach ($stylesArray as $key => $value) { |
| 296 |
$key = str_replace('__fk__', $fldKey, $key); |
| 297 |
$newStyleArray[$key] = $value; |
| 298 |
} |
| 299 |
return $newStyleArray; |
| 300 |
} |
| 301 |
|
| 302 |
// public function signatureFldIframeStyle() |
| 303 |
// { |
| 304 |
// $signatureIframeStyle = [ |
| 305 |
// '.__fk__-signature-iframe' => [ |
| 306 |
// 'position' => 'absolute', |
| 307 |
// 'top' => 0, |
| 308 |
// 'left' => 0, |
| 309 |
// 'width' => '100%', |
| 310 |
// 'height' => '100%', |
| 311 |
// 'opacity' => 0, |
| 312 |
// 'pointer-events'=> 'none', |
| 313 |
// 'border' => 'none', |
| 314 |
// ] |
| 315 |
// ]; |
| 316 |
|
| 317 |
// $this->addStyles('signature', $signatureIframeStyle); |
| 318 |
// } |
| 319 |
|
| 320 |
public static function replaceKeys(array $styles, string $search, string $replace): array |
| 321 |
{ |
| 322 |
$updatedStyles = []; |
| 323 |
foreach ($styles as $key => $value) { |
| 324 |
$newKey = str_replace($search, $replace, $key); |
| 325 |
|
| 326 |
if (is_array($value)) { |
| 327 |
$value = self::replaceKeys($value, $search, $replace); |
| 328 |
} |
| 329 |
$updatedStyles[$newKey] = $value; |
| 330 |
} |
| 331 |
return $updatedStyles; |
| 332 |
} |
| 333 |
} |
| 334 |
|