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

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