Config
4 years ago
Helpers
4 years ago
Migrations
4 years ago
Routes
3 years ago
Setup
3 years ago
Wizard
3 years ago
DefaultFormFactory.php
4 years ago
FormRepository.php
4 years ago
LocaleCollection.php
4 years ago
SettingsRepository.php
4 years ago
SettingsRepositoryFactory.php
4 years ago
FormRepository.php
83 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Onboarding; |
| 4 | |
| 5 | /** |
| 6 | * @since 2.8.0 |
| 7 | */ |
| 8 | class FormRepository |
| 9 | { |
| 10 | |
| 11 | /** @var SettingsRepository */ |
| 12 | protected $settingsRepository; |
| 13 | |
| 14 | /** @var DefaultFormFactory */ |
| 15 | protected $defaultFormFactory; |
| 16 | |
| 17 | /** |
| 18 | * @since 2.8.0 |
| 19 | * |
| 20 | * @param SettingsRepository $settingsRepository |
| 21 | * |
| 22 | */ |
| 23 | public function __construct( |
| 24 | SettingsRepositoryFactory $settingsRepositoryFactory, |
| 25 | DefaultFormFactory $defaultFormFactory |
| 26 | ) { |
| 27 | $this->settingsRepository = $settingsRepositoryFactory->make('give_onboarding'); |
| 28 | $this->defaultFormFactory = $defaultFormFactory; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @since 2.8.0 |
| 33 | * @return int Form ID |
| 34 | * |
| 35 | */ |
| 36 | public function getOrMake() |
| 37 | { |
| 38 | return $this->getDefaultFormID() ?: $this->makeAndPersist(); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * @since 2.8.0 |
| 43 | * @return int Form ID |
| 44 | * |
| 45 | */ |
| 46 | public function getDefaultFormID() |
| 47 | { |
| 48 | $formID = $this->settingsRepository->get('form_id'); |
| 49 | |
| 50 | return $this->isFormAvailable($formID) ? $formID : 0; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @since 2.8.0 |
| 55 | * |
| 56 | * @param int $formID |
| 57 | * |
| 58 | * @return bool |
| 59 | * |
| 60 | */ |
| 61 | protected function isFormAvailable($formID) |
| 62 | { |
| 63 | $status = get_post_status($formID); |
| 64 | |
| 65 | return ! (false === $status || 'trash' == $status); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @since 2.8.0 |
| 70 | * @return int Form ID |
| 71 | * |
| 72 | */ |
| 73 | protected function makeAndPersist() |
| 74 | { |
| 75 | $formID = $this->defaultFormFactory->make(); |
| 76 | |
| 77 | $this->settingsRepository->set('form_id', $formID); |
| 78 | $this->settingsRepository->save(); |
| 79 | |
| 80 | return $formID; |
| 81 | } |
| 82 | } |
| 83 |