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