| @@ -2,10 +2,11 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace BitCode\BitForm\Core\Fallback; |
| 4 | 4 | |
| 5 | 5 | use BitCode\BitForm\Core\Database\FormModel; |
| 6 | +use BitCode\BitForm\Core\Util\Log; | |
| 6 | 7 | use BitCode\BitForm\Core\Util\Utilities; |
| 7 | -use Jcof\Jcof; | |
| 8 | +use BitCode\BitForm\Jcof\Jcof; | |
| 8 | 9 | |
| 9 | 10 | class StylesFallback |
| 10 | 11 | { |
| 11 | 12 | public function signatureFldIframeStyle() |
| @@ -165,8 +166,123 @@ | ||
| 165 | 166 | } |
| 166 | 167 | } |
| 167 | 168 | |
| 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 | + /** | |
| 169 | 285 | * @method for replacing __fk__ with field key in styles array keys |
| 170 | 286 | * @example: .__fk__-stripe-btn:disabled to .bk-1-stripe-btn:disabled |
| 171 | 287 | * |
| 172 | 288 | * @param string $fldKey |
| @@ -199,5 +315,19 @@ | ||
| 199 | 315 | // ]; |
| 200 | 316 | |
| 201 | 317 | // $this->addStyles('signature', $signatureIframeStyle); |
| 202 | 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 | + } | |
| 203 | 333 | } |