PublicEmailController.php
1 month ago
PublicEmailRoute.php
1 month ago
ShareMetadataBuilder.php
2 months ago
ShareVisibility.php
2 months ago
index.php
2 months ago
ShareVisibility.php
91 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Newsletter\Sharing; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\NewsletterEntity; |
| 9 | use MailPoet\Entities\NewsletterOptionFieldEntity; |
| 10 | use MailPoet\Settings\SettingsController; |
| 11 | |
| 12 | class ShareVisibility { |
| 13 | public const SETTING_DEFAULT_VISIBILITY = 'sharing.default_visibility'; |
| 14 | public const VISIBILITY_DEFAULT = 'default'; |
| 15 | public const VISIBILITY_PUBLIC = 'public'; |
| 16 | public const VISIBILITY_PRIVATE = 'private'; |
| 17 | |
| 18 | private const ALLOWED_VISIBILITIES = [ |
| 19 | self::VISIBILITY_DEFAULT, |
| 20 | self::VISIBILITY_PUBLIC, |
| 21 | self::VISIBILITY_PRIVATE, |
| 22 | ]; |
| 23 | |
| 24 | /** @var SettingsController */ |
| 25 | private $settings; |
| 26 | |
| 27 | public function __construct( |
| 28 | SettingsController $settings |
| 29 | ) { |
| 30 | $this->settings = $settings; |
| 31 | } |
| 32 | |
| 33 | public function canShare(NewsletterEntity $newsletter): bool { |
| 34 | if (!$this->isSupported($newsletter)) { |
| 35 | return false; |
| 36 | } |
| 37 | return $this->getEffectiveVisibility($newsletter) === self::VISIBILITY_PUBLIC; |
| 38 | } |
| 39 | |
| 40 | public function isSupported(NewsletterEntity $newsletter): bool { |
| 41 | return $newsletter->getType() === NewsletterEntity::TYPE_STANDARD |
| 42 | && $newsletter->getStatus() === NewsletterEntity::STATUS_SENT |
| 43 | && $newsletter->getDeletedAt() === null |
| 44 | && (bool)$newsletter->getHash(); |
| 45 | } |
| 46 | |
| 47 | public function getUnavailableReason(NewsletterEntity $newsletter): string { |
| 48 | if ($newsletter->getDeletedAt() !== null) { |
| 49 | return __('Deleted emails cannot be shared.', 'mailpoet'); |
| 50 | } |
| 51 | if ($newsletter->getType() !== NewsletterEntity::TYPE_STANDARD) { |
| 52 | return __('Only standard emails can be shared for now.', 'mailpoet'); |
| 53 | } |
| 54 | if ($newsletter->getStatus() !== NewsletterEntity::STATUS_SENT) { |
| 55 | return __('Only sent emails can be shared.', 'mailpoet'); |
| 56 | } |
| 57 | if (!$newsletter->getHash()) { |
| 58 | return __('This email does not have a public sharing identifier yet.', 'mailpoet'); |
| 59 | } |
| 60 | if ($this->getEffectiveVisibility($newsletter) !== self::VISIBILITY_PUBLIC) { |
| 61 | return __('Sharing is turned off for this email.', 'mailpoet'); |
| 62 | } |
| 63 | return ''; |
| 64 | } |
| 65 | |
| 66 | public function getEffectiveVisibility(NewsletterEntity $newsletter): string { |
| 67 | $visibility = $this->getConfiguredVisibility($newsletter); |
| 68 | if ($visibility === self::VISIBILITY_DEFAULT) { |
| 69 | return $this->getDefaultVisibility(); |
| 70 | } |
| 71 | return $visibility; |
| 72 | } |
| 73 | |
| 74 | public function getConfiguredVisibility(NewsletterEntity $newsletter): string { |
| 75 | return $this->sanitize((string)$newsletter->getOptionValue(NewsletterOptionFieldEntity::NAME_SHARE_VISIBILITY)); |
| 76 | } |
| 77 | |
| 78 | public function sanitize(string $visibility): string { |
| 79 | return in_array($visibility, self::ALLOWED_VISIBILITIES, true) |
| 80 | ? $visibility |
| 81 | : self::VISIBILITY_DEFAULT; |
| 82 | } |
| 83 | |
| 84 | public function getDefaultVisibility(): string { |
| 85 | $visibility = (string)$this->settings->get(self::SETTING_DEFAULT_VISIBILITY, self::VISIBILITY_PUBLIC); |
| 86 | return in_array($visibility, [self::VISIBILITY_PUBLIC, self::VISIBILITY_PRIVATE], true) |
| 87 | ? $visibility |
| 88 | : self::VISIBILITY_PUBLIC; |
| 89 | } |
| 90 | } |
| 91 |