Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.30% covered (success)
91.30%
21 / 23
0.00% covered (danger)
0.00%
0 / 2
CRAP
n/a
0 / 0
Automattic\Jetpack\PremiumAnalytics\add_error_reporting_script_data
92.31% covered (success)
92.31%
12 / 13
0.00% covered (danger)
0.00%
0 / 1
5.01
Automattic\Jetpack\PremiumAnalytics\sanitize_error_reporting_origins
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
7.05
1<?php
2/**
3 * The configuration the dashboard's error reporter reads.
4 *
5 * @package automattic/jetpack-premium-analytics
6 */
7
8namespace Automattic\Jetpack\PremiumAnalytics;
9
10use Automattic\Jetpack\Assets;
11use Automattic\Jetpack\Status\Host;
12
13/**
14 * Adds the error reporting gate, package version and script origins to JetpackScriptData.
15 *
16 * @since $$next-version$$
17 *
18 * @param array $data The script data passed by the assets package.
19 * @return array
20 */
21function add_error_reporting_script_data( array $data ): array {
22    if ( ! isset( $data['premium_analytics'] ) || ! is_array( $data['premium_analytics'] ) ) {
23        $data['premium_analytics'] = array();
24    }
25
26    $origins = array( trailingslashit( Assets::normalize_path( plugins_url( '../build/', __FILE__ ) ) ) );
27
28    // Packages add their origin when the widget type registry hydrates.
29    if ( function_exists( __NAMESPACE__ . '\\get_answering_widget_type_registry' ) ) {
30        get_answering_widget_type_registry();
31    }
32
33    /**
34     * Filters the URL prefixes that mark a stack frame as Premium Analytics code.
35     *
36     * Packages that ship their own build for the dashboard add its base URL here.
37     *
38     * @since $$next-version$$
39     *
40     * @param string[] $origins Absolute URL prefixes, each ending in a slash.
41     */
42    $filtered = apply_filters( 'jetpack_premium_analytics_error_reporting_script_origins', $origins );
43
44    $host = new Host();
45
46    $data['premium_analytics']['error_reporting'] = array(
47        'enabled'        => $host->is_wpcom_simple() || $host->is_woa_site(),
48        'version'        => Analytics::PACKAGE_VERSION,
49        'script_origins' => sanitize_error_reporting_origins( $filtered, $origins[0] ),
50    );
51
52    return $data;
53}
54
55/**
56 * Keeps the package's own origin first and drops what a filter must not be able to publish.
57 *
58 * An origin with no host, `/` or `https://` among them, would match every stack line of that shape,
59 * so every error would look like dashboard code.
60 *
61 * @since $$next-version$$
62 *
63 * @param mixed  $filtered The filter result, which can be anything.
64 * @param string $own      The package's own build origin.
65 * @return string[] Absolute URL prefixes, each ending in a slash.
66 */
67function sanitize_error_reporting_origins( $filtered, string $own ): array {
68    $origins = array( $own );
69
70    if ( is_array( $filtered ) ) {
71        foreach ( $filtered as $origin ) {
72            if ( ! is_string( $origin ) || $origin === '' ) {
73                continue;
74            }
75
76            $origin_host = wp_parse_url( $origin, PHP_URL_HOST );
77            if ( ! is_string( $origin_host ) || $origin_host === '' ) {
78                continue;
79            }
80
81            $origins[] = trailingslashit( $origin );
82        }
83    }
84
85    return array_values( array_unique( $origins ) );
86}