posts . "` WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date ASC LIMIT 1";
$startingdate = (string)current( $wpdb->get_col( $query_first_post, 0 ) );
}
// Elapsed days
function date_diff_days($date1, $date2) {
$s = strtotime( $date2 ) - strtotime( $date1 );
$d = intval( $s / 86400 ) + 1;
return $d;
}
$elapseddays = date_diff_days( $startingdate, date( 'Y-m-d H:i:s' ) );
// Queries SQL
$queries = array(
// Date of first post
//'first_post' => "SELECT post_date FROM `" . $wpdb->posts . "` WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date ASC LIMIT 1",
// 5 posts which generate the most comments
'comments_per_post' => "SELECT ID AS id, post_title, post_date, comment_count FROM `" . $wpdb->posts . "` WHERE post_type = 'post' AND post_status = 'publish' AND comment_count > 0 AND post_date >= '" . $startingdate . "' ORDER BY comment_count DESC LIMIT 5",
// When do your posts generate the most comments?
'comments_per_day' => "SELECT COUNT(c.comment_ID) AS count, DATE_FORMAT(c.comment_date, '%w') AS day, p.post_date FROM `" . $wpdb->comments . "` c LEFT JOIN `" . $wpdb->posts . "` p ON c.comment_post_ID = p.ID WHERE comment_approved = 1 AND DATE_FORMAT(c.comment_date, '%w') IS NOT NULL AND p.post_date >= '" . $startingdate . "' AND p.post_status = 'publish' GROUP BY DATE_FORMAT(comment_date, '%w') ORDER BY day ASC",
// Total comments approved
'comments_total' => "SELECT COUNT(c.comment_ID) AS count FROM `" . $wpdb->comments . "` c LEFT JOIN `" . $wpdb->posts . "` p ON c.comment_post_ID = p.ID WHERE comment_approved = '1' AND p.post_date >= '" . $startingdate . "'",
// Total posts published
'posts_total' => "SELECT COUNT(ID), post_date FROM `" . $wpdb->posts . "` WHERE post_type = 'post' AND post_status = 'publish' AND post_date >= '" . $startingdate . "'",
// Total words per comments approved
'comments_word' => "SELECT SUM(LENGTH(c.comment_content) - LENGTH(REPLACE(c.comment_content, ' ', '')) +1) AS count, p.post_date FROM `" . $wpdb->comments . "` c LEFT JOIN `" . $wpdb->posts . "` p ON c.comment_post_ID = p.ID WHERE c.comment_approved = '1' AND p.post_date >= '" . $startingdate . "'",
// 5 authors who comment the most
'comments_per_author' => "SELECT u.ID, u.user_login, u.display_name, COUNT(c.comment_ID) AS comment_count FROM `" . $wpdb->comments . "` c LEFT JOIN `" . $wpdb->users . "` u ON c.user_id = u.ID LEFT JOIN `" . $wpdb->posts . "` p ON c.comment_post_ID = p.ID WHERE c.user_id != 0 AND p.post_date >= '" . $startingdate . "' GROUP BY c.user_id ORDER BY comment_count DESC LIMIT 5",
// Total words per posts approved
'posts_word' => "SELECT SUM(LENGTH(post_content) - LENGTH(REPLACE(post_content, ' ', '')) +1) AS count FROM `" . $wpdb->posts . "` WHERE post_status = 'publish' AND post_type = 'post' AND post_date >= '" . $startingdate . "'",
// When do you post the most ?
'posts_per_day' => "SELECT COUNT(ID) AS count, DATE_FORMAT(post_date, '%w') AS day FROM `" . $wpdb->posts . "` WHERE post_status = 'publish' AND post_type = 'post' AND post_date >= '" . $startingdate . "' GROUP BY DATE_FORMAT(post_date, '%w') ORDER BY day ASC",
// 5 posts which generate the most Facebook shares and likes
'posts' => "SELECT ID, post_title FROM `" . $wpdb->posts . "` WHERE post_status = 'publish' AND post_type = 'post' AND post_date >= '" . $startingdate . "'"
);
/********** ACTIONS **********/
add_action( 'admin_init', 'abm_admin_init' );
add_action( 'admin_menu', 'abm_admin_menu' );
add_filter( 'plugin_action_links_'. plugin_basename( __FILE__ ), 'abm_plugin_action_links', 10, 2 );
function abm_admin_init() {
wp_register_style( 'advanced-blog-metrics', plugins_url( 'style.css', __FILE__ ) );
wp_enqueue_style( 'advanced-blog-metrics' );
register_setting( 'abm_options', 'abm_options', 'abm_options_validate' );
add_settings_section( 'abm_options_general', __('Settings','adv-blog-metrics'), 'abm_options_general_text', 'abm_options' );
add_settings_field( 'abm_options_starting_date', '', 'abm_options_starting_date_text', 'abm_options', 'abm_options_general' );
// access only for administrator
if (current_user_can('administrator') || current_user_can('editor') ) {
add_action( 'wp_dashboard_setup', 'abm_dashboard_init' );
}
}
function abm_admin_menu() {
if (current_user_can('administrator') ) {
add_menu_page( __('Advanced Blog Metrics', 'adv-blog-metrics'), 'Advanced Blog Metrics', 'administrator', 'advanced-blog-metrics');
add_submenu_page('advanced-blog-metrics', __('Dashboard', 'adv-blog-metrics'), __('Dashboard', 'adv-blog-metrics'), 'administrator', 'advanced-blog-metrics', 'display_all_widgets');
add_submenu_page('advanced-blog-metrics', __('Settings', 'adv-blog-metrics'), __('Settings', 'adv-blog-metrics'), 'administrator', 'advanced-blog-metrics-options', 'abm_options_page');
}
elseif( current_user_can('editor') ){
add_menu_page( __('Advanced Blog Metrics', 'adv-blog-metrics'), 'Advanced Blog Metrics', 'editor', 'advanced-blog-metrics');
add_submenu_page('advanced-blog-metrics', __('Dashboard', 'adv-blog-metrics'), __('Dashboard', 'adv-blog-metrics'), 'editor', 'advanced-blog-metrics', 'display_all_widgets');
add_submenu_page('advanced-blog-metrics', __('Settings', 'adv-blog-metrics'), __('Settings', 'adv-blog-metrics'), 'editor', 'advanced-blog-metrics-options', 'abm_options_page');
}
}
/********** SETTINGS **********/
function abm_options_page() {
ob_start();
echo '
';
echo '
' .__('Advanced Blog Metrics','adv-blog-metrics'). '
' .__('If you leave this field empty, Advanced Blog Metrics uses the date of your first post by default.' ,'adv-blog-metrics').'
';
}
function abm_options_validate($posted) {
$options = get_option( 'abm_options' );
$cleaned = array();
if ( !empty( $posted['starting_date'] ) && !preg_match( '`[0-9]{4}\-[0-9]{2}\-[0-9]{2}`', $posted['starting_date'] ) ) {
add_settings_error('abm_options_starting_date', 'abm_options_starting_date_bad_format', __('You did not use the expected date format. Please, fill in the starting date with the following format: YYYY-MM-DD','adv-blog-metrics'));
} elseif ( !empty( $posted['starting_date'] ) ) {
list( $year, $month, $day ) = explode( '-', $posted['starting_date'] );
if ( !checkdate( $month, $day, $year ) ) {
add_settings_error('abm_options_starting_date', 'abm_options_starting_date_not_valid', __('You have entered a date which does not exist. Please, fill in the starting date with a valid date','adv-blog-metrics'));
}
}
if ( count( get_settings_errors() ) > 0 && array_key_exists( 'starting_date', $options ) ) {
$cleaned['starting_date'] = $options['starting_date'];
} else {
$cleaned['starting_date'] = $posted['starting_date'];
}
return $cleaned;
}
function display_all_widgets(){
// Widget #1
echo '
'.__('Dashboard', 'adv-blog-metrics').'
';
echo '
';
echo '
';
echo '
'.__('5 posts which generate the most comments','adv-blog-metrics').'
';
echo '
';
dashboard_comments_per_post();
echo '
';
echo '
';
// Widget #2
echo '
';
echo '
'.__('When do your posts generate the most comments?','adv-blog-metrics').'
';
echo '
';
dashboard_comments_per_day();
echo '
';
echo '
';
// Widget #3
echo '
';
echo '
'.__('Comments','adv-blog-metrics').'
';
echo '
';
dashboard_comments();
echo '
';
echo '
';
// Widget #4
echo '
';
echo '
'.__('5 authors who comment the most','adv-blog-metrics').'
';
echo '
';
dashboard_comments_per_author();
echo '
';
echo '
';
// Widget #5
echo '
';
echo '
'.__('Posts','adv-blog-metrics').'
';
echo '
';
dashboard_posts();
echo '
';
echo '
';
// Widget #6
echo '
';
echo '
'.__('When do you post the most?','adv-blog-metrics').'
';
echo '
';
dashboard_posts_per_day();
echo '
';
echo '
';
// Widget #7
echo '
';
echo '
'.__('5 posts which generate the most Facebook shares and likes','adv-blog-metrics').'
';
echo '
';
dashboard_posts_facebook();
echo '
';
echo '
';
echo '
';
}
/********** INIT **********/
function abm_dashboard_init() {
// Widget #1
wp_add_dashboard_widget( 'dashboard_comments_per_post', __('5 posts which generate the most comments','adv-blog-metrics'), 'dashboard_comments_per_post' );
// Widget #2
wp_add_dashboard_widget( 'dashboard_comments_per_day', __('When do your posts generate the most comments?','adv-blog-metrics'), 'dashboard_comments_per_day' );
// Widget #3
wp_add_dashboard_widget( 'dashboard_comments', __('Comments','adv-blog-metrics'), 'dashboard_comments' );
// Widget #4
wp_add_dashboard_widget( 'dashboard_comments_per_author', __('5 authors who comment the most','adv-blog-metrics'), 'dashboard_comments_per_author' );
// Widget #5
wp_add_dashboard_widget( 'dashboard_posts', __('Posts','adv-blog-metrics'), 'dashboard_posts' );
// Widget #6
wp_add_dashboard_widget( 'dashboard_posts_per_day', __('When do you post the most?','adv-blog-metrics'), 'dashboard_posts_per_day' );
// Widget #7
wp_add_dashboard_widget( 'dashboard_posts_facebook', __('5 posts which generate the most Facebook shares and likes','adv-blog-metrics'), 'dashboard_posts_facebook' );
}
function abm_plugin_action_links( $links, $file ) {
array_unshift( $links, '' . __( 'Settings','adv-blog-metrics' ) . '' );
return $links;
}
/***** WIDGETS ON DASHBOARD *****/
// widget #1 : Posts which generate the most comments
function dashboard_comments_per_post() {
global $queries, $wpdb;
$posts = $wpdb->get_results( $queries['comments_per_post'] );
$html = '
'.__('Note that you need to check "Users must be registered and logged in to comment" in the Wordpress Settings->Discussion to see data in the "5 authors who comments the most" widget.','adv-blog-metrics').'