PluginProbe
Extendify / 3.2.1
Extendify v3.2.1
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 0.5.0 0.6.0 All 127 releases
extendify / app / Notifications / Frontend.php

Frontend.php in Extendify 3.2.1, at app/Notifications/Frontend.php

130 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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('wp_footer', [$this, 'renderModalMountPoint']);
24 \add_action('admin_bar_menu', [$this, 'registerAdminBarNode'], 100);
25 \add_action('extendify_toolbar_right', [$this, 'renderToolbarMountPoint']);
26 }
27
28 /**
29 * @return bool
30 */
31 public static function shouldRender(string $slot)
32 {
33 return self::container($slot) !== '';
34 }
35
36 /**
37 * @return void
38 */
39 public function loadScriptsAndStyles()
40 {
41 $slots = [Slots::FRONTEND_BOTTOM, Slots::FRONTEND_TOPBAR, Slots::FRONTEND_MODAL];
42 foreach ($slots as $slot) {
43 if (self::shouldRender($slot)) {
44 Assets::enqueue();
45 return;
46 }
47 }
48 }
49
50 /**
51 * @return void
52 */
53 public function renderMountPoint()
54 {
55 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Mount escapes
56 echo self::container(Slots::FRONTEND_BOTTOM);
57 }
58
59 /**
60 * @return void
61 */
62 public function renderModalMountPoint()
63 {
64 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Mount escapes
65 echo self::container(Slots::FRONTEND_MODAL);
66 }
67
68 /**
69 * @return void
70 */
71 public function registerAdminBarNode(\WP_Admin_Bar $bar)
72 {
73 if (self::simpleToolbarActive()) {
74 return;
75 }
76
77 $container = self::container(Slots::FRONTEND_TOPBAR);
78 if ($container === '') {
79 return;
80 }
81
82 $bar->add_node([
83 'id' => self::ADMIN_BAR_NODE,
84 'parent' => 'top-secondary',
85 'title' => $container,
86 ]);
87 }
88
89 /**
90 * The simple toolbar is its own markup, not admin-bar nodes.
91 *
92 * @return void
93 */
94 public function renderToolbarMountPoint()
95 {
96 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Mount escapes
97 echo self::container(Slots::FRONTEND_TOPBAR);
98 }
99
100 /**
101 * Toolbar\Frontend loads only for partners with the flag, so asking it
102 * directly drops the core-bar node for everyone else.
103 *
104 * @return bool
105 */
106 private static function simpleToolbarActive()
107 {
108 return (bool) \apply_filters('extendify_simple_toolbar_active', false);
109 }
110
111 /**
112 * Partner messaging aimed at the site owner, so visitors never see the bars.
113 *
114 * @return string
115 */
116 private static function container(string $slot)
117 {
118 if (\is_admin() || !\is_user_logged_in() || !\current_user_can(Config::$requiredCapability)) {
119 return '';
120 }
121
122 // A bar pinned over the Customizer preview covers the site being previewed.
123 if (\is_customize_preview()) {
124 return '';
125 }
126
127 return Mount::container($slot);
128 }
129 }
130