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