Ajax
1 week ago
BackgroundProcessing
1 week ago
Dto
1 week ago
Entity
1 week ago
Exceptions
3 days ago
FileHeader
1 week ago
Interfaces
1 week ago
Job
3 days ago
Request
1 week ago
Service
3 days ago
Storage
1 week ago
Task
3 days ago
Traits
1 week ago
Utils
1 week ago
AdminMenuBadge.php
3 days ago
AfterRestore.php
1 week ago
BackupDeleter.php
1 week ago
BackupDownload.php
1 week ago
BackupFileIndex.php
1 week ago
BackupGlitchReason.php
1 week ago
BackupHeader.php
1 week ago
BackupNextOffer.php
3 days ago
BackupRepairer.php
1 week ago
BackupRetentionHandler.php
1 week ago
BackupScheduler.php
3 days ago
BackupServiceProvider.php
1 week ago
BackupValidator.php
1 week ago
BeforeUpdateRowStatus.php
1 week ago
FileHeader.php
1 week ago
FileHeaderAttribute.php
1 week ago
UpdateProtectionPausedNotice.php
1 week ago
WithBackupIdentifier.php
1 week ago
AdminMenuBadge.php
94 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backup; |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | class AdminMenuBadge |
| 10 | { |
| 11 | |
| 12 | private $backupScheduler; |
| 13 | |
| 14 | public function __construct(BackupScheduler $backupScheduler) |
| 15 | { |
| 16 | $this->backupScheduler = $backupScheduler; |
| 17 | } |
| 18 | |
| 19 | |
| 20 | public function maybeAddBadge() |
| 21 | { |
| 22 | if (!$this->canReadBadgeState() || !$this->backupScheduler->shouldShowMenuBadge()) { |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | global $menu, $submenu; |
| 27 | |
| 28 | $parentSlug = $this->appendBadgeToMenuItem($menu, ['wpstg_clone', 'wpstg_backup']); |
| 29 | |
| 30 | if ($parentSlug === null || !isset($submenu[$parentSlug])) { |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | $this->appendBadgeToMenuItem($submenu[$parentSlug], ['wpstg_backup']); |
| 35 | } |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | private function canReadBadgeState(): bool |
| 44 | { |
| 45 | return method_exists($this->backupScheduler, 'shouldShowMenuBadge'); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | private function appendBadgeToMenuItem(array &$menuItems, array $slugs) |
| 54 | { |
| 55 | foreach ($menuItems as $key => $item) { |
| 56 | if (!isset($item[2])) { |
| 57 | continue; |
| 58 | } |
| 59 | |
| 60 | if (!in_array($item[2], $slugs, true)) { |
| 61 | continue; |
| 62 | } |
| 63 | |
| 64 | $menuItems[$key][0] .= $this->getBadgeHtml(); |
| 65 | return $item[2]; |
| 66 | } |
| 67 | |
| 68 | return null; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | private function getBadgeHtml(): string |
| 73 | { |
| 74 | return sprintf( |
| 75 | ' <span class="update-plugins count-1"><span class="plugin-count" aria-hidden="true">!</span><span class="screen-reader-text">%s</span></span>', |
| 76 | esc_html($this->getBadgeLabel()) |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | private function getBadgeLabel(): string |
| 86 | { |
| 87 | if ($this->backupScheduler->getWarningType() === BackupScheduler::CRON_WARNING_TYPE_FAILURE) { |
| 88 | return __('Last scheduled backup failed.', 'wp-staging'); |
| 89 | } |
| 90 | |
| 91 | return __('Scheduled backup is overdue.', 'wp-staging'); |
| 92 | } |
| 93 | } |
| 94 |