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
DisabledWPCronNotice.php
80 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\Cron\CronHelper; |
| 9 | use MailPoet\Cron\CronTrigger; |
| 10 | use MailPoet\Settings\SettingsController; |
| 11 | use MailPoet\Util\Helpers; |
| 12 | use MailPoet\WP\Functions as WPFunctions; |
| 13 | use MailPoet\WP\Notice; |
| 14 | |
| 15 | class DisabledWPCronNotice { |
| 16 | |
| 17 | const DISMISS_NOTICE_TIMEOUT_SECONDS = YEAR_IN_SECONDS; |
| 18 | const OPTION_NAME = 'dismissed-wp-cron-disabled-notice'; |
| 19 | |
| 20 | /** @var WPFunctions */ |
| 21 | private $wp; |
| 22 | |
| 23 | /** @var CronHelper */ |
| 24 | private $cronHelper; |
| 25 | |
| 26 | /** @var SettingsController */ |
| 27 | private $settings; |
| 28 | |
| 29 | public function __construct( |
| 30 | WPFunctions $wp, |
| 31 | CronHelper $cronHelper, |
| 32 | SettingsController $settings |
| 33 | ) { |
| 34 | $this->wp = $wp; |
| 35 | $this->cronHelper = $cronHelper; |
| 36 | $this->settings = $settings; |
| 37 | } |
| 38 | |
| 39 | public function init($shouldDisplay) { |
| 40 | if (!$shouldDisplay) { |
| 41 | return null; |
| 42 | } |
| 43 | $isDismissed = $this->wp->getTransient(self::OPTION_NAME); |
| 44 | $currentMethod = $this->settings->get(CronTrigger::SETTING_CURRENT_METHOD); |
| 45 | $isWPCronMethodActive = $currentMethod === CronTrigger::METHOD_ACTION_SCHEDULER; |
| 46 | $isCronFunctional = $this->isCronFunctional(); |
| 47 | if (!$isDismissed && $isWPCronMethodActive && $this->isWPCronDisabled() && !$isCronFunctional) { |
| 48 | return $this->display(); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | public function isWPCronDisabled() { |
| 53 | return defined('DISABLE_WP_CRON') && DISABLE_WP_CRON; |
| 54 | } |
| 55 | |
| 56 | public function isCronFunctional(): bool { |
| 57 | // If a cron run was started/completed less than an hour ago, we consider it functional. |
| 58 | $lastRunThreshold = time() - HOUR_IN_SECONDS; |
| 59 | return ($this->cronHelper->getDaemon()['run_started_at'] ?? 0) > $lastRunThreshold |
| 60 | || ($this->cronHelper->getDaemon()['run_completed_at'] ?? 0) > $lastRunThreshold; |
| 61 | } |
| 62 | |
| 63 | public function display() { |
| 64 | $errorString = __('WordPress built-in cron is disabled with the DISABLE_WP_CRON constant on your website, this prevents MailPoet sending from working. Please enable WordPress built-in cron or choose a different cron method in MailPoet Settings.', 'mailpoet'); |
| 65 | |
| 66 | $buttonString = __('[link]Go to Settings[/link]', 'mailpoet'); |
| 67 | $error = $errorString . '<br><br>' . Helpers::replaceLinkTags($buttonString, 'admin.php?page=mailpoet-settings#advanced', [ |
| 68 | 'class' => 'button-primary', |
| 69 | ]); |
| 70 | |
| 71 | $extraClasses = 'mailpoet-dismissible-notice is-dismissible'; |
| 72 | |
| 73 | return Notice::displayError($error, $extraClasses, self::OPTION_NAME, true, false); |
| 74 | } |
| 75 | |
| 76 | public function disable() { |
| 77 | $this->wp->setTransient(self::OPTION_NAME, true, self::DISMISS_NOTICE_TIMEOUT_SECONDS); |
| 78 | } |
| 79 | } |
| 80 |