Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace OtLand;
- class PageView {
- protected static $cookie = 'otland_pageview';
- private static $read = array(),
- $unread = array();
- /**
- * Returns the predefined cookie variable.
- *
- * @access protected
- * @static true
- * @throws Exception
- * @return string
- **/
- protected static function getCookie() {
- if (!isset($_COOKIE[self::$cookie])) {
- if (empty(self::$read)) {
- self::setCookie(' ', false);
- } else {
- self::markAsRead(array());
- }
- }
- return $_COOKIE[self::$cookie];
- }
- /**
- * Sets the value of a predefined cookie variable.
- *
- * @param mixed $value
- * @param boolean $extend true
- * @access protected
- * @return void
- **/
- protected static function setCookie($value, $extend = true) {
- \setcookie(self::$cookie, ($extend && self::getCookie() != ' ' ? self::getCookie() . PATH_SEPARATOR : null) . $value);
- }
- /**
- * Returns true in case a post/page id has already been discovered.
- *
- * @param integer $id
- * @access public
- * @static true
- * @return boolean
- **/
- public static function isRead($id) {
- if (!preg_match('/' . PATH_SEPARATOR . '?' . $id . PATH_SEPARATOR . '?/', self::getCookie())) {
- return false;
- }
- return true;
- }
- /**
- * Returns true in case a post/page id has yet to be discovered.
- *
- * @param integer $id
- * @access public
- * @static true
- * @return boolean
- **/
- public static function isUnread($id) {
- return !self::isRead($id);
- }
- /**
- * Marks a specific post/page id as read.
- *
- * @param integer $id
- * @access public
- * @static true
- * @return boolean
- **/
- public static function markAsRead($ids) {
- $ids = (array) $ids;
- foreach ($ids as $id) {
- if (self::isRead($id)) {
- continue;
- }
- // Workaround for setting cookies once per request.
- self::$read[] = $id;
- }
- if (!empty(self::$read)) {
- self::setCookie(implode(PATH_SEPARATOR, self::$read));
- }
- }
- /**
- * Marks a specific post/page id as unread.
- *
- * @param integer $id
- * @access public
- * @static true
- * @return boolean
- **/
- public static function markAsUnread($ids) {
- $ids = (array) $ids;
- foreach ($ids as $id) {
- if (self::isUnread($id)) {
- continue;
- }
- // Workaround for setting cookies once per request.
- self::$unread[] = $id;
- }
- if (!empty(self::$unread)) {
- $storage = preg_replace('/' . PATH_SEPARATOR . '?(' . implode('|', self::$unread) . ')' . PATH_SEPARATOR . '?/', PATH_SEPARATOR, self::getCookie());
- $storage = trim($storage, PATH_SEPARATOR);
- self::setCookie($storage, false);
- }
- }
- /**
- * Returns an array of post/page ids.
- *
- * @param boolean $sort true
- * @access public
- * @static true
- * @return array
- **/
- public static function getAll($sort = true) {
- $tmp = explode(PATH_SEPARATOR, self::getCookie());
- // Remove the empty default value from the array.
- if (array_search(' ', $tmp) !== false) {
- unset($tmp[array_search(' ', $tmp)]);
- }
- // Convert all values into integer values.
- $tmp = (array) array_map('intval', $tmp);
- if ($sort) sort($tmp);
- return $tmp;
- }
- /**
- * Resets the storage cookie.
- *
- * @access public
- * @static true
- * @return void
- **/
- public static function reset() {
- setcookie(self::$cookie, '', time() - 3600);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement