ajax.php
7 years ago
columns.php
4 years ago
counter.php
4 years ago
crawler-detect.php
6 years ago
cron.php
6 years ago
dashboard.php
4 years ago
frontend.php
6 years ago
functions.php
4 years ago
query.php
6 years ago
settings-api.php
4 years ago
settings.php
4 years ago
update.php
6 years ago
widgets.php
4 years ago
frontend.php
208 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 | if ( is_admin() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) |
| 48 | return; |
| 49 | |
| 50 | $filter = apply_filters( 'pvc_shortcode_filter_hook', Post_Views_Counter()->options['display']['position'] ); |
| 51 | |
| 52 | if ( ! empty( $filter ) && in_array( $filter, array( 'before', 'after' ) ) ) { |
| 53 | // post content |
| 54 | add_filter( 'the_content', array( $this, 'add_post_views_count' ) ); |
| 55 | |
| 56 | // bbpress support |
| 57 | add_action( 'bbp_template_' . $filter . '_single_topic', array( $this, 'display_bbpress_post_views' ) ); |
| 58 | add_action( 'bbp_template_' . $filter . '_single_forum', array( $this, 'display_bbpress_post_views' ) ); |
| 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 forum/topic of bbPress. |
| 68 | * |
| 69 | * @return string |
| 70 | */ |
| 71 | public function display_bbpress_post_views() { |
| 72 | $post_id = get_the_ID(); |
| 73 | |
| 74 | // check only for forums and topics |
| 75 | if ( bbp_is_forum( $post_id ) || bbp_is_topic( $post_id ) ) |
| 76 | echo $this->add_post_views_count( '' ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Add post views counter to content. |
| 81 | * |
| 82 | * @param string $content |
| 83 | * @return mixed |
| 84 | */ |
| 85 | public function add_post_views_count( $content = '' ) { |
| 86 | $display = false; |
| 87 | |
| 88 | // get post types |
| 89 | $post_types = Post_Views_Counter()->options['display']['post_types_display']; |
| 90 | |
| 91 | // get pages |
| 92 | $pages = Post_Views_Counter()->options['display']['page_types_display']; |
| 93 | |
| 94 | // page visibility check |
| 95 | if ( $pages ) { |
| 96 | foreach ( $pages as $page ) { |
| 97 | switch ( $page ) { |
| 98 | case 'singular': |
| 99 | if ( is_singular( $post_types ) ) |
| 100 | $display = true; |
| 101 | break; |
| 102 | |
| 103 | case 'archive': |
| 104 | if ( is_archive() ) |
| 105 | $display = true; |
| 106 | break; |
| 107 | |
| 108 | case 'search': |
| 109 | if ( is_search() ) |
| 110 | $display = true; |
| 111 | break; |
| 112 | |
| 113 | case 'home': |
| 114 | if ( is_home() || is_front_page() ) |
| 115 | $display = true; |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // get groups to check it faster |
| 122 | $groups = Post_Views_Counter()->options['display']['restrict_display']['groups']; |
| 123 | |
| 124 | // whether to display views |
| 125 | if ( is_user_logged_in() ) { |
| 126 | // exclude logged in users? |
| 127 | if ( in_array( 'users', $groups, true ) ) |
| 128 | $display = false; |
| 129 | // exclude specific roles? |
| 130 | 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'] ) ) |
| 131 | $display = false; |
| 132 | // exclude guests? |
| 133 | } elseif ( in_array( 'guests', $groups, true ) ) |
| 134 | $display = false; |
| 135 | |
| 136 | // we don't want to mess custom loops |
| 137 | if ( ! in_the_loop() && ! class_exists( 'bbPress' ) ) |
| 138 | $display = false; |
| 139 | |
| 140 | if ( apply_filters( 'pvc_display_views_count', $display ) === true ) { |
| 141 | $filter = apply_filters( 'pvc_shortcode_filter_hook', Post_Views_Counter()->options['display']['position'] ); |
| 142 | |
| 143 | switch ( $filter ) { |
| 144 | case 'after': |
| 145 | $content = $content . do_shortcode( '[post-views]' ); |
| 146 | break; |
| 147 | |
| 148 | case 'before': |
| 149 | $content = do_shortcode( '[post-views]' ) . $content; |
| 150 | break; |
| 151 | |
| 152 | case 'manual': |
| 153 | default: |
| 154 | break; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return $content; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Enqueue frontend scripts and styles. |
| 163 | */ |
| 164 | public function wp_enqueue_scripts() { |
| 165 | $mode = Post_Views_Counter()->options['general']['counter_mode']; |
| 166 | $post_types = Post_Views_Counter()->options['general']['post_types_count']; |
| 167 | |
| 168 | if ( (bool) apply_filters( 'pvc_enqueue_styles', true ) === true ) { |
| 169 | // load dashicons |
| 170 | wp_enqueue_style( 'dashicons' ); |
| 171 | |
| 172 | // load style |
| 173 | wp_enqueue_style( 'post-views-counter-frontend', POST_VIEWS_COUNTER_URL . '/css/frontend.css', array(), Post_Views_Counter()->defaults['version'] ); |
| 174 | } |
| 175 | |
| 176 | if ( in_array( $mode, array( 'js', 'ajax', 'rest_api' ) ) ) { |
| 177 | // whether to count this post type or not |
| 178 | if ( empty( $post_types ) || ! is_singular( $post_types ) ) |
| 179 | return; |
| 180 | |
| 181 | wp_register_script( 'post-views-counter-frontend', POST_VIEWS_COUNTER_URL . '/js/frontend.js', array( 'jquery' ), Post_Views_Counter()->defaults['version'], true ); |
| 182 | wp_enqueue_script( 'post-views-counter-frontend' ); |
| 183 | |
| 184 | $js_args = array( |
| 185 | 'mode' => $mode, |
| 186 | 'requestURL' => esc_url_raw( $mode == 'rest_api' ? rest_url( 'post-views-counter/view-post/') : admin_url( 'admin-ajax.php' ) ), |
| 187 | 'postID' => get_the_ID(), |
| 188 | 'nonce' => ( $mode == 'rest_api' ? wp_create_nonce( 'wp_rest' ) : wp_create_nonce( 'pvc-check-post' ) ) |
| 189 | ); |
| 190 | |
| 191 | switch ( $mode ) { |
| 192 | case 'rest_api': |
| 193 | $js_args['requestURL'] = rest_url( 'post-views-counter/view-post/' ); |
| 194 | break; |
| 195 | |
| 196 | case 'ajax': |
| 197 | $js_args['requestURL'] = POST_VIEWS_COUNTER_URL . '/includes/ajax.php'; |
| 198 | break; |
| 199 | |
| 200 | default: |
| 201 | $js_args['requestURL'] = admin_url( 'admin-ajax.php' ); |
| 202 | break; |
| 203 | } |
| 204 | |
| 205 | wp_localize_script( 'post-views-counter-frontend', 'pvcArgsFrontend', apply_filters( 'pvc_frontend_script_args', $js_args ) ); |
| 206 | } |
| 207 | } |
| 208 | } |