PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.2.2
Post Views Counter v1.2.2
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 / frontend.php
post-views-counter / includes Last commit date
columns.php 10 years ago counter.php 10 years ago cron.php 10 years ago dashboard.php 10 years ago frontend.php 10 years ago functions.php 10 years ago query.php 10 years ago settings.php 10 years ago update.php 10 years ago widgets.php 10 years ago
frontend.php
183 lines
1 <?php
2 // exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) )
4 exit;
5
6 /**
7 * Post_Views_Counter_Frontend class.
8 */
9 class Post_Views_Counter_Frontend {
10
11 public function __construct() {
12 // actions
13 add_action( 'after_setup_theme', array( $this, 'register_shortcode' ) );
14 add_action( 'wp_enqueue_scripts', array( $this, 'frontend_scripts_styles' ) );
15 add_action( 'wp', array( $this, 'run' ) );
16 }
17
18 /**
19 * Register post-views shortcode function.
20 */
21 public function register_shortcode() {
22 add_shortcode( 'post-views', array( $this, 'post_views_shortcode' ) );
23 }
24
25 /**
26 * Post views shortcode function.
27 *
28 * @param array $args
29 * @return mixed
30 */
31 public function post_views_shortcode( $args ) {
32 $defaults = array(
33 'id' => get_the_ID()
34 );
35
36 $args = shortcode_atts( $defaults, $args );
37
38 return pvc_post_views( $args['id'], false );
39 }
40
41 /**
42 * Set up plugin hooks.
43 */
44 public function run() {
45
46 if ( is_admin() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) )
47 return;
48
49 $filter = apply_filters( 'pvc_shortcode_filter_hook', Post_Views_Counter()->options['display']['position'] );
50
51 if ( ! empty( $filter ) && in_array( $filter, array( 'before', 'after' ) ) ) {
52 // post content
53 add_filter( 'the_content', array( $this, 'add_post_views_count' ) );
54 // add_filter( 'the_excerpt', array( $this, 'add_post_views_count' ) );
55
56 // bbpress
57 add_filter( 'bbp_get_topic_content', array( $this, 'add_post_views_count' ) );
58 add_filter( 'bbp_get_reply_content', array( $this, 'add_post_views_count' ) );
59 } else {
60 // custom
61 if ( $filter != 'manual' && is_string( $filter ) )
62 add_filter( $filter, array( $this, 'add_post_views_count' ) );
63 }
64 }
65
66 /**
67 * Add post views counter to content.
68 *
69 * @param mixed $content
70 * @return mixed
71 */
72 public function add_post_views_count( $content = '' ) {
73 global $post, $wp_current_filter;
74
75 $display = false;
76
77 // get post types
78 $post_types = Post_Views_Counter()->options['display']['post_types_display'];
79
80 // get pages
81 $pages = Post_Views_Counter()->options['display']['page_types_display'];
82
83 // page visibility check
84 if ( $pages ) {
85 foreach ( $pages as $page ) {
86 switch ( $page ) {
87 case 'singular' :
88 if ( is_singular( $post_types ) ) $display = true;
89 break;
90 case 'archive' :
91 if ( is_archive() ) $display = true;
92 break;
93 case 'search' :
94 if ( is_search() ) $display = true;
95 break;
96 case 'home' :
97 if ( is_home() || is_front_page() ) $display = true;
98 break;
99 }
100 }
101 }
102
103 // get groups to check it faster
104 $groups = Post_Views_Counter()->options['display']['restrict_display']['groups'];
105
106 // whether to display views
107 if ( is_user_logged_in() ) {
108 // exclude logged in users?
109 if ( in_array( 'users', $groups, true ) )
110 $display = false;
111 // exclude specific roles?
112 elseif ( in_array( 'roles', $groups, true ) && Post_Views_Counter()->counter->is_user_role_excluded( Post_Views_Counter()->options['display']['restrict_display']['roles'] ) )
113 $display = false;
114 }
115 // exclude guests?
116 elseif ( in_array( 'guests', $groups, true ) )
117 $display = false;
118
119 // we don't want to mess custom loops
120 if ( ! in_the_loop() )
121 $display = false;
122
123 if ( apply_filters( 'pvc_display_views_count', $display ) === true ) {
124
125 $filter = apply_filters( 'pvc_shortcode_filter_hook', Post_Views_Counter()->options['display']['position'] );
126
127 switch ( $filter ) {
128 case 'after':
129 $content = $content . do_shortcode( '[post-views]' );
130 break;
131
132 case 'before':
133 $content = do_shortcode( '[post-views]' ) . $content;
134 break;
135
136 case 'manual':
137 default:
138 break;
139 }
140 }
141
142 return $content;
143 }
144
145 /**
146 * Enqueue frontend scripts and styles.
147 */
148 public function frontend_scripts_styles() {
149 $post_types = Post_Views_Counter()->options['display']['post_types_display'];
150
151 // load dashicons
152 wp_enqueue_style( 'dashicons' );
153
154 // load style
155 wp_enqueue_style( 'post-views-counter-frontend', POST_VIEWS_COUNTER_URL . '/css/frontend.css' );
156
157 if ( Post_Views_Counter()->options['general']['counter_mode'] === 'js' ) {
158 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
159
160 // whether to count this post type or not
161 if ( empty( $post_types ) || ! is_singular( $post_types ) )
162 return;
163
164 wp_register_script(
165 'post-views-counter-frontend', POST_VIEWS_COUNTER_URL . '/js/frontend.js', array( 'jquery' )
166 );
167
168 wp_enqueue_script( 'post-views-counter-frontend' );
169
170 wp_localize_script(
171 'post-views-counter-frontend',
172 'pvcArgsFrontend',
173 array(
174 'ajaxURL' => admin_url( 'admin-ajax.php' ),
175 'postID' => get_the_ID(),
176 'nonce' => wp_create_nonce( 'pvc-check-post' ),
177 'postType' => get_post_type()
178 )
179 );
180 }
181 }
182 }
183