' . esc_attr( $plugin_name ) . ''; } } if ( empty( $action ) && function_exists( 'is_main_site' ) && is_main_site() ) { $action = '' . esc_attr( $plugin_name ) . ''; } return $action; } function stats_display_nag_on_plugin_page() { if ( 8 <= STATS_VERSION && strpos( $_SERVER['REQUEST_URI'], 'plugins.php' ) ) stats_display_jetpack_nag(); } function stats_link_plugin_meta( $links, $file ) { $plugin = plugin_basename( __FILE__ ); // create link if ( $file == $plugin ) { if ( file_exists( WP_PLUGIN_DIR . '/jetpack' ) ) $message = __( 'Enable Jetpack Now!', 'stats' ); else $message = __( 'Get Jetpack Now!', 'stats' ); $install_link = stats_is_plugin_available( 'jetpack', $message ); if ( empty( $install_link ) ) $install_link = sprintf( '%2$s', 'http://downloads.wordpress.org/plugin/jetpack.latest-stable.zip', __( 'Get Jetpack Now!', 'stats' ) ); return array_merge( $links, array( $install_link ) ); } return $links; } function stats_admin_styles() { wp_enqueue_style( 'jetpack', plugins_url( '_inc/jetpack.css', __FILE__ ), false, '20110824' ); wp_enqueue_script( 'thickbox' ); wp_enqueue_style( 'thickbox' ); } function stats_display_jetpack_nag() { static $shown = false; if ( $shown ) { return; } $options = stats_get_options(); if ( ! $options['dismiss_jetpack_nag'] && ! class_exists( 'Jetpack' ) ) { $shown = true; if ( file_exists( WP_PLUGIN_DIR . '/jetpack' ) ) $message = __( 'Enable Jetpack Now!', 'stats' ); else $message = __( 'Get Jetpack Now!', 'stats' ); $install_link = stats_is_plugin_available( 'jetpack', $message, 'button-primary', 'wpcom-connect' ); if ( empty( $install_link ) ) $install_link = sprintf( '%2$s', 'http://downloads.wordpress.org/plugin/jetpack.latest-stable.zip', __( 'Get Jetpack Now!', 'stats' ) ); ?>
'', 'path' => '', 'blog_id' => false, 'admin_bar' => true, 'wp_me' => true, 'roles' => array('administrator','editor','author'), 'reg_users' => false, 'footer' => false, 'dismiss_jetpack_nag' => 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(); echo ""; if ( !$options['footer'] ) stats_set_option('footer', true); if ( empty($options['blog_id']) ) return; if ( !$options['reg_users'] && !empty($current_user->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'; $http = is_ssl() ? 'https' : 'http'; ?> $v ) $jskvs[] = "$k:'$v'"; return join(',', $jskvs); } function stats_admin_menu() { global $current_user; $roles = stats_get_option('roles'); $cap = 'administrator'; foreach ( $roles as $role ) { if ( current_user_can($role) ) { $cap = $role; break; } } if ( stats_get_option('blog_id') ) { $hook = add_submenu_page('index.php', __('Site Stats', 'stats'), __('Site Stats', 'stats'), $cap, 'stats', 'stats_reports_page'); add_action("load-$hook", 'stats_reports_load'); } $parent = stats_admin_parent(); $hook = add_submenu_page($parent, __('WordPress.com Stats Plugin', 'stats'), __('WordPress.com Stats', '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'); if ( ! class_exists( 'Jetpack' ) ) { add_action( "admin_print_styles", 'stats_admin_styles' ); add_action( 'admin_head', 'stats_display_nag_on_plugin_page' ); add_filter( 'plugin_row_meta', 'stats_link_plugin_meta', 10, 2 ); } } function stats_admin_parent() { if ( function_exists( 'is_multisite' ) && is_multisite() ) { $menus = get_site_option( 'menu_items' ); if ( isset($menus['plugins']) && $menus['plugins'] ) return 'plugins.php'; else return 'options-general.php'; } else { return 'plugins.php'; } } function stats_admin_path() { $parent = stats_admin_parent(); return "$parent?page=wpstats"; } function stats_reports_load() { add_action('admin_head', 'stats_reports_head'); wp_enqueue_script('jquery'); wp_enqueue_script('postbox'); } function stats_reports_head() { ?> = mt_rand( 0, 100 ) ) stats_display_jetpack_nag(); if ( isset( $_GET['dashboard'] ) ) return stats_dashboard_widget_content(); $blog_id = stats_get_option('blog_id'); $key = stats_get_api_key(); $day = isset( $_GET['day'] ) && preg_match( '/^\d{4}-\d{2}-\d{2}$/', $_GET['day'] ) ? $_GET['day'] : false; $q = array( 'noheader' => 'true', 'proxy' => '', 'page' => 'stats', 'key' => $key, 'day' => $day, 'blog' => $blog_id, 'charset' => get_option('blog_charset'), 'color' => get_user_option('admin_color', 3), 'ssl' => is_ssl(), ); $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($_REQUEST[$var]) ) continue; if ( is_array($vals) ) { if ( in_array($_REQUEST[$var], $vals) ) $q[$var] = $_REQUEST[$var]; } elseif ( $vals == 'int' ) { $q[$var] = intval($_REQUEST[$var]); } elseif ( $vals == 'date' ) { if ( preg_match('/^\d{4}-\d{2}-\d{2}$/', $_REQUEST[$var]) ) $q[$var] = $_REQUEST[$var]; } elseif ( $vals == null ) { $q[$var] = ''; } elseif ( $vals == 'data' ) { if ( substr($_REQUEST[$var], 0, 9) == 'index.php' ) $q[$var] = $_REQUEST[$var]; } } if ( isset( $_REQUEST['chart'] ) ) { if ( preg_match('/^[a-z0-9-]+$/', $_REQUEST['chart']) ) $url = "http://dashboard.wordpress.com/wp-includes/charts/{$_GET['chart']}.php"; } else { $url = "http://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 { if ( !empty( $get['headers']['content-type'] ) ) { $type = $get['headers']['content-type']; if ( substr($type, 0, 5) == 'image' ) { header('Content-Type: ' . $type); die($get['body']); } } $body = stats_convert_post_titles($get['body']); $body = stats_convert_swf_urls($body); $body = stats_convert_chart_urls($body); $body = stats_convert_image_urls($body); echo $body; } if ( isset( $_GET['noheader'] ) ) die; } function stats_convert_image_urls($html) { $url = ( is_ssl() ? 'https' : 'http' ) . '://dashboard.wordpress.com'; $html = preg_replace('|(["\'])(/i/stats.+)\\1|', '$1'.$url.'$2$1', $html); return $html; } function stats_convert_chart_urls($html) { $html = preg_replace('|https?://[-.a-z0-9]+/wp-includes/charts/([-.a-z0-9]+).php|', '?page=stats&noheader&chart=$1', $html); return $html; } function stats_convert_swf_urls($html) { global $wp_version; 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!", 'stats_convert_post_title', $html); return $html; } function stats_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() { if ( ! empty( $_REQUEST['action'] ) && $_REQUEST['_wpnonce'] == wp_create_nonce('stats') ) { switch( $_REQUEST['action'] ) { case 'reset' : stats_set_options(array()); wp_redirect( stats_admin_path() ); exit; case 'enter_key' : stats_check_key( $_POST['api_key'] ); wp_redirect( stats_admin_path() ); 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(); if ( isset($_POST['recover']) ) $options['blog_id'] = intval($_POST['recoverblog']); else $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( stats_admin_path() ); exit; case 'save_options' : $options = stats_get_options(); $options['admin_bar'] = isset($_POST['admin_bar']) && $_POST['admin_bar']; $options['wp_me'] = isset($_POST['wp_me']) && $_POST['wp_me']; $options['reg_users'] = isset($_POST['reg_users']) && $_POST['reg_users']; $options['dismiss_jetpack_nag'] = isset( $_POST['dismiss_jetpack_nag'] ) && $_POST['dismiss_jetpack_nag']; $options['roles'] = array('administrator'); foreach ( get_editable_roles() as $role => $details ) if ( isset($_POST["role_$role"]) && $_POST["role_$role"] ) $options['roles'][] = $role; stats_set_options($options); wp_redirect( stats_admin_path() ); 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() { stats_notice_blog_id(); // stats_notice_footer(); } function stats_notice_blog_id() { if ( stats_get_api_key() || isset($_GET['page']) && $_GET['page'] == 'wpstats' ) return; // Skip the notice if plugin activated network-wide. if ( function_exists('is_plugin_active_for_network') && is_plugin_active_for_network(plugin_basename(__FILE__)) ) return; echo "" . sprintf(__('WordPress.com Stats needs attention: please enter an API key or disable the plugin.', 'stats'), stats_admin_path()) . "
" . __('WordPress.com Stats is unable to work properly because your theme seems to lack the necessary footer code. Usually this can be fixed by adding the following code just before </body> in footer.php:', 'stats') . "
<?php wp_footer(); ?>
enter the correct API key.', 'stats'), $options['key_check'][0], $options['key_check'][1], wp_nonce_url('?page=wpstats&action=reset', 'stats')); ?>
your Dashboard to see your site stats.', 'stats'), 'index.php?page=stats'); ?>
your WordPress.com dashboard.', 'stats'), $options['blog_id']); ?>
your Global Dashboard to see your site stats.', 'stats'), 'https://dashboard.wordpress.com/wp-admin/index.php?page=stats&blog=' . $options['blog_id']); ?>
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() . ')', 'stats'); 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() . ')', 'stats'); 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() { // Trigger footer test wp_remote_get(get_bloginfo('siteurl')); } function stats_uninstall() { 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', 'stats'), 'wp_dashboard_empty', array( 'width' => 'full' ) ); wp_register_widget_control( 'dashboard_stats', __('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' => 1, 'search' => 7, 'active' => 7 ); if ( ( !$options = get_option( 'stats_dashboard_widget' ) ) || !is_array($options) ) $options = array(); // Ignore obsolete option values $intervals = array(1, 7, 31, 90, 365); foreach ( array('top', 'search', 'active') as $key ) if ( isset($options[$key]) && !in_array($options[$key], $intervals) ) unset($options[$key]); return array_merge( $defaults, $options ); } function stats_register_dashboard_widget_control() { $periods = array( '1' => __('day', 'stats'), '7' => __('week', 'stats'), '31' => __('month', 'stats') ); $intervals = array( '1' => __('the past day', 'stats'), '7' => __('the past week', 'stats'), '31' => __('the past month', 'stats'), '90' => __('the past quarter', 'stats'), '365' => __('the past year', 'stats') ); $options = stats_dashboard_widget_options(); $defaults = array( 'top' => 1, 'search' => 7, 'active' => 7, ); 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]; else $options[$key] = $defaults[$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 ( !isset( $_GET['width'] ) || ( !$width = (int) ( $_GET['width'] / 2 ) ) || $width < 250 ) $width = 370; if ( !isset( $_GET['height'] ) || ( !$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 = 'http://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 = stats_convert_post_titles($get['body']); $body = stats_convert_swf_urls($body); $body = stats_convert_chart_urls($body); $body = stats_convert_image_urls($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' ); /* translators: Stats dashboard widget postviews list: "$post_title $views Views" */ $printf = __( '%1$s %2$s Views' , 'stats'); } else { $csv_args = array( 'top' => '', 'active' => '', 'search' => '' ); /* translators: Stats dashboard widget postviews list: "$post_title, $views Views" */ $printf = __( '%1$s, %2$s views' , 'stats'); } 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[] = esc_html($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'] ) ); ?>