Completed
Push — master ( 43ba45...374e5a )
by Stephanie
20s queued 14s
created

FrmInbox::add_message()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 1
dl 0
loc 26
rs 9.504
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( $filter = false ) {
36
		$messages = $this->messages;
37
		if ( $filter === 'filter' ) {
38
			$this->filter_messages( $messages );
39
		}
40
		return $messages;
41
	}
42
43
	/**
44
	 * @since 4.05
45
	 */
46
	public function set_messages() {
47
		$this->messages = get_option( $this->option );
48
		if ( empty( $this->messages ) ) {
49
			$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...
50
		}
51
52
		$this->add_api_messages();
53
54
		/**
55
		 * Messages are in an array.
56
		 */
57
		$this->messages = apply_filters( 'frm_inbox', $this->messages );
58
	}
59
60
	/**
61
	 * @since 4.05
62
	 */
63
	private function add_api_messages() {
64
		$api = $this->get_api_info();
65
		if ( empty( $api ) ) {
66
			return;
67
		}
68
69
		foreach ( $api as $message ) {
70
			$this->add_message( $message );
71
		}
72
	}
73
74
	/**
75
	 * @param array $message
76
	 */
77
	public function add_message( $message ) {
78
		if ( isset( $this->messages[ $message['key'] ] ) && ! isset( $message['force'] ) ) {
79
			// Don't replace messages unless required.
80
			return;
81
		}
82
83
		if ( isset( $this->messages[ $message['key'] ] ) ) {
84
			// Move up and mark as new.
85
			unset( $this->messages[ $message['key'] ] );
86
		}
87
88
		$message = $this->fill_message( $message );
89
		$this->messages[ $message['key'] ] = array(
90
			'created' => $message['time'],
91
			'message' => $message['message'],
92
			'subject' => $message['subject'],
93
			'icon'    => $message['icon'],
94
			'cta'     => $message['cta'],
95
			'expires' => $message['expires'],
96
			'who'     => $message['who'],
97
		);
98
99
		$this->update_list();
100
101
		$this->clean_messages();
102
	}
103
104
	private function fill_message( $message ) {
105
		$defaults = array(
106
			'time'    => time(),
107
			'message' => '',
108
			'subject' => '',
109
			'icon'    => 'frm_tooltip_icon',
110
			'cta'     => '',
111
			'expires' => false,
112
			'who'     => 'all', // use 'free', 'personal', 'business', 'elite', 'grandfathered'
113
		);
114
115
		return array_merge( $defaults, $message );
116
	}
117
118
	private function clean_messages() {
119
		$removed  = false;
120
		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...
121
			$read    = isset( $message['read'] ) && ! empty( $message['read'] ) && isset( $message['read'][ get_current_user_id() ] ) && $message['read'][ get_current_user_id() ] < strtotime( '-1 month' );
122
			$dismissed = isset( $message['dismissed'] ) && ! empty( $message['dismissed'] ) && isset( $message['dismissed'][ get_current_user_id() ] ) && $message['dismissed'][ get_current_user_id() ] < strtotime( '-1 week' );
123
			$expired = $this->is_expired( $message );
124
			if ( $read || $expired || $dismissed ) {
125
				unset( $this->messages[ $t ] );
126
				$removed = true;
127
			}
128
		}
129
130
		if ( $removed ) {
131
			$this->update_list();
132
		}
133
	}
134
135
	private function filter_messages( &$messages ) {
136
		$user_id = get_current_user_id();
137
		foreach ( $messages as $k => $message ) {
138
			$dismissed = isset( $message['dismissed'] ) && isset( $message['dismissed'][ $user_id ] );
139
			if ( $this->is_expired( $message ) || $dismissed ) {
140
				unset( $messages[ $k ] );
141
			} elseif ( ! $this->is_for_user( $message ) ) {
142
				unset( $messages[ $k ] );
143
			}
144
		}
145
	}
146
147
	private function is_expired( $message ) {
148
		return isset( $message['expires'] ) && ! empty( $message['expires'] ) && $message['expires'] < time();
149
	}
150
151
	/**
152
	 * Show different messages for different accounts.
153
	 */
154
	private function is_for_user( $message ) {
155
		if ( ! isset( $message['who'] ) || $message['who'] === 'all' ) {
156
			return true;
157
		}
158
159
		return in_array( $this->get_user_type(), (array) $message['who'] );
160
	}
161
162
	private function get_user_type() {
163
		if ( ! FrmAppHelper::pro_is_installed() ) {
164
			return 'free';
165
		}
166
167
		return FrmAddonsController::license_type();
168
	}
169
170
	/**
171
	 * @param string $key
172
	 */
173 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...
174
		if ( ! isset( $this->messages[ $key ] ) ) {
175
			return;
176
		}
177
178
		if ( ! isset( $this->messages[ $key ]['read'] ) ) {
179
			$this->messages[ $key ]['read'] = array();
180
		}
181
		$this->messages[ $key ]['read'][ get_current_user_id() ] = time();
182
183
		$this->update_list();
184
	}
185
186
	/**
187
	 * @param string $key
188
	 *
189
	 * @since 4.05.02
190
	 */
191
	public function mark_unread( $key ) {
192
		$is_read = isset( $this->messages[ $key ] ) && isset( $this->messages[ $key ]['read'] ) && isset( $this->messages[ $key ]['read'][ get_current_user_id() ] );
193
		if ( $is_read ) {
194
			unset( $this->messages[ $key ]['read'][ get_current_user_id() ] );
195
			$this->update_list();
196
		}
197
	}
198
199
	/**
200
	 * @param string $key
201
	 */
202 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...
203
		if ( $key === 'all' ) {
204
			$this->dismiss_all();
205
			return;
206
		}
207
208
		if ( ! isset( $this->messages[ $key ] ) ) {
209
			return;
210
		}
211
212
		if ( ! isset( $this->messages[ $key ]['dismissed'] ) ) {
213
			$this->messages[ $key ]['dismissed'] = array();
214
		}
215
		$this->messages[ $key ]['dismissed'][ get_current_user_id() ] = time();
216
217
		$this->update_list();
218
	}
219
220
	/**
221
	 * @since 4.06
222
	 */
223
	private function dismiss_all() {
224
		$user_id = get_current_user_id();
225
		foreach ( $this->messages as $key => $message ) {
0 ignored issues
show
Bug introduced by
The expression $this->messages of type boolean is not traversable.
Loading history...
226
			if ( ! isset( $message['dismissed'] ) ) {
227
				$this->messages[ $key ]['dismissed'] = array();
228
			}
229
230
			if ( ! isset( $message['dismissed'][ $user_id ] ) ) {
231
				$this->messages[ $key ]['dismissed'][ $user_id ] = time();
232
			}
233
		}
234
		$this->update_list();
235
	}
236
237
	public function unread() {
238
		$messages = $this->get_messages( 'filter' );
239
		$user_id  = get_current_user_id();
240
		foreach ( $messages as $t => $message ) {
241
			if ( isset( $message['read'] ) && isset( $message['read'][ $user_id ] ) ) {
242
				unset( $messages[ $t ] );
243
			}
244
		}
245
		return $messages;
246
	}
247
248
	public function unread_html() {
249
		$html = '';
250
		$count = count( $this->unread() );
251
		if ( $count ) {
252
			$html = ' <span class="update-plugins frm_inbox_count"><span class="plugin-count">' . absint( $count ) . '</span></span>';
253
		}
254
		return $html;
255
	}
256
257
	/**
258
	 * @since 4.05.02
259
	 */
260
	public function remove( $key ) {
261
		if ( isset( $this->messages[ $key ] ) ) {
262
			unset( $this->messages[ $key ] );
263
			$this->update_list();
264
		}
265
	}
266
267
	private function update_list() {
268
		update_option( $this->option, $this->messages, 'no' );
269
	}
270
}
271