| 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 |
|