| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Noted; |
| 6 |
|
| 7 |
use WP_Admin_Bar; |
| 8 |
|
| 9 |
/** |
| 10 |
* The admin-bar Noted! button + the floating side panel HTML. |
| 11 |
* |
| 12 |
* Suppressed on block-editor screens — the {@see EditorSidebar} (JS-side |
| 13 |
* PluginSidebar) is the in-Gutenberg surface, and we don't want both UIs |
| 14 |
* fighting for the same screen real estate. |
| 15 |
*/ |
| 16 |
final class AdminBar |
| 17 |
{ |
| 18 |
public function __construct(private Settings $settings) |
| 19 |
{ |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Register WordPress hooks. |
| 24 |
*/ |
| 25 |
public function register(): void |
| 26 |
{ |
| 27 |
add_action('admin_bar_menu', [$this, 'addBarIcon'], 100); |
| 28 |
add_action('admin_footer', [$this, 'renderFloatingPanel']); |
| 29 |
add_action('wp_footer', [$this, 'renderFloatingPanel']); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Add the "Noted!" node to the WordPress admin bar. |
| 34 |
*/ |
| 35 |
public function addBarIcon(WP_Admin_Bar $bar): void |
| 36 |
{ |
| 37 |
if (! $this->shouldRender()) { |
| 38 |
return; |
| 39 |
} |
| 40 |
$bar->add_node([ |
| 41 |
'id' => 'noted', |
| 42 |
'title' => 'Noted!', |
| 43 |
'href' => '#', |
| 44 |
'meta' => ['class' => 'noted-icon'], |
| 45 |
]); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Output the floating panel HTML in the footer. |
| 50 |
*/ |
| 51 |
public function renderFloatingPanel(): void |
| 52 |
{ |
| 53 |
if (! $this->shouldRender()) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
$postId = PostType::currentPostId(); |
| 58 |
?> |
| 59 |
<div |
| 60 |
id="noted-panel" |
| 61 |
class="noted-panel wp-admin-styling" |
| 62 |
role="dialog" |
| 63 |
aria-modal="true" |
| 64 |
aria-labelledby="noted-panel-title" |
| 65 |
tabindex="-1" |
| 66 |
data-current-post="<?php echo esc_attr((string) $postId); ?>" |
| 67 |
> |
| 68 |
<button id="noted-close" class="noted-close-button" aria-label="<?php esc_attr_e('Close notes', 'noted'); ?>">×</button> |
| 69 |
<div class="noted-content"> |
| 70 |
<h2 id="noted-panel-title"><?php esc_html_e('Notes', 'noted'); ?></h2> |
| 71 |
<?php if ($postId) : ?> |
| 72 |
<div class="noted-tabs" role="tablist"> |
| 73 |
<button type="button" class="noted-tab is-active" data-tab="global" role="tab"><?php esc_html_e('Global', 'noted'); ?></button> |
| 74 |
<button type="button" class="noted-tab" data-tab="post" role="tab"><?php esc_html_e('This Post', 'noted'); ?></button> |
| 75 |
</div> |
| 76 |
<?php endif; ?> |
| 77 |
<form id="noted-form"> |
| 78 |
<p class="noted-field"> |
| 79 |
<label for="noted-title"><?php esc_html_e('Title', 'noted'); ?></label> |
| 80 |
<input type="text" id="noted-title" name="title" class="widefat"> |
| 81 |
</p> |
| 82 |
<p class="noted-field"> |
| 83 |
<label for="noted-description" class="noted-description-label"> |
| 84 |
<?php esc_html_e('Description', 'noted'); ?> |
| 85 |
<a |
| 86 |
class="noted-markdown-doc-link" |
| 87 |
href="<?php echo esc_url($this->settings->markdownDocumentationUrl()); ?>" |
| 88 |
target="_blank" |
| 89 |
rel="noopener noreferrer" |
| 90 |
aria-label="<?php echo esc_attr($this->settings->markdownDocumentationLinkAriaLabel()); ?>" |
| 91 |
data-tooltip="<?php echo esc_attr($this->settings->markdownDocumentationIconTooltip()); ?>" |
| 92 |
> |
| 93 |
<span class="dashicons dashicons-info" aria-hidden="true"></span> |
| 94 |
</a> |
| 95 |
</label> |
| 96 |
<textarea id="noted-description" name="description" class="widefat" rows="4"></textarea> |
| 97 |
</p> |
| 98 |
<button type="button" id="noted-add" class="button button-primary"><?php esc_html_e('Add Note', 'noted'); ?></button> |
| 99 |
</form> |
| 100 |
<div id="noted-list" class="noted-list-container"></div> |
| 101 |
</div> |
| 102 |
</div> |
| 103 |
<?php |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* True if the floating panel should appear on the current screen. |
| 108 |
* |
| 109 |
* The panel is intentionally shown on block-editor screens too — the |
| 110 |
* PluginSidebar and the floating panel coexist and stay in sync via |
| 111 |
* the `notedApi.subscribe` event channel. |
| 112 |
*/ |
| 113 |
public function shouldRender(): bool |
| 114 |
{ |
| 115 |
if (! $this->settings->userCanView()) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
if (! $this->settings->bool('show_global_panel', true)) { |
| 119 |
return false; |
| 120 |
} |
| 121 |
return true; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Detect whether the current request is an admin block-editor screen. |
| 126 |
*/ |
| 127 |
public static function isBlockEditorScreen(): bool |
| 128 |
{ |
| 129 |
if (! is_admin() || ! function_exists('get_current_screen')) { |
| 130 |
return false; |
| 131 |
} |
| 132 |
$screen = get_current_screen(); |
| 133 |
return $screen && method_exists($screen, 'is_block_editor') && $screen->is_block_editor(); |
| 134 |
} |
| 135 |
} |
| 136 |
|