summaryrefslogtreecommitdiff
path: root/Postman/PostmanState.php
blob: 4b077e0eb83a0e6a566c95975462cde0d5865fe1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}
if (! class_exists ( "PostmanState" )) {
	
	/**
	 * http://stackoverflow.com/questions/23880928/use-oauth-refresh-token-to-obtain-new-access-token-google-api
	 * http://pastebin.com/jA9sBNTk
	 *
	 * Make sure these emails are permitted (see http://en.wikipedia.org/wiki/E-mail_address#Internationalization):
	 */
	class PostmanState {
		
		private $logger;
		
		// the option database name
		const SLUG = 'postman_state';
		const TOO_LONG_SEC = 2592000; // 30 days
		                              
		// the options fields
		const VERSION = 'version';
		const INSTALL_DATE = 'install_date';
		const FILE_LOCKING_ENABLED = 'locking_enabled';
		const DELIVERY_SUCCESS_TOTAL = 'delivery_success_total';
		const DELIVERY_FAILURE_TOTAL = 'delivery_fail_total';
		
		// options data
		private $options;
		
		// singleton instance
		public static function getInstance() {
			static $inst = null;
			if ($inst === null) {
				$inst = new PostmanState ();
			}
			return $inst;
		}
		
		/**
		 * private constructor
		 */
		private function __construct() {
			$this->logger = new PostmanLogger(get_class($this));
			$this->load();
		}
		//
		public function save() {
			update_option ( self::SLUG, $this->options );
		}
		public function reload() {
			$this->load ();
		}
		private function load() {
			$this->options = get_option ( self::SLUG );
		}
		/**
		 * Shows the review feature of Postman up to thirty days after install
		 *
		 * @return boolean
		 */
		public function isTimeToReviewPostman() {
			if (! empty ( $this->options [self::INSTALL_DATE] )) {
				$successful = PostmanState::getInstance ()->getSuccessfulDeliveries () > 0;
				$maxTime = $this->options [self::INSTALL_DATE] + self::TOO_LONG_SEC;
				return $successful && time () <= $maxTime;
			}
		}
		public function isFileLockingEnabled() {
			if (isset ( $this->options [self::FILE_LOCKING_ENABLED] ))
				return $this->options [self::FILE_LOCKING_ENABLED];
			else
				return false;
		}
		public function setFileLockingEnabled($enabled) {
			$this->options [self::FILE_LOCKING_ENABLED] = $enabled;
			$this->save ();
		}
		public function getVersion() {
			if (! empty ( $this->options [self::VERSION] )) {
				return $this->options [self::VERSION];
			}
		}
		function getSuccessfulDeliveries() {
			if (isset ( $this->options [PostmanState::DELIVERY_SUCCESS_TOTAL] ))
				return $this->options [PostmanState::DELIVERY_SUCCESS_TOTAL];
			else
				return 0;
		}
		function setSuccessfulDelivery($total) {
			$this->options [PostmanState::DELIVERY_SUCCESS_TOTAL] = $total;
		}
		function incrementSuccessfulDelivery() {
			$this->setSuccessfulDelivery ( $this->getSuccessfulDeliveries () + 1 );
			$this->logger->debug ( 'incrementing success count: ' . $this->getSuccessfulDeliveries () );
			$this->save ();
		}
		function getFailedDeliveries() {
			if (isset ( $this->options [PostmanState::DELIVERY_FAILURE_TOTAL] ))
				return $this->options [PostmanState::DELIVERY_FAILURE_TOTAL];
			else
				return 0;
		}
		function setFailedDelivery($total) {
			$this->options [PostmanState::DELIVERY_FAILURE_TOTAL] = $total;
		}
		function incrementFailedDelivery() {
			$this->setFailedDelivery ( $this->getFailedDeliveries () + 1 );
			$this->logger->debug ( 'incrementing failure count: ' . $this->getFailedDeliveries () );
			$this->save ();
		}
	}
}