columns.php
10 years ago
counter.php
10 years ago
cron.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
135 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( 'after_setup_theme', 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 | * @param array $args |
| 30 | * @return mixed |
| 31 | */ |
| 32 | public function post_views_shortcode( $args ) { |
| 33 | $defaults = array( |
| 34 | 'id' => get_the_ID() |
| 35 | ); |
| 36 | |
| 37 | $args = shortcode_atts( $defaults, $args ); |
| 38 | |
| 39 | return pvc_post_views( $args['id'], false ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Add post views counter to content. |
| 44 | * |
| 45 | * @param mixed $content |
| 46 | * @return mixed |
| 47 | */ |
| 48 | public function add_post_views_count( $content ) { |
| 49 | if ( is_singular() && in_array( get_post_type(), Post_Views_Counter()->get_attribute( 'options', 'display', 'post_types_display' ), true ) ) { |
| 50 | |
| 51 | // get groups to check it faster |
| 52 | $groups = Post_Views_Counter()->get_attribute( 'options', 'display', 'restrict_display', 'groups' ); |
| 53 | |
| 54 | // whether to display views |
| 55 | if ( is_user_logged_in() ) { |
| 56 | // exclude logged in users? |
| 57 | if ( in_array( 'users', $groups, true ) ) |
| 58 | return $content; |
| 59 | // exclude specific roles? |
| 60 | 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' ) ) ) |
| 61 | return $content; |
| 62 | } |
| 63 | // exclude guests? |
| 64 | elseif ( in_array( 'guests', $groups, true ) ) |
| 65 | return $content; |
| 66 | |
| 67 | switch ( Post_Views_Counter()->get_attribute( 'options', 'display', 'position' ) ) { |
| 68 | case 'after': |
| 69 | return $content . '[post-views]'; |
| 70 | |
| 71 | case 'before': |
| 72 | return '[post-views]' . $content; |
| 73 | |
| 74 | default: |
| 75 | case 'manual': |
| 76 | return $content; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return $content; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Remove post views shortcode from excerpt. |
| 85 | * |
| 86 | * @param mixed $excerpt |
| 87 | * @return mixed |
| 88 | */ |
| 89 | public function remove_post_views_count( $excerpt ) { |
| 90 | remove_shortcode( 'post-views' ); |
| 91 | $excerpt = preg_replace( '/\[post-views[^\]]*\]/', '', $excerpt ); |
| 92 | return $excerpt; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Enqueue frontend scripts and styles. |
| 97 | */ |
| 98 | public function frontend_scripts_styles() { |
| 99 | $post_types = Post_Views_Counter()->get_attribute( 'options', 'display', 'post_types_display' ); |
| 100 | |
| 101 | // load dashicons |
| 102 | wp_enqueue_style( 'dashicons' ); |
| 103 | |
| 104 | wp_register_style( |
| 105 | 'post-views-counter-frontend', POST_VIEWS_COUNTER_URL . '/css/frontend.css' |
| 106 | ); |
| 107 | |
| 108 | wp_enqueue_style( 'post-views-counter-frontend' ); |
| 109 | |
| 110 | if ( Post_Views_Counter()->get_attribute( 'options', 'general', 'counter_mode' ) === 'js' ) { |
| 111 | $post_types = Post_Views_Counter()->get_attribute( 'options', 'general', 'post_types_count' ); |
| 112 | |
| 113 | // whether to count this post type or not |
| 114 | if ( empty( $post_types ) || ! is_singular( $post_types ) ) |
| 115 | return; |
| 116 | |
| 117 | wp_register_script( |
| 118 | 'post-views-counter-frontend', POST_VIEWS_COUNTER_URL . '/js/frontend.js', array( 'jquery' ) |
| 119 | ); |
| 120 | |
| 121 | wp_enqueue_script( 'post-views-counter-frontend' ); |
| 122 | |
| 123 | wp_localize_script( |
| 124 | 'post-views-counter-frontend', 'pvcArgsFrontend', array( |
| 125 | 'ajaxURL' => admin_url( 'admin-ajax.php' ), |
| 126 | 'postID' => get_the_ID(), |
| 127 | 'nonce' => wp_create_nonce( 'pvc-check-post' ), |
| 128 | 'postType' => get_post_type() |
| 129 | ) |
| 130 | ); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | } |
| 135 |