| 1 |
<?php |
| 2 |
|
| 3 |
namespace Extendify\Notifications; |
| 4 |
|
| 5 |
defined('ABSPATH') || die('No direct access.'); |
| 6 |
|
| 7 |
use Extendify\PartnerData; |
| 8 |
use Extendify\SiteVisibility; |
| 9 |
use Extendify\SiteSettings; |
| 10 |
|
| 11 |
/** |
| 12 |
* The site conditions a notification can ask to be gated on, each mapped to the |
| 13 |
* predicate that answers it. A notification without a trigger is unconditional. |
| 14 |
*/ |
| 15 |
class Triggers |
| 16 |
{ |
| 17 |
// phpcs:ignore PSR12.Properties.ConstantVisibility.NotFound -- 7.0 floor: no const visibility |
| 18 |
const PREDICATES = [ |
| 19 |
'trial-domain' => 'onTrialDomain', |
| 20 |
'unpublished' => 'siteUnpublished', |
| 21 |
'trial-block' => 'onExpiredTrialDomain', |
| 22 |
]; |
| 23 |
|
| 24 |
// phpcs:ignore PSR12.Properties.ConstantVisibility.NotFound -- 7.0 floor: no const visibility |
| 25 |
const TRIAL_WINDOW_DAYS = 16; |
| 26 |
|
| 27 |
public static function passes($trigger) |
| 28 |
{ |
| 29 |
if ($trigger === null || $trigger === '') { |
| 30 |
return true; |
| 31 |
} |
| 32 |
|
| 33 |
if (!is_string($trigger) || !isset(self::PREDICATES[$trigger])) { |
| 34 |
return false; |
| 35 |
} |
| 36 |
|
| 37 |
return call_user_func([self::class, self::PREDICATES[$trigger]]); |
| 38 |
} |
| 39 |
|
| 40 |
private static function siteUnpublished() |
| 41 |
{ |
| 42 |
return !SiteVisibility::isPublished(); |
| 43 |
} |
| 44 |
|
| 45 |
// Substring match, mirroring the domain-suggestion matcher in src/Assist/lib/domains.js. |
| 46 |
private static function onTrialDomain() |
| 47 |
{ |
| 48 |
$host = strtolower((string) \wp_parse_url(\home_url(), PHP_URL_HOST)); |
| 49 |
$sites = (array) PartnerData::setting('trialDomains'); |
| 50 |
|
| 51 |
foreach (array_filter($sites, 'is_string') as $site) { |
| 52 |
$site = strtolower(trim($site)); |
| 53 |
if ($site !== '' && str_contains($host, $site)) { |
| 54 |
return true; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
private static function onExpiredTrialDomain() |
| 62 |
{ |
| 63 |
return self::onTrialDomain() && self::olderThanTrialWindow(); |
| 64 |
} |
| 65 |
|
| 66 |
private static function olderThanTrialWindow() |
| 67 |
{ |
| 68 |
$createdAt = SiteSettings::getSiteCreatedAt(); |
| 69 |
$createdAt = $createdAt === null ? false : strtotime($createdAt); |
| 70 |
|
| 71 |
// A zero MySQL date parses to a negative timestamp, not false, and must not block. |
| 72 |
if ($createdAt === false || $createdAt <= 0) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
return $createdAt <= (time() - (self::TRIAL_WINDOW_DAYS * DAY_IN_SECONDS)); |
| 77 |
} |
| 78 |
} |
| 79 |
|