| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Noted; |
| 6 |
|
| 7 |
/** |
| 8 |
* Dashboard widget listing every pinned note (read-only). |
| 9 |
* |
| 10 |
* Self-curating by design — users opt notes in by pinning them. When |
| 11 |
* nothing is pinned the widget renders a short prompt instead of an |
| 12 |
* empty list. |
| 13 |
*/ |
| 14 |
final class DashboardWidget |
| 15 |
{ |
| 16 |
private const WIDGET_ID = 'noted_pinned_notes'; |
| 17 |
|
| 18 |
public function __construct(private Settings $settings) {} |
| 19 |
|
| 20 |
/** |
| 21 |
* Register WordPress hooks. |
| 22 |
*/ |
| 23 |
public function register(): void |
| 24 |
{ |
| 25 |
add_action('wp_dashboard_setup', [$this, 'registerWidget']); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Register the dashboard widget when the user has view access and |
| 30 |
* the feature is enabled. |
| 31 |
*/ |
| 32 |
public function registerWidget(): void |
| 33 |
{ |
| 34 |
if (! $this->settings->userCanView()) { |
| 35 |
return; |
| 36 |
} |
| 37 |
if (! $this->settings->bool('enable_dashboard_widget', true)) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
wp_add_dashboard_widget( |
| 42 |
self::WIDGET_ID, |
| 43 |
__('Pinned Notes', 'noted'), |
| 44 |
[$this, 'render'] |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Render the widget body. |
| 50 |
*/ |
| 51 |
public function render(): void |
| 52 |
{ |
| 53 |
$pinned = $this->pinnedNotes(); |
| 54 |
|
| 55 |
if (empty($pinned)) { |
| 56 |
?> |
| 57 |
<p class="noted-empty"> |
| 58 |
<?php esc_html_e('Pin a note to see it on your dashboard.', 'noted'); ?> |
| 59 |
</p> |
| 60 |
<?php |
| 61 |
return; |
| 62 |
} |
| 63 |
?> |
| 64 |
<div class="noted-dashboard-widget"> |
| 65 |
<?php foreach ($pinned as $note) : |
| 66 |
$timestamp = get_post_meta($note->ID, PostType::META_TIMESTAMP, true); |
| 67 |
$formatted = $timestamp |
| 68 |
? date_i18n(get_option('date_format') . ' · ' . get_option('time_format'), strtotime((string) $timestamp)) |
| 69 |
: ''; |
| 70 |
$username = (string) get_post_meta($note->ID, PostType::META_USERNAME, true); |
| 71 |
?> |
| 72 |
<article class="noted-card" data-note-id="<?php echo esc_attr((string) $note->ID); ?>"> |
| 73 |
<header class="noted-card__head"> |
| 74 |
<span class="noted-card__title"> |
| 75 |
<?php echo esc_html(get_the_title($note) ?: __('(untitled)', 'noted')); ?> |
| 76 |
</span> |
| 77 |
</header> |
| 78 |
<div class="noted-card__body"><?php echo wp_kses_post($note->post_content); ?></div> |
| 79 |
<footer class="noted-card__meta"> |
| 80 |
<?php |
| 81 |
/* translators: 1: WordPress username, 2: Date and time string. */ |
| 82 |
$metaLine = sprintf(__('%1$s · %2$s', 'noted'), $username, $formatted); |
| 83 |
echo esc_html(trim($metaLine, ' ·')); |
| 84 |
?> |
| 85 |
</footer> |
| 86 |
</article> |
| 87 |
<?php endforeach; ?> |
| 88 |
</div> |
| 89 |
<?php |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Fetch every pinned note (any scope), most recent first. |
| 94 |
* |
| 95 |
* @return list<\WP_Post> |
| 96 |
*/ |
| 97 |
private function pinnedNotes(): array |
| 98 |
{ |
| 99 |
$notes = get_posts([ |
| 100 |
'post_type' => PostType::SLUG, |
| 101 |
'posts_per_page' => -1, |
| 102 |
'post_status' => 'publish', |
| 103 |
'meta_query' => [[ |
| 104 |
'key' => PostType::META_PINNED, |
| 105 |
'value' => '1', |
| 106 |
]], |
| 107 |
]); |
| 108 |
return PostType::filterRenderable($notes); |
| 109 |
} |
| 110 |
} |
| 111 |
|