Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.00% covered (warning)
75.00%
6 / 8
100.00% covered (success)
100.00%
1 / 1
CRAP
n/a
0 / 0
wpcom_map_jetpack_stats_caps
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
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        $stats_roles = array( 'administrator', 'editor', 'author', 'contributor' );
26
27        // Is any of the user's roles in the available stats roles?
28        if ( ! empty( array_intersect( $user->roles, $stats_roles ) ) ) {
29            $caps = array( 'read' );
30        }
31    }
32
33    return $caps;
34}
35
36// Let Atomic sites handle this through the Jetpack settings.
37if ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) && ( ! defined( 'IS_ATOMIC' ) || ! IS_ATOMIC ) ) {
38    add_filter( 'map_meta_cap', 'wpcom_map_jetpack_stats_caps', 10, 3 );
39}