Screen Options, top right of this page)
$per_page = (int) get_user_meta($current_user->ID, 'advgb_post_notes_per_page', true);
if ($per_page < 1) {
$per_page = 20;
}
$per_page = min(999, $per_page);
// Column visibility from Screen Options; '_title' (Post) is always shown
$advgb_screen = get_current_screen();
$hidden_cols = $advgb_screen ? get_hidden_columns($advgb_screen) : [];
$advgb_show_col = function ($key) use ($hidden_cols) {
return ! in_array($key, $hidden_cols, true);
};
$current_page = max(1, isset($_GET['paged']) ? absint($_GET['paged']) : 1);
$filter_status = isset($_GET['note_status']) ? sanitize_text_field($_GET['note_status']) : '';
$filter_post_type = isset($_GET['post_type_filter']) ? sanitize_text_field($_GET['post_type_filter']) : '';
$filter_search = isset($_GET['s']) ? sanitize_text_field($_GET['s']) : '';
$page_url = admin_url('admin.php?page=advgb_post_notes');
// Build status arg
$status_arg = 'all';
if ($filter_status === 'open') {
$status_arg = 'hold';
} elseif ($filter_status === 'resolved') {
$status_arg = 'approve';
}
// Resolve which post IDs to restrict notes to
$post_id_limit = null; // null = no restriction (admins/editors who can edit others' posts)
if (! $can_edit_others) {
// Authors/contributors: only their own posts
$own_posts = get_posts([
'author' => $current_user->ID,
'posts_per_page' => -1,
'post_type' => 'any',
'post_status' => 'any',
'fields' => 'ids',
]);
$post_id_limit = ! empty($own_posts) ? $own_posts : [0];
}
// Post type filter: resolve to post IDs and intersect with existing limit
if ($filter_post_type) {
$type_posts = get_posts([
'posts_per_page' => -1,
'post_type' => $filter_post_type,
'post_status' => 'any',
'fields' => 'ids',
]);
if (empty($type_posts)) {
$post_id_limit = [0];
} elseif ($post_id_limit === null) {
$post_id_limit = $type_posts;
} else {
$post_id_limit = array_intersect($post_id_limit, $type_posts);
if (empty($post_id_limit)) {
$post_id_limit = [0];
}
}
}
// Post title search: resolve to post IDs and intersect
if ($filter_search) {
$search_posts = get_posts([
'posts_per_page' => -1,
'post_type' => 'any',
'post_status' => 'any',
's' => $filter_search,
'fields' => 'ids',
]);
if (empty($search_posts)) {
$post_id_limit = [0];
} elseif ($post_id_limit === null) {
$post_id_limit = $search_posts;
} else {
$post_id_limit = array_intersect($post_id_limit, $search_posts);
if (empty($post_id_limit)) {
$post_id_limit = [0];
}
}
}
// Base comment query args — parent=0 excludes internal resolution child comments WP may create
$base_args = [
'type' => 'note',
'status' => $status_arg,
'parent' => 0,
'orderby' => 'comment_date',
'order' => 'DESC',
];
if ($post_id_limit !== null) {
$base_args['post__in'] = array_values($post_id_limit);
}
// Total count
$count_args = $base_args;
$count_args['count'] = true;
$total_notes = get_comments($count_args);
$total_pages = $total_notes > 0 ? (int) ceil($total_notes / $per_page) : 1;
$current_page = min($current_page, $total_pages);
// Paginated results
$query_args = $base_args;
$query_args['number'] = $per_page;
$query_args['offset'] = ($current_page - 1) * $per_page;
$notes = get_comments($query_args);
// Post types for filter dropdown (public, excluding attachments)
$post_types = get_post_types(['public' => true], 'objects');
unset($post_types['attachment']);
/**
* Render an initials avatar matching the WP 6.9 Notes editor style.
* Color is deterministically derived from the author name so each
* author always gets the same hue across page loads.
*/
function advgb_note_author_avatar($name, $email = '', $size = 32)
{
// Extract up to two initials
$parts = array_filter(explode(' ', trim($name)));
$initials = '';
foreach (array_slice($parts, 0, 2) as $part) {
$initials .= mb_strtoupper(mb_substr($part, 0, 1));
}
if ($initials === '') {
$initials = '?';
}
// Pick from a palette of visually distinct colors, deterministically by name
$palette = [
'#e74c3c', // red
'#e67e22', // orange
'#f39c12', // amber
'#27ae60', // green
'#16a085', // teal
'#2980b9', // blue
'#8e44ad', // purple
'#c0392b', // dark red
'#1abc9c', // mint
'#2c3e50', // navy
'#d35400', // burnt orange
'#7f8c8d', // slate
];
$bg_color = $palette[ abs(crc32($name)) % count($palette) ];
$font_size = (int) ($size * 0.4);
// Request avatar with default=404 so missing Gravatars don't load the mystery-man image.
// The overlays the initials; onerror hides it and lets the initials show through.
$img_tag = '';
if ($email) {
$avatar_url = get_avatar_url($email, ['size' => $size * 2, 'default' => '404']);
if ($avatar_url) {
$img_tag = sprintf(
'
',
esc_url($avatar_url),
(int) $size,
(int) $size
);
}
}
printf(
'',
(int) $size,
esc_attr($bg_color),
(int) $font_size,
esc_html($initials),
wp_kses($img_tag, [
'img' => [
'src' => true,
'width' => true,
'height' => true,
'alt' => true,
'style' => true,
'onerror' => true,
],
])
);
}
// Pagination helper
$pagination_args = [
'base' => add_query_arg('paged', '%#%', $page_url),
'format' => '',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => '«',
'next_text' => '»',
'before_page_number' => '',
];
if ($filter_status) {
$pagination_args['base'] = add_query_arg('note_status', $filter_status, $pagination_args['base']);
}
if ($filter_post_type) {
$pagination_args['base'] = add_query_arg('post_type_filter', $filter_post_type, $pagination_args['base']);
}
if ($filter_search) {
$pagination_args['base'] = add_query_arg('s', $filter_search, $pagination_args['base']);
}
?>