Usage.php
6.09 KB
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
namespace AIOSEO\Plugin\Common\Admin;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Usage tracking class.
*
* @since 4.0.0
*/
abstract class Usage {
/**
* Returns the current plugin version type ("lite" or "pro").
*
* @since 4.1.3
*
* @return string The version type.
*/
abstract public function getType();
/**
* Source of notifications content.
*
* @since 4.0.0
*
* @var string
*/
private $url = 'https://aiousage.com/v1/track';
/**
* Whether or not usage tracking is enabled.
*
* @since 4.0.0
*
* @var bool
*/
protected $enabled = false;
/**
* Class Constructor.
*
* @since 4.0.0
*/
public function __construct() {
add_action( 'init', [ $this, 'init' ], 2 );
}
/**
* Runs on the init action.
*
* @since 4.0.0
*
* @return void
*/
public function init() {
try {
$action = 'aioseo_send_usage_data';
if ( ! $this->enabled ) {
aioseo()->actionScheduler->unschedule( $action );
return;
}
// Register the action handler.
add_action( $action, [ $this, 'process' ] );
if ( ! as_next_scheduled_action( $action ) ) {
as_schedule_recurring_action( $this->generateStartDate(), WEEK_IN_SECONDS, $action, [], 'aioseo' );
// Run the task immediately using an async action.
as_enqueue_async_action( $action, [], 'aioseo' );
}
} catch ( \Exception $e ) {
// Do nothing.
}
}
/**
* Processes the usage tracking.
*
* @since 4.0.0
*
* @return void
*/
public function process() {
if ( ! $this->enabled ) {
return;
}
wp_remote_post(
$this->getUrl(),
[
'timeout' => 10,
'headers' => array_merge( [
'Content-Type' => 'application/json; charset=utf-8'
], aioseo()->helpers->getApiHeaders() ),
'user-agent' => aioseo()->helpers->getApiUserAgent(),
'body' => wp_json_encode( $this->getData() )
]
);
}
/**
* Gets the URL for the notifications api.
*
* @since 4.0.0
*
* @return string The URL to use for the api requests.
*/
private function getUrl() {
if ( defined( 'AIOSEO_USAGE_TRACKING_URL' ) ) {
return AIOSEO_USAGE_TRACKING_URL;
}
return $this->url;
}
/**
* Retrieves the data to send in the usage tracking.
*
* @since 4.0.0
*
* @return array An array of data to send.
*/
protected function getData() {
$themeData = wp_get_theme();
$type = $this->getType();
return [
// Generic data (environment).
'url' => home_url(),
'php_version' => PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION,
'wp_version' => get_bloginfo( 'version' ),
'mysql_version' => aioseo()->core->db->db->db_version(),
'server_version' => isset( $_SERVER['SERVER_SOFTWARE'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ) ) : '',
'is_ssl' => is_ssl(),
'is_multisite' => is_multisite(),
'sites_count' => function_exists( 'get_blog_count' ) ? (int) get_blog_count() : 1,
'active_plugins' => $this->getActivePlugins(),
'theme_name' => $themeData->name,
'theme_version' => $themeData->version,
'user_count' => function_exists( 'get_user_count' ) ? get_user_count() : null,
'locale' => get_locale(),
'timezone_offset' => aioseo()->helpers->getTimeZoneOffset(),
'email' => get_bloginfo( 'admin_email' ),
// AIOSEO specific data.
'aioseo_version' => AIOSEO_VERSION,
'aioseo_license_key' => null,
'aioseo_license_type' => null,
'aioseo_is_pro' => false,
"aioseo_${type}_installed_date" => aioseo()->internalOptions->internal->installed,
'aioseo_settings' => $this->getSettings()
];
}
/**
* Get the settings and escape the quotes so it can be JSON encoded.
*
* @since 4.0.0
*
* @return array An array of settings data.
*/
private function getSettings() {
$settings = aioseo()->options->all();
array_walk_recursive( $settings, function( &$v ) {
if ( is_string( $v ) && strpos( $v, '"' ) !== false ) {
$v = str_replace( '"', '\"', $v );
}
});
$settings = $this->filterPrivateSettings( $settings );
$internal = aioseo()->internalOptions->all();
array_walk_recursive( $internal, function( &$v ) {
if ( is_string( $v ) && strpos( $v, '"' ) !== false ) {
$v = str_replace( '"', '\"', $v );
}
});
return [
'options' => $settings,
'internal' => $internal
];
}
/**
* Return a list of active plugins.
*
* @since 4.0.0
*
* @return array An array of active plugin data.
*/
private function getActivePlugins() {
if ( ! function_exists( 'get_plugins' ) ) {
include ABSPATH . '/wp-admin/includes/plugin.php';
}
$active = get_option( 'active_plugins', [] );
$plugins = array_intersect_key( get_plugins(), array_flip( $active ) );
return array_map(
static function ( $plugin ) {
if ( isset( $plugin['Version'] ) ) {
return $plugin['Version'];
}
return 'Not Set';
},
$plugins
);
}
/**
* Generate a random start date for usage tracking.
*
* @since 4.0.0
*
* @return integer The randomized start date.
*/
private function generateStartDate() {
$tracking = [
'days' => wp_rand( 0, 6 ) * DAY_IN_SECONDS,
'hours' => wp_rand( 0, 23 ) * HOUR_IN_SECONDS,
'minutes' => wp_rand( 0, 23 ) * HOUR_IN_SECONDS,
'seconds' => wp_rand( 0, 59 )
];
return strtotime( 'next sunday' ) + array_sum( $tracking );
}
/**
* Anonimizes or obfuscates the value of certain settings.
*
* @since 4.3.2
*
* @param array $settings The settings.
* @return array The altered settings.
*/
private function filterPrivateSettings( $settings ) {
if ( ! empty( $settings['advanced']['openAiKey'] ) ) {
$settings['advanced']['openAiKey'] = true;
}
if ( ! empty( $settings['localBusiness']['maps']['apiKey'] ) ) {
$settings['localBusiness']['maps']['apiKey'] = true;
}
return $settings;
}
}