| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Onboarding; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Give\Campaigns\Models\Campaign; |
| 7 |
use Give\Campaigns\ValueObjects\CampaignGoalType; |
| 8 |
use Give\Campaigns\ValueObjects\CampaignStatus; |
| 9 |
use Give\Campaigns\ValueObjects\CampaignType; |
| 10 |
use Give\DonationForms\Models\DonationForm; |
| 11 |
use Give\DonationForms\ValueObjects\DonationFormStatus; |
| 12 |
|
| 13 |
/** |
| 14 |
* @since 2.8.0 |
| 15 |
*/ |
| 16 |
class FormRepository |
| 17 |
{ |
| 18 |
|
| 19 |
/** @var SettingsRepository */ |
| 20 |
protected $settingsRepository; |
| 21 |
|
| 22 |
/** @var DefaultFormFactory */ |
| 23 |
protected $defaultFormFactory; |
| 24 |
|
| 25 |
/** |
| 26 |
* @since 2.8.0 |
| 27 |
* |
| 28 |
* @param SettingsRepositoryFactory $settingsRepositoryFactory |
| 29 |
* @param DefaultFormFactory $defaultFormFactory |
| 30 |
*/ |
| 31 |
public function __construct( |
| 32 |
SettingsRepositoryFactory $settingsRepositoryFactory, |
| 33 |
DefaultFormFactory $defaultFormFactory |
| 34 |
) { |
| 35 |
$this->settingsRepository = $settingsRepositoryFactory->make('give_onboarding'); |
| 36 |
$this->defaultFormFactory = $defaultFormFactory; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* @since 2.8.0 |
| 41 |
* @return int Form ID |
| 42 |
* |
| 43 |
*/ |
| 44 |
public function getOrMake() |
| 45 |
{ |
| 46 |
return $this->getDefaultFormID() ?: $this->makeAndPersist(); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @since 2.8.0 |
| 51 |
* @return int Form ID |
| 52 |
* |
| 53 |
*/ |
| 54 |
public function getDefaultFormID() |
| 55 |
{ |
| 56 |
$formID = $this->settingsRepository->get('form_id'); |
| 57 |
|
| 58 |
return $this->isFormAvailable($formID) ? $formID : 0; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* @since 2.8.0 |
| 63 |
* |
| 64 |
* @param int $formID |
| 65 |
* |
| 66 |
* @return bool |
| 67 |
* |
| 68 |
*/ |
| 69 |
protected function isFormAvailable($formID) |
| 70 |
{ |
| 71 |
$status = get_post_status($formID); |
| 72 |
|
| 73 |
return ! (false === $status || 'trash' == $status); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* @since 4.1.0 Update title to "Fundraising Campaign Form" |
| 78 |
* @since 4.0.0 Replace "Donation Form" with "Campaign Form" |
| 79 |
* @since 3.15.0 Create the default v3 form. |
| 80 |
* @since 2.8.0 |
| 81 |
* @return int Form ID |
| 82 |
* |
| 83 |
* @throws Exception |
| 84 |
*/ |
| 85 |
protected function makeAndPersist(): int |
| 86 |
{ |
| 87 |
$campaign = Campaign::create([ |
| 88 |
'type' => CampaignType::CORE(), |
| 89 |
'title' => __('Fundraising Campaign', 'give'), |
| 90 |
'shortDescription' => '', |
| 91 |
'longDescription' => '', |
| 92 |
'logo' => '', |
| 93 |
'image' => '', |
| 94 |
'primaryColor' => '#0b72d9', |
| 95 |
'secondaryColor' => '#27ae60', |
| 96 |
'goal' => 1000, |
| 97 |
'goalType' => CampaignGoalType::AMOUNT(), |
| 98 |
'status' => CampaignStatus::ACTIVE(), |
| 99 |
]); |
| 100 |
|
| 101 |
$form = DonationForm::find($campaign->defaultFormId); |
| 102 |
|
| 103 |
if ($form) { |
| 104 |
$form->title = $campaign->title; |
| 105 |
$form->status = DonationFormStatus::PUBLISHED(); |
| 106 |
$form->settings->designId = 'multi-step'; |
| 107 |
$form->settings->designSettingsImageUrl = GIVE_PLUGIN_URL . 'build/assets/dist/images/admin/onboarding/header-image.jpg'; |
| 108 |
$form->settings->designSettingsImageStyle = 'above'; |
| 109 |
$form->settings->designSettingsImageAlt = $campaign->title; |
| 110 |
|
| 111 |
$form->save(); |
| 112 |
} |
| 113 |
|
| 114 |
$this->settingsRepository->set('form_id', $campaign->defaultFormId); |
| 115 |
$this->settingsRepository->save(); |
| 116 |
|
| 117 |
return $campaign->defaultFormId; |
| 118 |
} |
| 119 |
} |
| 120 |
|