PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 4.08
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v4.08
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / models / FrmInbox.php

FrmInbox.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 4.08, at classes/models/FrmInbox.php

277 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 /**
7 * @since 4.05
8 */
9 class FrmInbox extends FrmFormApi {
10
11 protected $cache_key;
12
13 private $option = 'frm_inbox';
14
15 private $messages = false;
16
17 public function __construct( $for_parent = null ) {
18 $this->set_cache_key();
19 $this->set_messages();
20 }
21
22 /**
23 * @since 4.05
24 */
25 protected function set_cache_key() {
26 $this->cache_key = 'frm_inbox_cache';
27 }
28
29 /**
30 * @since 4.05
31 */
32 protected function api_url() {
33 return 'https://formidableforms.com/wp-json/inbox/v1/message/';
34 }
35
36 /**
37 * @since 4.05
38 */
39 public function get_messages( $filter = false ) {
40 $messages = $this->messages;
41 if ( $filter === 'filter' ) {
42 $this->filter_messages( $messages );
43 }
44 return $messages;
45 }
46
47 /**
48 * @since 4.05
49 */
50 public function set_messages() {
51 $this->messages = get_option( $this->option );
52 if ( empty( $this->messages ) ) {
53 $this->messages = array();
54 }
55
56 $this->add_api_messages();
57
58 /**
59 * Messages are in an array.
60 */
61 $this->messages = apply_filters( 'frm_inbox', $this->messages );
62 }
63
64 /**
65 * @since 4.05
66 */
67 private function add_api_messages() {
68 $api = $this->get_api_info();
69 if ( empty( $api ) ) {
70 return;
71 }
72
73 foreach ( $api as $message ) {
74 $this->add_message( $message );
75 }
76 }
77
78 /**
79 * @param array $message
80 */
81 public function add_message( $message ) {
82 if ( isset( $this->messages[ $message['key'] ] ) && ! isset( $message['force'] ) ) {
83 // Don't replace messages unless required.
84 return;
85 }
86
87 if ( isset( $this->messages[ $message['key'] ] ) ) {
88 // Move up and mark as new.
89 unset( $this->messages[ $message['key'] ] );
90 }
91
92 $this->fill_message( $message );
93 $this->messages[ $message['key'] ] = $message;
94
95 $this->update_list();
96
97 $this->clean_messages();
98 }
99
100 private function fill_message( &$message ) {
101 $defaults = array(
102 'time' => time(),
103 'message' => '',
104 'subject' => '',
105 'icon' => 'frm_tooltip_icon',
106 'cta' => '',
107 'expires' => false,
108 'who' => 'all', // use 'free', 'personal', 'business', 'elite', 'grandfathered'
109 'type' => '',
110 );
111
112 $message = array_merge( $defaults, $message );
113
114 $message['created'] = $message['time'];
115 unset( $message['time'] );
116 }
117
118 private function clean_messages() {
119 $removed = false;
120 foreach ( $this->messages as $t => $message ) {
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 $messages = apply_filters( 'frm_filter_inbox', $messages );
146 }
147
148 private function is_expired( $message ) {
149 return isset( $message['expires'] ) && ! empty( $message['expires'] ) && $message['expires'] < time();
150 }
151
152 /**
153 * Show different messages for different accounts.
154 */
155 private function is_for_user( $message ) {
156 if ( ! isset( $message['who'] ) || $message['who'] === 'all' ) {
157 return true;
158 }
159
160 return in_array( $this->get_user_type(), (array) $message['who'] );
161 }
162
163 private function get_user_type() {
164 if ( ! FrmAppHelper::pro_is_installed() ) {
165 return 'free';
166 }
167
168 return FrmAddonsController::license_type();
169 }
170
171 /**
172 * @param string $key
173 */
174 public function mark_read( $key ) {
175 if ( ! isset( $this->messages[ $key ] ) ) {
176 return;
177 }
178
179 if ( ! isset( $this->messages[ $key ]['read'] ) ) {
180 $this->messages[ $key ]['read'] = array();
181 }
182 $this->messages[ $key ]['read'][ get_current_user_id() ] = time();
183
184 $this->update_list();
185 }
186
187 /**
188 * @param string $key
189 *
190 * @since 4.05.02
191 */
192 public function mark_unread( $key ) {
193 $is_read = isset( $this->messages[ $key ] ) && isset( $this->messages[ $key ]['read'] ) && isset( $this->messages[ $key ]['read'][ get_current_user_id() ] );
194 if ( $is_read ) {
195 unset( $this->messages[ $key ]['read'][ get_current_user_id() ] );
196 $this->update_list();
197 }
198 }
199
200 /**
201 * @param string $key
202 */
203 public function dismiss( $key ) {
204 if ( $key === 'all' ) {
205 $this->dismiss_all();
206 return;
207 }
208
209 if ( ! isset( $this->messages[ $key ] ) ) {
210 return;
211 }
212
213 if ( ! isset( $this->messages[ $key ]['dismissed'] ) ) {
214 $this->messages[ $key ]['dismissed'] = array();
215 }
216 $this->messages[ $key ]['dismissed'][ get_current_user_id() ] = time();
217
218 $this->update_list();
219 }
220
221 /**
222 * @since 4.06
223 */
224 private function dismiss_all() {
225 $user_id = get_current_user_id();
226 foreach ( $this->messages as $key => $message ) {
227 if ( ! isset( $message['dismissed'] ) ) {
228 $this->messages[ $key ]['dismissed'] = array();
229 }
230
231 if ( ! isset( $message['dismissed'][ $user_id ] ) ) {
232 $this->messages[ $key ]['dismissed'][ $user_id ] = time();
233 }
234 }
235 $this->update_list();
236 }
237
238 public function unread() {
239 $messages = $this->get_messages( 'filter' );
240 $user_id = get_current_user_id();
241 foreach ( $messages as $t => $message ) {
242 if ( isset( $message['read'] ) && isset( $message['read'][ $user_id ] ) ) {
243 unset( $messages[ $t ] );
244 }
245 }
246 return $messages;
247 }
248
249 public function unread_html() {
250 $html = '';
251 $count = count( $this->unread() );
252 if ( $count ) {
253 $html = ' <span class="update-plugins frm_inbox_count"><span class="plugin-count">' . absint( $count ) . '</span></span>';
254
255 /**
256 * @since 4.06.01
257 */
258 $html = apply_filters( 'frm_inbox_badge', $html );
259 }
260 return $html;
261 }
262
263 /**
264 * @since 4.05.02
265 */
266 public function remove( $key ) {
267 if ( isset( $this->messages[ $key ] ) ) {
268 unset( $this->messages[ $key ] );
269 $this->update_list();
270 }
271 }
272
273 private function update_list() {
274 update_option( $this->option, $this->messages, 'no' );
275 }
276 }
277