Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
43.75% covered (danger)
43.75%
7 / 16
50.00% covered (danger)
50.00%
2 / 4
CRAP
n/a
0 / 0
Automattic\Jetpack\PremiumAnalytics\register_reports_page
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
Automattic\Jetpack\PremiumAnalytics\get_available_reports_page_records
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
Automattic\Jetpack\PremiumAnalytics\configure_reports_pages_script_data
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
Automattic\Jetpack\PremiumAnalytics\inject_reports_pages_script_data
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * Reports page API: the registration helper and the script data.
4 *
5 * The package's own pages register through this API from default-reports-pages.php,
6 * the way other plugins do.
7 *
8 * @package automattic/jetpack-premium-analytics
9 */
10
11namespace Automattic\Jetpack\PremiumAnalytics;
12
13// The record, its layout filter and the widget instance helper are the single-report page's.
14require_once __DIR__ . '/single-report-pages.php';
15require_once __DIR__ . '/class-reports-page-registry.php';
16
17/**
18 * Registers a reports page.
19 *
20 * @since $$next-version$$
21 *
22 * @param string $dashboard_name Dashboard identifier.
23 * @param string $id             Page identifier, namespaced.
24 * @param array  $args           Optional. Page arguments.
25 * @return Reports_Page|false The registered page on success, or false on failure.
26 */
27function register_reports_page( $dashboard_name, $id, $args = array() ) {
28    return Reports_Page_Registry::get_instance()->register( $dashboard_name, $id, $args );
29}
30
31/**
32 * The records of the available reports pages of the dashboard.
33 *
34 * @return array[]
35 */
36function get_available_reports_page_records() {
37    return array_map(
38        static function ( Reports_Page $page ) {
39            return $page->to_array();
40        },
41        Reports_Page_Registry::get_instance()->get_available_pages( DASHBOARD_NAME )
42    );
43}
44
45/**
46 * Publishes the available reports pages to the client.
47 */
48function configure_reports_pages_script_data() {
49    add_filter( 'jetpack_admin_js_script_data', __NAMESPACE__ . '\\inject_reports_pages_script_data', 20 );
50}
51
52/**
53 * Adds `premium_analytics.reports_pages` to the dashboard's script data; other admin screens get nothing.
54 *
55 * @param array $data Script data.
56 * @return array
57 */
58function inject_reports_pages_script_data( array $data ): array {
59    if ( ! Analytics::renders_dashboard() ) {
60        return $data;
61    }
62
63    if ( ! isset( $data['premium_analytics'] ) || ! is_array( $data['premium_analytics'] ) ) {
64        $data['premium_analytics'] = array();
65    }
66
67    $data['premium_analytics']['reports_pages'] = get_available_reports_page_records();
68
69    return $data;
70}