FormSettings.php
423 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonationForms\Properties; |
| 4 | |
| 5 | use Give\DonationForms\ValueObjects\DesignSettingsImageStyle; |
| 6 | use Give\DonationForms\ValueObjects\DesignSettingsLogoPosition; |
| 7 | use Give\DonationForms\ValueObjects\DesignSettingsSectionStyle; |
| 8 | use Give\DonationForms\ValueObjects\DesignSettingsTextFieldStyle; |
| 9 | use Give\DonationForms\ValueObjects\DonationFormStatus; |
| 10 | use Give\DonationForms\ValueObjects\GoalProgressType; |
| 11 | use Give\DonationForms\ValueObjects\GoalType; |
| 12 | use Give\Framework\Support\Contracts\Arrayable; |
| 13 | use Give\Framework\Support\Contracts\Jsonable; |
| 14 | |
| 15 | /** |
| 16 | * @since 3.16.0 Added $enableReceiptConfirmationPage property |
| 17 | * @since 3.12.0 Add goalProgressType |
| 18 | * @since 3.2.0 Remove addSlashesRecursive method |
| 19 | * @since 3.0.0 |
| 20 | */ |
| 21 | class FormSettings implements Arrayable, Jsonable |
| 22 | { |
| 23 | /** |
| 24 | * @var boolean |
| 25 | */ |
| 26 | public $showHeader; |
| 27 | /** |
| 28 | * @var boolean |
| 29 | */ |
| 30 | public $showHeading; |
| 31 | /** |
| 32 | * @var boolean |
| 33 | */ |
| 34 | public $showDescription; |
| 35 | /** |
| 36 | * @var string |
| 37 | */ |
| 38 | public $formTitle; |
| 39 | /** |
| 40 | * @var boolean |
| 41 | */ |
| 42 | public $enableDonationGoal; |
| 43 | /** |
| 44 | * @var boolean |
| 45 | */ |
| 46 | public $enableAutoClose; |
| 47 | /** |
| 48 | * @var GoalType |
| 49 | */ |
| 50 | public $goalType; |
| 51 | /** |
| 52 | * @var GoalProgressType |
| 53 | */ |
| 54 | public $goalProgressType; |
| 55 | /** |
| 56 | * @var string |
| 57 | */ |
| 58 | public $goalStartDate; |
| 59 | /** |
| 60 | * @var string |
| 61 | */ |
| 62 | public $goalEndDate; |
| 63 | /** |
| 64 | * @var string |
| 65 | */ |
| 66 | public $designId; |
| 67 | /** |
| 68 | * @var string |
| 69 | */ |
| 70 | public $heading; |
| 71 | /** |
| 72 | * @var string |
| 73 | */ |
| 74 | public $description; |
| 75 | /** |
| 76 | * @var string |
| 77 | */ |
| 78 | public $primaryColor; |
| 79 | /** |
| 80 | * @var string |
| 81 | */ |
| 82 | public $secondaryColor; |
| 83 | /** |
| 84 | * @var float |
| 85 | */ |
| 86 | public $goalAmount; |
| 87 | /** |
| 88 | * @since 3.2.0 Added registrationNotification property. |
| 89 | * @var string |
| 90 | */ |
| 91 | public $registrationNotification; |
| 92 | /** |
| 93 | * @var string |
| 94 | */ |
| 95 | public $customCss; |
| 96 | /** |
| 97 | * @var string |
| 98 | */ |
| 99 | public $goalAchievedMessage; |
| 100 | |
| 101 | /** |
| 102 | * @var string |
| 103 | */ |
| 104 | public $pageSlug; |
| 105 | |
| 106 | /** |
| 107 | * @var string |
| 108 | */ |
| 109 | public $receiptHeading; |
| 110 | |
| 111 | /** |
| 112 | * @var string |
| 113 | */ |
| 114 | public $receiptDescription; |
| 115 | |
| 116 | /** |
| 117 | * @var DonationFormStatus |
| 118 | */ |
| 119 | public $formStatus; |
| 120 | |
| 121 | /** |
| 122 | * @var array |
| 123 | */ |
| 124 | public $emailTemplateOptions; |
| 125 | |
| 126 | /** |
| 127 | * @var string |
| 128 | * @todo Extract to a value object. |
| 129 | */ |
| 130 | public $emailOptionsStatus; |
| 131 | |
| 132 | /** |
| 133 | * @var string |
| 134 | */ |
| 135 | public $emailTemplate; |
| 136 | |
| 137 | /** |
| 138 | * @var string |
| 139 | */ |
| 140 | public $emailLogo; |
| 141 | |
| 142 | /** |
| 143 | * @var string |
| 144 | */ |
| 145 | public $emailFromName; |
| 146 | |
| 147 | /** |
| 148 | * @var string |
| 149 | */ |
| 150 | public $emailFromEmail; |
| 151 | |
| 152 | /** |
| 153 | * @var boolean |
| 154 | */ |
| 155 | public $formGridCustomize; |
| 156 | |
| 157 | /** |
| 158 | * @var string |
| 159 | */ |
| 160 | public $formGridRedirectUrl; |
| 161 | |
| 162 | /** |
| 163 | * @var string |
| 164 | */ |
| 165 | public $formGridDonateButtonText; |
| 166 | |
| 167 | /** |
| 168 | * @var boolean |
| 169 | */ |
| 170 | public $formGridHideDocumentationLink; |
| 171 | |
| 172 | /** |
| 173 | * @var boolean |
| 174 | */ |
| 175 | public $offlineDonationsCustomize; |
| 176 | |
| 177 | /** |
| 178 | * @var string |
| 179 | */ |
| 180 | public $offlineDonationsInstructions; |
| 181 | |
| 182 | /** |
| 183 | * @var string |
| 184 | */ |
| 185 | public $donateButtonCaption; |
| 186 | /** |
| 187 | * @var string |
| 188 | */ |
| 189 | public $multiStepFirstButtonText; |
| 190 | /** |
| 191 | * @var string |
| 192 | */ |
| 193 | public $multiStepNextButtonText; |
| 194 | |
| 195 | /** |
| 196 | * @var array |
| 197 | */ |
| 198 | public $pdfSettings; |
| 199 | |
| 200 | /** |
| 201 | * @since 3.4.0 |
| 202 | * @var string |
| 203 | */ |
| 204 | public $designSettingsImageUrl; |
| 205 | |
| 206 | /** |
| 207 | * @since 3.11.0 |
| 208 | * @var string |
| 209 | */ |
| 210 | public $designSettingsImageAlt; |
| 211 | |
| 212 | /** |
| 213 | * @since 3.4.0 |
| 214 | * @var string |
| 215 | */ |
| 216 | public $designSettingsImageStyle; |
| 217 | |
| 218 | /** |
| 219 | * @since 3.4.0 |
| 220 | * @var string |
| 221 | */ |
| 222 | public $designSettingsLogoUrl; |
| 223 | |
| 224 | /** |
| 225 | * @since 3.4.0 |
| 226 | * @var string |
| 227 | */ |
| 228 | public $designSettingsLogoPosition; |
| 229 | |
| 230 | /** |
| 231 | * @since 3.4.0 |
| 232 | * @var string |
| 233 | */ |
| 234 | public $designSettingsSectionStyle; |
| 235 | |
| 236 | /** |
| 237 | * @since 3.4.0 |
| 238 | * @var string |
| 239 | */ |
| 240 | public $designSettingsTextFieldStyle; |
| 241 | |
| 242 | /** |
| 243 | * @since 3.6.0 |
| 244 | * @var string |
| 245 | */ |
| 246 | public $designSettingsImageColor; |
| 247 | |
| 248 | /** |
| 249 | * @since 3.6.0 |
| 250 | * @var string |
| 251 | */ |
| 252 | public $designSettingsImageOpacity; |
| 253 | |
| 254 | /** |
| 255 | * @since 3.7.0 |
| 256 | * @var string |
| 257 | */ |
| 258 | public $formExcerpt; |
| 259 | |
| 260 | /** |
| 261 | * @since 3.9.0 |
| 262 | * @var array |
| 263 | */ |
| 264 | public $currencySwitcherSettings; |
| 265 | /** |
| 266 | * @since 3.16.0 |
| 267 | * @var bool |
| 268 | */ |
| 269 | public $enableReceiptConfirmationPage; |
| 270 | |
| 271 | /** |
| 272 | * @since 3.16.0 Added $enableReceiptConfirmationPage |
| 273 | * @since 3.7.0 Added formExcerpt |
| 274 | * @since 3.11.0 Sanitize customCSS property |
| 275 | * @since 3.2.0 Added registrationNotification |
| 276 | * @since 3.0.0 |
| 277 | */ |
| 278 | public static function fromArray(array $array): self |
| 279 | { |
| 280 | $self = new self(); |
| 281 | |
| 282 | $self->showHeader = $array['showHeader'] ?? true; |
| 283 | $self->showHeading = $array['showHeading'] ?? true; |
| 284 | $self->heading = $array['heading'] ?? __('Support Our Cause', 'give'); |
| 285 | $self->showDescription = $array['showDescription'] ?? true; |
| 286 | $self->description = $array['description'] ?? __( |
| 287 | 'Help our organization by donating today! Donations go to making a difference for our cause.', |
| 288 | 'give' |
| 289 | ); |
| 290 | $self->formTitle = $array['formTitle'] ?? __('Donation Form', 'give'); |
| 291 | $self->donateButtonCaption = $array['donateButtonCaption'] ?? __('Donate now', 'give'); |
| 292 | $self->enableDonationGoal = $array['enableDonationGoal'] ?? false; |
| 293 | $self->enableAutoClose = $array['enableAutoClose'] ?? false; |
| 294 | $self->goalType = ! empty($array['goalType']) && GoalType::isValid($array['goalType']) ? new GoalType( |
| 295 | $array['goalType'] |
| 296 | ) : GoalType::AMOUNT(); |
| 297 | $self->goalProgressType = ! empty($array['goalProgressType']) && GoalProgressType::isValid($array['goalProgressType']) |
| 298 | ? new GoalProgressType($array['goalProgressType']) |
| 299 | : GoalProgressType::ALL_TIME(); |
| 300 | $self->goalStartDate = $array['goalStartDate'] ?? ''; |
| 301 | $self->goalEndDate = $array['goalEndDate'] ?? ''; |
| 302 | $self->designId = $array['designId'] ?? null; |
| 303 | $self->primaryColor = $array['primaryColor'] ?? '#69b86b'; |
| 304 | $self->secondaryColor = $array['secondaryColor'] ?? '#f49420'; |
| 305 | $self->goalAmount = $array['goalAmount'] ?? 0; |
| 306 | $self->registrationNotification = $array['registrationNotification'] ?? false; |
| 307 | $self->customCss = wp_strip_all_tags($array['customCss'] ?? ''); |
| 308 | $self->pageSlug = $array['pageSlug'] ?? ''; |
| 309 | $self->goalAchievedMessage = $array['goalAchievedMessage'] ?? __( |
| 310 | 'Thank you to all our donors, we have met our fundraising goal.', |
| 311 | 'give' |
| 312 | ); |
| 313 | $self->receiptHeading = $array['receiptHeading'] ?? __( |
| 314 | 'Hey {first_name}, thanks for your donation!', |
| 315 | 'give' |
| 316 | ); |
| 317 | $self->receiptDescription = $array['receiptDescription'] ?? __( |
| 318 | '{first_name}, your contribution means a lot and will be put to good use in making a difference. We’ve sent your donation receipt to {email}.', |
| 319 | 'give' |
| 320 | ); |
| 321 | |
| 322 | $self->enableReceiptConfirmationPage = $array['enableReceiptConfirmationPage'] ?? false; |
| 323 | |
| 324 | $self->formStatus = ! empty($array['formStatus']) ? new DonationFormStatus( |
| 325 | $array['formStatus'] |
| 326 | ) : DonationFormStatus::DRAFT(); |
| 327 | |
| 328 | $self->formGridCustomize = $array['formGridCustomize'] ?? false; |
| 329 | $self->formGridRedirectUrl = $array['formGridRedirectUrl'] ?? ''; |
| 330 | $self->formGridDonateButtonText = $array['formGridDonateButtonText'] ?? ''; |
| 331 | $self->formGridHideDocumentationLink = $array['formGridHideDocumentationLink'] ?? false; |
| 332 | |
| 333 | $self->emailTemplateOptions = $array['emailTemplateOptions'] ?? []; |
| 334 | |
| 335 | $self->emailOptionsStatus = $array['emailOptionsStatus'] ?? 'global'; |
| 336 | |
| 337 | $self->emailTemplate = $array['emailTemplate'] ?? 'default'; |
| 338 | |
| 339 | $self->emailFromName = $array['emailFromName'] ?? ''; |
| 340 | |
| 341 | $self->emailFromEmail = $array['emailFromEmail'] ?? ''; |
| 342 | |
| 343 | $self->emailLogo = $array['emailLogo'] ?? ''; |
| 344 | |
| 345 | $self->offlineDonationsCustomize = $array['offlineDonationsCustomize'] ?? false; |
| 346 | |
| 347 | $self->offlineDonationsInstructions = $array['offlineDonationsInstructions'] ?? ''; |
| 348 | |
| 349 | $self->multiStepFirstButtonText = $array['multiStepFirstButtonText'] ?? __('Donate now', 'give'); |
| 350 | |
| 351 | $self->multiStepNextButtonText = $array['multiStepNextButtonText'] ?? __('Continue', 'give'); |
| 352 | |
| 353 | $self->pdfSettings = isset($array['pdfSettings']) && is_array( |
| 354 | $array['pdfSettings'] |
| 355 | ) ? $array['pdfSettings'] : []; |
| 356 | |
| 357 | $self->designSettingsImageUrl = $array['designSettingsImageUrl'] ?? ''; |
| 358 | $self->designSettingsImageAlt = $array['designSettingsImageAlt'] ?? $self->formTitle; |
| 359 | $self->designSettingsImageStyle = ! empty($array['designSettingsImageStyle']) ? new DesignSettingsImageStyle( |
| 360 | $array['designSettingsImageStyle'] |
| 361 | ) : DesignSettingsImageStyle::BACKGROUND(); |
| 362 | |
| 363 | $self->designSettingsLogoUrl = $array['designSettingsLogoUrl'] ?? ''; |
| 364 | $self->designSettingsLogoPosition = ! empty($array['designSettingsLogoPosition']) ? new DesignSettingsLogoPosition( |
| 365 | $array['designSettingsLogoPosition'] |
| 366 | ) : DesignSettingsLogoPosition::LEFT(); |
| 367 | |
| 368 | $self->designSettingsSectionStyle = ! empty($array['designSettingsSectionStyle']) ? new DesignSettingsSectionStyle( |
| 369 | $array['designSettingsSectionStyle'] |
| 370 | ) : DesignSettingsSectionStyle::DEFAULT(); |
| 371 | |
| 372 | $self->designSettingsTextFieldStyle = ! empty($array['designSettingsTextFieldStyle']) ? new DesignSettingsTextFieldStyle( |
| 373 | $array['designSettingsTextFieldStyle'] |
| 374 | ) : DesignSettingsTextFieldStyle::DEFAULT(); |
| 375 | |
| 376 | $self->designSettingsImageColor = $array['designSettingsImageColor'] ?? ''; |
| 377 | |
| 378 | $self->designSettingsImageOpacity = $array['designSettingsImageOpacity'] ?? ''; |
| 379 | |
| 380 | $self->formExcerpt = $array['formExcerpt'] ?? ''; |
| 381 | |
| 382 | $self->currencySwitcherSettings = isset($array['currencySwitcherSettings']) && is_array( |
| 383 | $array['currencySwitcherSettings'] |
| 384 | ) ? $array['currencySwitcherSettings'] : []; |
| 385 | |
| 386 | return $self; |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * @since 3.0.0 |
| 391 | */ |
| 392 | public static function fromJson(string $json): self |
| 393 | { |
| 394 | return self::fromArray( |
| 395 | json_decode($json, true) |
| 396 | ); |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * @since 3.0.0 |
| 401 | */ |
| 402 | public function toArray(): array |
| 403 | { |
| 404 | return get_object_vars($this); |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * @since 3.2.0 Remove call to addSlashesRecursive method for emailTemplateOptions in favor of SanitizeDonationFormPreviewRequest class |
| 409 | * @since 3.0.0 |
| 410 | */ |
| 411 | public function toJson($options = 0): string |
| 412 | { |
| 413 | return json_encode( |
| 414 | array_merge( |
| 415 | $this->toArray(), |
| 416 | [ |
| 417 | 'goalType' => $this->goalType ? $this->goalType->getValue() : null, |
| 418 | ] |
| 419 | ) |
| 420 | ); |
| 421 | } |
| 422 | } |
| 423 |