Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Post_List_Link
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
4
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
3
1<?php
2/**
3 * Post list table integration.
4 *
5 * @package automattic/jetpack-premium-analytics
6 */
7
8namespace Automattic\Jetpack\PremiumAnalytics;
9
10/**
11 * Sends the views column in the posts and pages list tables to the dashboard's
12 * post detail page instead of the Stats one.
13 *
14 * @since $$next-version$$
15 */
16class Post_List_Link {
17
18    /**
19     * Claim the column's link. Idempotent, like the other register() calls.
20     *
21     * Outside the admin-chrome gate on purpose: Quick Edit re-renders the cell
22     * over AJAX, where that gate is closed.
23     *
24     * @return void
25     */
26    public static function register() {
27        add_filter( 'jetpack_stats_post_list_column_url', array( __CLASS__, 'filter_url' ), 10, 2 );
28    }
29
30    /**
31     * Point one row at its post detail page.
32     *
33     * Wins even where the row would otherwise point at Calypso: the dashboard is
34     * this site's analytics UI, and it only exists in wp-admin, so the
35     * admin-interface preference has no bearing on where analytics lives.
36     *
37     * The capability arm is defence in depth rather than a path a reader reaches:
38     * `Admin_Post_List_Column::add_stats_post_table()` already drops the whole
39     * column unless the user has `view_stats` or `manage_options`, the same
40     * primitives `jetpack_view_analytics` maps to. It stays because the filter is
41     * public and its next caller may not gate anything.
42     *
43     * @param string $url     Stats URL for the post.
44     * @param int    $post_id The post the row belongs to.
45     * @return string
46     */
47    public static function filter_url( $url, $post_id ) {
48        $post_id = (int) $post_id;
49
50        if ( $post_id <= 0 ) {
51            return $url;
52        }
53
54        if ( ! Capabilities::current_user_can_view_analytics() ) {
55            return $url;
56        }
57
58        return Analytics::dashboard_url( '/post/' . $post_id );
59    }
60}