/** * display post view count for given post * @param array $args an array of posts info contain: post_id, permalink, title * @param $display display or return */ function hwpvc_post_views($args = array(), $display = true) { if (isset($args['post_id'])) { $post_id = $args['post_id']; } else { $post_id = get_the_ID(); } //get permalink if (isset($args['permalink'])) { $permalink = $args['permalink']; } else { $permalink = ''; } //get default from function get_permalink();, but stored in __hw_post_views_count variable //get title if (isset($args['title'])) { $title = $args['title']; } else { $title = ''; } //get default from function get_the_title();, but stored in __hw_post_views_count variable //allow to update views if (isset($args['count']) && $args['count']) { $count = 'true'; } else { $count = 'false'; } // get all data $options = Post_Views_Counter()->get_attribute('options', 'display'); $hw_options = HW_Post_Views_Counter()->get_attribute('options', 'hoangweb'); //hoangweb options // $views = pvc_get_post_views( $post_id ); //prepare display $label = apply_filters('pvc_post_views_label', function_exists('icl_t') ? icl_t('Post Views Counter', 'Post Views Label', $options['label']) : $options['label'], $post_id); $icon_class = $options['icon_class'] !== '' ? ' ' . esc_attr($options['icon_class']) : ''; $icon = apply_filters('pvc_post_views_icon', '<span class="hw-hidden post-views-icon dashicons ' . $icon_class . '"></span>', $post_id); $use_firebase = isset($hw_options['use_firebase']) ? $hw_options['use_firebase'] : ''; $view = '<div class="hw-post-views-icon post-views post-' . $post_id . ' entry-meta">' . ($options['display_style']['icon'] && $icon_class !== '' ? $icon : '') . ($options['display_style']['text'] ? '<span class="post-views-label">' . $label . ' </span>' : '') . '<span title="' . $title . '" class="post-views-count hw-post-views-count preloading" data-count="' . $count . '" data-permalink="' . $permalink . '" data-id="' . $post_id . '" data-title="' . $title . '"> <!-- js process --> </span></div>'; $view = apply_filters('pvc_post_views_html', $view, $args, $use_firebase, $icon); if ($display) { echo $view; } else { return $view; } }
/** * display field for firebase path */ public function hw_firebase_path_field() { $firebase_path = esc_attr(HW_Post_Views_Counter()->get_attribute('options', 'hoangweb', 'firebase_path')); echo ' <div id="hw_pvc_firebase_path"> <fieldset> <input id="pvc-hw-firebase-path" type="text" name="post_views_counter_settings_hoangweb[firebase_path]" value="' . $firebase_path . '"/><label for="pvc-hw-firebase-path"></label> <p>' . esc_html__('Nhập địa chỉ firebase bạn muốn lưu trữ bộ đếm post views. VD: /hoangwebcom/.', 'post-views-counter') . '</p> </fieldset> </div>'; }
/** * frontend scripts styles */ public function _frontend_scripts_styles() { //$post_types = Post_Views_Counter()->get_attribute( 'options', 'display', 'post_types_display' ); $post_types = Post_Views_Counter()->get_attribute('options', 'general', 'post_types_count'); $hwpvc_options = HW_Post_Views_Counter()->get_attribute('options', 'hoangweb'); $this->serialize_obj['options'] = $hwpvc_options; //get current post $post_id = get_the_ID(); //single post or inside loop posts if ($post_id) { $this->serialize_obj['postID'] = $post_id; $this->serialize_obj['postType'] = get_post_type(); $this->serialize_obj['post_title'] = get_the_title($post_id); $this->serialize_obj['post_permalink'] = get_permalink($post_id); } // whether to count this post type or not if (is_single() && (empty($post_types) || !is_singular($post_types))) { //allow all return; } //firebase lib if (isset($hwpvc_options['use_firebase']) && $hwpvc_options['use_firebase']) { wp_register_script('firebase.js', 'https://cdn.firebase.com/js/client/2.2.6/firebase.js'); wp_enqueue_script('firebase.js'); } //css wp_register_style('hw-post-views-counter-frontend', HWPVC_PLUGIN_URL . '/css/hw-frontend.css'); wp_enqueue_style('hw-post-views-counter-frontend'); //js wp_register_script('hw-post-views-counter-frontend', HWPVC_PLUGIN_URL . '/js/hw-frontend.js', array('jquery')); wp_enqueue_script('hw-post-views-counter-frontend'); wp_localize_script('hw-post-views-counter-frontend', '__hw_post_views_count', $this->serialize_obj); }