Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
Stats_Links
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
4 / 4
9
100.00% covered (success)
100.00%
1 / 1
 register
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 filter_url
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 post_url
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 route_url
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * Stats entry points outside the dashboard.
4 *
5 * @package automattic/jetpack-premium-analytics
6 */
7
8namespace Automattic\Jetpack\PremiumAnalytics;
9
10/**
11 * Sends links to Stats pages (the admin bar, the post list Views column, the newsletter action bar) to the matching
12 * dashboard page instead.
13 *
14 * @since $$next-version$$
15 */
16class Stats_Links {
17
18    /**
19     * Claim the links. Idempotent, like the other register() calls.
20     *
21     * Outside the admin-chrome gate on purpose: the admin bar and the action bar also render on the front end, and Quick Edit re-renders the Views column over AJAX.
22     *
23     * @return void
24     */
25    public static function register() {
26        add_filter( 'jetpack_stats_url', array( __CLASS__, 'filter_url' ), 10, 2 );
27    }
28
29    /**
30     * Point a Stats link at the dashboard page for the view it opens.
31     *
32     * @param string $url  Stats URL.
33     * @param array  $args The page the link opens: `view`, plus `id` for the `post` view.
34     * @return string
35     */
36    public static function filter_url( $url, $args ) {
37        $view = is_array( $args ) ? ( $args['view'] ?? null ) : null;
38
39        if ( 'dashboard' === $view ) {
40            return self::route_url( $url, '/' );
41        }
42
43        if ( 'post' === $view ) {
44            return self::post_url( $url, $args['id'] ?? 0 );
45        }
46
47        return $url;
48    }
49
50    /**
51     * The post detail page, or `$url` when there is no real post to show.
52     *
53     * @param string $url     Stats URL for the post.
54     * @param mixed  $post_id The post.
55     * @return string
56     */
57    private static function post_url( $url, $post_id ) {
58        $post_id = (int) $post_id;
59
60        if ( $post_id <= 0 ) {
61            return $url;
62        }
63
64        return self::route_url( $url, '/post/' . $post_id );
65    }
66
67    /**
68     * A dashboard route, or `$url` for a user who cannot read Stats there.
69     *
70     * Wins even where the link would otherwise point at Calypso: this dashboard is the site's
71     * analytics UI and exists only in wp-admin, so the admin-interface preference doesn't apply.
72     * Checked here even where the caller already gates on the same primitives: the filter is public.
73     *
74     * @param string $url  Stats URL.
75     * @param string $path Dashboard route.
76     * @return string
77     */
78    private static function route_url( $url, $path ) {
79        if ( ! Capabilities::current_user_can_view_stats() ) {
80            return $url;
81        }
82
83        return Analytics::dashboard_url( $path );
84    }
85}