Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.86% |
26 / 28 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| Tracks | |
100.00% |
26 / 26 |
|
100.00% |
3 / 3 |
11 | |
100.00% |
1 / 1 |
| is_allowed | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| enqueue_transport | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
4 | |||
| add_identity | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Tracks consent, transport and identity for the Monetize dashboard. |
| 4 | * |
| 5 | * @package automattic/jetpack-monetize |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Monetize; |
| 9 | |
| 10 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 11 | use Automattic\Jetpack\Status; |
| 12 | use Automattic\Jetpack\Status\Host; |
| 13 | use Automattic\Jetpack\Terms_Of_Service; |
| 14 | use Automattic\Jetpack\Tracking; |
| 15 | |
| 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | exit( 0 ); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * What the page's JS needs to record Tracks events, as on Jetpack's other admin pages. |
| 22 | * |
| 23 | * @since 0.2.0 |
| 24 | */ |
| 25 | class Tracks { |
| 26 | |
| 27 | /** |
| 28 | * Whether the site and user allow Tracks. |
| 29 | * |
| 30 | * @return bool |
| 31 | */ |
| 32 | public static function is_allowed() { |
| 33 | // Simple users are WordPress.com users, so neither Jetpack's terms nor a user token apply. |
| 34 | if ( ( new Host() )->is_wpcom_simple() ) { |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | $tracking = new Tracking( 'jetpack', new Connection_Manager( 'jetpack' ) ); |
| 39 | |
| 40 | return $tracking->should_enable_tracking( new Terms_Of_Service(), new Status() ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Load the Tracks script on the dashboard; other packages load it only for some plans. |
| 45 | * |
| 46 | * Simple is skipped because WordPress.com's stats already print the same script there. |
| 47 | * |
| 48 | * @return void |
| 49 | */ |
| 50 | public static function enqueue_transport() { |
| 51 | if ( ( new Host() )->is_wpcom_simple() ) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | if ( Dashboard::is_dashboard_request() && self::is_allowed() ) { |
| 56 | wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js', array(), gmdate( 'YW' ), true ); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Add the connected WordPress.com user for Tracks, unless another package already did. |
| 62 | * |
| 63 | * @param array $data Script data. |
| 64 | * @return array |
| 65 | */ |
| 66 | public static function add_identity( $data ) { |
| 67 | if ( ! empty( $data['user']['current_user']['wpcom']['ID'] ) ) { |
| 68 | return $data; |
| 69 | } |
| 70 | |
| 71 | if ( ( new Host() )->is_wpcom_simple() ) { |
| 72 | // Local users are WordPress.com users on Simple. |
| 73 | $connected = array( |
| 74 | 'ID' => get_current_user_id(), |
| 75 | 'login' => wp_get_current_user()->user_login, |
| 76 | ); |
| 77 | } else { |
| 78 | $connected = ( new Connection_Manager() )->get_connected_user_data(); |
| 79 | } |
| 80 | if ( empty( $connected['ID'] ) || empty( $connected['login'] ) ) { |
| 81 | return $data; |
| 82 | } |
| 83 | |
| 84 | $data['user']['current_user']['wpcom'] = array_merge( |
| 85 | $data['user']['current_user']['wpcom'] ?? array(), |
| 86 | array( |
| 87 | 'ID' => $connected['ID'], |
| 88 | 'login' => $connected['login'], |
| 89 | ) |
| 90 | ); |
| 91 | |
| 92 | return $data; |
| 93 | } |
| 94 | } |