/** * Check whether WP Cron needs to add new task. */ public function check_cron() { if (!is_admin()) { return; } // set wp cron task if (Post_Views_Counter()->get_attribute('options', 'general', 'cron_run')) { // not set or need to be updated? if (!wp_next_scheduled('pvc_reset_counts') || Post_Views_Counter()->get_attribute('options', 'general', 'cron_update')) { // task is added but need to be updated if (Post_Views_Counter()->get_attribute('options', 'general', 'cron_update')) { // remove old schedule wp_clear_scheduled_hook('pvc_reset_counts'); // set update to false $general = Post_Views_Counter()->get_attribute('options', 'general'); $general['cron_update'] = false; // update settings update_option('post_views_counter_settings_general', $general); } // set schedule wp_schedule_event(Post_Views_Counter()->get_instance('counter')->get_timestamp(Post_Views_Counter()->get_attribute('options', 'general', 'reset_counts', 'type'), Post_Views_Counter()->get_attribute('options', 'general', 'reset_counts', 'number')), 'post_views_counter_interval', 'pvc_reset_counts'); } } else { // remove schedule wp_clear_scheduled_hook('pvc_reset_counts'); remove_action('pvc_reset_counts', array(&$this, 'reset_counts')); } }
/** * Enqueue frontend scripts and styles. */ public function frontend_scripts_styles() { $post_types = Post_Views_Counter()->options['display']['post_types_display']; // load dashicons wp_enqueue_style('dashicons'); // load style wp_enqueue_style('post-views-counter-frontend', POST_VIEWS_COUNTER_URL . '/css/frontend.css'); if (Post_Views_Counter()->options['general']['counter_mode'] === 'js') { $objpgarray = get_queried_object(); if ($objpgarray->term_id) { $taxtype = $objpgarray->taxonomy; $taxid = $objpgarray->term_id; $tax_types = Post_Views_Counter()->options['general']['tax_types_count']; // whether to count this post type or not if (!in_array($taxtype, $tax_types)) { return; } wp_register_script('post-views-counter-frontend', POST_VIEWS_COUNTER_URL . '/js/frontend.js', array('jquery')); wp_enqueue_script('post-views-counter-frontend'); wp_localize_script('post-views-counter-frontend', 'pvcArgsFrontend', array('ajaxURL' => admin_url('admin-ajax.php'), 'postID' => $taxid, 'nonce' => wp_create_nonce('pvc-check-post'), 'postType' => $taxtype, 'istax' => 1)); } else { $post_types = Post_Views_Counter()->options['general']['post_types_count']; // whether to count this post type or not if (empty($post_types) || !is_singular($post_types)) { return; } wp_register_script('post-views-counter-frontend', POST_VIEWS_COUNTER_URL . '/js/frontend.js', array('jquery')); wp_enqueue_script('post-views-counter-frontend'); $taxid = $taxtype = ''; if ($objpgarray->term_id) { $taxid = $objpgarray->term_id; $taxtype = $objpgarray->taxonomy; } wp_localize_script('post-views-counter-frontend', 'pvcArgsFrontend', array('ajaxURL' => admin_url('admin-ajax.php'), 'postID' => get_the_ID(), 'nonce' => wp_create_nonce('pvc-check-post'), 'postType' => get_post_type(), 'istax' => 0)); } //Else start } //js end }
/** * Check if there's a db update required */ public function check_update() { if (!current_user_can('manage_options')) { return; } // get current database version $current_db_version = get_option('post_views_counter_version', '1.0.0'); // new version? if (version_compare($current_db_version, Post_Views_Counter()->defaults['version'], '<')) { // update plugin version update_option('post_views_counter_version', Post_Views_Counter()->defaults['version']); } }
/** * 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; } }
/** * Enqueue frontend scripts and styles. */ public function frontend_scripts_styles() { $post_types = Post_Views_Counter()->get_attribute('options', 'display', 'post_types_display'); // load dashicons wp_enqueue_style('dashicons'); wp_register_style('post-views-counter-frontend', Post_Views_Counter_URL . '/css/frontend.css'); wp_enqueue_style('post-views-counter-frontend'); if (Post_Views_Counter()->get_attribute('options', 'general', 'counter_mode') === 'js') { $post_types = Post_Views_Counter()->get_attribute('options', 'general', 'post_types_count'); // whether to count this post type or not if (empty($post_types) || !is_singular($post_types)) { return; } wp_register_script('post-views-counter-frontend', Post_Views_Counter_URL . '/js/frontend.js', array('jquery')); wp_enqueue_script('post-views-counter-frontend'); wp_localize_script('post-views-counter-frontend', 'pvcArgsFrontend', array('ajaxURL' => admin_url('admin-ajax.php'), 'postID' => get_the_ID(), 'nonce' => wp_create_nonce('pvc-check-post'), 'postType' => get_post_type())); } }
function pvc_post_views($post_id = 0, $display = true) { // get all data $post_id = (int) (empty($post_id) ? get_the_ID() : $post_id); $options = Post_Views_Counter()->get_attribute('options', 'display'); $views = pvc_get_post_views($post_id); // prepares 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="post-views-icon dashicons ' . $icon_class . '"></span>', $post_id); $html = apply_filters('pvc_post_views_html', '<div class="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 class="post-views-count">' . number_format_i18n($views) . '</span> </div>', $post_id, $views, $label, $icon); if ($display) { echo $html; } else { return $html; } }
/** * Register post views column for specific post types */ public function register_new_column() { $screen = get_current_screen(); if (Post_Views_Counter()->get_attribute('options', 'general', 'post_views_column') && ($screen->base == 'edit' && in_array($screen->post_type, Post_Views_Counter()->get_attribute('options', 'general', 'post_types_count')))) { foreach (Post_Views_Counter()->get_attribute('options', 'general', 'post_types_count') as $post_type) { if ($post_type === 'page' && $screen->post_type === 'page') { // actions add_action('manage_pages_custom_column', array(&$this, 'add_new_column_content'), 10, 2); // filters add_filter('manage_pages_columns', array(&$this, 'add_new_column')); add_filter('manage_edit-page_sortable_columns', array(&$this, 'register_sortable_custom_column')); } elseif ($post_type === 'post' && $screen->post_type === 'post') { // actions add_action('manage_posts_custom_column', array(&$this, 'add_new_column_content'), 10, 2); // filters add_filter('manage_posts_columns', array(&$this, 'add_new_column')); add_filter('manage_edit-post_sortable_columns', array(&$this, 'register_sortable_custom_column')); } elseif ($screen->post_type === $post_type) { // actions add_action('manage_' . $post_type . '_posts_custom_column', array(&$this, 'add_new_column_content'), 10, 2); // filters add_filter('manage_' . $post_type . '_posts_columns', array(&$this, 'add_new_column')); add_filter('manage_edit-' . $post_type . '_sortable_columns', array(&$this, 'register_sortable_custom_column')); } } } }
/** * Enqueue admin scripts and styles */ public function admin_scripts_styles($page) { wp_register_style('pvc-admin', POST_VIEWS_COUNTER_URL . '/css/admin.css'); wp_register_style('pvc-chosen', POST_VIEWS_COUNTER_URL . '/assets/chosen/chosen.min.css'); wp_register_script('pvc-admin-chosen', POST_VIEWS_COUNTER_URL . '/assets/chosen/chosen.jquery.min.js', array('jquery'), $this->defaults['version']); wp_register_script('pvc-admin-settings', POST_VIEWS_COUNTER_URL . '/js/admin-settings.js', array('jquery', 'pvc-admin-chosen'), $this->defaults['version']); wp_register_script('pvc-admin-post', POST_VIEWS_COUNTER_URL . '/js/admin-post.js', array('jquery'), $this->defaults['version']); // load on PVC settings page if ($page === 'settings_page_post-views-counter') { wp_enqueue_script('pvc-admin-chosen'); wp_enqueue_script('pvc-admin-settings'); wp_localize_script('pvc-admin-settings', 'pvcArgsSettings', array('resetToDefaults' => __('Are you sure you want to reset these settings to defaults?', 'post-views-counter'))); wp_enqueue_style('pvc-chosen'); wp_enqueue_style('pvc-admin'); // load on single post page } elseif ($page = 'post.php') { $post_types = Post_Views_Counter()->get_attribute('options', 'general', 'post_types_count'); if (!in_array(get_post_type(), (array) $post_types)) { return; } wp_enqueue_style('pvc-admin'); wp_enqueue_script('pvc-admin-post'); } }
/** * 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); }
</li> <?php } $cat_list = get_the_term_list($post->ID, 'portfolio_cat', '', ', ', ''); if (in_array('cats', $porto_settings['portfolio-metas']) && $cat_list) { ?> <li> <i class="fa fa-tags"></i> <?php echo $cat_list; ?> </li> <?php } ?> <?php if (function_exists('Post_Views_Counter') && Post_Views_Counter()->options['display']['position'] == 'manual') { $post_count = do_shortcode('[post-views]'); if ($post_count) { ?> <li> <?php echo $post_count; ?> </li> <?php } } ?> </ul> </div>
/** * */ private function save_cookie($id, $cookie = array(), $expired = true) { $expiration = $this->get_timestamp(Post_Views_Counter()->get_attribute('options', 'general', 'time_between_counts', 'type'), Post_Views_Counter()->get_attribute('options', 'general', 'time_between_counts', 'number')); // is it new cookie? if (empty($cookie)) { // sets cookie setcookie('pvc_visits[0]', $expiration . 'b' . $id, $expiration, COOKIEPATH, COOKIE_DOMAIN, isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? true : false, true); } else { if ($expired) { // adds new id or changes expiration date if id already exists $cookie['visited_posts'][$id] = $expiration; } // creates copy for better foreach performance $visited_posts_expirations = $cookie['visited_posts']; // gets current gmt time $time = current_time('timestamp', true); // checks whether viewed id has expired - no need to keep it in cookie (less size) foreach ($visited_posts_expirations as $post_id => $post_expiration) { if ($time > $post_expiration) { unset($cookie['visited_posts'][$post_id]); } } // sets new last expiration date if needed $cookie['expiration'] = max($cookie['visited_posts']); $cookies = $imploded = array(); // creates pairs foreach ($cookie['visited_posts'] as $id => $exp) { $imploded[] = $exp . 'b' . $id; } // splits cookie into chunks (4000 bytes to make sure it is safe for every browser) $chunks = str_split(implode('a', $imploded), 4000); // more then one chunk? if (count($chunks) > 1) { $last_id = ''; foreach ($chunks as $chunk_id => $chunk) { // new chunk $chunk_c = $last_id . $chunk; // is it full-length chunk? if (strlen($chunk) === 4000) { // gets last part $last_part = strrchr($chunk_c, 'a'); // gets last id $last_id = substr($last_part, 1); // adds new full-lenght chunk $cookies[$chunk_id] = substr($chunk_c, 0, strlen($chunk_c) - strlen($last_part)); } else { // adds last chunk $cookies[$chunk_id] = $chunk_c; } } } else { // only one chunk $cookies[] = $chunks[0]; } foreach ($cookies as $key => $value) { // sets cookie setcookie('pvc_visits[' . $key . ']', $value, $cookie['expiration'], COOKIEPATH, COOKIE_DOMAIN, isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? true : false, true); } } }
/** * View post manually function. * * @since 1.2.0 * @param int $post_id * @return bool */ function pvc_view_post($post_id = 0) { $post_id = (int) (empty($post_id) ? get_the_ID() : $post_id); if (!$post_id) { return false; } Post_Views_Counter()->counter->check_post($post_id); return true; }
/** * Validate general settings. */ public function validate_settings($input) { if (isset($_POST['post_views_counter_import_wp_postviews'])) { global $wpdb; $views = $wpdb->get_results("SELECT post_id, meta_value FROM " . $wpdb->postmeta . " WHERE meta_key = 'views'", ARRAY_A, 0); if (!empty($views)) { $input = Post_Views_Counter()->get_attribute('defaults', 'general'); $input['wp_postviews_import'] = true; $sql = ''; foreach ($views as $view) { $sql[] = "(" . $view['post_id'] . ", 4, 'total', " . $view['meta_value'] . ")"; } $wpdb->query("INSERT INTO " . $wpdb->prefix . "post_views(id, type, period, count) VALUES " . implode(',', $sql) . " ON DUPLICATE KEY UPDATE count = " . (isset($_POST['post_views_counter_import_wp_postviews_override']) ? '' : 'count + ') . "VALUES(count)"); add_settings_error('wp_postviews_import', 'wp_postviews_import', __('WP-PostViews data imported succesfully.', 'post-views-counter'), 'updated'); } else { add_settings_error('wp_postviews_import', 'wp_postviews_import', __('There was no data to import.', 'post-views-counter'), 'updated'); } } elseif (isset($_POST['save_pvc_general'])) { // post types count if (isset($input['post_types_count'])) { $post_types = array(); foreach ($input['post_types_count'] as $post_type) { if (isset($this->post_types[$post_type])) { $post_types[] = $post_type; } } $input['post_types_count'] = array_unique($post_types); } else { $input['post_types_count'] = array(); } // counter mode $input['counter_mode'] = isset($input['counter_mode'], $this->modes[$input['counter_mode']]) ? $input['counter_mode'] : Post_Views_Counter()->get_attribute('defaults', 'general', 'counter_mode'); // post views column $input['post_views_column'] = isset($input['post_views_column']) ? (bool) $input['post_views_column'] : Post_Views_Counter()->get_attribute('defaults', 'general', 'post_views_column'); // time between counts $input['time_between_counts']['number'] = (int) (isset($input['time_between_counts']['number']) ? $input['time_between_counts']['number'] : Post_Views_Counter()->get_attribute('defaults', 'general', 'time_between_counts', 'number')); $input['time_between_counts']['type'] = isset($input['time_between_counts']['type'], $this->time_types[$input['time_between_counts']['type']]) ? $input['time_between_counts']['type'] : Post_Views_Counter()->get_attribute('defaults', 'general', 'time_between_counts', 'type'); // flush interval $input['flush_interval']['number'] = (int) (isset($input['flush_interval']['number']) ? $input['flush_interval']['number'] : Post_Views_Counter()->get_attribute('defaults', 'general', 'flush_interval', 'number')); $input['flush_interval']['type'] = isset($input['flush_interval']['type'], $this->time_types[$input['flush_interval']['type']]) ? $input['flush_interval']['type'] : Post_Views_Counter()->get_attribute('defaults', 'general', 'flush_interval', 'type'); // Since the settings are about to be saved and cache flush interval could've changed, // we want to make sure that any changes done on the settings page are in effect immediately // (instead of having to wait for the previous schedule to occur). // We achieve that by making sure to clear any previous cache flush schedules and // schedule the new one if the specified interval is > 0 Post_Views_Counter()->remove_cache_flush(); if ($input['flush_interval']['number'] > 0) { Post_Views_Counter()->schedule_cache_flush(); } // reset counts $input['reset_counts']['number'] = (int) (isset($input['reset_counts']['number']) ? $input['reset_counts']['number'] : Post_Views_Counter()->get_attribute('defaults', 'general', 'reset_counts', 'number')); $input['reset_counts']['type'] = isset($input['reset_counts']['type'], $this->time_types[$input['reset_counts']['type']]) ? $input['reset_counts']['type'] : Post_Views_Counter()->get_attribute('defaults', 'general', 'reset_counts', 'type'); // run cron on next visit? $input['cron_run'] = $input['reset_counts']['number'] > 0 ? true : false; $input['cron_update'] = $input['cron_run'] && (Post_Views_Counter()->get_attribute('options', 'general', 'reset_counts', 'number') !== $input['reset_counts']['number'] || Post_Views_Counter()->get_attribute('options', 'general', 'reset_counts', 'type') !== $input['reset_counts']['type']) ? true : false; // exclude if (isset($input['exclude']['groups'])) { $groups = array(); foreach ($input['exclude']['groups'] as $group) { if (isset($this->groups[$group])) { $groups[] = $group; } } $input['exclude']['groups'] = array_unique($groups); } else { $input['exclude']['groups'] = array(); } if (in_array('roles', $input['exclude']['groups'], true) && isset($input['exclude']['roles'])) { $roles = array(); foreach ($input['exclude']['roles'] as $role) { if (isset($this->user_roles[$role])) { $roles[] = $role; } } $input['exclude']['roles'] = array_unique($roles); } else { $input['exclude']['roles'] = array(); } // exclude ips if (isset($input['exclude_ips'])) { $ips = array(); foreach ($input['exclude_ips'] as $ip) { if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { $ips[] = $ip; } } $input['exclude_ips'] = array_unique($ips); } // restrict edit viewa $input['restrict_edit_views'] = isset($input['restrict_edit_views']) ? (bool) $input['restrict_edit_views'] : Post_Views_Counter()->get_attribute('defaults', 'general', 'restrict_edit_views'); // deactivation delete $input['deactivation_delete'] = isset($input['deactivation_delete']) ? (bool) $input['deactivation_delete'] : Post_Views_Counter()->get_attribute('defaults', 'general', 'deactivation_delete'); } elseif (isset($_POST['save_pvc_display'])) { // post views label $input['label'] = isset($input['label']) ? $input['label'] : Post_Views_Counter()->get_attribute('defaults', 'general', 'label'); if (function_exists('icl_register_string')) { icl_register_string('Post Views Counter', 'Post Views Label', $input['label']); } // position $input['position'] = isset($input['position'], $this->positions[$input['position']]) ? $input['position'] : Post_Views_Counter()->get_attribute('defaults', 'general', 'position'); // display style $input['display_style']['icon'] = isset($input['display_style']['icon']) ? true : false; $input['display_style']['text'] = isset($input['display_style']['text']) ? true : false; // link to post $input['link_to_post'] = isset($input['link_to_post']) ? (bool) $input['link_to_post'] : Post_Views_Counter()->get_attribute('defaults', 'general', 'link_to_post'); // icon class $input['icon_class'] = isset($input['icon_class']) ? trim($input['icon_class']) : Post_Views_Counter()->get_attribute('defaults', 'general', 'icon_class'); // post types display if (isset($input['post_types_display'])) { $post_types = array(); foreach ($input['post_types_display'] as $post_type) { if (isset($this->post_types[$post_type])) { $post_types[] = $post_type; } } $input['post_types_display'] = array_unique($post_types); } else { $input['post_types_display'] = array(); } // restrict display if (isset($input['restrict_display']['groups'])) { $groups = array(); foreach ($input['restrict_display']['groups'] as $group) { if ($group === 'robots') { continue; } if (isset($this->groups[$group])) { $groups[] = $group; } } $input['restrict_display']['groups'] = array_unique($groups); } else { $input['restrict_display']['groups'] = array(); } if (in_array('roles', $input['restrict_display']['groups'], true) && isset($input['restrict_display']['roles'])) { $roles = array(); foreach ($input['restrict_display']['roles'] as $role) { if (isset($this->user_roles[$role])) { $roles[] = $role; } } $input['restrict_display']['roles'] = array_unique($roles); } else { $input['restrict_display']['roles'] = array(); } } elseif (isset($_POST['reset_pvc_general'])) { $input = Post_Views_Counter()->get_attribute('defaults', 'general'); add_settings_error('reset_general_settings', 'settings_reset', __('General settings restored to defaults.', 'post-views-counter'), 'updated'); } elseif (isset($_POST['reset_pvc_display'])) { $input = Post_Views_Counter()->get_attribute('defaults', 'display'); add_settings_error('reset_general_settings', 'settings_reset', __('Display settings restored to defaults.', 'post-views-counter'), 'updated'); } return $input; }
/** * Get selected post types. */ public function load_post_types() { $this->pvc_post_types = Post_Views_Counter()->settings->post_types; }
/** * Get selected post types */ public function load_post_types() { $this->pvc_post_types = Post_Views_Counter()->get_instance('settings')->post_types; }
/** * Enqueue admin scripts and styles. * * @param string $pagenow */ public function admin_scripts_styles($pagenow) { if ($pagenow != 'index.php') { return; } // filter user_can_see_stats if (!apply_filters('pvc_user_can_see_stats', current_user_can('publish_posts'))) { return; } wp_register_style('pvc-admin-dashboard', POST_VIEWS_COUNTER_URL . '/css/admin-dashboard.css'); wp_enqueue_style('pvc-admin-dashboard'); wp_register_script('pvc-admin-dashboard', POST_VIEWS_COUNTER_URL . '/js/admin-dashboard.js', array('jquery', 'pvc-chart'), Post_Views_Counter()->defaults['version'], true); wp_register_script('pvc-chart', POST_VIEWS_COUNTER_URL . '/js/chart.min.js', array('jquery'), Post_Views_Counter()->defaults['version'], true); // set ajax args $ajax_args = array('ajaxURL' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('dashboard-chart')); wp_enqueue_script('pvc-admin-dashboard'); // wp_enqueue_script( 'pvc-chart' ); wp_localize_script('pvc-admin-dashboard', 'pvcArgs', $ajax_args); }
/** * Check if object cache is in use. * * @param bool $using * @return bool */ public function using_object_cache($using = null) { $using = wp_using_ext_object_cache($using); if ($using) { // check if explicitly disabled by flush_interval setting/option <= 0 $flush_interval_number = Post_Views_Counter()->get_attribute('options', 'general', 'flush_interval', 'number'); $using = $flush_interval_number <= 0 ? false : true; } return $using; }
/** * Enqueue admin scripts and styles. */ public function admin_scripts_styles($page) { wp_register_style('pvc-admin', POST_VIEWS_COUNTER_URL . '/css/admin.css'); wp_register_script('pvc-admin-settings', POST_VIEWS_COUNTER_URL . '/js/admin-settings.js', array('jquery'), $this->defaults['version']); wp_register_script('pvc-admin-post', POST_VIEWS_COUNTER_URL . '/js/admin-post.js', array('jquery'), $this->defaults['version']); wp_register_script('pvc-admin-quick-edit', POST_VIEWS_COUNTER_URL . '/js/admin-quick-edit.js', array('jquery', 'inline-edit-post'), $this->defaults['version']); // load on PVC settings page if ($page === 'settings_page_post-views-counter') { wp_enqueue_script('pvc-admin-settings'); wp_localize_script('pvc-admin-settings', 'pvcArgsSettings', array('resetToDefaults' => __('Are you sure you want to reset these settings to defaults?', 'post-views-counter'))); wp_enqueue_style('pvc-admin'); // load on single post page } elseif ($page === 'post.php' || $page === 'post-new.php') { $post_types = Post_Views_Counter()->options['general']['post_types_count']; global $post_type; if (!in_array($post_type, (array) $post_types)) { return; } wp_enqueue_style('pvc-admin'); wp_enqueue_script('pvc-admin-post'); } elseif ($page === 'edit.php') { $post_types = Post_Views_Counter()->options['general']['post_types_count']; global $post_type; if (!in_array($post_type, (array) $post_types)) { return; } wp_enqueue_style('pvc-admin'); wp_enqueue_script('pvc-admin-quick-edit'); } }
/** * Bulk save post views. * * @global object $wpdb; * @return type */ function save_bulk_post_views() { $post_ids = !empty($_POST['post_ids']) && is_array($post_ids) ? array_map('absint', $_POST['post_ids']) : array(); $count = !empty($_POST['post_views']) ? absint($_POST['post_views']) : null; // break if views editing is restricted $restrict = (bool) Post_Views_Counter()->get_attribute('options', 'general', 'restrict_edit_views'); if ($restrict === true && !current_user_can(apply_filters('pvc_restrict_edit_capability', 'manage_options'))) { die; } if (!empty($post_ids)) { foreach ($post_ids as $post_id) { // break if current user can't edit this post if (!current_user_can('edit_post', $post_id)) { continue; } global $wpdb; // insert or update db post views count $wpdb->query($wpdb->prepare("\n\t\t\t\t\t\tINSERT INTO " . $wpdb->prefix . "post_views (id, type, period, count)\n\t\t\t\t\t\tVALUES (%d, %d, %s, %d)\n\t\t\t\t\t\tON DUPLICATE KEY UPDATE count = %d", $post_id, 4, 'total', $count, $count)); } } die; }