| 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 4.14.2 update default form title |
| 21 |
* @since 3.22.0 Add locale support |
| 22 |
* @since 3.1.0 updated default form blocks to be generated from block models instead of json |
| 23 |
* @since 3.0.0 |
| 24 |
* |
| 25 |
* @return void |
| 26 |
* @throws Exception |
| 27 |
*/ |
| 28 |
public function __invoke() |
| 29 |
{ |
| 30 |
if (isset($_GET['page']) && FormBuilderRouteBuilder::SLUG === $_GET['page']) { |
| 31 |
$locale = $_GET['locale'] ?? ''; |
| 32 |
|
| 33 |
// Little hack for alpha users to make sure the form builder is loaded. |
| 34 |
if (!isset($_GET['donationFormID'])) { |
| 35 |
wp_redirect(FormBuilderRouteBuilder::makeCreateFormRoute($locale)->getUrl()); |
| 36 |
exit(); |
| 37 |
} |
| 38 |
if ('new' === $_GET['donationFormID']) { |
| 39 |
// Make sure the Form will be created using the proper locale |
| 40 |
$locale = $_GET['locale'] ?? ''; |
| 41 |
Language::switchToLocale($locale); |
| 42 |
|
| 43 |
$form = new DonationForm([ |
| 44 |
'title' => __('Donation Form', 'give'), |
| 45 |
'status' => DonationFormStatus::DRAFT(), |
| 46 |
'settings' => FormSettings::fromArray([ |
| 47 |
'enableDonationGoal' => true, |
| 48 |
'goalAmount' => 1000, |
| 49 |
'inheritCampaignColors' => true, |
| 50 |
]), |
| 51 |
'blocks' => (new GenerateDefaultDonationFormBlockCollection())(), |
| 52 |
]); |
| 53 |
|
| 54 |
Hooks::doAction('givewp_form_builder_new_form', $form); |
| 55 |
|
| 56 |
$form->save(); |
| 57 |
|
| 58 |
wp_redirect(FormBuilderRouteBuilder::makeEditFormRoute($form->id, $locale)->getUrl()); |
| 59 |
exit(); |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|