PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 5.0.10
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v5.0.10
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 5.0.10, at classes/models/FrmInbox.php

327 lines 7.7 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 /**
18 * @param array
19 */
20 private static $banner_messages;
21
22 public function __construct( $for_parent = null ) {
23 $this->set_cache_key();
24 $this->set_messages();
25 }
26
27 /**
28 * @since 4.05
29 */
30 protected function set_cache_key() {
31 $this->cache_key = 'frm_inbox_cache';
32 }
33
34 /**
35 * @since 4.05
36 */
37 protected function api_url() {
38 return 'https://formidableforms.com/wp-json/inbox/v1/message/';
39 }
40
41 /**
42 * @since 4.05
43 */
44 public function get_messages( $filter = false ) {
45 $messages = $this->messages;
46 if ( $filter === 'filter' ) {
47 $this->filter_messages( $messages );
48 }
49 return $messages;
50 }
51
52 /**
53 * @since 4.05
54 */
55 public function set_messages() {
56 $this->messages = get_option( $this->option );
57 if ( empty( $this->messages ) ) {
58 $this->messages = array();
59 }
60
61 $this->add_api_messages();
62
63 /**
64 * Messages are in an array.
65 */
66 $this->messages = apply_filters( 'frm_inbox', $this->messages );
67 }
68
69 /**
70 * @since 4.05
71 */
72 private function add_api_messages() {
73 $api = $this->get_api_info();
74 if ( empty( $api ) ) {
75 return;
76 }
77
78 foreach ( $api as $message ) {
79 $this->add_message( $message );
80 }
81 }
82
83 /**
84 * @param array|string $message
85 */
86 public function add_message( $message ) {
87 if ( ! is_array( $message ) || ! isset( $message['key'] ) ) {
88 // if the API response is invalid, $message may not be an array.
89 // if there are no messages from the API, it is returning a "No Entries Found" item with no key, so check for a key as well.
90 return;
91 }
92
93 if ( isset( $this->messages[ $message['key'] ] ) && ! isset( $message['force'] ) ) {
94 // Don't replace messages unless required.
95 return;
96 }
97
98 if ( $this->is_expired( $message ) ) {
99 return;
100 }
101
102 if ( isset( $this->messages[ $message['key'] ] ) ) {
103 // Move up and mark as new.
104 unset( $this->messages[ $message['key'] ] );
105 }
106
107 $this->fill_message( $message );
108 $this->messages[ $message['key'] ] = $message;
109
110 $this->update_list();
111
112 $this->clean_messages();
113 }
114
115 private function fill_message( &$message ) {
116 $defaults = array(
117 'time' => time(),
118 'message' => '',
119 'subject' => '',
120 'icon' => 'frm_tooltip_icon',
121 'cta' => '',
122 'expires' => false,
123 'who' => 'all', // use 'free', 'personal', 'business', 'elite', 'grandfathered'
124 'type' => '',
125 );
126
127 $message = array_merge( $defaults, $message );
128
129 $message['created'] = $message['time'];
130 unset( $message['time'] );
131 }
132
133 private function clean_messages() {
134 $removed = false;
135 foreach ( $this->messages as $t => $message ) {
136 $read = isset( $message['read'] ) && ! empty( $message['read'] ) && isset( $message['read'][ get_current_user_id() ] ) && $message['read'][ get_current_user_id() ] < strtotime( '-1 month' );
137 $dismissed = isset( $message['dismissed'] ) && ! empty( $message['dismissed'] ) && isset( $message['dismissed'][ get_current_user_id() ] ) && $message['dismissed'][ get_current_user_id() ] < strtotime( '-1 week' );
138 $expired = $this->is_expired( $message );
139 if ( $read || $expired || $dismissed ) {
140 unset( $this->messages[ $t ] );
141 $removed = true;
142 }
143 }
144
145 if ( $removed ) {
146 $this->update_list();
147 }
148 }
149
150 private function filter_messages( &$messages ) {
151 $user_id = get_current_user_id();
152 foreach ( $messages as $k => $message ) {
153 $dismissed = isset( $message['dismissed'] ) && isset( $message['dismissed'][ $user_id ] );
154 if ( empty( $k ) || $this->is_expired( $message ) || $dismissed ) {
155 unset( $messages[ $k ] );
156 } elseif ( ! $this->is_for_user( $message ) ) {
157 unset( $messages[ $k ] );
158 }
159 }
160 $messages = apply_filters( 'frm_filter_inbox', $messages );
161 }
162
163 private function is_expired( $message ) {
164 return isset( $message['expires'] ) && ! empty( $message['expires'] ) && $message['expires'] < time();
165 }
166
167 /**
168 * Show different messages for different accounts.
169 *
170 * @param array $message
171 * @return bool
172 */
173 private function is_for_user( $message ) {
174 if ( ! isset( $message['who'] ) || $message['who'] === 'all' ) {
175 return true;
176 }
177 $who = (array) $message['who'];
178 if ( in_array( 'all', $who, true ) || in_array( 'everyone', $who, true ) ) {
179 return true;
180 }
181 return in_array( $this->get_user_type(), $who, true );
182 }
183
184 private function get_user_type() {
185 if ( ! FrmAppHelper::pro_is_installed() ) {
186 return 'free';
187 }
188
189 return FrmAddonsController::license_type();
190 }
191
192 /**
193 * @param string $key
194 */
195 public function mark_read( $key ) {
196 if ( ! $key || ! isset( $this->messages[ $key ] ) ) {
197 return;
198 }
199
200 if ( ! isset( $this->messages[ $key ]['read'] ) ) {
201 $this->messages[ $key ]['read'] = array();
202 }
203 $this->messages[ $key ]['read'][ get_current_user_id() ] = time();
204
205 $this->update_list();
206 }
207
208 /**
209 * @param string $key
210 *
211 * @since 4.05.02
212 */
213 public function mark_unread( $key ) {
214 $is_read = isset( $this->messages[ $key ] ) && isset( $this->messages[ $key ]['read'] ) && isset( $this->messages[ $key ]['read'][ get_current_user_id() ] );
215 if ( $is_read ) {
216 unset( $this->messages[ $key ]['read'][ get_current_user_id() ] );
217 $this->update_list();
218 }
219 }
220
221 /**
222 * @param string $key
223 */
224 public function dismiss( $key ) {
225 if ( $key === 'all' ) {
226 $this->dismiss_all();
227 return;
228 }
229
230 if ( ! isset( $this->messages[ $key ] ) ) {
231 return;
232 }
233
234 if ( ! isset( $this->messages[ $key ]['dismissed'] ) ) {
235 $this->messages[ $key ]['dismissed'] = array();
236 }
237 $this->messages[ $key ]['dismissed'][ get_current_user_id() ] = time();
238
239 $this->update_list();
240 }
241
242 /**
243 * @since 4.06
244 */
245 private function dismiss_all() {
246 $user_id = get_current_user_id();
247 foreach ( $this->messages as $key => $message ) {
248 if ( ! isset( $message['dismissed'] ) ) {
249 $this->messages[ $key ]['dismissed'] = array();
250 }
251
252 if ( ! isset( $message['dismissed'][ $user_id ] ) ) {
253 $this->messages[ $key ]['dismissed'][ $user_id ] = time();
254 }
255 }
256 $this->update_list();
257 }
258
259 public function unread() {
260 $messages = $this->get_messages( 'filter' );
261 $user_id = get_current_user_id();
262 foreach ( $messages as $t => $message ) {
263 if ( isset( $message['read'] ) && isset( $message['read'][ $user_id ] ) ) {
264 unset( $messages[ $t ] );
265 }
266 }
267 return $messages;
268 }
269
270 public function unread_html() {
271 $html = '';
272 $count = count( $this->unread() );
273 if ( $count ) {
274 $html = ' <span class="update-plugins frm_inbox_count"><span class="plugin-count">' . absint( $count ) . '</span></span>';
275
276 /**
277 * @since 4.06.01
278 */
279 $html = apply_filters( 'frm_inbox_badge', $html );
280 }
281 return $html;
282 }
283
284 /**
285 * @since 4.05.02
286 */
287 public function remove( $key ) {
288 if ( isset( $this->messages[ $key ] ) ) {
289 unset( $this->messages[ $key ] );
290 $this->update_list();
291 }
292 }
293
294 private function update_list() {
295 update_option( $this->option, $this->messages, 'no' );
296 }
297
298 public static function maybe_show_banner() {
299 if ( empty( self::$banner_messages ) ) {
300 return;
301 }
302 $message = end( self::$banner_messages );
303 require FrmAppHelper::plugin_path() . '/classes/views/inbox/banner.php';
304 }
305
306 public static function maybe_disable_screen_options() {
307 self::$banner_messages = self::get_banner_messages();
308 if ( self::$banner_messages ) {
309 // disable screen options tab when displaying banner messages because it gets in the way of the banner.
310 add_filter( 'screen_options_show_screen', '__return_false' );
311 }
312 }
313
314 /**
315 * @return array
316 */
317 private static function get_banner_messages() {
318 $inbox = new self();
319 return array_filter(
320 $inbox->get_messages( 'filter' ),
321 function( $message ) {
322 return ! empty( $message['banner'] );
323 }
324 );
325 }
326 }
327