Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
CRAP
n/a
0 / 0
wpcom_map_jetpack_stats_caps
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Jetpack Stats on WordPress.com
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8/**
9 * Extends the default value for which roles can access the Stats menu item.
10 *
11 * This enables the `view_stats` capability to work with all roles that are available on Simple sites.
12 *
13 * @see https://wordpress.com/support/invite-people/user-roles/#access-to-stats
14 * @see projects/plugins/wpcomsh/feature-plugins/stats.php
15 *
16 * @param mixed $caps Caps.
17 * @param mixed $cap Cap.
18 * @param mixed $user_id User ID.
19 * @return array Possibly mapped capabilities for meta capability.
20 */
21function wpcom_map_jetpack_stats_caps( $caps, $cap, $user_id ) {
22    // Map view_stats to exists.
23    if ( 'view_stats' === $cap ) {
24        $user        = new WP_User( $user_id );
25        $user_role   = array_shift( $user->roles );
26        $stats_roles = array( 'administrator', 'editor', 'author', 'contributor' );
27
28        // Is the users role in the available stats roles?
29        if ( in_array( $user_role, $stats_roles, true ) ) {
30            $caps = array( 'read' );
31        }
32    }
33
34    return $caps;
35}
36
37// Let Atomic sites handle this through the Jetpack settings.
38if ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) && ( ! defined( 'IS_ATOMIC' ) || ! IS_ATOMIC ) ) {
39    add_filter( 'map_meta_cap', 'wpcom_map_jetpack_stats_caps', 10, 3 );
40}