PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.7.15
Post Views Counter v1.7.15
1.7.15 1.7.14 1.7.13 1.7.12 1.7.11 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.2 1.3.2.1 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.7.10 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9
post-views-counter / includes / class-toolbar.php
post-views-counter / includes Last commit date
class-admin.php 2 weeks ago class-columns-modal.php 2 weeks ago class-columns.php 2 weeks ago class-counter.php 2 weeks ago class-crawler-detect.php 2 weeks ago class-cron.php 2 weeks ago class-dashboard.php 2 weeks ago class-emails-mailer.php 2 weeks ago class-emails-period.php 2 weeks ago class-emails-query.php 2 weeks ago class-emails-scheduler.php 2 weeks ago class-emails-template.php 2 weeks ago class-emails.php 2 weeks ago class-frontend.php 2 weeks ago class-functions.php 2 weeks ago class-import.php 2 weeks ago class-integration-gutenberg.php 2 weeks ago class-integrations.php 2 weeks ago class-query.php 2 weeks ago class-settings-api.php 2 weeks ago class-settings-display.php 2 weeks ago class-settings-emails.php 2 weeks ago class-settings-general.php 2 weeks ago class-settings-integrations.php 2 weeks ago class-settings-other.php 2 weeks ago class-settings-reports.php 2 weeks ago class-settings.php 2 weeks ago class-toolbar.php 2 weeks ago class-traffic-signals.php 2 weeks ago class-update.php 2 weeks ago class-widgets.php 2 weeks ago functions.php 2 weeks ago
class-toolbar.php
232 lines
1 <?php
2 // exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) )
4 exit;
5
6 /**
7 * Post_Views_Counter_Toolbar class.
8 *
9 * @class Post_Views_Counter_Toolbar
10 */
11 class Post_Views_Counter_Toolbar {
12
13 /**
14 * Class constructor.
15 *
16 * @return void
17 */
18 public function __construct() {
19 // actions
20 add_action( 'wp_loaded', [ $this, 'maybe_load_admin_bar_menu' ] );
21 }
22
23 /**
24 * Add admin bar stats to a post.
25 *
26 * @return void
27 */
28 public function maybe_load_admin_bar_menu() {
29 // get main instance
30 $pvc = Post_Views_Counter();
31
32 // statistics disabled?
33 if ( ! apply_filters( 'pvc_display_toolbar_statistics', $pvc->options['display']['toolbar_statistics'] ) )
34 return;
35
36 // skip for not logged in users
37 if ( ! is_user_logged_in() )
38 return;
39
40 // skip users with turned off admin bar at frontend
41 if ( ! is_admin() && get_user_option( 'show_admin_bar_front' ) !== 'true' )
42 return;
43
44 if ( is_admin() )
45 add_action( 'admin_init', [ $this, 'admin_bar_maybe_add_style' ] );
46 else
47 add_action( 'wp', [ $this, 'admin_bar_maybe_add_style' ] );
48 }
49
50 /**
51 * Add admin bar stats to a post.
52 *
53 * @global string $pagenow
54 * @global string $post
55 *
56 * @param object $admin_bar
57 * @return void
58 */
59 public function admin_bar_menu( $admin_bar ) {
60 // get main instance
61 $pvc = Post_Views_Counter();
62
63 // set empty post
64 $post = null;
65
66 // admin?
67 if ( is_admin() && ! wp_doing_ajax() ) {
68 global $pagenow;
69
70 $post = ( $pagenow === 'post.php' && ! empty( $_GET['post'] ) ) ? get_post( (int) $_GET['post'] ) : $post;
71 // frontend?
72 } elseif ( is_singular() )
73 global $post;
74
75 // get countable post types
76 $post_types = (array) $pvc->options['general']['post_types_count'];
77
78 // break if display is not allowed
79 if ( empty( $post_types ) || empty( $post ) || ! in_array( $post->post_type, $post_types, true ) )
80 return;
81
82 if ( apply_filters( 'pvc_admin_display_post_views', true ) === false )
83 return;
84
85 $dt = new DateTime();
86
87 // get post views
88 $views = pvc_get_views(
89 [
90 'post_id' => $post->ID,
91 'post_type' => $post->post_type,
92 'fields' => 'date=>views',
93 'views_query' => [
94 'year' => $dt->format( 'Y' ),
95 'month' => $dt->format( 'm' )
96 ]
97 ]
98 );
99
100 $graph = '';
101
102 // get highest value
103 $views_copy = $views;
104
105 arsort( $views_copy, SORT_NUMERIC );
106
107 $highest = reset( $views_copy );
108
109 // find the multiplier
110 $multiplier = $highest * 0.05;
111
112 // generate ranges
113 $ranges = [];
114
115 for ( $i = 1; $i <= 20; $i ++ ) {
116 $ranges[$i] = round( $multiplier * $i );
117 }
118
119 // create graph
120 foreach ( $views as $date => $count ) {
121 $count_class = 0;
122
123 if ( $count > 0 ) {
124 foreach ( $ranges as $index => $range ) {
125 if ( $count <= $range ) {
126 $count_class = $index;
127 break;
128 }
129 }
130 }
131
132 // individual bars are decorative: the container carries the accessible name,
133 // while the title attribute keeps the tooltip for sighted mouse users
134 $graph .= '<span class="pvc-line-graph pvc-line-graph-' . (int) $count_class . '" aria-hidden="true" title="' . esc_attr( sprintf( _n( '%s post view', '%s post views', $count, 'post-views-counter' ), number_format_i18n( $count ) ) ) . '"></span>';
135 }
136
137 // one concise accessible name instead of one label per day
138 $graph_label = sprintf(
139 /* translators: 1: month and year, 2: total number of post views */
140 __( 'Post views chart for %1$s, %2$s total', 'post-views-counter' ),
141 date_i18n( 'F Y', $dt->getTimestamp() ),
142 number_format_i18n( array_sum( $views ) )
143 );
144
145 $admin_bar->add_menu(
146 [
147 'id' => 'pvc-post-views',
148 'title' => '<span class="pvc-graph-container" role="img" aria-label="' . esc_attr( $graph_label ) . '">' . $graph . '</span>',
149 'href' => false,
150 'meta' => [
151 'title' => false
152 ]
153 ]
154 );
155 }
156
157 /**
158 * Maybe add admin CSS.
159 *
160 * @global string $pagenow
161 * @global string $post
162 *
163 * @return void
164 */
165 public function admin_bar_maybe_add_style() {
166 // get main instance
167 $pvc = Post_Views_Counter();
168
169 // set empty post
170 $post = null;
171
172 // admin?
173 if ( is_admin() && ! wp_doing_ajax() ) {
174 global $pagenow;
175
176 $post = ( $pagenow === 'post.php' && ! empty( $_GET['post'] ) ) ? get_post( (int) $_GET['post'] ) : $post;
177 // frontend?
178 } elseif ( is_singular() )
179 global $post;
180
181 // get countable post types
182 $post_types = (array) $pvc->options['general']['post_types_count'];
183
184 // break if display is not allowed
185 if ( empty( $post_types ) || empty( $post ) || ! in_array( $post->post_type, $post_types, true ) )
186 return;
187
188 if ( apply_filters( 'pvc_admin_display_post_views', true ) === false )
189 return;
190
191 // add admin bar
192 add_action( 'admin_bar_menu', [ $this, 'admin_bar_menu' ], 100 );
193
194 // backend
195 if ( current_action() === 'admin_init' )
196 add_action( 'admin_head', [ $this, 'admin_bar_css' ] );
197 // frontend
198 elseif ( current_action() === 'wp' )
199 add_action( 'wp_head', [ $this, 'admin_bar_css' ] );
200 }
201
202 /**
203 * Add admin CSS.
204 *
205 * @return void
206 */
207 public function admin_bar_css() {
208 $html = '
209 <style type="text/css">
210 #wp-admin-bar-pvc-post-views .pvc-graph-container { padding-top: 6px; padding-bottom: 6px; position: relative; display: block; height: 100%; box-sizing: border-box; }
211 #wp-admin-bar-pvc-post-views .pvc-line-graph {
212 display: inline-block;
213 width: 1px;
214 margin-right: 1px;
215 background-color: #ccc;
216 vertical-align: baseline;
217 }
218 #wp-admin-bar-pvc-post-views .pvc-line-graph:hover { background-color: #eee; }
219 #wp-admin-bar-pvc-post-views .pvc-line-graph-0 { height: 1% }';
220
221 for ( $i = 1; $i <= 20; $i ++ ) {
222 $html .= '
223 #wp-admin-bar-pvc-post-views .pvc-line-graph-' . $i . ' { height: ' . $i * 5 . '% }';
224 }
225
226 $html .= '
227 </style>';
228
229 echo wp_kses( $html, [ 'style' => [] ] );
230 }
231 }
232