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 |