Completed
Push — fix/instant-search-non-ascii ( b82854...f54339 )
by
unknown
18:41 queued 10:23
created

Jetpack_Google_Analytics::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 0
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
/*
3
    Copyright 2006 Aaron D. Campbell (email : [email protected])
4
5
    This program is free software; you can redistribute it and/or modify
6
    it under the terms of the GNU General Public License as published by
7
    the Free Software Foundation; either version 2 of the License, or
8
    (at your option) any later version.
9
10
    This program is distributed in the hope that it will be useful,
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
    GNU General Public License for more details.
14
15
    You should have received a copy of the GNU General Public License
16
    along with this program; if not, write to the Free Software
17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
*/
19
20
/**
21
 * Jetpack_Google_Analytics is the class that handles ALL of the plugin functionality.
22
 * It helps us avoid name collisions
23
 * https://codex.wordpress.org/Writing_a_Plugin#Avoiding_Function_Name_Collisions
24
 */
25
26
if ( ! defined( 'ABSPATH' ) ) {
27
	exit;
28
}
29
30
require_once( plugin_basename( 'classes/wp-google-analytics-utils.php' ) );
31
require_once( plugin_basename( 'classes/wp-google-analytics-options.php' ) );
32
require_once( plugin_basename( 'classes/wp-google-analytics-legacy.php' ) );
33
require_once( plugin_basename( 'classes/wp-google-analytics-universal.php' ) );
34
require_once plugin_basename( 'classes/class-jetpack-google-amp-analytics.php' );
35
36
class Jetpack_Google_Analytics {
37
38
	/**
39
	 * @var Jetpack_Google_Analytics - Static property to hold our singleton instance
40
	 */
41
	static $instance = false;
42
43
	/**
44
	 * @var Static property to hold concrete analytics impl that does the work (universal or legacy)
45
	 */
46
	static $analytics = false;
47
48
	/**
49
	 * This is our constructor, which is private to force the use of get_instance()
50
	 *
51
	 * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
52
	 */
53
	private function __construct() {
54
		// At this time, we only leverage universal analytics when enhanced ecommerce is selected and WooCommerce is active.
55
		// Otherwise, don't bother emitting the tracking ID or fetching analytics.js
56
		if ( class_exists( 'WooCommerce' ) && Jetpack_Google_Analytics_Options::enhanced_ecommerce_tracking_is_enabled() ) {
57
			$analytics     = new Jetpack_Google_Analytics_Universal();
0 ignored issues
show
Unused Code introduced by
$analytics is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
58
			$amp_analytics = new Jetpack_Google_AMP_Analytics();
0 ignored issues
show
Unused Code introduced by
$amp_analytics is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
59
60
		} else {
61
			$analytics = new Jetpack_Google_Analytics_Legacy();
0 ignored issues
show
Unused Code introduced by
$analytics is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
62
		}
63
	}
64
65
	/**
66
	 * Function to instantiate our class and make it a singleton
67
	 */
68
	public static function get_instance() {
69
		if ( ! self::$instance ) {
70
			self::$instance = new self;
71
		}
72
73
		return self::$instance;
74
	}
75
76
	/**
77
	 * Add amp-analytics tags.
78
	 *
79
	 * @param array $analytics_entries An associative array of the analytics entries.
80
	 *
81
	 * @return array
82
	 */
83
	public static function amp_analytics_entries( $analytics_entries ) {
84
		if ( ! is_array( $analytics_entries ) ) {
85
			$analytics_entries = array();
86
		}
87
88
		$amp_tracking_codes = static::get_amp_tracking_codes( $analytics_entries );
89
		$jetpack_account    = Jetpack_Google_Analytics_Options::get_tracking_code();
90
91
		// Bypass tracking codes already set on AMP plugin.
92
		if ( in_array( $jetpack_account, $amp_tracking_codes, true ) ) {
93
			return $analytics_entries;
94
		}
95
96
		$config_data = wp_json_encode(
97
			array(
98
				'vars'     => array(
99
					'account' => Jetpack_Google_Analytics_Options::get_tracking_code(),
100
				),
101
				'triggers' => array(
102
					'trackPageview' => array(
103
						'on'      => 'visible',
104
						'request' => 'pageview',
105
					),
106
				),
107
			)
108
		);
109
110
		// Generate a hash string to uniquely identify this entry.
111
		$entry_id = substr( md5( 'googleanalytics' . $config_data ), 0, 12 );
112
113
		$analytics_entries[ $entry_id ] = array(
114
			'type'   => 'googleanalytics',
115
			'config' => $config_data,
116
		);
117
118
		return $analytics_entries;
119
	}
120
121
	/**
122
	 * Get AMP tracking codes.
123
	 *
124
	 * @param array $analytics_entries The codes available for AMP.
125
	 *
126
	 * @return array
127
	 */
128
	protected static function get_amp_tracking_codes( $analytics_entries ) {
129
		$entries  = array_column( $analytics_entries, 'config' );
130
		$accounts = array();
131
132
		foreach ( $entries as $entry ) {
133
			$entry = json_decode( $entry );
134
135
			if ( ! empty( $entry->vars->account ) ) {
136
				$accounts[] = $entry->vars->account;
137
			}
138
		}
139
140
		return $accounts;
141
	}
142
}
143
144
global $jetpack_google_analytics;
145
$jetpack_google_analytics = Jetpack_Google_Analytics::get_instance();
146