| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Admin. |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace Extendify\Notifications; |
| 8 |
|
| 9 |
defined('ABSPATH') || die('No direct access.'); |
| 10 |
|
| 11 |
class Admin |
| 12 |
{ |
| 13 |
public function __construct() |
| 14 |
{ |
| 15 |
\add_action('admin_enqueue_scripts', [$this, 'loadScriptsAndStyles']); |
| 16 |
\add_action('admin_notices', [$this, 'renderMountPoint']); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Adds the banner bundle on eligible admin screens |
| 21 |
* |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
public function loadScriptsAndStyles() |
| 25 |
{ |
| 26 |
if ($this->container() === '') { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
Assets::enqueue(); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Prints the banner mount node on eligible admin screens |
| 35 |
* |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
public function renderMountPoint() |
| 39 |
{ |
| 40 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Mount escapes |
| 41 |
echo $this->container(); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* notifications.js wraps a container carrying data-admin-notice in |
| 46 |
* admin-notice spacing. |
| 47 |
* |
| 48 |
* @return string |
| 49 |
*/ |
| 50 |
private function container() |
| 51 |
{ |
| 52 |
$slot = $this->currentSlot(); |
| 53 |
return $slot ? Mount::container($slot, ['data-admin-notice']) : ''; |
| 54 |
} |
| 55 |
|
| 56 |
private function currentSlot() |
| 57 |
{ |
| 58 |
$screen = \get_current_screen(); |
| 59 |
if (!$screen) { |
| 60 |
return null; |
| 61 |
} |
| 62 |
|
| 63 |
if ($screen->id === 'dashboard') { |
| 64 |
return Slots::ADMIN_DASHBOARD; |
| 65 |
} |
| 66 |
|
| 67 |
return $this->isExcludedScreen($screen) ? null : Slots::ADMIN_OTHERS; |
| 68 |
} |
| 69 |
|
| 70 |
private function isExcludedScreen($screen) |
| 71 |
{ |
| 72 |
if ($screen->base === 'post') { |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
$excludedIds = ['site-editor', 'plugins', 'plugin-install', 'plugin-editor']; |
| 77 |
if (in_array($screen->id, $excludedIds, true)) { |
| 78 |
return true; |
| 79 |
} |
| 80 |
|
| 81 |
return strpos($screen->id, '_page_') !== false; |
| 82 |
} |
| 83 |
} |
| 84 |
|