PluginProbe
WowOptin: Next-Gen Popup Maker – Create Stunning Popups and Optins for Lead Generation / trunk
WowOptin: Next-Gen Popup Maker – Create Stunning Popups and Optins for Lead Generation vtrunk
1.4.48 1.4.47 1.4.46 1.4.45 1.4.44 1.4.43 1.4.42 1.4.41 1.4.40 1.4.39 1.4.38 1.4.37 1.4.36 1.1.2 1.2.0 1.2.1 1.2.2 1.3.0 1.3.1 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 All 64 releases
optin / includes / utils / class-visitor-count.php

class-visitor-count.php in WowOptin: Next-Gen Popup Maker – Create Stunning Popups and Optins for Lead Generation trunk, at includes/utils/class-visitor-count.php

115 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore
2
3 namespace OPTN\Includes\Utils;
4
5 defined( 'ABSPATH' ) || exit;
6
7 /**
8 * Class for counting visitors.
9 */
10 class VisitorCount {
11
12 use \OPTN\Includes\Traits\Singleton;
13
14 private $current_week_option = 'optn_visitor_stats_current_week';
15 private $previous_week_option = 'optn_visitor_stats_previous_week';
16
17 const DEF_VALUE = array(
18 'visitors' => 0,
19 'pageviews' => 0,
20 );
21
22 public function schedule_rotation() {
23 if ( ! wp_next_scheduled( 'opnt_rotate_visitor_stats' ) ) {
24 wp_schedule_event( strtotime( 'next monday' ), 'weekly', 'opnt_rotate_visitor_stats' );
25 }
26
27 add_action( 'opnt_rotate_visitor_stats', array( $this, 'rotate_weekly_stats' ) );
28 }
29
30 public function track_visit() {
31 if ( is_admin() ||
32 wp_is_json_request() ||
33 wp_doing_ajax() ||
34 wp_doing_cron() ) {
35 return;
36 }
37
38 if ( preg_match( '/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT'] ?? '' ) ) { // phpcs:ignore
39 return;
40 }
41
42 if ( is_singular() || is_front_page() || is_home() ) {
43
44 $current_stats = $this->get_current_week_stats();
45
46 static $counted = false;
47 if ( ! $counted ) {
48 ++$current_stats['pageviews'];
49 $counted = true;
50 }
51
52 if ( $this->is_unique_visitor() ) {
53 ++$current_stats['visitors'];
54 }
55
56 update_option( $this->current_week_option, $current_stats );
57 }
58 }
59
60 public function get_stats() {
61 $current_stats = $this->get_current_week_stats();
62 $previous_stats = $this->get_previous_week_stats();
63
64 return array(
65 'visitors' => array(
66 'total' => Utils::format_number( $current_stats['visitors'] ),
67 'diff' => Utils::get_diff_pct( $previous_stats['visitors'], $current_stats['visitors'] ),
68 ),
69 'pageviews' => array(
70 'total' => Utils::format_number( $current_stats['pageviews'] ),
71 'diff' => Utils::get_diff_pct( $previous_stats['pageviews'], $current_stats['pageviews'] ),
72 ),
73 );
74 }
75
76 public function rotate_weekly_stats() {
77 // Move current week's stats to previous week
78 $current_stats = $this->get_current_week_stats();
79 update_option( $this->previous_week_option, $current_stats );
80
81 // Reset current week's stats
82 $new_stats = array(
83 'visitors' => 0,
84 'pageviews' => 0,
85 );
86 update_option( $this->current_week_option, $new_stats );
87 }
88
89 private function is_unique_visitor() {
90 $cookie_name = 'opnt_visitor_tracked_weekly';
91 if ( ! isset( $_COOKIE[ $cookie_name ] ) ) { // phpcs:ignore
92 $expiry = strtotime( 'next monday' ) - time();
93 setcookie( $cookie_name, '1', time() + $expiry, COOKIEPATH );
94 return true;
95 }
96 return false;
97 }
98
99 private function get_current_week_stats() {
100 $stats = get_option( $this->current_week_option );
101 if ( ! $stats ) {
102 $stats = self::DEF_VALUE;
103 update_option( $this->current_week_option, $stats );
104 }
105 return $stats;
106 }
107
108 private function get_previous_week_stats() {
109 return get_option(
110 $this->previous_week_option,
111 self::DEF_VALUE
112 );
113 }
114 }
115