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

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