| 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 |
* for adding fallback styles in form staticStyles object |
| 170 |
* |
| 171 |
* @return void |
| 172 |
*/ |
| 173 |
public function addStaticStyleForMultiStepForm(): void |
| 174 |
{ |
| 175 |
$stylesArray = [ |
| 176 |
'._frm-__fk__-stp-hdr-wrpr' => [ |
| 177 |
'flex-wrap' => 'wrap', |
| 178 |
], |
| 179 |
'@media (max-width: 576px)' => [ |
| 180 |
'._frm-__fk__-stp-hdr-lbl' => [ |
| 181 |
'font-size' => '12px', |
| 182 |
], |
| 183 |
'._frm-__fk__-stp-hdr-sub-titl' => [ |
| 184 |
'display' => 'none', |
| 185 |
], |
| 186 |
'._frm-__fk__-stp-progress-bar' => [ |
| 187 |
'font-size'=> '.65rem', |
| 188 |
'height' => '.8rem' |
| 189 |
], |
| 190 |
], |
| 191 |
]; |
| 192 |
$formModel = new FormModel(); |
| 193 |
$forms = $formModel->get( |
| 194 |
['id', 'form_content', 'builder_helper_state'] |
| 195 |
); |
| 196 |
|
| 197 |
if (is_wp_error($forms)) { |
| 198 |
return; |
| 199 |
} |
| 200 |
foreach ($forms as $form) { |
| 201 |
$hasStyleChanged = false; |
| 202 |
$formId = $form->id; |
| 203 |
$builderHelperState = json_decode($form->builder_helper_state); |
| 204 |
if (empty($builderHelperState->staticStyles)) { |
| 205 |
continue; |
| 206 |
} |
| 207 |
$staticStyleState = $builderHelperState->staticStyles; |
| 208 |
$staticStyleState = Jcof::parse($staticStyleState); |
| 209 |
|
| 210 |
$formContent = json_decode($form->form_content); |
| 211 |
$layouts = $formContent->layout; |
| 212 |
if (!is_array($layouts)) { |
| 213 |
continue; |
| 214 |
} |
| 215 |
|
| 216 |
foreach ($layouts as $layout) { |
| 217 |
if (!isset($staticStyleState['staticStyles'])) { |
| 218 |
$staticStyleState['staticStyles'] = []; |
| 219 |
} |
| 220 |
|
| 221 |
$newStyles = self::replaceKeys($stylesArray, '__fk__', "b{$formId}"); |
| 222 |
|
| 223 |
$staticStyleState['staticStyles'] = array_merge($staticStyleState['staticStyles'], $newStyles); |
| 224 |
|
| 225 |
$generatedStyleCSS = Utilities::convertToCSS($newStyles); |
| 226 |
Utilities::appendCSS($formId, $generatedStyleCSS); |
| 227 |
$hasStyleChanged = true; |
| 228 |
} |
| 229 |
|
| 230 |
if (!$hasStyleChanged) { |
| 231 |
continue; |
| 232 |
} |
| 233 |
$staticStyleState = Jcof::stringify($staticStyleState); |
| 234 |
$builderHelperState->staticStyles = $staticStyleState; |
| 235 |
$formModel->update( |
| 236 |
[ |
| 237 |
'builder_helper_state' => wp_json_encode($builderHelperState), |
| 238 |
], |
| 239 |
[ |
| 240 |
'id' => $formId, |
| 241 |
] |
| 242 |
); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* @method for replacing __fk__ with field key in styles array keys |
| 248 |
* @example: .__fk__-stripe-btn:disabled to .bk-1-stripe-btn:disabled |
| 249 |
* |
| 250 |
* @param string $fldKey |
| 251 |
* @param array $stylesArray |
| 252 |
* @return array $newStyleArray |
| 253 |
*/ |
| 254 |
private function replaceFldKey(string $fldKey, array $stylesArray): array |
| 255 |
{ |
| 256 |
$newStyleArray = []; |
| 257 |
foreach ($stylesArray as $key => $value) { |
| 258 |
$key = str_replace('__fk__', $fldKey, $key); |
| 259 |
$newStyleArray[$key] = $value; |
| 260 |
} |
| 261 |
return $newStyleArray; |
| 262 |
} |
| 263 |
|
| 264 |
// public function signatureFldIframeStyle() |
| 265 |
// { |
| 266 |
// $signatureIframeStyle = [ |
| 267 |
// '.__fk__-signature-iframe' => [ |
| 268 |
// 'position' => 'absolute', |
| 269 |
// 'top' => 0, |
| 270 |
// 'left' => 0, |
| 271 |
// 'width' => '100%', |
| 272 |
// 'height' => '100%', |
| 273 |
// 'opacity' => 0, |
| 274 |
// 'pointer-events'=> 'none', |
| 275 |
// 'border' => 'none', |
| 276 |
// ] |
| 277 |
// ]; |
| 278 |
|
| 279 |
// $this->addStyles('signature', $signatureIframeStyle); |
| 280 |
// } |
| 281 |
|
| 282 |
public static function replaceKeys(array $styles, string $search, string $replace): array |
| 283 |
{ |
| 284 |
$updatedStyles = []; |
| 285 |
foreach ($styles as $key => $value) { |
| 286 |
$newKey = str_replace($search, $replace, $key); |
| 287 |
|
| 288 |
if (is_array($value)) { |
| 289 |
$value = self::replaceKeys($value, $search, $replace); |
| 290 |
} |
| 291 |
$updatedStyles[$newKey] = $value; |
| 292 |
} |
| 293 |
return $updatedStyles; |
| 294 |
} |
| 295 |
} |
| 296 |
|