AfterMigrationNotice.php
2 years ago
BlackFridayNotice.php
2 months ago
ChangedTrackingNotice.php
3 years ago
DatabaseEngineNotice.php
1 year ago
DeprecatedFilterNotice.php
4 years ago
DisabledMailFunctionNotice.php
2 months ago
DisabledWPCronNotice.php
1 year ago
EmailWithInvalidSegmentNotice.php
3 years ago
HeadersAlreadySentNotice.php
2 months ago
InactiveSubscribersNotice.php
2 years ago
PHPVersionWarnings.php
2 months ago
PendingApprovalNotice.php
2 years ago
PermanentNotices.php
2 months ago
PremiumFeaturesAvailableNotice.php
2 years ago
SenderDomainAuthenticationNotices.php
5 months ago
SendingQueueBodyCleanupNotice.php
2 months ago
StuckPostNotificationNotice.php
2 months ago
UnauthorizedEmailInNewslettersNotice.php
5 months ago
UnauthorizedEmailNotice.php
5 months ago
WooCommerceVersionWarning.php
1 year ago
WordPressPlaygroundNotice.php
1 year ago
index.php
3 years ago
StuckPostNotificationNotice.php
102 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Util\Notices; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\NewsletterEntity; |
| 9 | use MailPoet\Newsletter\NewslettersRepository; |
| 10 | use MailPoet\WP\Functions as WPFunctions; |
| 11 | use MailPoet\WP\Notice; |
| 12 | |
| 13 | class StuckPostNotificationNotice { |
| 14 | const OPTION_NAME = 'mailpoet-stuck-post-notification-notice'; |
| 15 | const DISMISS_NOTICE_TIMEOUT_SECONDS = WEEK_IN_SECONDS; |
| 16 | |
| 17 | /** @var WPFunctions */ |
| 18 | private $wp; |
| 19 | |
| 20 | /** @var NewslettersRepository */ |
| 21 | private $newslettersRepository; |
| 22 | |
| 23 | public function __construct( |
| 24 | WPFunctions $wp, |
| 25 | NewslettersRepository $newslettersRepository |
| 26 | ) { |
| 27 | $this->wp = $wp; |
| 28 | $this->newslettersRepository = $newslettersRepository; |
| 29 | } |
| 30 | |
| 31 | public function init(bool $shouldDisplay): ?Notice { |
| 32 | if (!$shouldDisplay || $this->wp->getTransient(self::OPTION_NAME)) { |
| 33 | return null; |
| 34 | } |
| 35 | $stuckParents = $this->newslettersRepository->findStuckPostNotificationParents(); |
| 36 | if (empty($stuckParents)) { |
| 37 | return null; |
| 38 | } |
| 39 | return $this->display($stuckParents); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @param array<int, array{parent: NewsletterEntity, hasInvalid: bool}> $stuckParents |
| 44 | */ |
| 45 | private function display(array $stuckParents): Notice { |
| 46 | $extraClasses = 'mailpoet-dismissible-notice is-dismissible'; |
| 47 | return Notice::displayWarning($this->getMessage($stuckParents), $extraClasses, self::OPTION_NAME, false); |
| 48 | } |
| 49 | |
| 50 | public function disable(): void { |
| 51 | $this->wp->setTransient(self::OPTION_NAME, true, self::DISMISS_NOTICE_TIMEOUT_SECONDS); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @param array<int, array{parent: NewsletterEntity, hasInvalid: bool}> $stuckParents |
| 56 | */ |
| 57 | private function getMessage(array $stuckParents): string { |
| 58 | $count = count($stuckParents); |
| 59 | $heading = sprintf( |
| 60 | '<p><b>%s</b></p>', |
| 61 | $this->wp->escHtml(_n( |
| 62 | 'A post notification is stuck and may not have reached all of your subscribers.', |
| 63 | 'Some post notifications are stuck and may not have reached all of your subscribers.', |
| 64 | $count, |
| 65 | 'mailpoet' |
| 66 | )) |
| 67 | ); |
| 68 | |
| 69 | $items = ''; |
| 70 | foreach ($stuckParents as $entry) { |
| 71 | $items .= '<li>' . $this->renderItem($entry['parent'], $entry['hasInvalid']) . '</li>'; |
| 72 | } |
| 73 | |
| 74 | return $heading . '<ul>' . $items . '</ul>'; |
| 75 | } |
| 76 | |
| 77 | private function renderItem(NewsletterEntity $parent, bool $hasInvalid): string { |
| 78 | $reason = $hasInvalid |
| 79 | ? __('flagged as invalid', 'mailpoet') |
| 80 | : __('paused', 'mailpoet'); |
| 81 | |
| 82 | $historyUrl = $this->wp->adminUrl( |
| 83 | 'admin.php?page=mailpoet-newsletters#/notification/history/' . $parent->getId() |
| 84 | ); |
| 85 | |
| 86 | $line = sprintf( |
| 87 | // translators: %1$s is the post notification subject, %2$s is the status (paused / flagged as invalid) |
| 88 | __('"%1$s" is %2$s.', 'mailpoet'), |
| 89 | $this->wp->escHtml($parent->getSubject()), |
| 90 | $this->wp->escHtml($reason) |
| 91 | ); |
| 92 | |
| 93 | $link = sprintf( |
| 94 | ' <a href="%s">%s</a>', |
| 95 | $this->wp->escUrl($historyUrl), |
| 96 | $this->wp->escHtml(__('View sending history', 'mailpoet')) |
| 97 | ); |
| 98 | |
| 99 | return $line . $link; |
| 100 | } |
| 101 | } |
| 102 |