| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Fallback; |
| 4 |
|
| 5 |
use BitCode\BitForm\Admin\Form\AdminFormHandler; |
| 6 |
use BitCode\BitForm\Core\Database\DB; |
| 7 |
use BitCode\BitForm\Core\Database\FormModel; |
| 8 |
use BitCode\BitForm\Core\Form\FormHandler; |
| 9 |
use BitCode\BitForm\Core\Form\FormManager; |
| 10 |
use BitCode\BitForm\Core\Migration\MigrateForms; |
| 11 |
use BitCode\BitForm\Core\Migration\MigrationHelper; |
| 12 |
|
| 13 |
final class FormFallback |
| 14 |
{ |
| 15 |
public function resetJsGeneratedPageIds() |
| 16 |
{ |
| 17 |
$formModel = new FormModel(); |
| 18 |
$formModel->update( |
| 19 |
[ |
| 20 |
'generated_script_page_ids' => wp_json_encode((object) []), |
| 21 |
], |
| 22 |
[ |
| 23 |
'status' => 1 |
| 24 |
] |
| 25 |
); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Name/Address children renamed with their parent before 3.3.2 kept the old prefix, so their |
| 30 |
* inputs posted away from the parent and its entry value saved empty. Safe to re-run: only |
| 31 |
* forms whose names actually change are written. |
| 32 |
* |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
public function syncCompositeChildNames() |
| 36 |
{ |
| 37 |
$formModel = new FormModel(); |
| 38 |
$forms = $formModel->get( |
| 39 |
['id', 'form_content'], |
| 40 |
[ |
| 41 |
'form_content' => ['operator' => 'LIKE', 'value' => '%"childFields"%'], |
| 42 |
] |
| 43 |
); |
| 44 |
if (is_wp_error($forms) || !is_array($forms)) { |
| 45 |
return; |
| 46 |
} |
| 47 |
foreach ($forms as $form) { |
| 48 |
$formContent = json_decode($form->form_content ?? ''); |
| 49 |
if (!is_object($formContent) || !isset($formContent->fields)) { |
| 50 |
continue; |
| 51 |
} |
| 52 |
if (!FormManager::syncCompositeChildNames($formContent->fields)) { |
| 53 |
continue; |
| 54 |
} |
| 55 |
$encoded = wp_json_encode($formContent); |
| 56 |
if (!is_string($encoded)) { |
| 57 |
continue; |
| 58 |
} |
| 59 |
$formModel->update(['form_content' => $encoded], ['id' => $form->id]); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
public function v1formMigragion() |
| 64 |
{ |
| 65 |
$olderVersion = get_option('bitforms_version'); |
| 66 |
$isMigratedToV2 = get_option('bitforms_migrated_to_v2'); |
| 67 |
$isMigratingToV2 = get_option('bitforms_migrating_to_v2'); |
| 68 |
if (!$isMigratedToV2 && !$isMigratingToV2 && $olderVersion) { |
| 69 |
$formHandler = FormHandler::getInstance(); |
| 70 |
if (!$formHandler->admin) { |
| 71 |
$formHandler->admin = new AdminFormHandler(); |
| 72 |
} |
| 73 |
$formHandler->admin->startMigrationProcess(); |
| 74 |
update_option('bitforms_db_version', '2.0'); |
| 75 |
DB::migrate(); |
| 76 |
$migrateFormsHandler = new MigrateForms(); |
| 77 |
$all_forms = $migrateFormsHandler->migrateToV2(); |
| 78 |
foreach ($all_forms as $form) { |
| 79 |
$formatFormData = MigrationHelper::formatFormData($form); |
| 80 |
$formHandler->admin->updateForm(null, $formatFormData); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|