Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 3
CRAP
n/a
0 / 0
Newspack_Blocks\get_authors_roles
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
Newspack_Blocks\get_authors_roles_slugs
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
Newspack_Blocks\is_avatar_displayable
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * Helper functions to fetch authors.
4 *
5 * @package Newspack_Blocks
6 */
7
8namespace Newspack_Blocks;
9
10/**
11 * Get a list of user roles on this site that have the edit_posts capability.
12 *
13 * @return array List of roles with edit_posts capability. Each array item is an array with a slug and label property.
14 */
15function get_authors_roles() {
16    global $wp_roles;
17
18    $editable_roles = [];
19
20    foreach ( $wp_roles->roles as $role_slug => $role ) {
21        if ( isset( $role['capabilities'] ) && isset( $role['capabilities']['edit_posts'] ) && $role['capabilities']['edit_posts'] ) {
22            $editable_roles[] = [
23                'slug'  => $role_slug,
24                'label' => $role['name'],
25            ];
26        }
27    }
28
29    /**
30     * Deprecated filter.
31     *
32     * @param array $editable_roles Array of editable role names as registered via add_role.
33     *
34     * @deprecated Use the newspack_blocks_authors_roles filter instead.
35     * @return array Filtered array of roles.
36     */
37    $editable_roles = apply_filters( 'newspack_blocks_author_list_editable_roles', $editable_roles );
38
39    /**
40     * Filter the array of editable roles so other plugins can add/remove as needed.
41     * The array should be a flat array of the name of each role as registered via add_role.
42     * https://developer.wordpress.org/reference/functions/add_role/
43     *
44     * @param array $editable_roles Array of roles that can edit posts. Each array item is an array with a slug and label property.
45     *
46     * @return array Filtered array of roles.
47     */
48    return apply_filters( 'newspack_blocks_authors_roles', $editable_roles );
49}
50
51/**
52 * Get a list of user roles on this site that have the edit_posts capability.
53 *
54 * @return string[] List of roles with edit_posts capability.
55 */
56function get_authors_roles_slugs() {
57    return array_map(
58        function( $role ) {
59            return $role['slug'];
60        },
61        get_authors_roles()
62    );
63}
64
65/**
66 * Whether an avatar should be displayed, honoring a "hide default avatar" preference.
67 *
68 * Gravatar-served images never carry core's `avatar-default` class (core only
69 * emits it when no avatar was found, and the Gravatar branch always reports
70 * found), so a generated fallback like `d=mm` is indistinguishable from a real
71 * avatar by class alone. When hiding defaults, callers must fetch the avatar
72 * with Gravatar's `blank` fallback so it can be detected via the `d=blank`
73 * param — mirroring the Author List block's long-standing behavior.
74 *
75 * @param string|false $avatar       Avatar HTML as returned by get_avatar()/coauthors_get_avatar().
76 * @param bool         $hide_default Whether default (fallback) avatars should be hidden.
77 *
78 * @return bool True if the avatar should be displayed.
79 */
80function is_avatar_displayable( $avatar, $hide_default ) {
81    if ( ! $avatar ) {
82        return false;
83    }
84    if ( ! $hide_default ) {
85        return true;
86    }
87    $is_default = false !== strpos( $avatar, 'avatar-default' ) || false !== strpos( $avatar, 'd=blank' );
88    return ! $is_default;
89}