AfterMigrationNotice.php
4 years ago
BlackFridayNotice.php
4 years ago
ChangedTrackingNotice.php
4 years ago
DeprecatedFilterNotice.php
3 years ago
EmailWithInvalidSegmentNotice.php
4 years ago
HeadersAlreadySentNotice.php
4 years ago
InactiveSubscribersNotice.php
4 years ago
PHPVersionWarnings.php
4 years ago
PermanentNotices.php
4 years ago
UnauthorizedEmailInNewslettersNotice.php
4 years ago
UnauthorizedEmailNotice.php
3 years ago
index.php
4 years ago
HeadersAlreadySentNotice.php
95 lines
| 1 | <?php |
| 2 | |
| 3 | namespace MailPoet\Util\Notices; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Settings\SettingsController; |
| 9 | use MailPoet\Settings\TrackingConfig; |
| 10 | use MailPoet\Subscription\Captcha; |
| 11 | use MailPoet\Util\Helpers; |
| 12 | use MailPoet\WP\Functions as WPFunctions; |
| 13 | use MailPoet\WP\Notice; |
| 14 | |
| 15 | class HeadersAlreadySentNotice { |
| 16 | |
| 17 | const DISMISS_NOTICE_TIMEOUT_SECONDS = YEAR_IN_SECONDS; |
| 18 | const OPTION_NAME = 'dismissed-headers-already-sent-notice'; |
| 19 | |
| 20 | /** @var SettingsController */ |
| 21 | private $settings; |
| 22 | |
| 23 | /** @var TrackingConfig */ |
| 24 | private $trackingConfig; |
| 25 | |
| 26 | /** @var WPFunctions */ |
| 27 | private $wp; |
| 28 | |
| 29 | public function __construct( |
| 30 | SettingsController $settings, |
| 31 | TrackingConfig $trackingConfig, |
| 32 | WPFunctions $wp |
| 33 | ) { |
| 34 | $this->settings = $settings; |
| 35 | $this->trackingConfig = $trackingConfig; |
| 36 | $this->wp = $wp; |
| 37 | } |
| 38 | |
| 39 | public function init($shouldDisplay) { |
| 40 | if (!$shouldDisplay) { |
| 41 | return null; |
| 42 | } |
| 43 | $captchaEnabled = $this->settings->get('captcha.type') === Captcha::TYPE_BUILTIN; |
| 44 | $trackingEnabled = $this->trackingConfig->isEmailTrackingEnabled(); |
| 45 | if ($this->areHeadersAlreadySent()) { |
| 46 | return $this->display($captchaEnabled, $trackingEnabled); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | public function areHeadersAlreadySent() { |
| 51 | return !get_transient(self::OPTION_NAME) |
| 52 | && ($this->headersSent() || $this->isWhitespaceInBuffer()); |
| 53 | } |
| 54 | |
| 55 | protected function headersSent() { |
| 56 | return headers_sent(); |
| 57 | } |
| 58 | |
| 59 | public function isWhitespaceInBuffer() { |
| 60 | $content = ob_get_contents(); |
| 61 | if (!$content) { |
| 62 | return false; |
| 63 | } |
| 64 | return preg_match('/^\s+$/', $content); |
| 65 | } |
| 66 | |
| 67 | public function display($captchaEnabled, $trackingEnabled) { |
| 68 | if (!$captchaEnabled && !$trackingEnabled) { |
| 69 | return null; |
| 70 | } |
| 71 | |
| 72 | $errorString = __('It looks like there\'s an issue with some of the PHP files on your website which is preventing MailPoet from functioning correctly. If not resolved, you may experience:', 'mailpoet'); |
| 73 | $errorStringTracking = __('Inaccurate tracking of email opens and clicks', 'mailpoet'); |
| 74 | $errorStringCaptcha = __('CAPTCHA not rendering correctly', 'mailpoet'); |
| 75 | $errorString = $errorString . '<br>' |
| 76 | . ($trackingEnabled ? ('<br> - ' . $errorStringTracking) : '') |
| 77 | . ($captchaEnabled ? ('<br> - ' . $errorStringCaptcha) : ''); |
| 78 | |
| 79 | $howToResolveString = __('[link]Learn how to fix this issue and restore functionality[/link]', 'mailpoet'); |
| 80 | $error = $errorString . '<br><br>' . Helpers::replaceLinkTags($howToResolveString, 'https://kb.mailpoet.com/article/325-the-captcha-image-doesnt-show-up', [ |
| 81 | 'target' => '_blank', |
| 82 | 'data-beacon-article' => '5f20fb5904286306f8078acb', |
| 83 | 'class' => 'button-primary', |
| 84 | ]); |
| 85 | |
| 86 | $extraClasses = 'mailpoet-dismissible-notice is-dismissible'; |
| 87 | |
| 88 | return Notice::displayError($error, $extraClasses, self::OPTION_NAME, true, false); |
| 89 | } |
| 90 | |
| 91 | public function disable() { |
| 92 | $this->wp->setTransient(self::OPTION_NAME, true, self::DISMISS_NOTICE_TIMEOUT_SECONDS); |
| 93 | } |
| 94 | } |
| 95 |