class-jetpack-google-amp-analytics.php
2 years ago
wp-google-analytics-legacy.php
2 years ago
wp-google-analytics-options.php
2 years ago
wp-google-analytics-universal.php
2 years ago
wp-google-analytics-utils.php
2 years ago
wp-google-analytics-utils.php
95 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * Jetpack_Google_Analytics_Options provides a single interface to module options |
| 4 | * |
| 5 | * @author allendav |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Bail if accessed directly |
| 10 | */ |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Jetpack_Google_Analytics_Utils main class. |
| 17 | */ |
| 18 | class Jetpack_Google_Analytics_Utils { |
| 19 | /** |
| 20 | * Gets product categories or varation attributes as a formatted concatenated string |
| 21 | * |
| 22 | * @param WC_Product $product Product to get categories/variations for. |
| 23 | * @return string |
| 24 | */ |
| 25 | public static function get_product_categories_concatenated( $product ) { |
| 26 | if ( ! class_exists( 'WooCommerce' ) ) { |
| 27 | return ''; |
| 28 | } |
| 29 | |
| 30 | if ( ! $product ) { |
| 31 | return ''; |
| 32 | } |
| 33 | |
| 34 | $variation_data = $product->is_type( 'variation' ) ? wc_get_product_variation_attributes( $product->get_id() ) : ''; |
| 35 | if ( is_array( $variation_data ) && ! empty( $variation_data ) ) { |
| 36 | $line = wc_get_formatted_variation( $variation_data, true ); |
| 37 | } else { |
| 38 | $out = array(); |
| 39 | $categories = get_the_terms( $product->get_id(), 'product_cat' ); |
| 40 | if ( $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 | * @param WC_Product $product Product to get SKU/ID for. |
| 54 | * @return string |
| 55 | */ |
| 56 | public static function get_product_sku_or_id( $product ) { |
| 57 | if ( ! class_exists( 'WooCommerce' ) ) { |
| 58 | return ''; |
| 59 | } |
| 60 | |
| 61 | if ( ! $product ) { |
| 62 | return ''; |
| 63 | } |
| 64 | |
| 65 | return $product->get_sku() ? $product->get_sku() : '#' . $product->get_id(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Checks if filter is set and dnt is enabled. |
| 70 | * |
| 71 | * @return bool |
| 72 | */ |
| 73 | public static function is_dnt_enabled() { |
| 74 | /** |
| 75 | * Filter the option which decides honor DNT or not. |
| 76 | * |
| 77 | * @module google-analytics |
| 78 | * @since 11.3 |
| 79 | * |
| 80 | * @param bool $honor_dnt Honors DNT for clients who don't want to be tracked. Set to true to enable. |
| 81 | */ |
| 82 | if ( false === apply_filters( 'jetpack_honor_dnt_header_for_wga', Jetpack_Google_Analytics_Options::honor_dnt_is_enabled() ) ) { |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | foreach ( $_SERVER as $name => $value ) { |
| 87 | if ( 'http_dnt' === strtolower( $name ) && 1 === (int) $value ) { |
| 88 | return true; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return false; |
| 93 | } |
| 94 | } |
| 95 |