| 1 |
<?php |
| 2 |
/** |
| 3 |
* Confirmation mails that did not reach the mail server recently. |
| 4 |
* |
| 5 |
* A failed send used to leave an opt-in silently "unconfirmed"; site owners |
| 6 |
* found out through complaints. Since 5.8 every send is recorded |
| 7 |
* (OptInMailTracker) and this check counts the failures of the last seven |
| 8 |
* days, linking to the filtered opt-in list. |
| 9 |
* |
| 10 |
* @package Forge12\DoubleOptIn\Health |
| 11 |
* @since 5.8.0 |
| 12 |
*/ |
| 13 |
|
| 14 |
declare( strict_types=1 ); |
| 15 |
|
| 16 |
namespace Forge12\DoubleOptIn\Health; |
| 17 |
|
| 18 |
use Forge12\DoubleOptIn\Repository\OptInMailStatusRepository; |
| 19 |
|
| 20 |
if ( ! defined( 'ABSPATH' ) ) { |
| 21 |
exit; |
| 22 |
} |
| 23 |
|
| 24 |
final class MailFailuresHealthCheck implements HealthCheckInterface { |
| 25 |
|
| 26 |
public const CACHE_KEY = 'f12_doi_mail_failures_7d'; |
| 27 |
|
| 28 |
private const WINDOW = 7 * 86400; |
| 29 |
|
| 30 |
/** @var OptInMailStatusRepository */ |
| 31 |
private $repository; |
| 32 |
|
| 33 |
public function __construct( OptInMailStatusRepository $repository ) { |
| 34 |
$this->repository = $repository; |
| 35 |
} |
| 36 |
|
| 37 |
public function getId(): string { |
| 38 |
return 'f12_doi_mail_failures'; |
| 39 |
} |
| 40 |
|
| 41 |
public function getLabel(): string { |
| 42 |
return __( 'Confirmation mail delivery', 'double-opt-in' ); |
| 43 |
} |
| 44 |
|
| 45 |
public function getPackage(): string { |
| 46 |
return 'core'; |
| 47 |
} |
| 48 |
|
| 49 |
public function run(): HealthCheckResult { |
| 50 |
$failed = $this->failedLastWeek(); |
| 51 |
|
| 52 |
if ( $failed === 0 ) { |
| 53 |
return new HealthCheckResult( |
| 54 |
HealthCheckResult::STATUS_GOOD, |
| 55 |
__( 'Confirmation mails reach the mail server', 'double-opt-in' ), |
| 56 |
__( 'No confirmation mail failed in the last seven days.', 'double-opt-in' ), |
| 57 |
'0' |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
return new HealthCheckResult( |
| 62 |
HealthCheckResult::STATUS_RECOMMENDED, |
| 63 |
sprintf( |
| 64 |
/* translators: %d: number of failed confirmation mails */ |
| 65 |
_n( '%d confirmation mail could not be sent', '%d confirmation mails could not be sent', $failed, 'double-opt-in' ), |
| 66 |
$failed |
| 67 |
), |
| 68 |
__( 'In the last seven days WordPress could not hand these confirmation mails to the mail server, so the visitors never received a link. The opt-in list shows the error for each one. Check the mail settings of your site, for example with an SMTP plugin, then resend the confirmation from the opt-in.', 'double-opt-in' ), |
| 69 |
(string) $failed, |
| 70 |
__( 'Show the affected opt-ins', 'double-opt-in' ), |
| 71 |
admin_url( 'admin.php?page=f12-doi-admin#/optins?mail=failed' ) |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
private function failedLastWeek(): int { |
| 76 |
$cached = get_transient( self::CACHE_KEY ); |
| 77 |
if ( $cached !== false ) { |
| 78 |
return (int) $cached; |
| 79 |
} |
| 80 |
$count = $this->repository->countFailedSince( self::WINDOW ); |
| 81 |
set_transient( self::CACHE_KEY, $count, 3600 ); |
| 82 |
return $count; |
| 83 |
} |
| 84 |
} |
| 85 |
|