Akismet
9 months ago
Exceptions
1 year ago
EmailAddressWhiteList.php
1 year ago
ServiceProvider.php
1 year ago
ServiceProvider.php
66 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonationSpam; |
| 4 | |
| 5 | use Give\DonationSpam\Akismet\Actions\ValidateDonation; |
| 6 | use Give\DonationSpam\Exceptions\SpamDonationException; |
| 7 | use Give\ServiceProviders\ServiceProvider as ServiceProviderInterface; |
| 8 | |
| 9 | /** |
| 10 | * @since 3.15.0 |
| 11 | */ |
| 12 | class ServiceProvider implements ServiceProviderInterface |
| 13 | { |
| 14 | /** |
| 15 | * @since 3.15.0 |
| 16 | * @inheritDoc |
| 17 | */ |
| 18 | public function register(): void |
| 19 | { |
| 20 | /** |
| 21 | * @since 3.15.1 Case filtered value as an array to enforce type. |
| 22 | * @since 3.15.0 |
| 23 | */ |
| 24 | give()->singleton(EmailAddressWhiteList::class, function () { |
| 25 | return new EmailAddressWhiteList( |
| 26 | (array)apply_filters('give_akismet_whitelist_emails', give_akismet_get_whitelisted_emails()) |
| 27 | ); |
| 28 | }); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @since 3.22.0 updated Akismet validation to use new givewp_donation_form_fields_validated action |
| 33 | * @since 3.15.0 |
| 34 | * |
| 35 | * @inheritDoc |
| 36 | * |
| 37 | * @throws SpamDonationException |
| 38 | */ |
| 39 | public function boot(): void |
| 40 | { |
| 41 | if ($this->isAkismetEnabledAndConfigured()) { |
| 42 | add_action('givewp_donation_form_fields_validated', static function (array $data) { |
| 43 | give(ValidateDonation::class)( |
| 44 | $data['email'] ?? '', |
| 45 | $data['comment'] ?? '', |
| 46 | $data['firstName'] ?? '', |
| 47 | $data['lastName'] ?? '' |
| 48 | ); |
| 49 | }); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @since 3.15.0 |
| 55 | * @return bool |
| 56 | */ |
| 57 | public function isAkismetEnabledAndConfigured(): bool |
| 58 | { |
| 59 | return |
| 60 | give_check_akismet_key() |
| 61 | && give_is_setting_enabled( |
| 62 | give_get_option('akismet_spam_protection', 'enabled') |
| 63 | ); |
| 64 | } |
| 65 | } |
| 66 |