Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/updraftp.../central/modules
File: analytics.php
<?php
[0] Fix | Delete
[1] Fix | Delete
if (!defined('UPDRAFTCENTRAL_CLIENT_DIR')) die('No access.');
[2] Fix | Delete
[3] Fix | Delete
/**
[4] Fix | Delete
* Handles Analytics Commands
[5] Fix | Delete
*
[6] Fix | Delete
* @method array ga_checker()
[7] Fix | Delete
* @method array get_access_token()
[8] Fix | Delete
* @method array set_authorization_code()
[9] Fix | Delete
*/
[10] Fix | Delete
class UpdraftCentral_Analytics_Commands extends UpdraftCentral_Commands {
[11] Fix | Delete
[12] Fix | Delete
private $scope = 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/analytics.readonly';
[13] Fix | Delete
[14] Fix | Delete
private $endpoint = 'https://accounts.google.com/o/oauth2/auth';
[15] Fix | Delete
[16] Fix | Delete
private $token_info_endpoint = 'https://www.googleapis.com/oauth2/v1/tokeninfo';
[17] Fix | Delete
[18] Fix | Delete
private $access_key = 'updraftcentral_auth_server_access';
[19] Fix | Delete
[20] Fix | Delete
private $auth_endpoint;
[21] Fix | Delete
[22] Fix | Delete
private $client_id;
[23] Fix | Delete
[24] Fix | Delete
private $view_key = 'updraftcentral_analytics_views';
[25] Fix | Delete
[26] Fix | Delete
private $tracking_id_key = 'updraftcentral_analytics_tracking_id';
[27] Fix | Delete
[28] Fix | Delete
private $expiration;
[29] Fix | Delete
[30] Fix | Delete
/**
[31] Fix | Delete
* Constructor
[32] Fix | Delete
*/
[33] Fix | Delete
public function __construct() {
[34] Fix | Delete
$this->auth_endpoint = defined('UPDRAFTPLUS_GOOGLECLOUD_CALLBACK_URL') ? UPDRAFTPLUS_GOOGLECLOUD_CALLBACK_URL : 'https://auth.updraftplus.com/auth/googleanalytics';
[35] Fix | Delete
$this->client_id = defined('UPDRAFTPLUS_GOOGLECLOUD_CLIENT_ID') ? UPDRAFTPLUS_GOOGLECLOUD_CLIENT_ID : '306245874349-6s896c3tjpra26ns3dpplhqcl6rv6qlb.apps.googleusercontent.com';
[36] Fix | Delete
[37] Fix | Delete
// Set transient expiration - default for 24 hours
[38] Fix | Delete
$this->expiration = 86400;
[39] Fix | Delete
}
[40] Fix | Delete
[41] Fix | Delete
/**
[42] Fix | Delete
* Checks whether Google Analytics (GA) is installed or setup
[43] Fix | Delete
*
[44] Fix | Delete
* N.B. This check assumes GA is installed either using "wp_head" or "wp_footer" (e.g. attached
[45] Fix | Delete
* to the <head/> or somewhere before </body>). It does not recursively check all the pages
[46] Fix | Delete
* of the website to find if GA is installed on each or one of those pages, but only on the main/root page.
[47] Fix | Delete
*
[48] Fix | Delete
* @return array $result An array containing "ga_installed" property which returns "true" if GA (Google Analytics) is installed, "false" otherwise.
[49] Fix | Delete
*/
[50] Fix | Delete
public function ga_checker() {
[51] Fix | Delete
[52] Fix | Delete
try {
[53] Fix | Delete
[54] Fix | Delete
// Retrieves the tracking code/id if available
[55] Fix | Delete
$tracking_id = $this->get_tracking_id();
[56] Fix | Delete
$installed = true;
[57] Fix | Delete
[58] Fix | Delete
// If tracking code/id is currently not available then we
[59] Fix | Delete
// parse the needed information from the buffered content through
[60] Fix | Delete
// the "wp_head" and "wp_footer" hooks.
[61] Fix | Delete
if (false === $tracking_id) {
[62] Fix | Delete
$info = $this->extract_tracking_id();
[63] Fix | Delete
[64] Fix | Delete
$installed = $info['installed'];
[65] Fix | Delete
$tracking_id = $info['tracking_id'];
[66] Fix | Delete
}
[67] Fix | Delete
[68] Fix | Delete
// Get access token to be use to generate the report.
[69] Fix | Delete
$access_token = $this->_get_token();
[70] Fix | Delete
[71] Fix | Delete
if (empty($access_token)) {
[72] Fix | Delete
// If we don't get a valid access token then that would mean
[73] Fix | Delete
// the access has been revoked by the user or UpdraftCentral was not authorized yet
[74] Fix | Delete
// to access the user's analytics data, thus, we're clearing
[75] Fix | Delete
// any previously stored user access so we're doing some housekeeping here.
[76] Fix | Delete
$this->clear_user_access();
[77] Fix | Delete
}
[78] Fix | Delete
[79] Fix | Delete
// Wrap and combined information for the requesting
[80] Fix | Delete
// client's consumption
[81] Fix | Delete
$result = array(
[82] Fix | Delete
'ga_installed' => $installed,
[83] Fix | Delete
'tracking_id' => $tracking_id,
[84] Fix | Delete
'client_id' => $this->client_id,
[85] Fix | Delete
'redirect_uri' => $this->auth_endpoint,
[86] Fix | Delete
'scope' => $this->scope,
[87] Fix | Delete
'access_token' => $access_token,
[88] Fix | Delete
'endpoint' => $this->endpoint
[89] Fix | Delete
);
[90] Fix | Delete
[91] Fix | Delete
} catch (Exception $e) {
[92] Fix | Delete
$result = array('error' => true, 'message' => 'generic_response_error', 'values' => array($e->getMessage()));
[93] Fix | Delete
}
[94] Fix | Delete
[95] Fix | Delete
return $this->_response($result);
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
/**
[99] Fix | Delete
* Extracts Google Tracking ID from contents rendered through the "wp_head" and "wp_footer" action hooks
[100] Fix | Delete
*
[101] Fix | Delete
* @internal
[102] Fix | Delete
* @return array $result An array containing the result of the extraction.
[103] Fix | Delete
*/
[104] Fix | Delete
private function extract_tracking_id() {
[105] Fix | Delete
[106] Fix | Delete
// Define result array
[107] Fix | Delete
$result = array();
[108] Fix | Delete
[109] Fix | Delete
// Retrieve header content
[110] Fix | Delete
ob_start();
[111] Fix | Delete
do_action('wp_head');
[112] Fix | Delete
$header_content = ob_get_clean();
[113] Fix | Delete
[114] Fix | Delete
// Extract analytics information if available.
[115] Fix | Delete
$output = $this->parse_content($header_content);
[116] Fix | Delete
$result['installed'] = $output['installed'];
[117] Fix | Delete
$result['tracking_id'] = $output['tracking_id'];
[118] Fix | Delete
[119] Fix | Delete
// If it was not found, then now try the footer
[120] Fix | Delete
if (empty($result['tracking_id'])) {
[121] Fix | Delete
// Retrieve footer content
[122] Fix | Delete
ob_start();
[123] Fix | Delete
do_action('wp_footer');
[124] Fix | Delete
$footer_content = ob_get_clean();
[125] Fix | Delete
$output = $this->parse_content($footer_content);
[126] Fix | Delete
$result['installed'] = $output['installed'];
[127] Fix | Delete
$result['tracking_id'] = $output['tracking_id'];
[128] Fix | Delete
}
[129] Fix | Delete
[130] Fix | Delete
if (!empty($result['tracking_id'])) {
[131] Fix | Delete
set_transient($this->tracking_id_key, $result['tracking_id'], $this->expiration);
[132] Fix | Delete
}
[133] Fix | Delete
[134] Fix | Delete
return $result;
[135] Fix | Delete
}
[136] Fix | Delete
[137] Fix | Delete
/**
[138] Fix | Delete
* Gets access token
[139] Fix | Delete
*
[140] Fix | Delete
* Validates whether the system currently have a valid token to use when connecting to Google Analytics API.
[141] Fix | Delete
* If not, then it will send a token request based on the authorization code we stored during the
[142] Fix | Delete
* authorization phase. Otherwise, it will return an empty token.
[143] Fix | Delete
*
[144] Fix | Delete
* @return array $result An array containing the Google Analytics API access token.
[145] Fix | Delete
*/
[146] Fix | Delete
public function get_access_token() {
[147] Fix | Delete
[148] Fix | Delete
try {
[149] Fix | Delete
[150] Fix | Delete
// Loads or request a valid token to use
[151] Fix | Delete
$access_token = $this->_get_token();
[152] Fix | Delete
[153] Fix | Delete
if (!empty($access_token)) {
[154] Fix | Delete
$result = array('access_token' => $access_token);
[155] Fix | Delete
} else {
[156] Fix | Delete
$result = array('error' => true, 'message' => 'ga_token_retrieval_failed', 'values' => array());
[157] Fix | Delete
}
[158] Fix | Delete
[159] Fix | Delete
} catch (Exception $e) {
[160] Fix | Delete
$result = array('error' => true, 'message' => 'generic_response_error', 'values' => array($e->getMessage()));
[161] Fix | Delete
}
[162] Fix | Delete
[163] Fix | Delete
return $this->_response($result);
[164] Fix | Delete
}
[165] Fix | Delete
[166] Fix | Delete
/**
[167] Fix | Delete
* Clears any previously stored user access
[168] Fix | Delete
*
[169] Fix | Delete
* @return bool
[170] Fix | Delete
*/
[171] Fix | Delete
public function clear_user_access() {
[172] Fix | Delete
return delete_option($this->access_key);
[173] Fix | Delete
}
[174] Fix | Delete
[175] Fix | Delete
/**
[176] Fix | Delete
* Saves user is and access token received from the auth server
[177] Fix | Delete
*
[178] Fix | Delete
* @param array $query Parameter array containing the user id and access token from the auth server.
[179] Fix | Delete
* @return array $result An array containing a "success" or "failure" message as a result of the current process.
[180] Fix | Delete
*/
[181] Fix | Delete
public function save_user_access($query) {
[182] Fix | Delete
[183] Fix | Delete
try {
[184] Fix | Delete
[185] Fix | Delete
$token = get_option($this->access_key, false);
[186] Fix | Delete
$result = array();
[187] Fix | Delete
[188] Fix | Delete
if (false === $token) {
[189] Fix | Delete
$token = array(
[190] Fix | Delete
'user_id' => base64_decode(urldecode($query['user_id'])),
[191] Fix | Delete
'access_token' => base64_decode(urldecode($query['access_token']))
[192] Fix | Delete
);
[193] Fix | Delete
[194] Fix | Delete
if (false !== update_option($this->access_key, $token)) {
[195] Fix | Delete
$result = array('error' => false, 'message' => 'ga_access_saved', 'values' => array());
[196] Fix | Delete
} else {
[197] Fix | Delete
$result = array('error' => true, 'message' => 'ga_access_saving_failed', 'values' => array($query['access_token']));
[198] Fix | Delete
}
[199] Fix | Delete
}
[200] Fix | Delete
[201] Fix | Delete
} catch (Exception $e) {
[202] Fix | Delete
$result = array('error' => true, 'message' => 'generic_response_error', 'values' => array($e->getMessage()));
[203] Fix | Delete
}
[204] Fix | Delete
[205] Fix | Delete
return $this->_response($result);
[206] Fix | Delete
}
[207] Fix | Delete
[208] Fix | Delete
/**
[209] Fix | Delete
* Saves the tracking code/id manually (user input)
[210] Fix | Delete
*
[211] Fix | Delete
* @param array $query Parameter array containing the tracking code/id to save.
[212] Fix | Delete
* @return array $result An array containing the result of the process.
[213] Fix | Delete
*/
[214] Fix | Delete
public function save_tracking_id($query) {
[215] Fix | Delete
try {
[216] Fix | Delete
$tracking_id = $query['tracking_id'];
[217] Fix | Delete
$saved = false;
[218] Fix | Delete
[219] Fix | Delete
if (!empty($tracking_id)) {
[220] Fix | Delete
$saved = set_transient($this->tracking_id_key, $tracking_id, $this->expiration);
[221] Fix | Delete
}
[222] Fix | Delete
[223] Fix | Delete
$result = array('saved' => $saved);
[224] Fix | Delete
} catch (Exception $e) {
[225] Fix | Delete
$result = array('error' => true, 'message' => 'generic_response_error', 'values' => array($e->getMessage()));
[226] Fix | Delete
}
[227] Fix | Delete
[228] Fix | Delete
return $this->_response($result);
[229] Fix | Delete
}
[230] Fix | Delete
[231] Fix | Delete
/**
[232] Fix | Delete
* Retrieves any available access token either previously saved info or
[233] Fix | Delete
* from a new request from the Google Server.
[234] Fix | Delete
*
[235] Fix | Delete
* @internal
[236] Fix | Delete
* @return string $authorization_code
[237] Fix | Delete
*/
[238] Fix | Delete
private function _get_token() {
[239] Fix | Delete
[240] Fix | Delete
// Retrieves the tracking code/id if available
[241] Fix | Delete
$tracking_id = $this->get_tracking_id();
[242] Fix | Delete
$access_token = '';
[243] Fix | Delete
[244] Fix | Delete
$token = get_option($this->access_key, false);
[245] Fix | Delete
if (false !== $token) {
[246] Fix | Delete
$access_token = isset($token['access_token']) ? $token['access_token'] : '';
[247] Fix | Delete
$user_id = isset($token['user_id']) ? $token['user_id'] : '';
[248] Fix | Delete
[249] Fix | Delete
if ((!empty($access_token) && !$this->_token_valid($access_token)) || (!empty($user_id) && empty($access_token) && !empty($tracking_id))) {
[250] Fix | Delete
if (!empty($user_id)) {
[251] Fix | Delete
$args = array(
[252] Fix | Delete
'headers' => apply_filters('updraftplus_auth_headers', array())
[253] Fix | Delete
);
[254] Fix | Delete
[255] Fix | Delete
$response = wp_remote_get($this->auth_endpoint.'?user_id='.$user_id.'&code=ud_googleanalytics_code', $args);
[256] Fix | Delete
if (is_wp_error($response)) {
[257] Fix | Delete
throw new Exception($response->get_error_message());
[258] Fix | Delete
} else {
[259] Fix | Delete
if (is_array($response)) {
[260] Fix | Delete
[261] Fix | Delete
$body = json_decode($response['body'], true);
[262] Fix | Delete
$token_response = array();
[263] Fix | Delete
[264] Fix | Delete
if (is_array($body) && !isset($body['error'])) {
[265] Fix | Delete
$token_response = json_decode(base64_decode($body[0]), true);
[266] Fix | Delete
}
[267] Fix | Delete
[268] Fix | Delete
if (is_array($token_response) && isset($token_response['access_token'])) {
[269] Fix | Delete
$access_token = $token_response['access_token'];
[270] Fix | Delete
} else {
[271] Fix | Delete
// If we don't get any valid response then that would mean that the
[272] Fix | Delete
// permission was already revoked. Thus, we need to re-authorize the
[273] Fix | Delete
// user before using the analytics feature once again.
[274] Fix | Delete
$access_token = '';
[275] Fix | Delete
}
[276] Fix | Delete
[277] Fix | Delete
$token['access_token'] = $access_token;
[278] Fix | Delete
update_option($this->access_key, $token);
[279] Fix | Delete
}
[280] Fix | Delete
}
[281] Fix | Delete
}
[282] Fix | Delete
}
[283] Fix | Delete
}
[284] Fix | Delete
[285] Fix | Delete
return $access_token;
[286] Fix | Delete
}
[287] Fix | Delete
[288] Fix | Delete
/**
[289] Fix | Delete
* Verifies whether the access token is still valid for use
[290] Fix | Delete
*
[291] Fix | Delete
* @internal
[292] Fix | Delete
* @param string $token The access token to be check and validated
[293] Fix | Delete
* @return bool
[294] Fix | Delete
* @throws Exception If an error has occurred while connecting to the Google Server.
[295] Fix | Delete
*/
[296] Fix | Delete
private function _token_valid($token) {
[297] Fix | Delete
[298] Fix | Delete
$response = wp_remote_get($this->token_info_endpoint.'?access_token='.$token);
[299] Fix | Delete
if (is_wp_error($response)) {
[300] Fix | Delete
throw new Exception($response->get_error_message());
[301] Fix | Delete
} else {
[302] Fix | Delete
if (is_array($response)) {
[303] Fix | Delete
$response = json_decode($response['body'], true);
[304] Fix | Delete
if (!empty($response)) {
[305] Fix | Delete
if (!isset($response['error']) && !isset($response['error_description'])) {
[306] Fix | Delete
return true;
[307] Fix | Delete
}
[308] Fix | Delete
}
[309] Fix | Delete
}
[310] Fix | Delete
}
[311] Fix | Delete
[312] Fix | Delete
return false;
[313] Fix | Delete
}
[314] Fix | Delete
[315] Fix | Delete
/**
[316] Fix | Delete
* Parses and extracts the google analytics information (NEEDED)
[317] Fix | Delete
*
[318] Fix | Delete
* @internal
[319] Fix | Delete
* @param string $content The content to parse
[320] Fix | Delete
* @return array An array containing the status of the process along with the tracking code/id
[321] Fix | Delete
*/
[322] Fix | Delete
private function parse_content($content) {
[323] Fix | Delete
[324] Fix | Delete
$installed = false;
[325] Fix | Delete
$gtm_installed = false;
[326] Fix | Delete
$tracking_id = '';
[327] Fix | Delete
$script_file_found = false;
[328] Fix | Delete
$tracking_id_found = false;
[329] Fix | Delete
[330] Fix | Delete
// Pull google analytics script file(s)
[331] Fix | Delete
preg_match_all('/<script\b[^>]*>([\s\S]*?)<\/script>/i', $content, $scripts);
[332] Fix | Delete
for ($i=0; $i < count($scripts[0]); $i++) {
[333] Fix | Delete
// Check for Google Analytics file
[334] Fix | Delete
if (stristr($scripts[0][$i], 'ga.js') || stristr($scripts[0][$i], 'analytics.js')) {
[335] Fix | Delete
$script_file_found = true;
[336] Fix | Delete
}
[337] Fix | Delete
[338] Fix | Delete
// Check for Google Tag Manager file
[339] Fix | Delete
// N.B. We are not checking for GTM but this check will be useful when
[340] Fix | Delete
// showing the notice to the user if we haven't found Google Analytics
[341] Fix | Delete
// directly being installed on the page.
[342] Fix | Delete
if (stristr($scripts[0][$i], 'gtm.js')) {
[343] Fix | Delete
$gtm_installed = true;
[344] Fix | Delete
}
[345] Fix | Delete
}
[346] Fix | Delete
[347] Fix | Delete
// Pull tracking code
[348] Fix | Delete
preg_match_all('/UA-[0-9]{5,}-[0-9]{1,}/i', $content, $codes);
[349] Fix | Delete
if (count($codes) > 0) {
[350] Fix | Delete
if (!empty($codes[0])) {
[351] Fix | Delete
$tracking_id_found = true;
[352] Fix | Delete
$tracking_id = $codes[0][0];
[353] Fix | Delete
}
[354] Fix | Delete
}
[355] Fix | Delete
[356] Fix | Delete
// If we found both the script and the tracking code then it is safe
[357] Fix | Delete
// to say that Google Analytics (GA) is installed. Thus, we're returning
[358] Fix | Delete
// "true" as a response.
[359] Fix | Delete
if ($script_file_found && $tracking_id_found) {
[360] Fix | Delete
$installed = true;
[361] Fix | Delete
}
[362] Fix | Delete
[363] Fix | Delete
// Return result of process.
[364] Fix | Delete
return array(
[365] Fix | Delete
'installed' => $installed,
[366] Fix | Delete
'gtm_installed' => $gtm_installed,
[367] Fix | Delete
'tracking_id' => $tracking_id
[368] Fix | Delete
);
[369] Fix | Delete
}
[370] Fix | Delete
[371] Fix | Delete
/**
[372] Fix | Delete
* Retrieves the "analytics_tracking_id" transient
[373] Fix | Delete
*
[374] Fix | Delete
* @internal
[375] Fix | Delete
* @return mixed Returns the value of the saved transient. Returns "false" if the transient does not exist.
[376] Fix | Delete
*/
[377] Fix | Delete
private function get_tracking_id() {
[378] Fix | Delete
return get_transient($this->tracking_id_key);
[379] Fix | Delete
}
[380] Fix | Delete
[381] Fix | Delete
[382] Fix | Delete
/**
[383] Fix | Delete
* Returns the current tracking id
[384] Fix | Delete
*
[385] Fix | Delete
* @return array $result An array containing the Google Tracking ID.
[386] Fix | Delete
*/
[387] Fix | Delete
public function get_current_tracking_id() {
[388] Fix | Delete
try {
[389] Fix | Delete
[390] Fix | Delete
// Get current site transient stored for this key
[391] Fix | Delete
$tracking_id = get_transient($this->tracking_id_key);
[392] Fix | Delete
[393] Fix | Delete
// Checks whether we have a valid token
[394] Fix | Delete
$access_token = $this->_get_token();
[395] Fix | Delete
if (empty($access_token)) {
[396] Fix | Delete
$tracking_id = '';
[397] Fix | Delete
}
[398] Fix | Delete
[399] Fix | Delete
if (false === $tracking_id) {
[400] Fix | Delete
$result = $this->extract_tracking_id();
[401] Fix | Delete
} else {
[402] Fix | Delete
$result = array(
[403] Fix | Delete
'installed' => true,
[404] Fix | Delete
'tracking_id' => $tracking_id
[405] Fix | Delete
);
[406] Fix | Delete
}
[407] Fix | Delete
[408] Fix | Delete
} catch (Exception $e) {
[409] Fix | Delete
$result = array('error' => true, 'message' => 'generic_response_error', 'values' => array($e->getMessage()));
[410] Fix | Delete
}
[411] Fix | Delete
[412] Fix | Delete
return $this->_response($result);
[413] Fix | Delete
}
[414] Fix | Delete
[415] Fix | Delete
/**
[416] Fix | Delete
* Clears user access from database
[417] Fix | Delete
*
[418] Fix | Delete
* @return array $result An array containing the "Remove" confirmation whether the action succeeded or not.
[419] Fix | Delete
*/
[420] Fix | Delete
public function remove_user_access() {
[421] Fix | Delete
try {
[422] Fix | Delete
[423] Fix | Delete
// Clear user access
[424] Fix | Delete
$is_cleared = $this->clear_user_access();
[425] Fix | Delete
[426] Fix | Delete
if (false !== $is_cleared) {
[427] Fix | Delete
$result = array('removed' => true);
[428] Fix | Delete
} else {
[429] Fix | Delete
$result = array('error' => true, 'message' => 'user_access_remove_failed', 'values' => array());
[430] Fix | Delete
}
[431] Fix | Delete
[432] Fix | Delete
} catch (Exception $e) {
[433] Fix | Delete
$result = array('error' => true, 'message' => 'generic_response_error', 'values' => array($e->getMessage()));
[434] Fix | Delete
}
[435] Fix | Delete
[436] Fix | Delete
return $this->_response($result);
[437] Fix | Delete
}
[438] Fix | Delete
}
[439] Fix | Delete
[440] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function