Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
16.22% |
6 / 37 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Utils | |
16.22% |
6 / 37 |
|
25.00% |
1 / 4 |
362.77 | |
0.00% |
0 / 1 |
| get_product_categories_concatenated | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
72 | |||
| get_product_sku_or_id | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
| is_dnt_enabled | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
5 | |||
| is_truthy | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
56 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Utils class. |
| 4 | * |
| 5 | * @package automattic/jetpack-google-analytics |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Google_Analytics; |
| 9 | |
| 10 | /** |
| 11 | * Utils main class. |
| 12 | */ |
| 13 | class Utils { |
| 14 | /** |
| 15 | * Gets product categories or varation attributes as a formatted concatenated string |
| 16 | * |
| 17 | * @phan-suppress PhanUndeclaredTypeParameter |
| 18 | * |
| 19 | * @param \WC_Product $product Product to get categories/variations for. |
| 20 | * @return string |
| 21 | */ |
| 22 | public static function get_product_categories_concatenated( $product ) { |
| 23 | if ( ! class_exists( 'WooCommerce' ) ) { |
| 24 | return ''; |
| 25 | } |
| 26 | |
| 27 | if ( ! $product ) { |
| 28 | return ''; |
| 29 | } |
| 30 | |
| 31 | // @phan-suppress-next-line PhanUndeclaredClassMethod, PhanUndeclaredFunction |
| 32 | $variation_data = $product->is_type( 'variation' ) ? \wc_get_product_variation_attributes( $product->get_id() ) : ''; |
| 33 | if ( is_array( $variation_data ) && ! empty( $variation_data ) ) { |
| 34 | // @phan-suppress-next-line PhanUndeclaredFunction |
| 35 | $line = \wc_get_formatted_variation( $variation_data, true ); |
| 36 | } else { |
| 37 | $out = array(); |
| 38 | // @phan-suppress-next-line PhanUndeclaredClassMethod |
| 39 | $categories = get_the_terms( $product->get_id(), 'product_cat' ); |
| 40 | if ( is_array( $categories ) ) { |
| 41 | foreach ( $categories as $category ) { |
| 42 | $out[] = $category->name; |
| 43 | } |
| 44 | } |
| 45 | $line = implode( '/', $out ); |
| 46 | } |
| 47 | return $line; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Gets a product's SKU with fallback to just ID. IDs are prepended with a hash symbol. |
| 52 | * |
| 53 | * @phan-suppress PhanUndeclaredTypeParameter |
| 54 | * |
| 55 | * @param \WC_Product $product Product to get SKU/ID for. |
| 56 | * @return string |
| 57 | */ |
| 58 | public static function get_product_sku_or_id( $product ) { |
| 59 | if ( ! class_exists( 'WooCommerce' ) ) { |
| 60 | return ''; |
| 61 | } |
| 62 | |
| 63 | if ( ! $product ) { |
| 64 | return ''; |
| 65 | } |
| 66 | |
| 67 | // @phan-suppress-next-line PhanUndeclaredClassMethod |
| 68 | return $product->get_sku() ? $product->get_sku() : '#' . $product->get_id(); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Checks if filter is set and dnt is enabled. |
| 73 | * |
| 74 | * @return bool |
| 75 | */ |
| 76 | public static function is_dnt_enabled() { |
| 77 | /** |
| 78 | * Filter the option which decides honor DNT or not. |
| 79 | * |
| 80 | * @module google-analytics |
| 81 | * @since jetpack-11.3 |
| 82 | * |
| 83 | * @param bool $honor_dnt Honors DNT for clients who don't want to be tracked. Set to true to enable. |
| 84 | */ |
| 85 | if ( false === apply_filters( 'jetpack_honor_dnt_header_for_wga', Options::honor_dnt_is_enabled() ) ) { |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | foreach ( $_SERVER as $name => $value ) { |
| 90 | if ( 'http_dnt' === strtolower( $name ) && 1 === (int) $value ) { |
| 91 | return true; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Determine if a string is truthy. If it's not a string, which can happen with |
| 100 | * not well-formed data coming from Jetpack sites, we still consider it a truthy value. |
| 101 | * |
| 102 | * @since 0.2.0 |
| 103 | * |
| 104 | * @param mixed $value true, 1, "1", "t", and "true" (case insensitive) are truthy, everything else isn't. |
| 105 | * @return bool |
| 106 | */ |
| 107 | public static function is_truthy( $value ) { |
| 108 | if ( true === $value ) { |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | if ( 1 === $value ) { |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | if ( ! is_string( $value ) ) { |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | switch ( strtolower( $value ) ) { |
| 121 | case '1': |
| 122 | case 't': |
| 123 | case 'true': |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | return false; |
| 128 | } |
| 129 | } |