Completed
Push — master ( 881903...6c57e3 )
by Stephanie
15s queued 11s
created

FrmInbox::unread_html()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 8
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 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...
149
		if ( ! isset( $this->messages[ $key ] ) ) {
150
			return;
151
		}
152
153
		if ( ! isset( $this->messages[ $key ]['dismissed'] ) ) {
154
			$this->messages[ $key ]['dismissed'] = array();
155
		}
156
		$this->messages[ $key ]['dismissed'][ get_current_user_id() ] = time();
157
158
		$this->update_list();
159
	}
160
161
	private function update_list() {
162
		update_option( $this->option, $this->messages, 'no' );
163
	}
164
165
	public function unread() {
166
		$messages = $this->get_messages();
167
		$user_id  = get_current_user_id();
168
		foreach ( $messages as $t => $message ) {
0 ignored issues
show
Bug introduced by
The expression $messages of type boolean is not traversable.
Loading history...
169
			if ( isset( $message['read'] ) && isset( $message['read'][ $user_id ] ) ) {
170
				unset( $messages[ $t ] );
171
			}
172
		}
173
		return $messages;
174
	}
175
176
	public function unread_html() {
177
		$html = '';
178
		$count = count( $this->unread() );
179
		if ( $count ) {
180
			$html = ' <span class="update-plugins frm_inbox_count"><span class="plugin-count">' . absint( $count ) . '</span></span>';
181
		}
182
		return $html;
183
	}
184
}
185