function cacheGet($key) { global $wp_version; if (version_compare($wp_version, '2.8', '>=')) { $trans_id = get_option('gad_trans_id'); if ($trans_id === false) { $trans_id = 0; } return get_transient('gad_cache_' . $trans_id . '_' . md5($key)); } else { $lh = SimpleFileCache::lock(); $filename = sys_get_temp_dir() . '/gad_cache_' . md5($key) . '.dat'; $result = ''; if ($f = @fopen($filename, "r")) { $data = fread($f, filesize($filename)); $result = unserialize($data); fclose($f); } SimpleFileCache::unlock($lh); return $result; } }
function admin_handle_other_options($info_message = '') { if (isset($_POST['SubmitOptions'])) { if (function_exists('current_user_can') && !current_user_can('manage_options')) { die(__('Cheatin’ uh?')); } @SimpleFileCache::clearCache(); if (isset($_POST['ga_forget_pass'])) { delete_option('gad_login_pass'); } if (isset($_POST['ga_forget_all'])) { delete_option('gad_oauth_token'); delete_option('gad_oauth_secret'); delete_option('gad_account_id'); delete_option('gad_display_level'); delete_option('gad_cache_timeout'); delete_option('gad_goal_one'); delete_option('gad_goal_two'); delete_option('gad_goal_three'); delete_option('gad_goal_four'); delete_option('gad_login_email'); delete_option('gad_login_pass'); delete_option('gad_auth_token'); delete_option('gad_disable_post_stats'); $this->admin_plugin_options('Everything Reset'); return; } delete_option('gad_account_id'); add_option('gad_account_id', $_POST['ga_account_id']); if (isset($_POST['ga_forget_auth'])) { delete_option('gad_oauth_token'); delete_option('gad_oauth_secret'); delete_option('gad_auth_token'); $this->admin_plugin_options('Auth Reset'); return; } if (isset($_POST['ga_display_level'])) { delete_option('gad_display_level'); if ($_POST['ga_display_level'] != '') { add_option('gad_display_level', $_POST['ga_display_level']); } } if (isset($_POST['ga_disable_post_stats'])) { add_option('gad_disable_post_stats', 'true'); } else { delete_option('gad_disable_post_stats'); } if (isset($_POST['ga_cache_timeout'])) { delete_option('gad_cache_timeout'); if ($_POST['ga_cache_timeout'] != '') { add_option('gad_cache_timeout', $_POST['ga_cache_timeout']); } } delete_option('gad_goal_one'); delete_option('gad_goal_two'); delete_option('gad_goal_three'); delete_option('gad_goal_four'); if (isset($_POST['ga_goal_one'])) { if ($_POST['ga_goal_one'] != '') { add_option('gad_goal_one', $_POST['ga_goal_one']); } } if (isset($_POST['ga_goal_two'])) { if ($_POST['ga_goal_two'] != '') { add_option('gad_goal_two', $_POST['ga_goal_two']); } } if (isset($_POST['ga_goal_three'])) { if ($_POST['ga_goal_three'] != '') { add_option('gad_goal_three', $_POST['ga_goal_three']); } } if (isset($_POST['ga_goal_four'])) { if ($_POST['ga_goal_four'] != '') { add_option('gad_goal_four', $_POST['ga_goal_four']); } } $info_message = 'Options Saved'; } if (get_option('gad_auth_token') == 'gad_see_oauth') { $ga = new GALib('oauth', NULL, get_option('gad_oauth_token'), get_option('gad_oauth_secret'), '', get_option('gad_cache_timeout') !== false ? get_option('gad_cache_timeout') : 60); } else { $ga = new GALib('client', get_option('gad_auth_token'), NULL, NULL, '', get_option('gad_cache_timeout') !== false ? get_option('gad_cache_timeout') : 60); } $account_hash = $ga->account_query(); if ($ga->isError()) { if ($ga->isAuthError()) { delete_option('gad_auth_token'); // this is removed so login will happen again $this->admin_plugin_options(); return; } else { $ui = new GADAdminOptionsUI(); $ui->error_message = 'Error gathering analytics data from Google: ' . strip_tags($ga->getErrorMessage()); $ui->display_admin_halting_error(); return; } } $ui = new GADAdminOptionsUI(); $ui->info_message = $info_message; $ui->error_message = ''; $ui->display_admin_handle_other_options($account_hash); }
function display_admin_handle_other_options($account_hash) { ?> <div class="wrap" style="padding-top: 50px;"> <?php $this->display_messages(); if (sizeof($account_hash) != 0) { $current_account_id = isset($_POST['ga_account_id']) ? $_POST['ga_account_id'] : get_option('gad_account_id') !== false ? get_option('gad_account_id') : ''; } if (!isset($current_account_id) || $current_account_id == '') { ?> <div class="updated"> <p><b>Note:</b> You will need to select an account and <b>click "Save Changes"</b> before the analytics dashboard will work.</p> </div> <?php } ?> <form action="" method="post"> <table class="form-table"> <tr valign="top"> <th scope="row"><label for="ga_account_id">Available Accounts</label></th> <td> <?php if (sizeof($account_hash) == 0) { echo '<span id="ga_account_id">No accounts available.</span>'; } else { echo '<select id="ga_account_id" name="ga_account_id">'; foreach ($account_hash as $account_id => $account_name) { echo '<option value="' . $account_id . '" ' . ($current_account_id == $account_id ? 'selected' : '') . '>' . $account_name . '</option>'; } echo '</select>'; } ?> </td> </tr> <?php if (get_option('gad_login_pass') !== false) { ?> <tr valign="top"> <th scope="row"><label for="ga_forget_pass">Forget Password</label></th> <td><input name="ga_forget_pass" type="checkbox" id="ga_forget_pass" value="ga_forget_pass" /></td> </tr> <?php } ?> <tr valign="top"> <th scope="row"><label for="ga_forget_auth">Forget Authentication</label></th> <td><input name="ga_forget_auth" type="checkbox" id="ga_forget_auth" value="ga_forget_auth" /></td> </tr> <tr valign="top"> <th scope="row"><label for="ga_forget_all">Forget Everything</label></th> <td><input name="ga_forget_all" type="checkbox" id="ga_forget_all" value="ga_forget_all" /></td> </tr> <tr valign="top"> <th scope="row"><label for="ga_display_level">Dashboard Level</label></th> <td> <?php $ga_display_level = get_option('gad_display_level'); ?> <select name="ga_display_level" id="ga_display_level"> <option value="">None - Admin only</option> <option value="level_7" <?php echo $ga_display_level == 'level_7' ? 'selected' : ''; ?> >Editor</option> <option value="level_2" <?php echo $ga_display_level == 'level_2' ? 'selected' : ''; ?> >Author</option> <option value="level_1" <?php echo $ga_display_level == 'level_1' ? 'selected' : ''; ?> >Contributor</option> <option value="level_0" <?php echo $ga_display_level == 'level_0' ? 'selected' : ''; ?> >Subscriber</option> </select> </td> </tr> <?php if (SimpleFileCache::canCache()) { ?> <tr valign="top"> <th scope="row"><label for="ga_cache_timeout">Cache Timeout (seconds)</label></th> <td><input value="<?php echo get_option('gad_cache_timeout') !== false ? get_option('gad_cache_timeout') : '60'; ?> " name="ga_cache_timeout" id="ga_cache_timeout"/></td> </tr> <?php } else { ?> <tr valign="top"> <th colspan="2"><span style="padding: 10px;" class="error">The configuration of your server will prevent response caching.</span></th> </tr> <?php } ?> <tr valign="top"> <th scope="row"> </th> <td> </td> </tr> <tr valign="top"> <th scope="row"><label for="ga_goal_one">Goal #1 Label</label></th> <td><input value="<?php echo get_option('gad_goal_one'); ?> " name="ga_goal_one" id="ga_goal_one"/></td> </tr> <tr valign="top"> <th scope="row"><label for="ga_goal_two">Goal #2 Label</label></th> <td><input value="<?php echo get_option('gad_goal_two'); ?> " name="ga_goal_two" id="ga_goal_two"/></td> </tr> <tr valign="top"> <th scope="row"><label for="ga_goal_three">Goal #3 Label</label></th> <td><input value="<?php echo get_option('gad_goal_three'); ?> " name="ga_goal_three" id="ga_goal_three"/></td> </tr> <tr valign="top"> <th scope="row"><label for="ga_goal_four">Goal #4 Label</label></th> <td><input value="<?php echo get_option('gad_goal_four'); ?> " name="ga_goal_four" id="ga_goal_four"/></td> </tr> <tr valign="top"> <th scope="row"><label for="ga_disable_post_stats">Disable Post Stats Display</label></th> <td><input name="ga_disable_post_stats" type="checkbox" id="ga_disable_post_stats" value="ga_disable_post_stats" <?php echo get_option('gad_disable_post_stats') == 'true' ? 'checked' : ''; ?> /></td> </tr> </table> <p class="submit"> <input type="submit" name="SubmitOptions" class="button-primary" value="<?php _e('Save Changes'); ?> " /> </p> </form> </div> <?php }
function complex_report_query($start_date, $end_date, $dimensions = array(), $metrics = array(), $sort = array(), $filters = array()) { $url = $this->base_url . 'data'; $url .= '?ids=' . $this->ids; $url .= sizeof($dimensions) > 0 ? '&dimensions=' . join(array_reverse($dimensions), ',') : ''; $url .= sizeof($metrics) > 0 ? '&metrics=' . join($metrics, ',') : ''; $url .= sizeof($sort) > 0 ? '&sort=' . join($sort, ',') : ''; $url .= sizeof($filters) > 0 ? '&filters=' . urlencode(join($filters, ',')) : ''; $url .= '&start-date=' . $start_date; $url .= '&end-date=' . $end_date; if (!SimpleFileCache::isExpired($url, $this->cache_timeout)) { $this->http_code = 200; // We never cache bad requests return SimpleFileCache::cacheGet($url); } else { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->createAuthHeader($url, 'GET'))); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $return = curl_exec($ch); if (curl_errno($ch)) { $this->error_message = curl_error($ch); return false; } $this->http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($this->http_code != 200) { $this->error_message = $return; return false; } else { $xml = simplexml_load_string($return); curl_close($ch); $return_values = array(); foreach ($xml->entry as $entry) { $metrics = array(); foreach ($entry->xpath('dxp:metric') as $metric) { $metric_attributes = $metric->attributes(); $metrics[(string) $metric_attributes['name']] = (string) $metric_attributes['value']; } $last_dimension_var_name = null; foreach ($entry->xpath('dxp:dimension') as $dimension) { $dimension_attributes = $dimension->attributes(); $dimension_var_name = 'dimensions_' . strtr((string) $dimension_attributes['name'], ':', '_'); ${$dimension_var_name} = array(); if ($last_dimension_var_name == null) { ${$dimension_var_name} = array('name' => (string) $dimension_attributes['name'], 'value' => (string) $dimension_attributes['value'], 'children' => $metrics); } else { ${$dimension_var_name} = array('name' => (string) $dimension_attributes['name'], 'value' => (string) $dimension_attributes['value'], 'children' => ${$last_dimension_var_name}); } $last_dimension_var_name = $dimension_var_name; } array_push($return_values, ${$last_dimension_var_name}); } SimpleFileCache::cachePut($url, $return_values, $this->cache_timeout); return $return_values; } } }