PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.7.5
Post Views Counter v1.7.5
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 5 months ago class-columns-modal.php 5 months ago class-columns.php 5 months ago class-counter.php 5 months ago class-crawler-detect.php 5 months ago class-cron.php 5 months ago class-dashboard.php 5 months ago class-frontend.php 5 months ago class-functions.php 5 months ago class-import.php 5 months ago class-integration-gutenberg.php 5 months ago class-integrations.php 5 months ago class-query.php 5 months ago class-settings-api.php 5 months ago class-settings-display.php 5 months ago class-settings-general.php 5 months ago class-settings-integrations.php 5 months ago class-settings-other.php 5 months ago class-settings-reports.php 5 months ago class-settings.php 5 months ago class-toolbar.php 5 months ago class-traffic-signals.php 5 months ago class-update.php 5 months ago class-widgets.php 5 months ago functions.php 5 months ago
class-toolbar.php
222 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 = $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 $graph .= '<span class="pvc-line-graph pvc-line-graph-' . $count_class . '" title="' . sprintf( _n( '%s post view', '%s post views', $count, 'post-views-counter' ), number_format_i18n( $count ) ) . '"></span>';
133 }
134
135 $admin_bar->add_menu(
136 [
137 'id' => 'pvc-post-views',
138 'title' => '<span class="pvc-graph-container">' . $graph . '</span>',
139 'href' => false,
140 'meta' => [
141 'title' => false
142 ]
143 ]
144 );
145 }
146
147 /**
148 * Maybe add admin CSS.
149 *
150 * @global string $pagenow
151 * @global string $post
152 *
153 * @return void
154 */
155 public function admin_bar_maybe_add_style() {
156 // get main instance
157 $pvc = Post_Views_Counter();
158
159 // set empty post
160 $post = null;
161
162 // admin?
163 if ( is_admin() && ! wp_doing_ajax() ) {
164 global $pagenow;
165
166 $post = ( $pagenow === 'post.php' && ! empty( $_GET['post'] ) ) ? get_post( (int) $_GET['post'] ) : $post;
167 // frontend?
168 } elseif ( is_singular() )
169 global $post;
170
171 // get countable post types
172 $post_types = $pvc->options['general']['post_types_count'];
173
174 // break if display is not allowed
175 if ( empty( $post_types ) || empty( $post ) || ! in_array( $post->post_type, $post_types, true ) )
176 return;
177
178 if ( apply_filters( 'pvc_admin_display_post_views', true ) === false )
179 return;
180
181 // add admin bar
182 add_action( 'admin_bar_menu', [ $this, 'admin_bar_menu' ], 100 );
183
184 // backend
185 if ( current_action() === 'admin_init' )
186 add_action( 'admin_head', [ $this, 'admin_bar_css' ] );
187 // frontend
188 elseif ( current_action() === 'wp' )
189 add_action( 'wp_head', [ $this, 'admin_bar_css' ] );
190 }
191
192 /**
193 * Add admin CSS.
194 *
195 * @return void
196 */
197 public function admin_bar_css() {
198 $html = '
199 <style type="text/css">
200 #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; }
201 #wp-admin-bar-pvc-post-views .pvc-line-graph {
202 display: inline-block;
203 width: 1px;
204 margin-right: 1px;
205 background-color: #ccc;
206 vertical-align: baseline;
207 }
208 #wp-admin-bar-pvc-post-views .pvc-line-graph:hover { background-color: #eee; }
209 #wp-admin-bar-pvc-post-views .pvc-line-graph-0 { height: 1% }';
210
211 for ( $i = 1; $i <= 20; $i ++ ) {
212 $html .= '
213 #wp-admin-bar-pvc-post-views .pvc-line-graph-' . $i . ' { height: ' . $i * 5 . '% }';
214 }
215
216 $html .= '
217 </style>';
218
219 echo wp_kses( $html, [ 'style' => [] ] );
220 }
221 }
222