columns.php
12 years ago
counter.php
12 years ago
cron.php
12 years ago
frontend.php
11 years ago
functions.php
12 years ago
query.php
12 years ago
settings.php
12 years ago
update.php
12 years ago
widgets.php
12 years ago
frontend.php
143 lines
| 1 | <?php |
| 2 | if(!defined('ABSPATH')) exit; |
| 3 | |
| 4 | new Post_Views_Counter_Frontend(); |
| 5 | |
| 6 | class Post_Views_Counter_Frontend |
| 7 | { |
| 8 | public function __construct() |
| 9 | { |
| 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 | /** |
| 21 | * |
| 22 | */ |
| 23 | public function register_shortcode() |
| 24 | { |
| 25 | add_shortcode('post-views', array(&$this, 'post_views_shortcode')); |
| 26 | } |
| 27 | |
| 28 | |
| 29 | /** |
| 30 | * |
| 31 | */ |
| 32 | public function post_views_shortcode($args) |
| 33 | { |
| 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 | /** |
| 45 | * |
| 46 | */ |
| 47 | public function add_post_views_count($content) |
| 48 | { |
| 49 | if(is_singular() && in_array(get_post_type(), Post_Views_Counter()->get_attribute('options', 'display', 'post_types_display'), true)) |
| 50 | { |
| 51 | // gets 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 | { |
| 57 | // exclude logged in users? |
| 58 | if(in_array('users', $groups, true)) |
| 59 | return $content; |
| 60 | // exclude specific roles? |
| 61 | 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'))) |
| 62 | return $content; |
| 63 | } |
| 64 | // exclude guests? |
| 65 | elseif(in_array('guests', $groups, true)) |
| 66 | return $content; |
| 67 | |
| 68 | switch(Post_Views_Counter()->get_attribute('options', 'display', 'position')) |
| 69 | { |
| 70 | case 'after': |
| 71 | return $content.'[post-views]'; |
| 72 | |
| 73 | case 'before': |
| 74 | return '[post-views]'.$content; |
| 75 | |
| 76 | default: |
| 77 | case 'manual': |
| 78 | return $content; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return $content; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /** |
| 87 | * Remove post views shortcode from excerpt |
| 88 | */ |
| 89 | public function remove_post_views_count($excerpt) |
| 90 | { |
| 91 | remove_shortcode('post-views'); |
| 92 | $excerpt = preg_replace ('/\[post-views[^\]]*\]/', '', $excerpt); |
| 93 | return $excerpt; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | /** |
| 98 | * |
| 99 | */ |
| 100 | public function frontend_scripts_styles() |
| 101 | { |
| 102 | $post_types = Post_Views_Counter()->get_attribute('options', 'display', 'post_types_display'); |
| 103 | |
| 104 | // loads dashicons |
| 105 | wp_enqueue_style('dashicons'); |
| 106 | |
| 107 | wp_register_style( |
| 108 | 'post-views-counter-frontend', |
| 109 | POST_VIEWS_COUNTER_URL.'/css/frontend.css' |
| 110 | ); |
| 111 | |
| 112 | wp_enqueue_style('post-views-counter-frontend'); |
| 113 | |
| 114 | if(Post_Views_Counter()->get_attribute('options', 'general', 'counter_mode') === 'js') |
| 115 | { |
| 116 | $post_types = Post_Views_Counter()->get_attribute('options', 'general', 'post_types_count'); |
| 117 | |
| 118 | // whether to count this post type |
| 119 | if(empty($post_types) || !is_singular($post_types)) |
| 120 | return; |
| 121 | |
| 122 | wp_register_script( |
| 123 | 'post-views-counter-frontend', |
| 124 | POST_VIEWS_COUNTER_URL.'/js/frontend.js', |
| 125 | array('jquery') |
| 126 | ); |
| 127 | |
| 128 | wp_enqueue_script('post-views-counter-frontend'); |
| 129 | |
| 130 | wp_localize_script( |
| 131 | 'post-views-counter-frontend', |
| 132 | 'pvcArgsFrontend', |
| 133 | array( |
| 134 | 'ajaxURL' => admin_url('admin-ajax.php'), |
| 135 | 'postID' => get_the_ID(), |
| 136 | 'nonce' => wp_create_nonce('pvc-check-post'), |
| 137 | 'postType' => get_post_type() |
| 138 | ) |
| 139 | ); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | ?> |