| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) exit; |
| 3 |
|
| 4 |
function user_notes_render_profile_section($wp_user) { |
| 5 |
if (!user_notes_current_user_can_view($wp_user->ID)) return; |
| 6 |
|
| 7 |
$notes = User_Notes_Repo::get_for_user($wp_user->ID); |
| 8 |
$can_delete = user_notes_current_user_can_delete($wp_user->ID); |
| 9 |
?> |
| 10 |
<h3><?php esc_html_e('User Notes', 'user-notes'); ?></h3> |
| 11 |
|
| 12 |
<div id="user-notes-app" class="user-notes-app" data-user-id="<?php echo esc_attr($wp_user->ID); ?>" data-can-delete="<?php echo $can_delete ? '1' : '0'; ?>"> |
| 13 |
<div class="user-notes-add"> |
| 14 |
<textarea class="user-notes-new-body" rows="3" placeholder="<?php esc_attr_e('Add a note…', 'user-notes'); ?>"></textarea> |
| 15 |
<div class="user-notes-add-actions"> |
| 16 |
<label><input type="checkbox" class="user-notes-new-starred" /> <?php esc_html_e('Star this note', 'user-notes'); ?></label> |
| 17 |
<button type="button" class="button button-primary user-notes-add-btn"><?php esc_html_e('Add Note', 'user-notes'); ?></button> |
| 18 |
</div> |
| 19 |
</div> |
| 20 |
|
| 21 |
<ul class="user-notes-list"> |
| 22 |
<?php foreach ($notes as $note): ?> |
| 23 |
<?php user_notes_render_note_item($note, $can_delete); ?> |
| 24 |
<?php endforeach; ?> |
| 25 |
</ul> |
| 26 |
<?php if (empty($notes)): ?> |
| 27 |
<p class="user-notes-empty"><?php esc_html_e('No notes yet.', 'user-notes'); ?></p> |
| 28 |
<?php endif; ?> |
| 29 |
</div> |
| 30 |
<?php |
| 31 |
} |
| 32 |
add_action('show_user_profile', 'user_notes_render_profile_section'); |
| 33 |
add_action('edit_user_profile', 'user_notes_render_profile_section'); |
| 34 |
|
| 35 |
function user_notes_render_note_item($note, $can_delete) { |
| 36 |
$edited = ($note->updated_at && $note->updated_at !== $note->created_at); |
| 37 |
?> |
| 38 |
<li class="user-notes-item <?php echo $note->starred ? 'is-starred' : ''; ?>" data-note-id="<?php echo esc_attr($note->id); ?>"> |
| 39 |
<div class="user-notes-meta"> |
| 40 |
<button type="button" class="user-notes-star" title="<?php esc_attr_e('Toggle star', 'user-notes'); ?>"> |
| 41 |
<span class="dashicons <?php echo $note->starred ? 'dashicons-star-filled' : 'dashicons-star-empty'; ?>"></span> |
| 42 |
</button> |
| 43 |
<span class="user-notes-author"><?php echo esc_html(user_notes_format_author($note->author_id)); ?></span> |
| 44 |
<span class="user-notes-time" title="<?php echo esc_attr($note->created_at); ?>"><?php echo esc_html(user_notes_format_time($note->created_at)); ?></span> |
| 45 |
<?php if ($edited): ?> |
| 46 |
<span class="user-notes-edited" title="<?php echo esc_attr($note->updated_at); ?>">(<?php echo esc_html(__('edited', 'user-notes') . ' ' . user_notes_format_time($note->updated_at)); ?>)</span> |
| 47 |
<?php endif; ?> |
| 48 |
<span class="user-notes-actions"> |
| 49 |
<a href="#" class="user-notes-edit"><?php esc_html_e('Edit', 'user-notes'); ?></a> |
| 50 |
<?php if ($can_delete): ?> |
| 51 |
| <a href="#" class="user-notes-delete"><?php esc_html_e('Delete', 'user-notes'); ?></a> |
| 52 |
<?php endif; ?> |
| 53 |
</span> |
| 54 |
</div> |
| 55 |
<div class="user-notes-body"><?php echo wp_kses_post(wpautop($note->body)); ?></div> |
| 56 |
<div class="user-notes-raw" style="display:none;"><?php echo esc_textarea($note->body); ?></div> |
| 57 |
</li> |
| 58 |
<?php |
| 59 |
} |
| 60 |
|
| 61 |
/* Users list column */ |
| 62 |
|
| 63 |
add_filter('manage_users_columns', function ($cols) { |
| 64 |
$cols['user_notes_note'] = __('Notes', 'user-notes'); |
| 65 |
return $cols; |
| 66 |
}); |
| 67 |
|
| 68 |
add_action('manage_users_custom_column', function ($val, $col_name, $user_id) { |
| 69 |
if ($col_name !== 'user_notes_note') return $val; |
| 70 |
if (!user_notes_current_user_can_view($user_id)) return '—'; |
| 71 |
|
| 72 |
return user_notes_column_html($user_id); |
| 73 |
}, 10, 3); |
| 74 |
|
| 75 |
/** |
| 76 |
* Markup for the Users-list Notes cell. Kept as a function so its output is |
| 77 |
* mirrored by the JS that re-renders the cell after edits in the modal. |
| 78 |
*/ |
| 79 |
function user_notes_column_html($user_id) { |
| 80 |
$count = User_Notes_Repo::count_for_user($user_id); |
| 81 |
$edit_url = admin_url('user-edit.php?user_id=' . $user_id . '#user-notes-app'); |
| 82 |
|
| 83 |
if (!$count) { |
| 84 |
return '<a class="user-notes-col-add" href="' . esc_url($edit_url) . '" data-user-id="' . esc_attr($user_id) . '">' |
| 85 |
. '<span class="dashicons dashicons-plus-alt2" aria-hidden="true"></span>' |
| 86 |
. esc_html__('Add Note', 'user-notes') . '</a>'; |
| 87 |
} |
| 88 |
|
| 89 |
$starred = User_Notes_Repo::count_starred_for_user($user_id); |
| 90 |
|
| 91 |
// Latest note as a hover tooltip — detail without cluttering the cell. |
| 92 |
$latest = User_Notes_Repo::latest_for_user($user_id); |
| 93 |
$excerpt = ''; |
| 94 |
if ($latest) { |
| 95 |
$plain = trim(preg_replace('/\s+/', ' ', wp_strip_all_tags($latest->body))); |
| 96 |
$excerpt = function_exists('mb_substr') ? mb_substr($plain, 0, 120) : substr($plain, 0, 120); |
| 97 |
if (mb_strlen($plain) > 120) $excerpt .= '…'; |
| 98 |
} |
| 99 |
|
| 100 |
$html = '<a class="user-notes-col" href="' . esc_url($edit_url) . '" data-user-id="' . esc_attr($user_id) . '"' |
| 101 |
. ($excerpt ? ' title="' . esc_attr($excerpt) . '"' : '') . '>'; |
| 102 |
|
| 103 |
/* translators: %s: number of notes */ |
| 104 |
$count_label = sprintf(_n('%s note', '%s notes', $count, 'user-notes'), number_format_i18n($count)); |
| 105 |
$html .= '<span class="user-notes-badge user-notes-badge-count" title="' . esc_attr($count_label) . '">' |
| 106 |
. '<span class="dashicons dashicons-admin-comments" aria-hidden="true"></span>' |
| 107 |
. '<span class="user-notes-badge-num">' . esc_html(number_format_i18n($count)) . '</span>' |
| 108 |
. '</span>'; |
| 109 |
|
| 110 |
if ($starred > 0) { |
| 111 |
/* translators: %s: number of starred notes */ |
| 112 |
$star_label = sprintf(_n('%s starred', '%s starred', $starred, 'user-notes'), number_format_i18n($starred)); |
| 113 |
$html .= '<span class="user-notes-badge user-notes-badge-star" title="' . esc_attr($star_label) . '">' |
| 114 |
. '<span class="dashicons dashicons-star-filled" aria-hidden="true"></span>' |
| 115 |
. '<span class="user-notes-badge-num">' . esc_html(number_format_i18n($starred)) . '</span>' |
| 116 |
. '</span>'; |
| 117 |
} |
| 118 |
|
| 119 |
$html .= '</a>'; |
| 120 |
|
| 121 |
return $html; |
| 122 |
} |
| 123 |
|