Completed
Pull Request — master (#174)
by Stephanie
02:25
created

FrmInbox::set_cache_key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @since 4.05
4
 */
5
class FrmInbox extends FrmFormApi {
6
7
	protected $cache_key;
8
9
	private $option = 'frm_inbox';
10
11
	private $messages = false;
12
13
	public function __construct( $for_parent = null ) {
14
		$this->set_cache_key();
15
		$this->set_messages();
16
	}
17
18
	/**
19
	 * @since 4.05
20
	 */
21
	protected function set_cache_key() {
22
		$this->cache_key = 'frm_inbox_cache';
23
	}
24
25
	/**
26
	 * @since 4.05
27
	 */
28
	protected function api_url() {
29
		return 'https://formidableforms.com/wp-json/inbox/v1/message/';
30
	}
31
32
	/**
33
	 * @since 4.05
34
	 */
35
	public function get_messages() {
36
		return $this->messages;
37
	}
38
39
	/**
40
	 * @since 4.05
41
	 */
42
	public function set_messages() {
43
		$this->messages = get_option( $this->option );
44
		if ( empty( $this->messages ) ) {
45
			$this->messages = array();
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type boolean of property $messages.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
46
		}
47
48
		$this->add_api_messages();
49
50
		/**
51
		 * Messages are in an array.
52
		 */
53
		$this->messages = apply_filters( 'frm_inbox', $this->messages );
54
	}
55
56
	/**
57
	 * @since 4.05
58
	 */
59
	private function add_api_messages() {
60
		$api = $this->get_api_info();
61
		if ( empty( $api ) ) {
62
			return;
63
		}
64
65
		foreach ( $api as $message ) {
66
			$this->add_message( $message );
67
		}
68
	}
69
70
	/**
71
	 * @param array $message
72
	 */
73
	public function add_message( $message ) {
74
		if ( isset( $this->messages[ $message['key'] ] ) && ! isset( $message['force'] ) ) {
75
			// Don't replace messages unless required.
76
			return;
77
		}
78
79
		if ( isset( $this->messages[ $message['key'] ] ) ) {
80
			// Move up and mark as new.
81
			unset( $this->messages[ $message['key'] ] );
82
		}
83
84
		$message = $this->fill_message( $message );
85
		$this->messages[ $message['key'] ] = array(
86
			'created' => $message['time'],
87
			'message' => $message['message'],
88
			'subject' => $message['subject'],
89
			'icon'    => $message['icon'],
90
			'cta'     => $message['cta'],
91
			'expires' => $message['expires'],
92
		);
93
94
		$this->update_list();
95
96
		$this->clean_messages();
97
	}
98
99
	private function fill_message( $message ) {
100
		$defaults = array(
101
			'time'    => time(),
102
			'message' => '',
103
			'subject' => '',
104
			'icon'    => 'frm_tooltip_icon',
105
			'cta'     => '',
106
			'expires' => false,
107
		);
108
109
		return array_merge( $defaults, $message );
110
	}
111
112
	private function clean_messages() {
113
		$removed  = false;
114
		foreach ( $this->messages as $t => $message ) {
0 ignored issues
show
Bug introduced by
The expression $this->messages of type boolean is not traversable.
Loading history...
115
			$read    = isset( $message['read'] ) && ! empty( $message['read'] ) && isset( $message['read'][ get_current_user_id() ] ) && $message['read'][ get_current_user_id() ] < strtotime( '-1 month' );
116
			$dismissed = isset( $message['dismissed'] ) && ! empty( $message['dismissed'] ) && isset( $message['dismissed'][ get_current_user_id() ] ) && $message['dismissed'][ get_current_user_id() ] < strtotime( '-1 week' );
117
			$expired = isset( $message['expires'] ) && ! empty( $message['expires'] ) && $message['expires'] < time();
118
			if ( $read || $expired || $dismissed ) {
119
				unset( $this->messages[ $t ] );
120
				$removed = true;
121
			}
122
		}
123
124
		if ( $removed ) {
125
			$this->update_list();
126
		}
127
	}
128
129
	/**
130
	 * @param string $key
131
	 */
132 View Code Duplication
	public function mark_read( $key ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
		if ( ! isset( $this->messages[ $key ] ) ) {
134
			return;
135
		}
136
137
		if ( ! isset( $this->messages[ $key ]['read'] ) ) {
138
			$this->messages[ $key ]['read'] = array();
139
		}
140
		$this->messages[ $key ]['read'][ get_current_user_id() ] = time();
141
142
		$this->update_list();
143
	}
144
145
	/**
146
	 * @param string $key
147
	 *
148
	 * @since 4.05.02
149
	 */
150
	public function mark_unread( $key ) {
151
		$is_read = isset( $this->messages[ $key ] ) && isset( $this->messages[ $key ]['read'] ) && isset( $this->messages[ $key ]['read'][ get_current_user_id() ] );
152
		if ( $is_read ) {
153
			unset( $this->messages[ $key ]['read'][ get_current_user_id() ] );
154
			$this->update_list();
155
		}
156
	}
157
158
	/**
159
	 * @param string $key
160
	 */
161 View Code Duplication
	public function dismiss( $key ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
162
		if ( ! isset( $this->messages[ $key ] ) ) {
163
			return;
164
		}
165
166
		if ( ! isset( $this->messages[ $key ]['dismissed'] ) ) {
167
			$this->messages[ $key ]['dismissed'] = array();
168
		}
169
		$this->messages[ $key ]['dismissed'][ get_current_user_id() ] = time();
170
171
		$this->update_list();
172
	}
173
174
	public function unread() {
175
		$messages = $this->get_messages();
176
		$user_id  = get_current_user_id();
177
		foreach ( $messages as $t => $message ) {
0 ignored issues
show
Bug introduced by
The expression $messages of type boolean is not traversable.
Loading history...
178
			if ( isset( $message['read'] ) && isset( $message['read'][ $user_id ] ) ) {
179
				unset( $messages[ $t ] );
180
			}
181
		}
182
		return $messages;
183
	}
184
185
	public function unread_html() {
186
		$html = '';
187
		$count = count( $this->unread() );
188
		if ( $count ) {
189
			$html = ' <span class="update-plugins frm_inbox_count"><span class="plugin-count">' . absint( $count ) . '</span></span>';
190
		}
191
		return $html;
192
	}
193
194
	/**
195
	 * @since 4.05.02
196
	 */
197
	public function remove( $key ) {
198
		if ( isset( $this->messages[ $key ] ) ) {
199
			unset( $this->messages[ $key ] );
200
			$this->update_list();
201
		}
202
	}
203
204
	private function update_list() {
205
		update_option( $this->option, $this->messages, 'no' );
206
	}
207
}
208