暫無描述

HeadersAlreadySentNotice.php 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace MailPoet\Util\Notices;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Settings\SettingsController;
  5. use MailPoet\Subscription\Captcha;
  6. use MailPoet\Util\Helpers;
  7. use MailPoet\WP\Functions as WPFunctions;
  8. use MailPoet\WP\Notice;
  9. class HeadersAlreadySentNotice {
  10. const DISMISS_NOTICE_TIMEOUT_SECONDS = YEAR_IN_SECONDS;
  11. const OPTION_NAME = 'dismissed-headers-already-sent-notice';
  12. /** @var SettingsController */
  13. private $settings;
  14. /** @var WPFunctions */
  15. private $wp;
  16. public function __construct(
  17. SettingsController $settings,
  18. WPFunctions $wp
  19. ) {
  20. $this->settings = $settings;
  21. $this->wp = $wp;
  22. }
  23. public function init($shouldDisplay) {
  24. if (!$shouldDisplay) {
  25. return null;
  26. }
  27. $captchaEnabled = $this->settings->get('captcha.type') === Captcha::TYPE_BUILTIN;
  28. $trackingEnabled = $this->settings->get('tracking.enabled');
  29. if ($this->areHeadersAlreadySent()) {
  30. return $this->display($captchaEnabled, $trackingEnabled);
  31. }
  32. }
  33. public function areHeadersAlreadySent() {
  34. return !get_transient(self::OPTION_NAME)
  35. && ($this->headersSent() || $this->isWhitespaceInBuffer());
  36. }
  37. protected function headersSent() {
  38. return headers_sent();
  39. }
  40. public function isWhitespaceInBuffer() {
  41. $content = ob_get_contents();
  42. if (!$content) {
  43. return false;
  44. }
  45. return preg_match('/^\s+$/', $content);
  46. }
  47. public function display($captchaEnabled, $trackingEnabled) {
  48. if (!$captchaEnabled && !$trackingEnabled) {
  49. return null;
  50. }
  51. $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');
  52. $errorStringTracking = __('Inaccurate tracking of email opens and clicks', 'mailpoet');
  53. $errorStringCaptcha = __('CAPTCHA not rendering correctly', 'mailpoet');
  54. $errorString = $errorString . '<br>'
  55. . ($trackingEnabled ? ('<br> - ' . $errorStringTracking) : '')
  56. . ($captchaEnabled ? ('<br> - ' . $errorStringCaptcha) : '');
  57. $howToResolveString = __('[link]Learn how to fix this issue and restore functionality[/link]', 'mailpoet');
  58. $error = $errorString . '<br><br>' . Helpers::replaceLinkTags($howToResolveString, 'https://kb.mailpoet.com/article/325-the-captcha-image-doesnt-show-up', [
  59. 'target' => '_blank',
  60. 'data-beacon-article' => '5f20fb5904286306f8078acb',
  61. 'class' => 'button-primary',
  62. ]);
  63. $extraClasses = 'mailpoet-dismissible-notice is-dismissible';
  64. return Notice::displayError($error, $extraClasses, self::OPTION_NAME, true, false);
  65. }
  66. public function disable() {
  67. $this->wp->setTransient(self::OPTION_NAME, true, self::DISMISS_NOTICE_TIMEOUT_SECONDS);
  68. }
  69. }