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