columns.php
9 years ago
counter.php
9 years ago
crawler-detect.php
9 years ago
cron.php
9 years ago
dashboard.php
9 years ago
frontend.php
9 years ago
functions.php
9 years ago
query.php
9 years ago
settings.php
9 years ago
update.php
9 years ago
widgets.php
9 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( 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 |