Config
4 years ago
Helpers
4 years ago
Migrations
4 years ago
Routes
1 year ago
Setup
1 year ago
Wizard
1 year ago
BlockFactory.php
1 year ago
DefaultFormFactory.php
4 years ago
FormRepository.php
1 year ago
LocaleCollection.php
4 years ago
SettingsRepository.php
4 years ago
SettingsRepositoryFactory.php
4 years ago
FormRepository.php
102 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Onboarding; |
| 4 | |
| 5 | use Give\DonationForms\Models\DonationForm; |
| 6 | use Give\DonationForms\Properties\FormSettings; |
| 7 | use Give\DonationForms\ValueObjects\DonationFormStatus; |
| 8 | use Give\FormBuilder\Actions\GenerateDefaultDonationFormBlockCollection; |
| 9 | use Give\Log\Log; |
| 10 | |
| 11 | /** |
| 12 | * @since 2.8.0 |
| 13 | */ |
| 14 | class FormRepository |
| 15 | { |
| 16 | |
| 17 | /** @var SettingsRepository */ |
| 18 | protected $settingsRepository; |
| 19 | |
| 20 | /** @var DefaultFormFactory */ |
| 21 | protected $defaultFormFactory; |
| 22 | |
| 23 | /** |
| 24 | * @since 2.8.0 |
| 25 | * |
| 26 | * @param SettingsRepositoryFactory $settingsRepositoryFactory |
| 27 | * @param DefaultFormFactory $defaultFormFactory |
| 28 | */ |
| 29 | public function __construct( |
| 30 | SettingsRepositoryFactory $settingsRepositoryFactory, |
| 31 | DefaultFormFactory $defaultFormFactory |
| 32 | ) { |
| 33 | $this->settingsRepository = $settingsRepositoryFactory->make('give_onboarding'); |
| 34 | $this->defaultFormFactory = $defaultFormFactory; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @since 2.8.0 |
| 39 | * @return int Form ID |
| 40 | * |
| 41 | */ |
| 42 | public function getOrMake() |
| 43 | { |
| 44 | return $this->getDefaultFormID() ?: $this->makeAndPersist(); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @since 2.8.0 |
| 49 | * @return int Form ID |
| 50 | * |
| 51 | */ |
| 52 | public function getDefaultFormID() |
| 53 | { |
| 54 | $formID = $this->settingsRepository->get('form_id'); |
| 55 | |
| 56 | return $this->isFormAvailable($formID) ? $formID : 0; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @since 2.8.0 |
| 61 | * |
| 62 | * @param int $formID |
| 63 | * |
| 64 | * @return bool |
| 65 | * |
| 66 | */ |
| 67 | protected function isFormAvailable($formID) |
| 68 | { |
| 69 | $status = get_post_status($formID); |
| 70 | |
| 71 | return ! (false === $status || 'trash' == $status); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @since 3.15.0 Create the default v3 form. |
| 76 | * @since 2.8.0 |
| 77 | * @return int Form ID |
| 78 | * |
| 79 | */ |
| 80 | protected function makeAndPersist() |
| 81 | { |
| 82 | $form = new DonationForm([ |
| 83 | 'title' => __('GiveWP Donation Form', 'give'), |
| 84 | 'status' => DonationFormStatus::PUBLISHED(), |
| 85 | 'settings' => FormSettings::fromArray([ |
| 86 | 'designId' => 'multi-step', |
| 87 | 'designSettingsImageUrl' => GIVE_PLUGIN_URL . '/assets/dist/images/admin/onboarding/header-image.jpg', |
| 88 | 'designSettingsImageStyle' => 'above', |
| 89 | 'designSettingsImageAlt' => 'GiveWP Onboarding Donation Form', |
| 90 | ]), |
| 91 | 'blocks' => (new GenerateDefaultDonationFormBlockCollection())(), |
| 92 | ]); |
| 93 | |
| 94 | $form->save(); |
| 95 | |
| 96 | $this->settingsRepository->set('form_id', $form->id); |
| 97 | $this->settingsRepository->save(); |
| 98 | |
| 99 | return $form->id; |
| 100 | } |
| 101 | } |
| 102 |