summaryrefslogtreecommitdiff
path: root/Postman/PostmanSession.php
blob: f1128b4c9b3ab04c860f75fc89da0f3820b12e53 (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
<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}
if (! class_exists ( 'PostmanSession' )) {
	
	/**
	 * Persist session state to the database
	 *
	 * I heard plugins are forbidden from writing to the http session
	 * on some hosts, such as WPEngine, so this class write session
	 * state to the database instead.
	 *
	 * What's better about this is I don't have to prefix all my
	 * variables with , in fear of colliding with another
	 * plugin's similiarily named variables.
	 *
	 * @author jasonhendriks
	 *        
	 */
	class PostmanSession {
		// length of time to keep items around
		const MINUTES_IN_SECONDS = 60;
		
		//
		const OAUTH_IN_PROGRESS = 'oauth_in_progress';
		const ACTION = 'action';
		const ERROR_MESSAGE = 'error_message';
		
		// singleton instance
		public static function getInstance() {
			static $inst = null;
			if ($inst === null) {
				$inst = new PostmanSession ();
			}
			return $inst;
		}
		
		/**
		 * OAuth is in progress $state is the randomly generated
		 * transaction ID
		 *
		 * @param mixed $state        	
		 */
		public function isSetOauthInProgress() {
			return get_transient ( self::OAUTH_IN_PROGRESS ) != false;
		}
		public function setOauthInProgress($state) {
			set_transient ( self::OAUTH_IN_PROGRESS, $state, 3 * self::MINUTES_IN_SECONDS );
		}
		public function getOauthInProgress() {
			return get_transient ( self::OAUTH_IN_PROGRESS );
		}
		public function unsetOauthInProgress() {
			delete_transient ( self::OAUTH_IN_PROGRESS );
		}
		
		/**
		 * Sometimes I need to keep track of what I'm doing between requests
		 *
		 * @param mixed $action        	
		 */
		public function isSetAction() {
			return get_transient ( self::ACTION ) != false;
		}
		public function setAction($action) {
			set_transient ( self::ACTION, $action, 30 * self::MINUTES_IN_SECONDS );
		}
		public function getAction() {
			return get_transient ( self::ACTION );
		}
		public function unsetAction() {
			delete_transient ( self::ACTION );
		}
		
		/**
		 * Sometimes I need to keep track of what I'm doing between requests
		 *
		 * @param mixed $message        	
		 */
		public function isSetErrorMessage() {
			return get_transient ( self::ERROR_MESSAGE ) != false;
		}
		public function setMessage($message) {
			set_transient ( self::ERROR_MESSAGE, $message, 30 * self::MINUTES_IN_SECONDS );
		}
		public function getMessage() {
			return get_transient ( self::ERROR_MESSAGE );
		}
		public function unsetMessage() {
			delete_transient ( self::ERROR_MESSAGE );
		}
		
	}
}