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

FrmInbox::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
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
			$expired = isset( $message['expires'] ) && ! empty( $message['expires'] ) && $message['expires'] < time();
117
			if ( $read || $expired ) {
118
				unset( $this->messages[ $t ] );
119
				$removed = true;
120
			}
121
		}
122
123
		if ( $removed ) {
124
			$this->update_list();
125
		}
126
	}
127
128
	/**
129
	 * @param string $key in time format.
130
	 */
131
	public function mark_read( $key ) {
132
		if ( ! isset( $this->messages[ $key ] ) ) {
133
			return;
134
		}
135
136
		if ( ! isset( $this->messages[ $key ]['read'] ) ) {
137
			$this->messages[ $key ]['read'] = array();
138
		}
139
		$this->messages[ $key ]['read'][ get_current_user_id() ] = time();
140
141
		$this->update_list();
142
	}
143
144
	private function update_list() {
145
		update_option( $this->option, $this->messages, 'no' );
146
	}
147
148
	public function unread() {
149
		$messages = $this->get_messages();
150
		$user_id  = get_current_user_id();
151
		foreach ( $messages as $t => $message ) {
0 ignored issues
show
Bug introduced by
The expression $messages of type boolean is not traversable.
Loading history...
152
			if ( isset( $message['read'] ) && isset( $message['read'][ $user_id ] ) ) {
153
				unset( $messages[ $t ] );
154
			}
155
		}
156
		return $messages;
157
	}
158
159
	public function unread_html() {
160
		$html = '';
161
		$count = count( $this->unread() );
162
		if ( $count ) {
163
			$html = '<span class="update-plugins frm_inbox_count"><span class="plugin-count">' . absint( $count ) . '</span></span>';
164
		}
165
		return $html;
166
	}
167
}
168