class-admin.php
7 months ago
class-columns.php
7 months ago
class-counter.php
7 months ago
class-crawler-detect.php
7 months ago
class-cron.php
7 months ago
class-dashboard.php
7 months ago
class-frontend.php
7 months ago
class-functions.php
7 months ago
class-query.php
7 months ago
class-settings-api.php
7 months ago
class-settings.php
7 months ago
class-toolbar.php
7 months ago
class-update.php
7 months ago
class-widgets.php
7 months ago
functions.php
7 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 |