forked from gambitph/Titan-Framework
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass-titan-tracking.php
252 lines (216 loc) · 7.41 KB
/
class-titan-tracking.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/**
* Titan Framework Tracker Class
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* Titan Framework Tracker Class
* In charge of the opt-in procedure and performing the actual data sending for tracking Titan
* intallation details.
*
* @author Benjamin Intal
**/
class TitanFrameworkTracker {
const REMOTE_URL = 'http://www.titanframework.net/wp-admin/admin-ajax.php'; // TODO: This isn't live yet, although will not error out
const TRACKER_INTERVAL = 10; // temporarily 10 secs for debugging FIXME: WEEK_IN_SECONDS;
const REMOTE_ACTION = 'tracker';
const OPT_AJAX_ACTION = 'tf_tracker_opted';
const NONCE_NAME = 'tracker_nonce';
// Internal variables
private $frameworkInstance;
private $optInOption;
private $transientName;
/**
* Class constructor
*
* @param TitanFramework $frameworkInstance an instance of the framework object
* @return void
* @since 1.6
*/
function __construct( $frameworkInstance ) {
$this->frameworkInstance = $frameworkInstance;
$this->optInOption = $this->frameworkInstance->optionNamespace . '_tf_tracker';
$this->transientName = $this->optInOption . '_transient';
add_action( 'admin_notices', array( $this, 'includeOptInScript' ) );
add_action( 'wp_ajax_' . self::OPT_AJAX_ACTION, array( $this, 'ajaxTrackerOptedHandler' ) );
add_action( 'admin_footer', array( $this, 'performTracking' ) );
add_action( 'wp_footer', array( $this, 'performTracking' ) );
}
/**
* Adds a notice in the admin for tracking opt-in
*
* @return void
* @since 1.6
*/
public function includeOptInScript() {
// Check our settings
if ( ! $this->frameworkInstance->settings['tracking'] ) {
delete_option( $this->optInOption );
delete_transient( $this->transientName, $this->transientName );
return;
}
// Check if opted in/out before, quit
if ( false !== get_option( $this->optInOption ) ) {
return;
}
// Check if first time, ask to opt-in
echo '<div class="updated" style="border-left-color: #3498db">
<p>
' . __( 'Help us make Titan Framework better by enabling your site to periodically send us tracking data. This is so we can know where and how Titan is being used.', TF_I18NDOMAIN ) . '
<button name="opt" value="1" class="' . $this->optInOption . ' button button-primary">' . __( 'Help us and track', TF_I18NDOMAIN ) . '</button>
<button name="opt" value="0" class="' . $this->optInOption . ' button button-default">' . __( "Don't track", TF_I18NDOMAIN ) . '</button>
<script>
jQuery(document).ready(function($) {
$(".' . $this->optInOption . '").click(function() {
var data = {
"' . self::NONCE_NAME . '": "' . wp_create_nonce( __CLASS__ ) . '",
"action": "' . self::OPT_AJAX_ACTION . '",
"opt": $(this).val()
};
var $this = $(this);
$.post(ajaxurl, data, function(response) {
$this.parents(".updated:eq(0)").fadeOut();
});
$(".' . $this->optInOption . '").attr("disabled", "disabled");
});
});
</script>
</p>
</div>';
}
/**
* Ajax handler for the opt-in question from the admin notice
*
* @return void
* @since 1.6
*/
public function ajaxTrackerOptedHandler() {
check_ajax_referer( __CLASS__, self::NONCE_NAME );
if ( $_POST['opt'] == '1' ) {
update_option( $this->optInOption, '1' );
// Send out tracking stuff immediately during ajax
$this->performTracking();
} else {
update_option( $this->optInOption, '0' );
}
die();
}
/**
* Performs the sending out of data for the tracking, this uses the transient API
* to perform data sending only every week.
*
* @return void
* @since 1.6
*/
public function performTracking() {
// Only do this when settings permit and the user opted-in
if ( ! $this->frameworkInstance->settings['tracking'] ) {
return;
}
if ( get_option( $this->optInOption ) !== '1' ) {
return;
}
// Send out our tracking data if it's time already
if ( false === get_transient( $this->transientName ) ) {
$response = wp_remote_post(
self::REMOTE_URL,
array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(),
'body' => $this->formDataToSend(),
'cookies' => array(),
)
);
// Periodically repeat
set_transient( $this->transientName, $this->transientName, self::TRACKER_INTERVAL );
}
}
/**
* Gathers all the data that we can get (not sensitive) to send out as tracker
* information.
*
* @return array WP installation data
* @since 1.6
*/
private function formDataToSend() {
$data = array();
// Action of the receiving WP host
$data['action'] = self::REMOTE_ACTION;
// WordPress installation details
$data['wp'] = array(
'name' => get_bloginfo( 'name' ),
'home_url' => home_url(),
'description' => get_bloginfo( 'description' ),
'version' => get_bloginfo( 'version' ),
'text_direction' => get_bloginfo( 'text_direction' ),
'language' => get_bloginfo( 'language' ),
);
// Titan Framework details
$data['titan'] = array();
if ( defined( 'TF_VERSION' ) ) {
$data['titan']['version'] = TF_VERSION;
}
// Get option & container stats
if ( ! empty( $this->frameworkInstance->optionsUsed ) ) {
$data['titan']['num_options'] = count( $this->frameworkInstance->optionsUsed );
$data['titan']['option_count'] = array();
$data['titan']['container_count'] = array();
$data['titan']['container_option_count'] = array();
foreach ( $this->frameworkInstance->optionsUsed as $option ) {
if ( ! empty( $option->settings['type'] ) ) {
if ( empty( $data['titan']['option_count'][ $option->settings['type'] ] ) ) {
$data['titan']['option_count'][ $option->settings['type'] ] = 0;
}
$data['titan']['option_count'][ $option->settings['type'] ]++;
}
if ( empty( $data['titan']['container_count'][ get_class( $option->owner ) ] ) ) {
$data['titan']['container_count'][ get_class( $option->owner ) ] = 0;
}
$data['titan']['container_count'][ get_class( $option->owner ) ]++;
if ( empty( $data['titan']['container_option_count'][ get_class( $option->owner ) ] ) ) {
$data['titan']['container_option_count'][ get_class( $option->owner ) ] = array();
}
$data['titan']['container_option_count'][ get_class( $option->owner ) ][] = count( $option->owner->options );
}
}
// Average the number of options per container
if ( ! empty( $data['titan']['container_option_count'] ) ) {
foreach ( $data['titan']['container_option_count'] as $key => $countArr ) {
$runningTotal = 0;
foreach ( $countArr as $count ) {
$runningTotal += $count;
}
$data['titan']['container_option_count'][ $key ] = $runningTotal / count( $countArr );
}
}
// Current theme details
$theme = wp_get_theme();
$data['theme'] = array(
'name' => $theme->get( 'Name' ),
'version' => $theme->get( 'Version' ),
'themeuri' => $theme->get( 'ThemeURI' ),
'author' => $theme->get( 'Author' ),
'authoruri' => $theme->get( 'AuthorURI' ),
);
// Plugin details
$data['plugins'] = array();
if ( $plugins = get_plugins() ) {
foreach ( $plugins as $key => $pluginData ) {
if ( is_plugin_active( $key ) ) {
$data['plugins'][ $key ] = $pluginData;
}
}
}
// PHP details
$data['php'] = array(
'phpversion' => phpversion(),
);
// Super basic security remote check
$data['hash'] = md5( serialize( $data ) );
return $data;
}
}