Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
40.91% |
9 / 22 |
|
25.00% |
1 / 4 |
CRAP | n/a |
0 / 0 |
|
| Automattic\Jetpack\Jetpack_Mu_Wpcom\Common\get_iso_639_locale | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| Automattic\Jetpack\Jetpack_Mu_Wpcom\Common\determine_iso_639_locale | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| Automattic\Jetpack\Jetpack_Mu_Wpcom\Common\wpcom_enqueue_tracking_scripts | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| Automattic\Jetpack\Jetpack_Mu_Wpcom\Common\wpcom_record_tracks_event | |
40.00% |
2 / 5 |
|
0.00% |
0 / 1 |
7.46 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * File for various functionality which needs to be added to Simple and Atomic |
| 4 | * sites. |
| 5 | * |
| 6 | * @package automattic/jetpack-mu-wpcom |
| 7 | */ |
| 8 | |
| 9 | namespace Automattic\Jetpack\Jetpack_Mu_Wpcom\Common; |
| 10 | |
| 11 | use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State; |
| 12 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 13 | use Automattic\Jetpack\Status; |
| 14 | use Automattic\Jetpack\Terms_Of_Service; |
| 15 | use Automattic\Jetpack\Tracking; |
| 16 | |
| 17 | require_once __DIR__ . '/wpcom-enqueue-dynamic-script/class-wpcom-enqueue-dynamic-script.php'; |
| 18 | |
| 19 | /** |
| 20 | * Returns ISO 639 conforming locale string. |
| 21 | * |
| 22 | * @param string $language a language tag to be converted e.g. "en_US". |
| 23 | * @return string ISO 639 locale string e.g. "en" |
| 24 | */ |
| 25 | function get_iso_639_locale( $language ) { |
| 26 | $language = strtolower( $language ); |
| 27 | |
| 28 | if ( in_array( $language, array( 'pt_br', 'pt-br', 'zh_tw', 'zh-tw', 'zh_cn', 'zh-cn' ), true ) ) { |
| 29 | $language = str_replace( '_', '-', $language ); |
| 30 | } else { |
| 31 | $language = preg_replace( '/([-_].*)$/i', '', $language ); |
| 32 | } |
| 33 | |
| 34 | if ( empty( $language ) ) { |
| 35 | return 'en'; |
| 36 | } |
| 37 | |
| 38 | return $language; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Returns ISO 639 conforming locale string of the current user. |
| 43 | * |
| 44 | * @return string ISO 639 locale string e.g. "en" |
| 45 | */ |
| 46 | function determine_iso_639_locale() { |
| 47 | $locale = get_user_locale(); |
| 48 | return get_iso_639_locale( $locale ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Enqueue the tracking scripts for the given script handle. |
| 53 | * |
| 54 | * @param string $handle A script handle. |
| 55 | */ |
| 56 | function wpcom_enqueue_tracking_scripts( string $handle ) { |
| 57 | Connection_Initial_State::render_script( $handle ); |
| 58 | |
| 59 | $status = new Status(); |
| 60 | $connection = new Connection_Manager(); |
| 61 | $tracking = new Tracking( 'jetpack-mu-wpcom', $connection ); |
| 62 | $can_use_analytics = $tracking->should_enable_tracking( new Terms_Of_Service(), $status ); |
| 63 | |
| 64 | if ( $can_use_analytics ) { |
| 65 | Tracking::register_tracks_functions_scripts( true ); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Record tracks event. |
| 71 | * |
| 72 | * @param mixed $event_name The event. |
| 73 | * @param mixed $event_properties The event property. |
| 74 | * |
| 75 | * @return void |
| 76 | */ |
| 77 | function wpcom_record_tracks_event( $event_name, $event_properties ) { |
| 78 | if ( function_exists( 'wpcomsh_record_tracks_event' ) ) { |
| 79 | wpcomsh_record_tracks_event( $event_name, $event_properties ); |
| 80 | } elseif ( function_exists( 'require_lib' ) && function_exists( 'tracks_record_event' ) ) { |
| 81 | require_lib( 'tracks/client' ); |
| 82 | tracks_record_event( get_current_user_id(), $event_name, $event_properties ); |
| 83 | } |
| 84 | } |