get( ['id', 'form_content'] ); if (!is_wp_error($forms)) { foreach ($forms as $form) { $formID = $form->id; $formContent = json_decode($form->form_content); $fields = $formContent->fields; foreach ($fields as $fldKey => $fldData) { if ('button' === $fldData->typ && !isset($fldData->valid)) { $fldData->valid = (object) []; } $fields->{$fldKey} = $fldData; } $formContent->fields = $fields; $formModel->update( [ 'form_content' => wp_json_encode($formContent), ], [ 'id' => $formID, ] ); } } } public function addButton() { // if version is lower or equal than 1.3.13 $formModel = new FormModel(); $forms = $formModel->get( ['id', 'form_content'], [ 'form_content' => ['operator' => 'LIKE', 'value' => '%\"buttons\":%'], ] ); if (!is_wp_error($forms)) { $adminFormHandler = new AdminFormHandler(); foreach ($forms as $form) { $formID = $form->id; $formContent = json_decode($form->form_content); $fields = json_encode($formContent->fields); if (isset($formContent->buttons)) { if (false === strpos($fields, '"btnTyp":"submit"')) { $align = isset($formContent->buttons->align) ? $formContent->buttons->align : 'right'; $btnSiz = isset($formContent->buttons->btnSiz) ? $formContent->buttons->btnSiz : 'md'; $fulW = isset($formContent->buttons->fulW) ? $formContent->buttons->fulW : false; $btnData = [ 'typ' => 'button', 'btnSiz' => $btnSiz, 'fulW' => $fulW, ]; $newID = 0; foreach ($formContent->fields as $key => $value) { $oldID = \preg_replace('/bf?\d+-(\d+)-?/', '$1', $key); if ($oldID > $newID) { $newID = $oldID; } } $newID = $newID + 1; $sBtnID = "bf${formID}-${newID}"; $lY = 0; $mY = 0; $sY = 0; foreach ($formContent->layout->lg as $lg) { if ($lg->y > $lY) { $lY = $lg->y; } } $lY = $lY + $lg->h + 1; foreach ($formContent->layout->md as $md) { if ($md->y > $mY) { $mY = $md->y; } } $mY = $mY + $md->h + 1; foreach ($formContent->layout->sm as $sm) { if ($sm->y > $sY) { $sY = $sm->y; } } $sY = $sY + $sm->h + 1; if (isset($formContent->buttons->rstBtnTxt)) { $newID += 1; $rBtnID = "bf${formID}-${newID}"; if ($fulW) { $btnData['btnTyp'] = 'submit'; $btnData['align'] = $align; $btnData['txt'] = $formContent->buttons->subBtnTxt; $formContent->fields->{$sBtnID} = (object) $btnData; $rBtnData = $btnData; $rBtnData['btnTyp'] = 'reset'; $rBtnData['align'] = $align; $rBtnData['txt'] = $formContent->buttons->rstBtnTxt; $formContent->fields->{$rBtnID} = (object) $rBtnData; } else { $btnData['btnTyp'] = 'submit'; $btnData['align'] = 'right'; $btnData['txt'] = $formContent->buttons->subBtnTxt; $formContent->fields->{$sBtnID} = (object) $btnData; $rBtnData = $btnData; $rBtnData['btnTyp'] = 'reset'; $rBtnData['align'] = 'left'; $rBtnData['txt'] = $formContent->buttons->rstBtnTxt; $formContent->fields->{$rBtnID} = (object) $rBtnData; } $subBtnLay = [ 'h' => 2, 'i' => $sBtnID, 'minH' => 2, 'x' => 0, 'y' => 1000, ]; $subBtnLay['w'] = 3; $subBtnLay['y'] = $lY; $formContent->layout->lg[] = (object) $subBtnLay; $subBtnLay['w'] = 2; $subBtnLay['y'] = $mY; $formContent->layout->md[] = (object) $subBtnLay; $subBtnLay['w'] = 1; $subBtnLay['y'] = $sY; $formContent->layout->sm[] = (object) $subBtnLay; $subBtnLay['i'] = $rBtnID; $subBtnLay['x'] = 3; $subBtnLay['w'] = 3; $subBtnLay['y'] = $lY; $formContent->layout->lg[] = (object) $subBtnLay; $subBtnLay['x'] = 2; $subBtnLay['w'] = 2; $subBtnLay['y'] = $mY; $formContent->layout->md[] = (object) $subBtnLay; $subBtnLay['w'] = 1; $subBtnLay['x'] = 1; $subBtnLay['y'] = $sY; $formContent->layout->sm[] = (object) $subBtnLay; } else { $btnData['btnTyp'] = 'submit'; $btnData['txt'] = $formContent->buttons->subBtnTxt; $btnData['align'] = $align; $formContent->fields->{$sBtnID} = (object) $btnData; $subBtnLay = [ 'h' => 2, 'i' => $sBtnID, 'minH' => 2, 'x' => 0, 'w' => 6, ]; $subBtnLay['y'] = $lY; $formContent->layout->lg[] = (object) $subBtnLay; $subBtnLay['y'] = $mY; $formContent->layout->md[] = (object) $subBtnLay; $subBtnLay['y'] = $sY; $formContent->layout->sm[] = (object) $subBtnLay; } } $adminFormHandler->saveLayoutStyleSheet($formContent->layout, 'bitform-layout-' . $formID . '.css'); unset($formContent->buttons); $formModel->update( [ 'form_content' => wp_json_encode($formContent), ], [ 'id' => $formID, ] ); } } } } }