-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.php
More file actions
127 lines (104 loc) · 4.05 KB
/
dashboard.php
File metadata and controls
127 lines (104 loc) · 4.05 KB
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
<?php
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
// Stat block renderer
function pubcounts_dash_stat_block( $title, $datapoint, $prevdata) {
// Display the comparison to the previous month's post count
$post_count_diff = $datapoint - $prevdata;
if ($post_count_diff > 0) {
$comparison = '<span class="b-stat-block__change-value count-inc dashicons-before dashicons-arrow-up">+' . $post_count_diff . '</span>';
} elseif ($post_count_diff < 0) {
$comparison = '<span class="b-stat-block__change-value count-dec dashicons-before dashicons-arrow-down">' . $post_count_diff . '</span>';
} else {
$comparison = '<div class="b-stat-block__change-value">+/- 0</div>';
}
$html = sprintf( '<div class="b-stat-block">
<div class="b-stat-block__datapoint-wrapper">
<h3 class="b-stat-block__title">
<span class="b-stat-block__title-inner">%1$s</span>
</h3>
<div class="b-stat-block__datapoint">%2$s</div>
</div>
<div class="b-stat-block__change-wrapper">
<div class="b-stat-block__change" title="Comparison to previous month">%3$s</div>
</div>
</div>',
$title,
$datapoint,
$comparison
);
return $html;
}
// Register the dashboard widget
function pubcounts_post_count_dashboard_widget() {
wp_add_dashboard_widget(
'pubcounts',
'Published Counts',
'pubcounts_post_count_display'
);
}
add_action('wp_dashboard_setup', 'pubcounts_post_count_dashboard_widget');
function pubcounts_post_count_display() {
global $current_month_and_year;
$options = get_option( 'pubcounts_options' );
// if ( ( array_key_exists( 'pubcounts_field_amusements', $options ) ) && ( $options['pubcounts_field_amusements'] == 'show' ) ) {
if ( $options['pubcounts_field_amusements'] == 'show' ) {
$theme = pubcounts_get_monthly_theme();
echo '<h2 class="has-theme" style="background-image: linear-gradient(to left, ' . $theme['color'] . ' 0, white 50%);">' . $theme['emoji'] . ' ' . $current_month_and_year . '</h2>';
} else {
echo '<h2 class="no-theme">' . $current_month_and_year . '</h2>';
}
// Get the post count for the current month
$args = [
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'monthnum' => date('m'),
'year' => date('Y')
];
$query = new WP_Query($args);
$current_month_post_count = $query->post_count;
// Get the post count for the previous month
$prev_month_args = [
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'monthnum' => date('m', strtotime('last month')),
'year' => date('Y', strtotime('last month'))
];
$prev_month_query = new WP_Query($prev_month_args);
$prev_month_post_count = $prev_month_query->post_count;
$prev_month_posts_link = '/wp-admin/edit.php?s&post_status=all&post_type=post&action=-1&m=' . $prev_month_args['year'] . $prev_month_args['monthnum'] . '&cat=0&filter_action=Filter&paged=1&action2=-1';
// Get the page count for the current month
$page_args = [
'post_type' => 'page',
'post_status' => 'publish',
'posts_per_page' => -1,
'monthnum' => date('m'),
'year' => date('Y')
];
$page_query = new WP_Query($page_args);
$current_month_page_count = $page_query->post_count;
// Display the current month's post count and list of posts
echo '<div class="b-stat-blocks">';
echo pubcounts_dash_stat_block( 'Posts Published', $current_month_post_count, $prev_month_post_count );
echo pubcounts_dash_stat_block( 'Pages', $current_month_page_count, 0 );
echo '</div>';
// Add a link to the admin report page
echo '<p style="font-weight: bold; margin-bottom: 2rem;"><a href="' . admin_url('admin.php?page=pubcounts_report') . '">' . __( 'View Full Report', 'pubcounts' ) . '</a></p>';
if ($query->have_posts()) {
echo '<div class="pub-posts">';
echo '<h3>Posts</h3><ol>';
while ($query->have_posts()) {
$query->the_post();
echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a> (' . get_the_date() . ')</li>';
}
echo '</ol></div>';
}
echo '<footer>';
echo 'Plugin version: <strong>' . PUBCOUNTS_VERSION . '</strong>.</span>';
echo '</footer>';
wp_reset_postdata();
}