Created
August 15, 2012 19:37
-
-
Save jdevalk/3362937 to your computer and use it in GitHub Desktop.
Improved PressTrends tracking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* PressTrends tracking | |
*/ | |
function presstrends_plugin() { | |
// PressTrends Account API Key | |
$api_key = 'n6svrrn650hyckud8ghhs1o497hcm0g1o3s5'; | |
$auth = 'dhs4fy3nhnx5x0y6gkdfwt7fz2wprguqk'; | |
// Start of Metrics | |
global $wpdb; | |
$data = get_transient( 'presstrends_cache_data' ); | |
if ( !$data || $data == '' ) { | |
$api_base = 'http://api.presstrends.io/index.php/api/pluginsites/update/auth/'; | |
$url = $api_base . $auth . '/api/' . $api_key . '/'; | |
$count_posts = wp_count_posts(); | |
$count_pages = wp_count_posts( 'page' ); | |
$comments_count = wp_count_comments(); | |
// wp_get_theme was introduced in 3.4, for compatibility with older versions, let's do a workaround for now. | |
if ( function_exists( 'wp_get_theme' ) ) { | |
$theme_data = wp_get_theme(); | |
$theme_name = urlencode( $theme_data->Name ); | |
} else { | |
$theme_data = get_theme_data( get_stylesheet_directory() . '/style.css' ); | |
$theme_name = $theme_data['Name']; | |
} | |
$plugin_name = '&'; | |
foreach ( get_plugins() as $plugin_info ) { | |
$plugin_name .= $plugin_info['Name'] . '&'; | |
} | |
$plugin_data = get_plugin_data( __FILE__ ); | |
$posts_with_comments = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type='post' AND comment_count > 0" ); | |
$data = array( | |
'url' => stripslashes( str_replace( array( 'http://', '/', ':' ), '', site_url() ) ), | |
'posts' => $count_posts->publish, | |
'pages' => $count_pages->publish, | |
'comments' => $comments_count->total_comments, | |
'approved' => $comments_count->approved, | |
'spam' => $comments_count->spam, | |
'pingbacks' => $wpdb->get_var( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_type = 'pingback'" ), | |
'post_conversion' => ( $count_posts->publish > 0 && $posts_with_comments > 0 ) ? number_format( ( $posts_with_comments / $count_posts->publish ) * 100, 0, '.', '' ) : 0, | |
'theme_version' => $plugin_data['Version'], | |
'theme_name' => $theme_name, | |
'site_name' => str_replace( ' ', '', get_bloginfo( 'name' ) ), | |
'plugins' => count( get_option( 'active_plugins' ) ), | |
'plugin' => urlencode( $plugin_name ), | |
'wpversion' => get_bloginfo( 'version' ), | |
); | |
foreach ( $data as $k => $v ) { | |
$url .= $k . '/' . $v . '/'; | |
} | |
wp_remote_get( $url ); | |
set_transient( 'presstrends_cache_data', $data, 60 * 60 * 24 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
your API key is still there