columns.php
11 years ago
counter.php
11 years ago
cron.php
11 years ago
frontend.php
11 years ago
functions.php
11 years ago
query.php
11 years ago
settings.php
11 years ago
update.php
11 years ago
widgets.php
11 years ago
frontend.php
126 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) |
| 3 | exit; |
| 4 | |
| 5 | new Post_Views_Counter_Frontend(); |
| 6 | |
| 7 | class Post_Views_Counter_Frontend { |
| 8 | |
| 9 | public function __construct() { |
| 10 | // actions |
| 11 | add_action( 'wp_loaded', array( &$this, 'register_shortcode' ) ); |
| 12 | add_action( 'wp_enqueue_scripts', array( &$this, 'frontend_scripts_styles' ) ); |
| 13 | |
| 14 | // filters |
| 15 | add_filter( 'the_content', array( &$this, 'add_post_views_count' ) ); |
| 16 | add_filter( 'the_excerpt', array( &$this, 'remove_post_views_count' ) ); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Register post-views shortcode function. |
| 21 | */ |
| 22 | public function register_shortcode() { |
| 23 | add_shortcode( 'post-views', array( &$this, 'post_views_shortcode' ) ); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Post views shortcode function. |
| 28 | */ |
| 29 | public function post_views_shortcode( $args ) { |
| 30 | $defaults = array( |
| 31 | 'id' => get_the_ID() |
| 32 | ); |
| 33 | |
| 34 | $args = shortcode_atts( $defaults, $args ); |
| 35 | |
| 36 | return pvc_post_views( $args['id'], false ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Add post views counter to content. |
| 41 | */ |
| 42 | public function add_post_views_count( $content ) { |
| 43 | if ( is_singular() && in_array( get_post_type(), Post_Views_Counter()->get_attribute( 'options', 'display', 'post_types_display' ), true ) ) { |
| 44 | |
| 45 | // get groups to check it faster |
| 46 | $groups = Post_Views_Counter()->get_attribute( 'options', 'display', 'restrict_display', 'groups' ); |
| 47 | |
| 48 | // whether to display views |
| 49 | if ( is_user_logged_in() ) { |
| 50 | // exclude logged in users? |
| 51 | if ( in_array( 'users', $groups, true ) ) |
| 52 | return $content; |
| 53 | // exclude specific roles? |
| 54 | elseif ( in_array( 'roles', $groups, true ) && Post_Views_Counter()->get_instance( 'counter' )->is_user_roles_excluded( Post_Views_Counter()->get_attribute( 'options', 'display', 'restrict_display', 'roles' ) ) ) |
| 55 | return $content; |
| 56 | } |
| 57 | // exclude guests? |
| 58 | elseif ( in_array( 'guests', $groups, true ) ) |
| 59 | return $content; |
| 60 | |
| 61 | switch ( Post_Views_Counter()->get_attribute( 'options', 'display', 'position' ) ) { |
| 62 | case 'after': |
| 63 | return $content . '[post-views]'; |
| 64 | |
| 65 | case 'before': |
| 66 | return '[post-views]' . $content; |
| 67 | |
| 68 | default: |
| 69 | case 'manual': |
| 70 | return $content; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return $content; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Remove post views shortcode from excerpt. |
| 79 | */ |
| 80 | public function remove_post_views_count( $excerpt ) { |
| 81 | remove_shortcode( 'post-views' ); |
| 82 | $excerpt = preg_replace( '/\[post-views[^\]]*\]/', '', $excerpt ); |
| 83 | return $excerpt; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Enqueue frontend scripts and styles. |
| 88 | */ |
| 89 | public function frontend_scripts_styles() { |
| 90 | $post_types = Post_Views_Counter()->get_attribute( 'options', 'display', 'post_types_display' ); |
| 91 | |
| 92 | // load dashicons |
| 93 | wp_enqueue_style( 'dashicons' ); |
| 94 | |
| 95 | wp_register_style( |
| 96 | 'post-views-counter-frontend', POST_VIEWS_COUNTER_URL . '/css/frontend.css' |
| 97 | ); |
| 98 | |
| 99 | wp_enqueue_style( 'post-views-counter-frontend' ); |
| 100 | |
| 101 | if ( Post_Views_Counter()->get_attribute( 'options', 'general', 'counter_mode' ) === 'js' ) { |
| 102 | $post_types = Post_Views_Counter()->get_attribute( 'options', 'general', 'post_types_count' ); |
| 103 | |
| 104 | // whether to count this post type or not |
| 105 | if ( empty( $post_types ) || ! is_singular( $post_types ) ) |
| 106 | return; |
| 107 | |
| 108 | wp_register_script( |
| 109 | 'post-views-counter-frontend', POST_VIEWS_COUNTER_URL . '/js/frontend.js', array( 'jquery' ) |
| 110 | ); |
| 111 | |
| 112 | wp_enqueue_script( 'post-views-counter-frontend' ); |
| 113 | |
| 114 | wp_localize_script( |
| 115 | 'post-views-counter-frontend', 'pvcArgsFrontend', array( |
| 116 | 'ajaxURL' => admin_url( 'admin-ajax.php' ), |
| 117 | 'postID' => get_the_ID(), |
| 118 | 'nonce' => wp_create_nonce( 'pvc-check-post' ), |
| 119 | 'postType' => get_post_type() |
| 120 | ) |
| 121 | ); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | } |
| 126 |