| 1 |
<?php |
| 2 |
|
| 3 |
namespace Extendify\Notifications; |
| 4 |
|
| 5 |
defined('ABSPATH') || die('No direct access.'); |
| 6 |
|
| 7 |
use Extendify\PartnerData; |
| 8 |
|
| 9 |
/** |
| 10 |
* The site conditions a notification can ask to be gated on, each mapped to the |
| 11 |
* predicate that answers it. A notification without a trigger is unconditional. |
| 12 |
*/ |
| 13 |
class Triggers |
| 14 |
{ |
| 15 |
// phpcs:ignore PSR12.Properties.ConstantVisibility.NotFound -- 7.0 floor: no const visibility |
| 16 |
const PREDICATES = [ |
| 17 |
'staging-domain' => 'onStagingDomain', |
| 18 |
]; |
| 19 |
|
| 20 |
public static function passes($trigger) |
| 21 |
{ |
| 22 |
if ($trigger === null || $trigger === '') { |
| 23 |
return true; |
| 24 |
} |
| 25 |
|
| 26 |
if (!is_string($trigger) || !isset(self::PREDICATES[$trigger])) { |
| 27 |
return false; |
| 28 |
} |
| 29 |
|
| 30 |
return call_user_func([self::class, self::PREDICATES[$trigger]]); |
| 31 |
} |
| 32 |
|
| 33 |
// Substring match, mirroring the domain-suggestion matcher in src/Assist/lib/domains.js. |
| 34 |
private static function onStagingDomain() |
| 35 |
{ |
| 36 |
$host = strtolower((string) \wp_parse_url(\home_url(), PHP_URL_HOST)); |
| 37 |
$sites = (array) PartnerData::setting('stagingSites'); |
| 38 |
|
| 39 |
foreach (array_filter($sites, 'is_string') as $site) { |
| 40 |
$site = strtolower(trim($site)); |
| 41 |
if ($site !== '' && str_contains($host, $site)) { |
| 42 |
return true; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
return false; |
| 47 |
} |
| 48 |
} |
| 49 |
|