CreateFormRoute.php
1 year ago
EditFormRoute.php
1 year ago
RegisterFormBuilderPageRoute.php
1 year ago
RegisterFormBuilderRestRoutes.php
1 year ago
CreateFormRoute.php
63 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\FormBuilder\Routes; |
| 4 | |
| 5 | use Exception; |
| 6 | use Give\DonationForms\Models\DonationForm; |
| 7 | use Give\DonationForms\Properties\FormSettings; |
| 8 | use Give\DonationForms\ValueObjects\DonationFormStatus; |
| 9 | use Give\FormBuilder\Actions\GenerateDefaultDonationFormBlockCollection; |
| 10 | use Give\FormBuilder\FormBuilderRouteBuilder; |
| 11 | use Give\Helpers\Hooks; |
| 12 | use Give\Helpers\Language; |
| 13 | |
| 14 | /** |
| 15 | * Route to create a new form |
| 16 | */ |
| 17 | class CreateFormRoute |
| 18 | { |
| 19 | /** |
| 20 | * @since 3.22.0 Add locale support |
| 21 | * @since 3.1.0 updated default form blocks to be generated from block models instead of json |
| 22 | * @since 3.0.0 |
| 23 | * |
| 24 | * @return void |
| 25 | * @throws Exception |
| 26 | */ |
| 27 | public function __invoke() |
| 28 | { |
| 29 | if (isset($_GET['page']) && FormBuilderRouteBuilder::SLUG === $_GET['page']) { |
| 30 | $locale = $_GET['locale'] ?? ''; |
| 31 | |
| 32 | // Little hack for alpha users to make sure the form builder is loaded. |
| 33 | if (!isset($_GET['donationFormID'])) { |
| 34 | wp_redirect(FormBuilderRouteBuilder::makeCreateFormRoute($locale)->getUrl()); |
| 35 | exit(); |
| 36 | } |
| 37 | if ('new' === $_GET['donationFormID']) { |
| 38 | // Make sure the Form will be created using the proper locale |
| 39 | $locale = $_GET['locale'] ?? ''; |
| 40 | Language::switchToLocale($locale); |
| 41 | |
| 42 | $form = new DonationForm([ |
| 43 | 'title' => __('GiveWP Donation Form', 'give'), |
| 44 | 'status' => DonationFormStatus::DRAFT(), |
| 45 | 'settings' => FormSettings::fromArray([ |
| 46 | 'enableDonationGoal' => true, |
| 47 | 'goalAmount' => 1000, |
| 48 | 'inheritCampaignColors' => true, |
| 49 | ]), |
| 50 | 'blocks' => (new GenerateDefaultDonationFormBlockCollection())(), |
| 51 | ]); |
| 52 | |
| 53 | Hooks::doAction('givewp_form_builder_new_form', $form); |
| 54 | |
| 55 | $form->save(); |
| 56 | |
| 57 | wp_redirect(FormBuilderRouteBuilder::makeEditFormRoute($form->id, $locale)->getUrl()); |
| 58 | exit(); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 |