PluginProbe
Extendify / 3.2.3
Extendify v3.2.3
3.2.3 3.2.2 3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 All 129 releases
extendify / app / Notifications / Triggers.php

Triggers.php in Extendify 3.2.3, at app/Notifications/Triggers.php

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