Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.59% |
25 / 27 |
|
50.00% |
2 / 4 |
CRAP | n/a |
0 / 0 |
|
| Automattic\Jetpack\PremiumAnalytics\is_error_reporting_enabled | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| Automattic\Jetpack\PremiumAnalytics\delegate_wp_admin_error_reporting | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| Automattic\Jetpack\PremiumAnalytics\add_error_reporting_script_data | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
4.01 | |||
| Automattic\Jetpack\PremiumAnalytics\sanitize_error_reporting_origins | |
90.00% |
9 / 10 |
|
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 | |
| 8 | namespace Automattic\Jetpack\PremiumAnalytics; |
| 9 | |
| 10 | use Automattic\Jetpack\Assets; |
| 11 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 12 | use Automattic\Jetpack\Status\Host; |
| 13 | |
| 14 | /** |
| 15 | * Whether the dashboard reports its own uncaught JavaScript errors on this reader's screen. |
| 16 | * |
| 17 | * The logging was approved for WordPress.com users on WordPress.com sites (p2y3YZ-bzB-p2), and an |
| 18 | * Atomic site can carry a local user with no WordPress.com account, so Atomic asks the connection. |
| 19 | * |
| 20 | * @since 0.19.0 |
| 21 | * |
| 22 | * @return bool |
| 23 | */ |
| 24 | function is_error_reporting_enabled(): bool { |
| 25 | $host = new Host(); |
| 26 | |
| 27 | if ( $host->is_wpcom_simple() ) { |
| 28 | return true; |
| 29 | } |
| 30 | |
| 31 | return $host->is_woa_site() && ( new Connection_Manager() )->is_user_connected(); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Stands the wp-admin error reporter down on the dashboard screen, where the dashboard reports |
| 36 | * for itself. |
| 37 | * |
| 38 | * The jetpack-mu-wpcom reporter sends every window error to the same endpoint with the full URL |
| 39 | * and an unredacted stack, so both reporters on one screen file every error twice. |
| 40 | * |
| 41 | * @since 0.19.0 |
| 42 | * |
| 43 | * @param bool $delegated Whether another reporter already owns the screen. |
| 44 | * @return bool |
| 45 | */ |
| 46 | function delegate_wp_admin_error_reporting( $delegated ): bool { |
| 47 | return is_error_reporting_enabled() ? true : (bool) $delegated; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Adds the error reporting gate, package version and script origins to JetpackScriptData. |
| 52 | * |
| 53 | * @since 0.19.0 |
| 54 | * |
| 55 | * @param array $data The script data passed by the assets package. |
| 56 | * @return array |
| 57 | */ |
| 58 | function add_error_reporting_script_data( array $data ): array { |
| 59 | if ( ! isset( $data['premium_analytics'] ) || ! is_array( $data['premium_analytics'] ) ) { |
| 60 | $data['premium_analytics'] = array(); |
| 61 | } |
| 62 | |
| 63 | $origins = array( trailingslashit( Assets::normalize_path( plugins_url( '../build/', __FILE__ ) ) ) ); |
| 64 | |
| 65 | // Packages add their origin when the widget type registry hydrates. |
| 66 | if ( function_exists( __NAMESPACE__ . '\\get_answering_widget_type_registry' ) ) { |
| 67 | get_answering_widget_type_registry(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Filters the URL prefixes that mark a stack frame as Premium Analytics code. |
| 72 | * |
| 73 | * Packages that ship their own build for the dashboard add its base URL here. |
| 74 | * |
| 75 | * @since 0.19.0 |
| 76 | * |
| 77 | * @param string[] $origins Absolute URL prefixes, each ending in a slash. |
| 78 | */ |
| 79 | $filtered = apply_filters( 'jetpack_premium_analytics_error_reporting_script_origins', $origins ); |
| 80 | |
| 81 | $data['premium_analytics']['error_reporting'] = array( |
| 82 | 'enabled' => is_error_reporting_enabled(), |
| 83 | 'version' => Analytics::PACKAGE_VERSION, |
| 84 | 'script_origins' => sanitize_error_reporting_origins( $filtered, $origins[0] ), |
| 85 | ); |
| 86 | |
| 87 | return $data; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Keeps the package's own origin first and drops what a filter must not be able to publish. |
| 92 | * |
| 93 | * An origin with no host, `/` or `https://` among them, would match every stack line of that shape, |
| 94 | * so every error would look like dashboard code. |
| 95 | * |
| 96 | * @since 0.19.0 |
| 97 | * |
| 98 | * @param mixed $filtered The filter result, which can be anything. |
| 99 | * @param string $own The package's own build origin. |
| 100 | * @return string[] Absolute URL prefixes, each ending in a slash. |
| 101 | */ |
| 102 | function sanitize_error_reporting_origins( $filtered, string $own ): array { |
| 103 | $origins = array( $own ); |
| 104 | |
| 105 | if ( is_array( $filtered ) ) { |
| 106 | foreach ( $filtered as $origin ) { |
| 107 | if ( ! is_string( $origin ) || $origin === '' ) { |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | $origin_host = wp_parse_url( $origin, PHP_URL_HOST ); |
| 112 | if ( ! is_string( $origin_host ) || $origin_host === '' ) { |
| 113 | continue; |
| 114 | } |
| 115 | |
| 116 | $origins[] = trailingslashit( $origin ); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return array_values( array_unique( $origins ) ); |
| 121 | } |