Skip to content
Permalink
Browse files

Got sitemap stats graph working on admin page and added styles.

The sitemap stats graph on the admin page now fetches data via AJAX every
5 minutes. By default shows the last 10 days of indexed url counts.
  • Loading branch information...
Mobius5150 committed Feb 12, 2014
1 parent 62099db commit a16b85ac328e3c2b9efc34a3c5e5dc16efc2e9e9
Showing with 220 additions and 9 deletions.
  1. +26 −0 css/style.css
  2. +135 −0 js/msm-sitemap-admin.js
  3. +59 −9 msm-sitemap.php
@@ -33,4 +33,30 @@
height: 50px;
text-align: center;
width: 100%;
}

.stats-footer span {
vertical-align: middle;
}

.stats-placeholder {
background: #fff;
border: 1px solid #ddd;
height: 250px;
width: 600px;
}

#stats-graph-summary {
color: #999999;
display: none;
font-style: italic;
}

#tooltip {
position: absolute;
display: none;
border: 1px solid #ddd;
padding: 2px;
background-color: #f5f5f5;
opacity: 0.80;
}
@@ -0,0 +1,135 @@
jQuery( document ).ready( function() {
// Setup the blank flot plot
var placeholder = jQuery( '.stats-placeholder' );

// The largest value shown on the graph
var max = { date: '', count: -1 };

// Move the tooltip to body
jQuery( '#tooltip' ).appendTo( 'body' );

var plot_options = {
xaxis: {
mode: 'time',
timeformat: '%b %e',
color: '#ddd',
tickColor: 'rgba(255, 255, 255, 0)',
},
yaxis: {
color: '#ddd',
tickColor: '#ddd',
tickDecimals: 0,
tickFormatter: function ( v, a ) { return parseInt( v ); },
},
grid: {
show: true,
color: '#ddd',
borderWidth: 1,
margin: 15,
labelMargin: 10,
hoverable: true,
autoHighlight: true,
},
colors: [ '#ececec' ],

};

var data = [
{
data: [],
bars: {
align: "center",
barWidth: 12 * 60 * 60 * 1000,
borderColor: '#ececec',
fill: true,
fillColor: '#ececec',
show: true,
},
highlightColor: '#2EA2CC',
}
];

jQuery.plot( placeholder, data, plot_options );

// Start querying the server for sitemap counts
msm_query_sitemap_counts();

function msm_query_sitemap_counts() {
var data = {
action: 'msm-sitemap-get-sitemap-counts',
_wpnonce: jQuery( '#_wpnonce' ).val(),
};

jQuery.ajax( {
url: ajaxurl,
type: 'GET',
data: data,
dataType: 'json',
success: msm_sitemap_counts_received
} );

// Refresh to get update counts
setTimeout( msm_query_sitemap_counts, 60000 );
}

function msm_sitemap_counts_received( response ) {
// Update the top stats
jQuery( '#sitemap-count' ).html( response['total_sitemaps'] );
jQuery( '#sitemap-indexed-url-count' ).html( response['total_indexed_urls'] );

var formatted_data = [];
for ( var e in response['sitemap_indexed_urls'] ) {
formatted_data.push( [ new Date(e).getTime(), response['sitemap_indexed_urls'][e], e ] );

// Check for a maximum
if ( response['sitemap_indexed_urls'][e] > max.count ) {
max.count = response['sitemap_indexed_urls'][e];
max.date = e;
}
}

// Update the plot
data[0].data = formatted_data;
jQuery.plot( placeholder, data, plot_options );

// Update the maxima information
update_stats_maximum();
}

function update_stats_maximum() {
jQuery( '#stats-graph-max' ).html( max.count );
jQuery( '#stats-graph-max-date' ).html( max.date );
jQuery( '#stats-graph-num-days' ).html( data[0].data.length );

jQuery( '#stats-graph-summary' ).fadeIn();
}

function show_tooltip( x, y, contents ) {
jQuery( '#tooltip' ).stop();
jQuery( '#tooltip .content' ).html( contents );
jQuery( '#tooltip' ).css( {
top: y + plot_options.grid.margin,
left: x + plot_options.grid.margin,
} ).fadeIn( 'fast' );
}

var previous_point = null;
placeholder.bind( 'plothover', function ( event, pos, item ) {
if ( item ) {
if ( previous_point != item.dataIndex ) {
previous_point = item.dataIndex;

// Get and format the data values
var x = item.series.xaxis.tickFormatter( item.datapoint[0], item.series.xaxis );
var y = item.series.yaxis.tickFormatter( item.datapoint[1], item.series.yaxis );

show_tooltip( item.pageX, item.pageY, x + ': ' + y );
}
} else {
jQuery( '#tooltip' ).stop();
jQuery( '#tooltip' ).fadeOut( 'fast' );
previous_point = null;
}
});

} );
@@ -64,9 +64,14 @@ public static function sitemap_init() {
add_filter( 'robots_txt', array( __CLASS__, 'robots_txt' ), 10, 2 );
add_action( 'admin_menu', array( __CLASS__, 'metro_sitemap_menu' ) );
add_action( 'msm_cron_update_sitemap', array( __CLASS__, 'update_sitemap_from_modified_posts' ) );
add_action( 'wp_ajax_msm-sitemap-get-sitemap-counts', array( __CLASS__, 'ajax_get_sitemap_counts' ) );
wp_register_script( 'msm-sitemap-admin', plugins_url( '/js/msm-sitemap-admin.js', __FILE__ ), array( 'jquery', 'flot' ) );
wp_register_script( 'flot-time', plugins_url( '/js/flot/jquery.flot.time.js', __FILE__ ), array( 'jquery', 'flot' ) );
wp_register_script( 'flot', plugins_url( '/js/flot/jquery.flot.js', __FILE__ ), array( 'jquery' ) );
wp_register_style( 'msm-sitemap-css', plugins_url( 'css/style.css', __FILE__ ) );
wp_register_style( 'noticons', '//s0.wordpress.com/i/noticons/noticons.css' );
}
/**
@@ -78,8 +83,28 @@ public static function metro_sitemap_menu() {
}
public static function add_admin_scripts() {
wp_enqueue_script( 'flot' );
wp_enqueue_script( 'msm-sitemap-admin' );
wp_enqueue_script( 'flot-time' );
wp_enqueue_style( 'msm-sitemap-css' );
wp_enqueue_style( 'noticons' );
}
public static function ajax_get_sitemap_counts() {
check_admin_referer( 'msm-sitemap-action' );
$n = 10;
if ( isset( $_REQUEST['num_days'] ) ) {
$n = intval( $_REQUEST['num_days'] );
}
$data = array(
'total_indexed_urls' => number_format( Metro_Sitemap::get_total_indexed_url_count() ),
'total_sitemaps' => number_format( Metro_Sitemap::count_sitemaps() ),
'sitemap_indexed_urls' => self::get_recent_sitemap_url_counts( $n ),
);
die( json_encode( $data ) );
}
/**
@@ -142,16 +167,20 @@ public static function render_sitemap_options_page() {
printf( __( '<p><b>Years to process:</b> %s </p>' ), implode( ',', $years_to_process ) );
}
// $sitemap_update_next_run
//<div><em><?php printf( __( 'Sitemaps will be updated in %s.', 'metro-sitemaps' ), human_time_diff( $sitemap_update_next_run ) ); </em></div>
?>
<p><strong><?php _e( 'Last updated:', 'metro-sitemaps' ); ?></strong> <?php echo human_time_diff( $sitemap_update_last_run ); ?> ago</p>
<p><strong><?php _e( 'Next update:', 'metro-sitemaps' ); ?></strong> <?php echo $modified_posts_count . ' ' . $modified_posts_label; ?> will be updated in <?php echo human_time_diff( $sitemap_update_next_run ); ?></p>

<div class="stats-container">
<div class="stats-box"><strong><?php echo number_format( Metro_Sitemap::count_sitemaps() ); ?></strong><?php _e( 'Sitemaps', 'metro-sitemaps' ); ?></div>
<div class="stats-box"><strong><?php echo number_format( Metro_Sitemap::get_total_indexed_url_count() ); ?></strong><?php _e( 'Indexed URLs', 'metro-sitemaps' ); ?></div>
<div class="stats-footer"><?php _e( 'Updated', 'metro-sitemaps' ); ?> <strong><?php echo human_time_diff( $sitemap_update_last_run ); ?> <?php _e( 'ago', 'metro-sitemaps' ) ?></strong></div>
<div class="stats-box"><strong id="sitemap-count"><?php echo number_format( Metro_Sitemap::count_sitemaps() ); ?></strong><?php _e( 'Sitemaps', 'metro-sitemaps' ); ?></div>
<div class="stats-box"><strong id="sitemap-indexed-url-count"><?php echo number_format( Metro_Sitemap::get_total_indexed_url_count() ); ?></strong><?php _e( 'Indexed URLs', 'metro-sitemaps' ); ?></div>
<div class="stats-footer"><span><span class="noticon noticon-time"></span><?php _e( 'Updated', 'metro-sitemaps' ); ?> <strong><?php echo human_time_diff( $sitemap_update_last_run ); ?> <?php _e( 'ago', 'metro-sitemaps' ) ?></strong></span></div>
</div>

<h3><?php _e( 'Latest Sitemaps', 'metro-sitemaps' ); ?></h3>
<div class="stats-container stats-placeholder"></div>
<div id="stats-graph-summary"><?php printf( __( 'Max: %s on %s. Showing the last %s days.', 'metro-sitemaps' ), '<span id="stats-graph-max"></span>', '<span id="stats-graph-max-date"></span>', '<span id="stats-graph-num-days"></span>' ); ?></div>

<h3><?php _e( 'Generate', 'metro-sitemaps' ); ?></h3>
<form action="<?php echo menu_page_url( 'metro-sitemap', false ) ?>" method="post" style="float: left;">
<?php wp_nonce_field( 'msm-sitemap-action' ); ?>
<?php foreach ( $actions as $action ):
@@ -160,6 +189,7 @@ public static function render_sitemap_options_page() {
<?php endforeach; ?>
</form>
</div>
<div id="tooltip"><strong class="content"></strong> <?php _e( 'indexed urls', 'metro-sitemaps' ); ?></div>
<?php
}
@@ -195,7 +225,27 @@ public static function count_sitemaps() {
public static function get_total_indexed_url_count() {
return intval( get_option( 'msm_sitemap_indexed_url_count', 0 ) );
}
/**
* Returns the $n most recent sitemap indexed url counts.
*
* @param int $n The number of days of sitemap stats to grab.
* @return array An array of sitemap stats
*/
public static function get_recent_sitemap_url_counts( $n = 7 ) {
$stats = array();
for ( $i = 0; $i < $n; $i++ ) {
$date = date( 'Y-m-d', strtotime( "-$i days" ) );
list( $year, $month, $day ) = explode( '-', $date );
$stats[$date] = self::get_indexed_url_count( $year, $month, $day );
}
return $stats;
}
public static function is_blog_public() {
return ( 1 == get_option( 'blog_public' ) );
}

0 comments on commit a16b85a

Please sign in to comment.
You can’t perform that action at this time.