Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.54% covered (success)
98.54%
202 / 205
85.71% covered (warning)
85.71%
6 / 7
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%
20 / 20
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\get_ai_company_single_report_page_default_layout
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
Automattic\Jetpack\PremiumAnalytics\register_default_single_report_pages
98.65% covered (success)
98.65%
73 / 74
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, top AI companies, bot activity, 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-top-ai-companies', 'ai-traffic/top-ai-companies', 6, 2, 2 ),
51        get_dashboard_default_widget_instance( 'post-bot-activity', 'ai-traffic/bot-activity', 7, 1, 2 ),
52        get_dashboard_default_widget_instance( 'post-all-time-traffic', 'jpa/post-all-time-traffic', 8, 3, 2 ),
53    );
54}
55
56/**
57 * The Page traffic section of the page report: the post traffic layout, its two post cards titled for a page.
58 *
59 * @return array
60 */
61function get_page_traffic_single_report_section_default_layout() {
62    $page_types = array(
63        'jpa/post-detail-highlights' => 'jpa/post-detail-highlights--page',
64        'jpa/post-views'             => 'jpa/post-views--page',
65    );
66
67    return array_map(
68        static function ( $widget ) use ( $page_types ) {
69            $widget['type'] = $page_types[ $widget['type'] ] ?? $widget['type'];
70
71            return $widget;
72        },
73        get_post_traffic_single_report_section_default_layout()
74    );
75}
76
77/**
78 * The Email opens section of the post page.
79 *
80 * @return array
81 */
82function get_email_opens_single_report_section_default_layout() {
83    return array(
84        get_dashboard_default_widget_instance( 'email-opens-highlights', 'jpa/email-top-row', 1, 3, 1, array( 'metric' => 'opens' ) ),
85        get_dashboard_default_widget_instance( 'email-opens-trend', 'jpa/email-time-series--total-opens', 2, 3, 2, array( 'metric' => 'opens' ) ),
86        get_dashboard_default_widget_instance(
87            'email-opens-countries',
88            'jpa/email-breakdown--location-opens',
89            3,
90            1,
91            2,
92            array(
93                'view'   => 'countries',
94                'metric' => 'opens',
95            )
96        ),
97        get_dashboard_default_widget_instance(
98            'email-opens-devices',
99            'jpa/email-breakdown--platforms-opens',
100            4,
101            1,
102            2,
103            array(
104                'view'   => 'devices',
105                'metric' => 'opens',
106            )
107        ),
108        get_dashboard_default_widget_instance(
109            'email-opens-clients',
110            'jpa/email-breakdown--clients-opens',
111            5,
112            1,
113            2,
114            array(
115                'view'   => 'clients',
116                'metric' => 'opens',
117            )
118        ),
119    );
120}
121
122/**
123 * The Email clicks section of the post page.
124 *
125 * @return array
126 */
127function get_email_clicks_single_report_section_default_layout() {
128    return array(
129        get_dashboard_default_widget_instance( 'email-clicks-highlights', 'jpa/email-top-row', 1, 3, 1, array( 'metric' => 'clicks' ) ),
130        get_dashboard_default_widget_instance( 'email-clicks-trend', 'jpa/email-time-series--total-clicks', 2, 3, 2, array( 'metric' => 'clicks' ) ),
131        // Width 2: the map unmounts below a 720px container floor.
132        get_dashboard_default_widget_instance(
133            'email-clicks-countries',
134            'jpa/email-breakdown--location-clicks',
135            3,
136            2,
137            2,
138            array(
139                'view'    => 'countries',
140                'metric'  => 'clicks',
141                'showMap' => true,
142            )
143        ),
144        get_dashboard_default_widget_instance(
145            'email-clicks-devices',
146            'jpa/email-breakdown--platforms-clicks',
147            4,
148            1,
149            2,
150            array(
151                'view'   => 'devices',
152                'metric' => 'clicks',
153            )
154        ),
155        get_dashboard_default_widget_instance(
156            'email-clicks-clients',
157            'jpa/email-breakdown--clients-clicks',
158            5,
159            1,
160            2,
161            array(
162                'view'   => 'clients',
163                'metric' => 'clicks',
164            )
165        ),
166        get_dashboard_default_widget_instance(
167            'email-clicks-links',
168            'jpa/email-breakdown--top-links',
169            6,
170            2,
171            2,
172            array(
173                'view'   => 'links',
174                'metric' => 'clicks',
175            )
176        ),
177    );
178}
179
180/**
181 * The AI company page's fixed composition: the summary chart, then the company's top pages and its bots by type.
182 *
183 * @return array
184 */
185function get_ai_company_single_report_page_default_layout() {
186    return array(
187        get_dashboard_default_widget_instance( 'ai-company-summary', 'ai-traffic/ai-company-summary', 1, 3, 2 ),
188        get_dashboard_default_widget_instance( 'ai-company-top-pages', 'ai-traffic/top-ai-pages--company', 2, 2, DASHBOARD_TILE_LIST ),
189        get_dashboard_default_widget_instance( 'ai-company-bots', 'ai-traffic/bot-activity--company', 3, 1, DASHBOARD_TILE_LIST ),
190    );
191}
192
193/**
194 * Registers the package's own single-report pages into the registry being hydrated.
195 *
196 * @param Single_Report_Page_Registry|null $registry The registry the action hands over.
197 */
198function register_default_single_report_pages( $registry = null ) {
199    if ( ! $registry instanceof Single_Report_Page_Registry ) {
200        $registry = Single_Report_Page_Registry::get_instance();
201    }
202
203    $post_sections = array(
204        array(
205            'slug'           => 'post-traffic',
206            'label'          => __( 'Post traffic', 'jetpack-premium-analytics-pkg' ),
207            'default_layout' => __NAMESPACE__ . '\\get_post_traffic_single_report_section_default_layout',
208        ),
209        array(
210            'slug'           => 'email-opens',
211            'label'          => __( 'Email opens', 'jetpack-premium-analytics-pkg' ),
212            'default_layout' => __NAMESPACE__ . '\\get_email_opens_single_report_section_default_layout',
213            'controls'       => false,
214        ),
215        array(
216            'slug'           => 'email-clicks',
217            'label'          => __( 'Email clicks', 'jetpack-premium-analytics-pkg' ),
218            'default_layout' => __NAMESPACE__ . '\\get_email_clicks_single_report_section_default_layout',
219            'controls'       => false,
220        ),
221    );
222
223    $pages = array(
224        'analytics/author'     => array(
225            'path'              => 'author',
226            'label'             => __( 'Author', 'jetpack-premium-analytics-pkg' ),
227            'dashboard_section' => 'traffic',
228            'feature'           => 'authors',
229            'scope_param'       => 'author_id',
230            'icon'              => 'jpa/post-author',
231            'reports_page'      => 'authors',
232            'default_layout'    => __NAMESPACE__ . '\\get_author_single_report_page_default_layout',
233            'preferences_scope' => 'jetpack-premium-analytics/author-detail',
234        ),
235        'analytics/post'       => array(
236            'entity'            => array(
237                'kind' => 'postType',
238                'name' => 'post',
239            ),
240            'dashboard_section' => 'traffic',
241            'icon'              => 'jpa/post',
242            'sections'          => $post_sections,
243            // Its adapter reads the entry from Stats, so the type need not be in REST.
244            'is_available'      => array( Capabilities::class, 'current_user_can_view_stats' ),
245        ),
246        'analytics/page'       => array(
247            'entity'            => array(
248                'kind' => 'postType',
249                'name' => 'page',
250            ),
251            'dashboard_section' => 'traffic',
252            'icon'              => 'jpa/page',
253            // The post page's: a page opens on either report, so both keep one arrangement.
254            'preferences_scope' => 'jetpack-premium-analytics/post-detail',
255            'sections'          => array(
256                array(
257                    'slug'           => 'post-traffic',
258                    'label'          => __( 'Page traffic', 'jetpack-premium-analytics-pkg' ),
259                    'default_layout' => __NAMESPACE__ . '\\get_page_traffic_single_report_section_default_layout',
260                ),
261            ),
262        ),
263        'analytics/ai-company' => array(
264            'path'              => 'ai-company',
265            'label'             => __( 'AI company', 'jetpack-premium-analytics-pkg' ),
266            'dashboard_section' => 'ai',
267            'scope_param'       => 'company',
268            // The keys the edge templates give companies: lowercase words joined by hyphens, except `other`, the sum of the companies past the template's limit.
269            'id_pattern'        => '^(?!other$)[a-z0-9]+(-[a-z0-9]+)*$',
270            'icon'              => 'jpa/cloud',
271            'reports_page'      => 'ai-companies',
272            'default_layout'    => __NAMESPACE__ . '\\get_ai_company_single_report_page_default_layout',
273            'is_available'      => array( AI_Traffic::class, 'is_available' ),
274        ),
275    );
276
277    foreach ( $pages as $id => $args ) {
278        if ( ! $registry->is_registered( DASHBOARD_NAME, $id ) ) {
279            $registry->register( DASHBOARD_NAME, $id, $args );
280        }
281    }
282}
283
284add_action( Single_Report_Page_Registry::REGISTER_ACTION, __NAMESPACE__ . '\\register_default_single_report_pages' );