-
Notifications
You must be signed in to change notification settings - Fork 0
/
advanced-blog-metrics.php
649 lines (547 loc) · 30.7 KB
/
advanced-blog-metrics.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
<?php
/*
Plugin Name: Advanced Blog Metrics
Plugin URI: http://www.atalanta.fr/advanced-blog-metrics-wordpress-plugin
Description: Advanced Blog Metrics is an analytics tool dedicated to bloggers. This plugin allows you to improve your blog performance
Version: 1.5
Author: Atalanta
Author URI: http://www.atalanta.fr/
License: GPL2
*/
/* @var $wpdb wpdb */
global $wpdb;
load_plugin_textdomain( 'adv-blog-metrics', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
/********** OPTIONS **********/
// general options of the plugin
$options = (array)get_option('abm_options');
// Comment Registration required
$commentregistration = (bool)get_option('comment_registration');
// start of week
$startofweek = get_option('start_of_week');
// Weekdays
$days = array( __('Sunday','adv-blog-metrics'), __('Monday','adv-blog-metrics'), __('Tuesday','adv-blog-metrics'), __('Wednesday','adv-blog-metrics'), __('Thursday','adv-blog-metrics'), __('Friday', 'adv-blog-metrics') , __('Saturday','adv-blog-metrics') );
// Starting date
if (!empty($options['starting_date'])) {
$startingdate = $options['starting_date'] . ' 00:00:00';
} else {
$query_first_post = "SELECT post_date FROM `" . $wpdb->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) 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 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', '<label for="abm_options_starting_date">'.__('Starting date','adv-blog-metrics').'</label>', '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<br />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<br />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 '<div class="wrap">';
echo '<h2>' .__('Advanced Blog Metrics','adv-blog-metrics'). '</h2>';
echo '<form action="options.php" method="post">';
settings_errors();
settings_fields( 'abm_options' );
do_settings_sections( 'abm_options' );
submit_button();
echo '</form>';
echo '</div>';
ob_end_flush();
}
function abm_options_general_text() { }
function abm_options_starting_date_text() {
$options = get_option( 'abm_options' );
echo '<input type="text" size="12" maxlength="10" id="abm_options_starting_date" name="abm_options[starting_date]" value="' . $options['starting_date'] . '" title="'.__('Date format: YYYY-MM-DD','adv-blog-metrics') . '" /><span id="local-time">'.__('Date format: YYYY-MM-DD','adv-blog-metrics') . '</span>';
echo '<p class="description">' .__('If you leave this field empty, Advanced Blog Metrics uses the date of your first post by default.' ,'adv-blog-metrics').'</p>';
}
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 '<h2>'.__('Dashboard', 'adv-blog-metrics').'</h2>';
echo '<div id="onglet-widgets" class="metabox-holder postbox-container" style="width:50%">';
echo '<div class="postbox">';
echo '<h3 class="hndle">'.__('5 posts which generate the most comments','adv-blog-metrics').'</h3>';
echo '<div class="inside">';
dashboard_comments_per_post();
echo '</div>';
echo '</div>';
// Widget #2
echo '<div class="postbox">';
echo '<h3 class="hndle">'.__('When do your posts generate the most comments?','adv-blog-metrics').'</h3>';
echo '<div class="inside">';
dashboard_comments_per_day();
echo '</div>';
echo '</div>';
// Widget #3
echo '<div class="postbox">';
echo '<h3 class="hndle">'.__('Comments','adv-blog-metrics').'</h3>';
echo '<div class="inside">';
dashboard_comments();
echo '</div>';
echo '</div>';
// Widget #4
echo '<div class="postbox">';
echo '<h3 class="hndle">'.__('5 authors who comment the most','adv-blog-metrics').'</h3>';
echo '<div class="inside">';
dashboard_comments_per_author();
echo '</div>';
echo '</div>';
// Widget #5
echo '<div class="postbox">';
echo '<h3 class="hndle">'.__('Posts','adv-blog-metrics').'</h3>';
echo '<div class="inside">';
dashboard_posts();
echo '</div>';
echo '</div>';
// Widget #6
echo '<div class="postbox">';
echo '<h3 class="hndle">'.__('When do you post the most?','adv-blog-metrics').'</h3>';
echo '<div class="inside">';
dashboard_posts_per_day();
echo '</div>';
echo '</div>';
// Widget #7
echo '<div class="postbox">';
echo '<h3 class="hndle">'.__('5 posts which generate the most Facebook shares and likes','adv-blog-metrics').'</h3>';
echo '<div class="inside">';
dashboard_posts_facebook();
echo '</div>';
echo '</div>';
echo '</div>';
}
/********** 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, '<a href="' . admin_url( 'admin.php?page=advanced-blog-metrics' ) . '">' . __( 'Settings','adv-blog-metrics' ) . '</a>' );
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 = '<table cellpadding="0" cellspacing="0" class="table-list">';
$html .= '<thead><tr><th width="85%">'.__('Post','adv-blog-metrics').'</th><th class="comment_count">'.__('Comments','adv-blog-metrics').'</th></tr></thead>';
$html .= '<tbody>';
foreach ( $posts as $post ) {
$html .= '<tr>';
$html .= '<th><a href="' . get_permalink( $post->ID ) . '">' . $post->post_title . '</a></th>';
$html .= '<td class="comment_count">' . $post->comment_count . '</td>';
$html .= '</tr>';
}
$html .= '</tbody>';
$html .= '</table>';
echo $html;
}
// Widget #2 : When do your posts generate the most comments?
function dashboard_comments_per_day() {
global $queries, $wpdb, $days, $startofweek;
$posts = array();
$results = $wpdb->get_results( $queries['comments_per_day'] );
foreach($results as $result) {
$posts[$result->day] = $result->count;
}
if( count($posts) >0 ) {
$max = max($posts);
$html = '<table cellpadding="0" cellspacing="0" class="table-cols">';
$html .= '<tr>';
for ($num_day = $startofweek; $num_day <= 6; $num_day++) {
$html .= '<td class="value' . ($max == $posts[$num_day] ? ' max' : '') . '">';
$html .= '<span class="count">' . $posts[$num_day] . '</span>';
$html .= '<div class="pourcent" style="height: ' . round( ( $posts[$num_day] / $max ) * 150, 0 ) . 'px"></div>';
$html .= '</td>';
}
for ($num_day = 0; $num_day < $startofweek; $num_day++) {
$html .= '<td class="value' . ($max == $posts[$num_day] ? ' max' : '') . '">';
$html .= '<span class="count">' . $posts[$num_day] . '</span>';
$html .= '<div class="pourcent" style="height: ' . round( ( $posts[$num_day] / $max ) * 150, 0 ) . 'px"></div>';
$html .= '</td>';
}
$html .= '</tr><tr>';
for ($num_day = $startofweek; $num_day <= 6; $num_day++) {
$html .= '<th class="day' . ( $max == $posts[$num_day] ? ' max' : '' ) . '"><span>' . strtoupper( substr( $days[$num_day], 0, 3 ) ) . '</span></th>';
}
for ($num_day = 0; $num_day < $startofweek; $num_day++) {
$html .= '<th class="day' . ( $max == $posts[$num_day] ? ' max' : '' ) . '"><span>' . strtoupper( substr( $days[$num_day], 0, 3 ) ) . '</span></th>';
}
$html .= '</tr>';
$html .= '</table>';
}
else {
$html = '<table cellpadding="0" cellspacing="0" class="table-cols">';
$html .= '<tr>';
for ($num_day = $startofweek; $num_day <= 6; $num_day++) {
$html .= '<td class="value">';
$html .= '<span class="count">0</span>';
$html .= '<div class="pourcent" style="height:0"></div>';
$html .= '</td>';
}
for ($num_day = 0; $num_day < $startofweek; $num_day++) {
$html .= '<td class="value">';
$html .= '<span class="count">0</span>';
$html .= '<div class="pourcent" style="height:0"></div>';
$html .= '</td>';
}
$html .= '</tr><tr>';
for ($num_day = $startofweek; $num_day <= 6; $num_day++) {
$html .= '<th class="day"><span>' . strtoupper( substr( $days[$num_day], 0, 3 ) ) . '</span></th>';
}
for ($num_day = 0; $num_day < $startofweek; $num_day++) {
$html .= '<th class="day"><span>' . strtoupper( substr( $days[$num_day], 0, 3 ) ) . '</span></th>';
}
$html .= '</tr>';
$html .= '</table>';
}
echo $html;
}
// Widget #3 : Comments
function dashboard_comments() {
global $queries, $wpdb, $elapseddays;
$total = current( $wpdb->get_col( $queries['comments_total'], 0 ) );
$posts = current( $wpdb->get_col( $queries['posts_total'], 0 ) );
$words = current( $wpdb->get_col( $queries['comments_word'], 0 ) );
if( 0!==(int)$elapseddays && 0!==(int)$posts && 0!==(int)$total ) {
$html = '<table cellpadding="0" cellspacing="0" class="table-list table-summary">';
$html .= '<thead>';
$html .= '<tr>';
$html .= '<th width="25%">'.__('Approved comments','adv-blog-metrics').'</th>';
$html .= '<th width="25%">'.__('Comments per Day','adv-blog-metrics').'</th>';
$html .= '<th width="25%">'.__('Comments per Post','adv-blog-metrics').'</th>';
$html .= '<th width="25%">'.__('Words per Comment','adv-blog-metrics').'</th>';
$html .= '</tr>';
$html .= '</thead>';
$html .= '<tbody>';
$html .= '<tr>';
$html .= '<td class="number"><span>' . $total . '</span></td>';
$html .= '<td class="number"><span>' . round( $total / $elapseddays, 2 ) . '</span></td>';
$html .= '<td class="number"><span>' . round( $total / $posts, 1 ) . '</span></td>';
$html .= '<td class="number"><span>' . round( $words / $total ) . '</span></td>';
$html .= '</tr>';
$html .= '</tbody>';
$html .= '</table>';
}
else{
$html = '<table cellpadding="0" cellspacing="0" class="table-list table-summary">';
$html .= '<thead>';
$html .= '<tr>';
$html .= '<th width="25%">'.__('Approved comments','adv-blog-metrics').'</th>';
$html .= '<th width="25%">'.__('Comments per Day','adv-blog-metrics').'</th>';
$html .= '<th width="25%">'.__('Comments per Post','adv-blog-metrics').'</th>';
$html .= '<th width="25%">'.__('Words per Comment','adv-blog-metrics').'</th>';
$html .= '</tr>';
$html .= '</thead>';
$html .= '<tbody>';
$html .= '<tr>';
$html .= '<td class="number"><span>' . $total . '</span></td>';
if ( 0!==(int)$elapseddays )
$html .= '<td class="number"><span>' . round( $total / $elapseddays, 2 ) . '</span></td>';
else
$html .= '<td class="number"><span>0</span></td>';
if ( 0!==(int)$posts )
$html .= '<td class="number"><span>' . round( $total / $posts, 1 ) . '</span></td>';
else
$html .= '<td class="number"><span>0</span></td>';
if ( 0!==(int)$total )
$html .= '<td class="number"><span>' . round( $words / $total ) . '</span></td>';
else
$html .= '<td class="number"><span>0</span></td>';
$html .= '</tr>';
$html .= '</tbody>';
$html .= '</table>';
}
echo $html;
}
// Widget #4 : Authors who comment the most
function dashboard_comments_per_author() {
global $queries, $wpdb, $commentregistration;
if ($commentregistration) {
$authors = $wpdb->get_results($queries['comments_per_author']);
}
$html = '<table cellpadding="0" cellspacing="0" class="table-list">';
$html .= '<thead><tr><th width="85%">'.__('Author','adv-blog-metrics').'</th><th class="comment_count">'.__('Comments','adv-blog-metrics').'</th></tr></thead><tbody>';
if ($commentregistration) {
foreach ($authors as $author) {
$html .= '<tr>';
$html .= '<th><a href="' . get_admin_url() . 'user-edit.php?user_id='.$author->ID . '" title="Modify">' . $author->display_name . '</a></th>';
$html .= '<td class="comment_count">' . $author->comment_count . '</td>';
$html .= '</tr>';
}
} else {
$html .= '<tr><td colspan="2"><p>'.__('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').'</p><p><a class="button" href="' . get_admin_url() . 'options-discussion.php">'.__('View discussion options','adv-blog-metrics').'</p></td></tr>';
}
$html .= '</tbody></table>';
echo $html;
}
// Widget #5 : Posts
function dashboard_posts() {
global $queries, $wpdb, $elapseddays;
$total = current( $wpdb->get_col( $queries['posts_total'], 0 ) );
$comments = current( $wpdb->get_col( $queries['comments_total'], 0 ) );
$words = current( $wpdb->get_col( $queries['posts_word'], 0 ) );
if( 0!==(int)$elapseddays && 0!==(int)$posts && 0!==(int)$total ) {
$html = '<table cellpadding="0" cellspacing="0" class="table-list table-summary">';
$html .= '<thead><tr>';
$html .= '<th width="25%">'._('Posts','adv-blog-metrics').'</th>';
$html .= '<th width="25%">'._('Posts per Day','adv-blog-metrics').'</th>';
$html .= '<th width="25%">'._('Comments per Post','adv-blog-metrics').'</th>';
$html .= '<th width="25%">'._('Words per Post','adv-blog-metrics').'</th>';
$html .= '</tr></thead><tbody><tr>';
$html .= '<td class="number"><span>' . $total . '</span></td>';
$html .= '<td class="number"><span>' . round( $total / $elapseddays, 2 ) . '</span></td>';
$html .= '<td class="number"><span>' . round( $comments / $total, 1 ) . '</span></td>';
$html .= '<td class="number"><span>' . round( $words / $total ) . '</span></td>';
$html .= '</tr></tbody>';
$html .= '</table>';
}
else {
$html = '<table cellpadding="0" cellspacing="0" class="table-list table-summary">';
$html .= '<thead><tr>';
$html .= '<th width="25%">'.__('Posts','adv-blog-metrics').'</th>';
$html .= '<th width="25%">'.__('Posts per Day','adv-blog-metrics').'</th>';
$html .= '<th width="25%">'.__('Comments per Post','adv-blog-metrics').'</th>';
$html .= '<th width="25%">'.__('Words per Post','adv-blog-metrics').'</th>';
$html .= '</tr></thead><tbody><tr>';
$html .= '<td class="number"><span>' . $total . '</span></td>';
if( 0!==(int)$elapseddays )
$html .= '<td class="number"><span>' . round( $total / $elapseddays, 2 ). '</span></td>';
else
$html .= '<td class="number"><span>0</span></td>';
if( 0!==(int)$total ) {
$html .= '<td class="number"><span>' . round( $comments / $total, 1 ) . '</span></td>';
$html .= '<td class="number"><span>' . round( $words / $total ) . '</span></td>';
}
else {
$html .= '<td class="number"><span>0</span></td>';
$html .= '<td class="number"><span>0</span></td>';
}
$html .= '</tr></tbody>';
$html .= '</table>';
}
echo $html;
}
// Widget #6 : When do you post the most?
function dashboard_posts_per_day() {
global $queries, $wpdb, $days, $startofweek;
$posts = array();
$results = $wpdb->get_results( $queries['posts_per_day'] );
foreach($results as $result) {
$posts[$result->day] = $result->count;
}
if( count($posts) >0 ) {
$max = max($posts);
$html = '<table cellpadding="0" cellspacing="0" class="table-cols">';
$html .= '<tr>';
for ($num_day = $startofweek; $num_day <= 6; $num_day++) {
$html .= '<td class="value' . ($max == $posts[$num_day] ? ' max' : '') . '">';
$html .= '<span class="count">' . $posts[$num_day] . '</span>';
$html .= '<div class="pourcent" style="height: ' . round( ( $posts[$num_day] / $max ) * 150, 0 ) . 'px"></div>';
$html .= '</td>';
}
for ($num_day = 0; $num_day < $startofweek; $num_day++) {
$html .= '<td class="value' . ($max == $posts[$num_day] ? ' max' : '') . '">';
$html .= '<span class="count">' . $posts[$num_day] . '</span>';
$html .= '<div class="pourcent" style="height: ' . round( ( $posts[$num_day] / $max ) * 150, 0 ) . 'px"></div>';
$html .= '</td>';
}
$html .= '</tr><tr>';
for ($num_day = $startofweek; $num_day <= 6; $num_day++) {
$html .= '<th class="day' . ( $max == $posts[$num_day] ? ' max' : '' ) . '"><span>' . strtoupper( substr( $days[$num_day], 0, 3 ) ) . '</span></th>';
}
for ($num_day = 0; $num_day < $startofweek; $num_day++) {
$html .= '<th class="day' . ( $max == $posts[$num_day] ? ' max' : '' ) . '"><span>' . strtoupper( substr( $days[$num_day], 0, 3 ) ) . '</span></th>';
}
$html .= '</tr>';
$html .= '</table>';
}
else {
$html = '<table cellpadding="0" cellspacing="0" class="table-cols">';
$html .= '<tr>';
for ($num_day = $startofweek; $num_day <= 6; $num_day++) {
$html .= '<td class="value">';
$html .= '<span class="count">0</span>';
$html .= '<div class="pourcent" style="height:0"></div>';
$html .= '</td>';
}
for ($num_day = 0; $num_day < $startofweek; $num_day++) {
$html .= '<td class="value">';
$html .= '<span class="count">0</span>';
$html .= '<div class="pourcent" style="height:0"></div>';
$html .= '</td>';
}
$html .= '</tr><tr>';
for ($num_day = $startofweek; $num_day <= 6; $num_day++) {
$html .= '<th class="day"><span>' . strtoupper( substr( $days[$num_day], 0, 3 ) ) . '</span></th>';
}
for ($num_day = 0; $num_day < $startofweek; $num_day++) {
$html .= '<th class="day"><span>' . strtoupper( substr( $days[$num_day], 0, 3 ) ) . '</span></th>';
}
$html .= '</tr>';
$html .= '</table>';
}
echo $html;
}
// Widget #7 : 5 posts which generate the most Facebook shares and likes
function dashboard_posts_facebook() {
global $queries, $wpdb;
// Add the option if it does not exist yet
add_option('abm_data', '');
// data of the plugin
$data = get_option('abm_data');
// If the admin cliks on the button, the queries will occur and it could be long depending on the number of posts
if( isset($_POST['sub_posts_on_facebook']) && 'Get/Update data' == $_POST['sub_posts_on_facebook'] ) {
$posts_likes = array();
$posts_shares = array();
$query_urls = array();
$posts_permalink_title = array();
$nb_url_by_query = 100;
$posts = $wpdb->get_results( $queries['posts'] );
foreach( $posts as $post ) {
$query_urls[] = get_permalink( $post->ID );
$posts_permalink_title[get_permalink( $post->ID )] = $post->post_title;
}
$array_permalinks = array_chunk($query_urls, $nb_url_by_query);
foreach( $array_permalinks as $permalinks ){
$q = implode("','", $permalinks);
$fql_query_url = "https://graph.facebook.com/fql?q=SELECT+url,+share_count,+like_count+FROM+link_stat+WHERE+url+IN('".$q."')";
$fql_query_result = file_get_contents($fql_query_url);
$fql_query_obj = json_decode($fql_query_result, true);
foreach($fql_query_obj[data] as $a) {
$posts_likes[] = array('title' =>$posts_permalink_title[$a['url']], 'permalink' => $a['url'], 'like_count' => $a['like_count']);
$posts_shares[] = array('title' =>$posts_permalink_title[$a['url']], 'permalink' => $a['url'], 'share_count' => $a['share_count']);
}
}
foreach( $posts_likes as $k => $v ) {
$likes[$k] = $v['like_count'];
}
array_multisort( $likes, SORT_DESC, $posts_likes );
$posts_likes_output = array_slice($posts_likes, 0, 5);
foreach( $posts_shares as $k => $v ) {
$shares[$k] = $v['share_count'];
}
array_multisort( $shares, SORT_DESC, $posts_shares );
$posts_shares_output = array_slice( $posts_shares, 0, 5 );
$array_shares_and_likes = array_merge( array('posts_likes' => $posts_likes_output ), array('posts_shares' => $posts_shares_output) );
update_option( 'abm_data', $array_shares_and_likes );
?>
<script type="text/javascript">
<!--
window.location = '/wp-admin';
//-->
</script>
<?php
}
// display
$html = '<form name="form_posts_on_facebook" id="form_posts_on_facebook" action="" method="post">';
$html .= '<input type="submit" name="sub_posts_on_facebook" id="sub_posts_on_facebook" value="'.__('Get/Update data','adv-blog-metrics').'" />';
$html .= '<span id="info-get-data">'.__('(Synchronization may last several minutes depending on the number of posts)','adv-blog-metrics').'</span>';
$html .= '</form>';
echo $html;
// About LIKES
$html_likes = '<table cellpadding="0" cellspacing="0" class="table-list">';
$html_likes .= '<thead><tr><th width="85%">'.__('Post','adv-blog-metrics').'</th><th class="facebook_count"><img alt="like" src="http://www.atalanta.fr/advanced-blog-metrics/facebook-like.png" /></th></tr></thead><tbody>';
if( isset($data) && !empty($data) ) {
foreach ($data['posts_likes'] as $post) {
$html_likes .= '<tr>';
$html_likes .= '<th><a href="' . $post['permalink'] . '">' . $post['title'] . '</a></th>';
$html_likes .= '<td class="facebook_count">' . $post['like_count'] . '</td>';
$html_likes .= '</tr>';
}
}
$html_likes .= '</tbody></table>';
echo $html_likes, "<br />";
// About SHARES
$html_shares = '<table cellpadding="0" cellspacing="0" class="table-list">';
$html_shares .= '<thead><tr><th width="85%">'.__('Post','adv-blog-metrics').'</th><th class="facebook_count"><img alt="share" src="http://www.atalanta.fr/advanced-blog-metrics/facebook-share.png" /></th></tr></thead><tbody>';
if( isset($data) && !empty($data) ) {
foreach ($data['posts_shares'] as $post) {
$html_shares .= '<tr>';
$html_shares .= '<th><a href="' . $post['permalink'] . '">' . $post['title'] . '</a></th>';
$html_shares .= '<td class="facebook_count">' . $post['share_count'] . '</td>';
$html_shares .= '</tr>';
}
}
$html_shares .= '</tbody></table>';
echo $html_shares, "<br />";
}