| 1 |
<?php |
| 2 |
|
| 3 |
namespace Extendify\Notifications; |
| 4 |
|
| 5 |
defined('ABSPATH') || die('No direct access.'); |
| 6 |
|
| 7 |
use Extendify\Shared\DataProvider\NotificationData; |
| 8 |
|
| 9 |
/** |
| 10 |
* Admin and front end differ in which slot a hook means, not in what the |
| 11 |
* container for a slot looks like. |
| 12 |
*/ |
| 13 |
class Mount |
| 14 |
{ |
| 15 |
// phpcs:disable PSR12.Properties.ConstantVisibility.NotFound -- 7.0 floor: no const visibility |
| 16 |
const MEMO_GROUP = 'extendify-notifications'; |
| 17 |
// phpcs:enable PSR12.Properties.ConstantVisibility.NotFound |
| 18 |
|
| 19 |
/** |
| 20 |
* The core admin bar takes a node's markup, not its output. |
| 21 |
* |
| 22 |
* @param string $slot - One of Slots::ALLOWLIST. |
| 23 |
* @param array $attributes - Bare attribute names the caller's own surface needs. |
| 24 |
* @return string - Empty when nothing is available to fill the slot. |
| 25 |
*/ |
| 26 |
public static function container(string $slot, array $attributes = []) |
| 27 |
{ |
| 28 |
if (!self::available($slot)) { |
| 29 |
return ''; |
| 30 |
} |
| 31 |
|
| 32 |
return sprintf( |
| 33 |
'<div class="extendify-notifications" data-ext-notification' |
| 34 |
. ' data-slot="%1$s" data-test="notification-%1$s"%2$s></div>', |
| 35 |
\esc_attr($slot), |
| 36 |
$attributes ? ' ' . implode(' ', array_map('esc_attr', $attributes)) : '' |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* @return bool |
| 42 |
*/ |
| 43 |
private static function available(string $slot) |
| 44 |
{ |
| 45 |
if (!Slots::isKnown($slot)) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
// A persistent cache would carry the answer into requests where the |
| 50 |
// trigger no longer passes. |
| 51 |
\wp_cache_add_non_persistent_groups(self::MEMO_GROUP); |
| 52 |
|
| 53 |
$memoized = false; |
| 54 |
$memo = \wp_cache_get($slot, self::MEMO_GROUP, false, $memoized); |
| 55 |
if ($memoized) { |
| 56 |
return (bool) $memo; |
| 57 |
} |
| 58 |
|
| 59 |
// Up to three hooks ask for the same slot in one request. |
| 60 |
$available = Availability::anyIn($slot, NotificationData::get()); |
| 61 |
\wp_cache_set($slot, $available, self::MEMO_GROUP); |
| 62 |
return $available; |
| 63 |
} |
| 64 |
} |
| 65 |
|