Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
78.12% |
25 / 32 |
|
50.00% |
3 / 6 |
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 | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
2.01 | |||
| Automattic\Jetpack\Jetpack_Mu_Wpcom\Common\wpcom_record_tracks_event | |
40.00% |
2 / 5 |
|
0.00% |
0 / 1 |
7.46 | |||
| Automattic\Jetpack\Jetpack_Mu_Wpcom\Common\wpcom_get_launch_site_url | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| Automattic\Jetpack\Jetpack_Mu_Wpcom\Common\wpcom_get_launch_site_back_to | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| 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 | } |
| 85 | |
| 86 | /** |
| 87 | * The Calypso launch-flow URL for this site. |
| 88 | * |
| 89 | * @param array $args Query args to add, or to override `siteSlug` and `ref` with. |
| 90 | * @return string |
| 91 | */ |
| 92 | function wpcom_get_launch_site_url( array $args = array() ): string { |
| 93 | $args = array_merge( |
| 94 | array( |
| 95 | 'siteSlug' => (string) wp_parse_url( home_url(), PHP_URL_HOST ), |
| 96 | 'ref' => 'wp-admin', |
| 97 | ), |
| 98 | $args |
| 99 | ); |
| 100 | |
| 101 | return add_query_arg( array_map( 'rawurlencode', $args ), 'https://wordpress.com/setup/launch-site' ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * The current request's URL on the site's own host, for the launch flow's `back_to`. |
| 106 | * |
| 107 | * Calypso drops a `back_to` unless it is https on the launched site's host, so the request host is not used. |
| 108 | * |
| 109 | * @return string |
| 110 | */ |
| 111 | function wpcom_get_launch_site_back_to(): string { |
| 112 | $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '/'; |
| 113 | |
| 114 | return 'https://' . wp_parse_url( home_url(), PHP_URL_HOST ) . $request_uri; |
| 115 | } |