| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Front-end loader for notifications. |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace Extendify\Notifications; |
| 8 |
|
| 9 |
defined('ABSPATH') || die('No direct access.'); |
| 10 |
|
| 11 |
use Extendify\Config; |
| 12 |
|
| 13 |
class Frontend |
| 14 |
{ |
| 15 |
// phpcs:disable PSR12.Properties.ConstantVisibility.NotFound -- 7.0 floor: no const visibility |
| 16 |
const ADMIN_BAR_NODE = 'extendify-notifications'; |
| 17 |
// phpcs:enable PSR12.Properties.ConstantVisibility.NotFound |
| 18 |
|
| 19 |
public function __construct() |
| 20 |
{ |
| 21 |
\add_action('wp_enqueue_scripts', [$this, 'loadScriptsAndStyles']); |
| 22 |
\add_action('wp_footer', [$this, 'renderMountPoint']); |
| 23 |
\add_action('admin_bar_menu', [$this, 'registerAdminBarNode'], 100); |
| 24 |
\add_action('extendify_toolbar_right', [$this, 'renderToolbarMountPoint']); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* @return bool |
| 29 |
*/ |
| 30 |
public static function shouldRender(string $slot) |
| 31 |
{ |
| 32 |
return self::container($slot) !== ''; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
public function loadScriptsAndStyles() |
| 39 |
{ |
| 40 |
if (!self::shouldRender(Slots::FRONTEND_BOTTOM) && !self::shouldRender(Slots::FRONTEND_TOPBAR)) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
Assets::enqueue(); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
public function renderMountPoint() |
| 51 |
{ |
| 52 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Mount escapes |
| 53 |
echo self::container(Slots::FRONTEND_BOTTOM); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* @return void |
| 58 |
*/ |
| 59 |
public function registerAdminBarNode(\WP_Admin_Bar $bar) |
| 60 |
{ |
| 61 |
if (self::simpleToolbarActive()) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
$container = self::container(Slots::FRONTEND_TOPBAR); |
| 66 |
if ($container === '') { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
$bar->add_node([ |
| 71 |
'id' => self::ADMIN_BAR_NODE, |
| 72 |
'parent' => 'top-secondary', |
| 73 |
'title' => $container, |
| 74 |
]); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* The simple toolbar is its own markup, not admin-bar nodes. |
| 79 |
* |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
public function renderToolbarMountPoint() |
| 83 |
{ |
| 84 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Mount escapes |
| 85 |
echo self::container(Slots::FRONTEND_TOPBAR); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Toolbar\Frontend loads only for partners with the flag, so asking it |
| 90 |
* directly drops the core-bar node for everyone else. |
| 91 |
* |
| 92 |
* @return bool |
| 93 |
*/ |
| 94 |
private static function simpleToolbarActive() |
| 95 |
{ |
| 96 |
return (bool) \apply_filters('extendify_simple_toolbar_active', false); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Partner messaging aimed at the site owner, so visitors never see the bars. |
| 101 |
* |
| 102 |
* @return string |
| 103 |
*/ |
| 104 |
private static function container(string $slot) |
| 105 |
{ |
| 106 |
if (\is_admin() || !\is_user_logged_in() || !\current_user_can(Config::$requiredCapability)) { |
| 107 |
return ''; |
| 108 |
} |
| 109 |
|
| 110 |
// A bar pinned over the Customizer preview covers the site being previewed. |
| 111 |
if (\is_customize_preview()) { |
| 112 |
return ''; |
| 113 |
} |
| 114 |
|
| 115 |
return Mount::container($slot); |
| 116 |
} |
| 117 |
} |
| 118 |
|