'', 'path' => '', 'blog_id' => false, ); if ( is_array( $options ) && !empty( $options ) ) $options = array_merge( $defaults, $options ); else $options = $defaults; // Send new bloginfo with gmt_offset if ( $options['version'] < 3 ) $update_bloginfo = true; $options['version'] = STATS_VERSION; stats_set_options( $options ); if ( $update_bloginfo ) stats_update_bloginfo(); return $options; } function stats_footer() { global $wp_the_query, $current_user; $options = stats_get_options(); if ( !empty($current_user->ID) || empty($options['blog_id']) ) return; $a['blog'] = $options['blog_id']; $a['v'] = 'ext'; if ( $wp_the_query->is_single || $wp_the_query->is_page ) $a['post'] = $wp_the_query->get_queried_object_id(); else $a['post'] = '0'; ?> $v ) $jskvs[] = "$k:'$v'"; return join(',', $jskvs); } function stats_admin_menu() { if ( stats_get_option('blog_id') ) { $hook = add_submenu_page('index.php', __('Site Stats'), __('Site Stats'), 'publish_posts', 'stats', 'stats_reports_page'); add_action("load-$hook", 'stats_reports_load'); } $hook = add_submenu_page('plugins.php', __('WordPress.com Stats Plugin'), __('WordPress.com Stats'), 'manage_options', 'wpstats', 'stats_admin_page'); add_action("load-$hook", 'stats_admin_load'); add_action("admin_head-$hook", 'stats_admin_head'); add_action('admin_notices', 'stats_admin_notices'); } function stats_reports_load() { add_action('admin_head', 'stats_reports_head'); } function stats_reports_head() { ?> 'true', 'proxy' => '', 'page' => 'stats', 'key' => $key, 'day' => $day, 'blog' => $blog_id, 'charset' => get_option('blog_charset'), ); $args = array( 'view' => array('referrers', 'postviews', 'searchterms', 'clicks', 'post', 'table'), 'numdays' => 'int', 'day' => 'date', 'unit' => array(1, 7, 31), 'summarize' => null, 'post' => 'int', 'width' => 'int', 'height' => 'int', 'data' => 'data', ); foreach ( $args as $var => $vals ) { if ( ! isset($_GET[$var]) ) continue; if ( is_array($vals) ) { if ( in_array($_GET[$var], $vals) ) $q[$var] = $_GET[$var]; } elseif ( $vals == 'int' ) { $q[$var] = intval($_GET[$var]); } elseif ( $vals == 'date' ) { if ( preg_match('/^\d{4}-\d{2}-\d{2}$/', $_GET[$var]) ) $q[$var] = $_GET[$var]; } elseif ( $vals == null ) { $q[$var] = ''; } elseif ( $vals == 'data' ) { if ( substr($_GET[$var], 0, 9) == 'index.php' ) $q[$var] = $_GET[$var]; } } if ( isset( $_GET['chart'] ) ) { if ( preg_match('/^[a-z0-9-]+$/', $_GET['chart']) ) $url = "https://dashboard.wordpress.com/wp-includes/charts/{$_GET['chart']}.php"; } else { $url = "https://dashboard.wordpress.com/wp-admin/index.php"; } $url = add_query_arg($q, $url); $get = wp_remote_get($url, array('timeout'=>300)); if ( is_wp_error($get) || empty($get['body']) ) { $http = $_SERVER['https'] ? 'https' : 'http'; $day = $day ? "&day=$day" : ''; echo ""; } else { $body = convert_post_titles($get['body']); $body = convert_swf_urls($body); echo $body; } if ( isset( $_GET['noheader'] ) ) die; } function convert_swf_urls($html) { if ( version_compare($wp_version, '2.8', '<') ) { $path = dirname(plugin_basename(__FILE__)); if ( $path == '.' ) $path = ''; $swf_url = trailingslashit( plugins_url( $path ) ) . 'open-flash-chart.swf?data='; } else { $swf_url = trailingslashit( plugins_url( '', __FILE__) ) . 'open-flash-chart.swf?data='; } $html = preg_replace('!(.*?"; if ( ! preg_match_all("!$pattern!", $html, $matches) ) return $html; $posts = get_posts(array( 'include' => implode(',', $matches[1]), 'post_type' => 'any', 'numberposts' => -1, )); foreach ( $posts as $post ) $stats_posts[$post->ID] = $post; $html = preg_replace_callback("!$pattern!", 'convert_post_title', $html); return $html; } function convert_post_title($matches) { global $stats_posts; $post_id = $matches[1]; if ( isset($stats_posts[$post_id]) ) return ''.get_the_title($post_id).''; return $matches[0]; } function stats_admin_load() { global $plugin_page; if ( ! empty( $_POST['action'] ) && $_POST['_wpnonce'] == wp_create_nonce('stats') ) { switch( $_POST['action'] ) { case 'reset' : stats_set_options(array()); wp_redirect( "plugins.php?page=$plugin_page" ); exit; case 'enter_key' : stats_check_key( $_POST['api_key'] ); wp_redirect( "plugins.php?page=$plugin_page" ); exit; case 'add_or_replace' : $key_check = stats_get_option('key_check'); stats_set_api_key($key_check[0]); if ( isset($_POST['add']) ) { stats_get_blog_id($key_check[0]); } else { extract( parse_url( get_option( 'home' ) ) ); $path = rtrim( $path, '/' ); if ( empty( $path ) ) $path = '/'; $options = stats_get_options(); $options['blog_id'] = intval($_POST['blog_id']); $options['api_key'] = $key_check[0]; $options['host'] = $host; $options['path'] = $path; stats_set_options($options); stats_update_bloginfo(); } if ( stats_get_option('blog_id') ) stats_set_option('key_check', false); wp_redirect( "plugins.php?page=$plugin_page" ); exit; } } $options = stats_get_options(); if ( empty( $options['blog_id']) && empty( $options['key_check'] ) && stats_get_api_key() ) stats_check_key( stats_get_api_key() ); } function stats_admin_notices() { if ( stats_get_api_key() ) return; echo "
" . sprintf(__('WordPress.com Stats needs attention: please enter an API key or disable the plugin.'), "plugins.php?page=wpstats") . "
your Dashboard to see your site stats. If you are asked to log in, use your WordPress.com username and password.'), 'index.php?page=stats'); ?>
' . __('your Global Dashboard') . ''); ?>
query( 'wpStats.check_key', $api_key, stats_get_blog() ); if ( $client->isError() ) { if ( $client->getErrorCode() == -32300 ) $options['error'] = __('Your blog was unable to connect to WordPress.com. Please ask your host for help. (' . $client->getErrorMessage() . ')'); else $options['error'] = $client->getErrorMessage(); stats_set_options( $options ); return false; } else { $options['error'] = false; } $options['key_check'] = $client->getResponse(); stats_set_options($options); return true; } function stats_get_blog_id($api_key) { $options = stats_get_options(); require_once( ABSPATH . WPINC . '/class-IXR.php' ); $client = new IXR_Client( STATS_XMLRPC_SERVER ); extract( parse_url( get_option( 'home' ) ) ); $path = rtrim( $path, '/' ); if ( empty( $path ) ) $path = '/'; $client->query( 'wpStats.get_blog_id', $api_key, stats_get_blog() ); if ( $client->isError() ) { if ( $client->getErrorCode() == -32300 ) $options['error'] = __('Your blog was unable to connect to WordPress.com. Please ask your host for help. (' . $client->getErrorMessage() . ')'); else $options['error'] = $client->getErrorMessage(); stats_set_options( $options ); return false; } else { $options['error'] = false; } $response = $client->getResponse(); $blog_id = isset($response['blog_id']) ? (int) $response['blog_id'] : false; $options[ 'host' ] = $host; $options[ 'path' ] = $path; $options[ 'blog_id' ] = $blog_id; stats_set_options( $options ); stats_set_api_key( $api_key ); return $blog_id; } function stats_activate() { $options = stats_get_options(); if ( empty($options['blog_id']) && $api_key = stats_get_api_key() ) stats_get_blog_id($api_key); } function stats_deactivate() { delete_option('stats_options'); delete_option('stats_dashboard_widget'); } /* Dashboard Stuff: WP >= 2.5 */ function stats_register_dashboard_widget() { if ( ( !$blog_id = stats_get_option('blog_id') ) || !stats_get_api_key() || !current_user_can( 'manage_options' ) ) return; // wp_dashboard_empty: we load in the content after the page load via JS wp_register_sidebar_widget( 'dashboard_stats', __( 'Stats' ), 'wp_dashboard_empty', array( 'width' => 'full' ) ); wp_register_widget_control( 'dashboard_stats', __( 'Stats' ), 'stats_register_dashboard_widget_control', array(), array( 'widget_id' => 'dashboard_stats', ) ); add_action( 'admin_head', 'stats_dashboard_head' ); } function stats_dashboard_widget_options() { $defaults = array( 'chart' => 1, 'top' => 7, 'search' => 7, 'active' => 7 ); if ( ( !$options = get_option( 'stats_dashboard_widget' ) ) || !is_array($options) ) $options = array(); return array_merge( $defaults, $options ); } function stats_register_dashboard_widget_control() { $periods = array( '1' => __('day'), '7' => __('week'), '31' => __('month') ); $intervals = array( '-1' => __('all time'), '1' => __('the past day'), '7' => __('the past week'), '31' => __('the past month'), '90' => __('the past quarter'), '365' => __('the past year') ); $options = stats_dashboard_widget_options(); if ( 'post' == strtolower($_SERVER['REQUEST_METHOD']) && isset( $_POST['widget_id'] ) && 'dashboard_stats' == $_POST['widget_id'] ) { if ( isset($periods[$_POST['chart']]) ) $options['chart'] = $_POST['chart']; foreach ( array( 'top', 'search', 'active' ) as $key ) if ( isset($intervals[$_POST[$key]]) ) $options[$key] = $_POST[$key]; update_option( 'stats_dashboard_widget', $options ); } ?>.
.
.
.
false, 'days' => false, 'limit' => 3, 'post_id' => false, 'summarize' => '' ); $args = wp_parse_args( $args, $defaults ); $args['table'] = $table; $args['blog_id'] = $blog_id; $args['api_key'] = $key; $stats_csv_url = add_query_arg( $args, 'http://stats.wordpress.com/csv.php' ); $key = md5( $stats_csv_url ); // Get cache $stats_cache = get_option( 'stats_cache' ); if ( !$stats_cache || !is_array($stats_cache) ) $stats_cache = array(); // Return or expire this key if ( isset($stats_cache[$key]) ) { $time = key($stats_cache[$key]); if ( time() - $time < 300 ) return $stats_cache[$key][$time]; unset( $stats_cache[$key] ); } $stats_rows = array(); do { if ( !$stats = stats_get_remote_csv( $stats_csv_url ) ) break; $labels = array_shift( $stats ); if ( 0 === stripos( $labels[0], 'error' ) ) break; $stats_rows = array(); for ( $s = 0; isset($stats[$s]); $s++ ) { $row = array(); foreach ( $labels as $col => $label ) $row[$label] = $stats[$s][$col]; $stats_rows[] = $row; } } while(0); // Expire old keys foreach ( $stats_cache as $k => $cache ) if ( !is_array($cache) || 300 < time() - key($cache) ) unset($stats_cache[$k]); // Set cache $stats_cache[$key] = array( time() => $stats_rows ); update_option( 'stats_cache', $stats_cache ); return $stats_rows; } function stats_get_remote_csv( $url ) { $url = clean_url( $url, null, 'url' ); // Yay! if ( ini_get('allow_url_fopen') ) { $fp = @fopen($url, 'r'); if ( $fp ) { //stream_set_timeout($fp, $timeout); // Requires php 4.3 $data = array(); while ( $remote_read = fgetcsv($fp, 1000) ) $data[] = $remote_read; fclose($fp); return $data; } } // Boo - we need to use wp_remote_fopen for maximium compatibility if ( !$csv = wp_remote_fopen( $url ) ) return false; return stats_str_getcsv( $csv ); } // rather than parsing the csv and its special cases, we create a new file and do fgetcsv on it. function stats_str_getcsv( $csv ) { if ( !$temp = tmpfile() ) // tmpfile() automatically unlinks return false; $data = array(); fwrite($temp, $csv, strlen($csv)); fseek($temp, 0); while ( false !== $row = fgetcsv($temp, 1000) ) $data[] = $row; fclose($temp); return $data; } function stats_dashboard_widget_content() { $blog_id = stats_get_option('blog_id'); if ( ( !$width = (int) ( $_GET['width'] / 2 ) ) || $width < 250 ) $width = 370; if ( ( !$height = (int) $_GET['height'] - 36 ) || $height < 230 ) $height = 230; $_width = $width - 5; $_height = $height - ( $GLOBALS['is_winIE'] ? 16 : 5 ); // hack! $options = stats_dashboard_widget_options(); $q = array( 'noheader' => 'true', 'proxy' => '', 'page' => 'stats', 'blog' => $blog_id, 'key' => stats_get_api_key(), 'chart' => '', 'unit' => $options['chart'], 'width' => $_width, 'height' => $_height, ); $url = 'https://dashboard.wordpress.com/wp-admin/index.php'; $url = add_query_arg($q, $url); $get = wp_remote_get($url, array('timeout'=>300)); if ( is_wp_error($get) || empty($get['body']) ) { $http = $_SERVER['https'] ? 'https' : 'http'; $src = clean_url( "$http://dashboard.wordpress.com/wp-admin/index.php?page=estats&blog=$blog_id&noheader=true&chart&unit=$options[chart]&width=$_width&height=$_height" ); echo ""; } else { $body = convert_swf_urls($get['body']); echo $body; } $post_ids = array(); if ( version_compare( '2.7-z', $GLOBALS['wp_version'], '<=' ) ) { $csv_args = array( 'top' => '&limit=8', 'active' => '&limit=5', 'search' => '&limit=5' ); $printf = __( '%s %s Views' ); } else { $csv_args = array( 'top' => '', 'active' => '', 'search' => '' ); $printf = __( '%s, %s views' ); } foreach ( $top_posts = stats_get_csv( 'postviews', "days=$options[top]$csv_args[top]" ) as $post ) $post_ids[] = $post['post_id']; foreach ( $active_posts = stats_get_csv( 'postviews', "days=$options[active]$csv_args[active]" ) as $post ) $post_ids[] = $post['post_id']; // cache get_posts( array( 'include' => join( ',', array_unique($post_ids) ) ) ); $searches = array(); foreach ( $search_terms = stats_get_csv( 'searchterms', "days=$options[search]$csv_args[search]" ) as $search_term ) $searches[] = wp_specialchars($search_term['searchterm']); ?>' . get_the_title( $post['post_id'] ) . '', // '' . $post['post_title'] . '', number_format_i18n( $post['views'] ) ); ?>
' . get_the_title( $post['post_id'] ) . '', // '' . $post['post_title'] . '', number_format_i18n( $post['views'] ) ); ?>