Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.39% covered (success)
98.39%
183 / 186
83.33% covered (warning)
83.33%
5 / 6
CRAP
n/a
0 / 0
Automattic\Jetpack\PremiumAnalytics\get_author_single_report_page_default_layout
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
Automattic\Jetpack\PremiumAnalytics\get_post_traffic_single_report_section_default_layout
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
1
Automattic\Jetpack\PremiumAnalytics\get_page_traffic_single_report_section_default_layout
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
Automattic\Jetpack\PremiumAnalytics\get_email_opens_single_report_section_default_layout
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
1 / 1
1
Automattic\Jetpack\PremiumAnalytics\get_email_clicks_single_report_section_default_layout
100.00% covered (success)
100.00%
49 / 49
100.00% covered (success)
100.00%
1 / 1
1
Automattic\Jetpack\PremiumAnalytics\register_default_single_report_pages
98.39% covered (success)
98.39%
61 / 62
0.00% covered (danger)
0.00%
0 / 1
4
1<?php
2/**
3 * The package's own single-report pages, registered through the single-report page API.
4 *
5 * @package automattic/jetpack-premium-analytics
6 */
7
8namespace Automattic\Jetpack\PremiumAnalytics;
9
10require_once __DIR__ . '/single-report-pages.php';
11
12/**
13 * The author page's fixed composition: performance, the scoped spotlights, top posts, all-time traffic.
14 *
15 * @return array
16 */
17function get_author_single_report_page_default_layout() {
18    return array(
19        get_dashboard_default_widget_instance( 'author-performance', 'jpa/author-performance', 1, 3, 2, array( 'chartType' => 'bar' ) ),
20        get_dashboard_default_widget_instance( 'author-popular-post', 'jpa/popular-post--author', 2, 2, 2, array( 'authorScoped' => true ) ),
21        get_dashboard_default_widget_instance( 'author-latest-post', 'jpa/latest-post--author', 3, 1, 2, array( 'authorScoped' => true ) ),
22        get_dashboard_default_widget_instance( 'author-top-posts', 'jpa/author-top-posts', 4, 3, 2 ),
23        get_dashboard_default_widget_instance( 'author-all-time-traffic', 'jpa/views-over-years--author', 5, 3, 2, array( 'authorScoped' => true ) ),
24    );
25}
26
27/**
28 * The Post traffic section of the post page: highlights, views, likes, comments, UTM, all-time traffic.
29 *
30 * @return array
31 */
32function get_post_traffic_single_report_section_default_layout() {
33    return array(
34        get_dashboard_default_widget_instance( 'post-detail-highlights', 'jpa/post-detail-highlights', 1, 3, 1 ),
35        get_dashboard_default_widget_instance( 'post-views', 'jpa/post-views', 2, 3, 2, array( 'chartType' => 'bar' ) ),
36        get_dashboard_default_widget_instance( 'post-likes', 'jpa/post-likes', 3, 1, 2 ),
37        get_dashboard_default_widget_instance( 'post-comments', 'jpa/post-comments', 4, 1, 2 ),
38        // No "View all" or "Download CSV": the site-wide UTM report would drop this post's scope.
39        get_dashboard_default_widget_instance(
40            'post-utm',
41            'jpa/utm-insights--utm',
42            5,
43            1,
44            2,
45            array(
46                'utmDimension'   => 'utm_source,utm_medium',
47                'showReportLink' => false,
48            )
49        ),
50        get_dashboard_default_widget_instance( 'post-all-time-traffic', 'jpa/post-all-time-traffic', 6, 3, 2 ),
51    );
52}
53
54/**
55 * The Page traffic section of the page report: the post traffic layout, its two post cards titled for a page.
56 *
57 * @return array
58 */
59function get_page_traffic_single_report_section_default_layout() {
60    $page_types = array(
61        'jpa/post-detail-highlights' => 'jpa/post-detail-highlights--page',
62        'jpa/post-views'             => 'jpa/post-views--page',
63    );
64
65    return array_map(
66        static function ( $widget ) use ( $page_types ) {
67            $widget['type'] = $page_types[ $widget['type'] ] ?? $widget['type'];
68
69            return $widget;
70        },
71        get_post_traffic_single_report_section_default_layout()
72    );
73}
74
75/**
76 * The Email opens section of the post page.
77 *
78 * @return array
79 */
80function get_email_opens_single_report_section_default_layout() {
81    return array(
82        get_dashboard_default_widget_instance( 'email-opens-highlights', 'jpa/email-top-row', 1, 3, 1, array( 'metric' => 'opens' ) ),
83        get_dashboard_default_widget_instance( 'email-opens-trend', 'jpa/email-time-series--total-opens', 2, 3, 2, array( 'metric' => 'opens' ) ),
84        get_dashboard_default_widget_instance(
85            'email-opens-countries',
86            'jpa/email-breakdown--location-opens',
87            3,
88            1,
89            2,
90            array(
91                'view'   => 'countries',
92                'metric' => 'opens',
93            )
94        ),
95        get_dashboard_default_widget_instance(
96            'email-opens-devices',
97            'jpa/email-breakdown--platforms-opens',
98            4,
99            1,
100            2,
101            array(
102                'view'   => 'devices',
103                'metric' => 'opens',
104            )
105        ),
106        get_dashboard_default_widget_instance(
107            'email-opens-clients',
108            'jpa/email-breakdown--clients-opens',
109            5,
110            1,
111            2,
112            array(
113                'view'   => 'clients',
114                'metric' => 'opens',
115            )
116        ),
117    );
118}
119
120/**
121 * The Email clicks section of the post page.
122 *
123 * @return array
124 */
125function get_email_clicks_single_report_section_default_layout() {
126    return array(
127        get_dashboard_default_widget_instance( 'email-clicks-highlights', 'jpa/email-top-row', 1, 3, 1, array( 'metric' => 'clicks' ) ),
128        get_dashboard_default_widget_instance( 'email-clicks-trend', 'jpa/email-time-series--total-clicks', 2, 3, 2, array( 'metric' => 'clicks' ) ),
129        // Width 2: the map unmounts below a 720px container floor.
130        get_dashboard_default_widget_instance(
131            'email-clicks-countries',
132            'jpa/email-breakdown--location-clicks',
133            3,
134            2,
135            2,
136            array(
137                'view'    => 'countries',
138                'metric'  => 'clicks',
139                'showMap' => true,
140            )
141        ),
142        get_dashboard_default_widget_instance(
143            'email-clicks-devices',
144            'jpa/email-breakdown--platforms-clicks',
145            4,
146            1,
147            2,
148            array(
149                'view'   => 'devices',
150                'metric' => 'clicks',
151            )
152        ),
153        get_dashboard_default_widget_instance(
154            'email-clicks-clients',
155            'jpa/email-breakdown--clients-clicks',
156            5,
157            1,
158            2,
159            array(
160                'view'   => 'clients',
161                'metric' => 'clicks',
162            )
163        ),
164        get_dashboard_default_widget_instance(
165            'email-clicks-links',
166            'jpa/email-breakdown--top-links',
167            6,
168            2,
169            2,
170            array(
171                'view'   => 'links',
172                'metric' => 'clicks',
173            )
174        ),
175    );
176}
177
178/**
179 * Registers the package's own single-report pages into the registry being hydrated.
180 *
181 * @param Single_Report_Page_Registry|null $registry The registry the action hands over.
182 */
183function register_default_single_report_pages( $registry = null ) {
184    if ( ! $registry instanceof Single_Report_Page_Registry ) {
185        $registry = Single_Report_Page_Registry::get_instance();
186    }
187
188    $post_sections = array(
189        array(
190            'slug'           => 'post-traffic',
191            'label'          => __( 'Post traffic', 'jetpack-premium-analytics-pkg' ),
192            'default_layout' => __NAMESPACE__ . '\\get_post_traffic_single_report_section_default_layout',
193        ),
194        array(
195            'slug'           => 'email-opens',
196            'label'          => __( 'Email opens', 'jetpack-premium-analytics-pkg' ),
197            'default_layout' => __NAMESPACE__ . '\\get_email_opens_single_report_section_default_layout',
198            'controls'       => false,
199        ),
200        array(
201            'slug'           => 'email-clicks',
202            'label'          => __( 'Email clicks', 'jetpack-premium-analytics-pkg' ),
203            'default_layout' => __NAMESPACE__ . '\\get_email_clicks_single_report_section_default_layout',
204            'controls'       => false,
205        ),
206    );
207
208    $pages = array(
209        'analytics/author' => array(
210            'path'              => 'author',
211            'label'             => __( 'Author', 'jetpack-premium-analytics-pkg' ),
212            'dashboard_section' => 'traffic',
213            'feature'           => 'authors',
214            'scope_param'       => 'author_id',
215            'icon'              => 'jpa/post-author',
216            'reports_page'      => 'authors',
217            'default_layout'    => __NAMESPACE__ . '\\get_author_single_report_page_default_layout',
218            'preferences_scope' => 'jetpack-premium-analytics/author-detail',
219        ),
220        'analytics/post'   => array(
221            'entity'            => array(
222                'kind' => 'postType',
223                'name' => 'post',
224            ),
225            'dashboard_section' => 'traffic',
226            'icon'              => 'jpa/post',
227            'sections'          => $post_sections,
228            // Its adapter reads the entry from Stats, so the type need not be in REST.
229            'is_available'      => array( Capabilities::class, 'current_user_can_view_stats' ),
230        ),
231        'analytics/page'   => array(
232            'entity'            => array(
233                'kind' => 'postType',
234                'name' => 'page',
235            ),
236            'dashboard_section' => 'traffic',
237            'icon'              => 'jpa/page',
238            'sections'          => array(
239                array(
240                    'slug'           => 'post-traffic',
241                    'label'          => __( 'Page traffic', 'jetpack-premium-analytics-pkg' ),
242                    'default_layout' => __NAMESPACE__ . '\\get_page_traffic_single_report_section_default_layout',
243                ),
244            ),
245        ),
246    );
247
248    foreach ( $pages as $id => $args ) {
249        if ( ! $registry->is_registered( DASHBOARD_NAME, $id ) ) {
250            $registry->register( DASHBOARD_NAME, $id, $args );
251        }
252    }
253}
254
255add_action( Single_Report_Page_Registry::REGISTER_ACTION, __NAMESPACE__ . '\\register_default_single_report_pages' );