PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.8
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.8
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
← All changes | classes/controllers/FrmDashboardController.php +145 -106 6.326.8 View file →
@@ -5,8 +5,13 @@
5 5
6 6 class FrmDashboardController {
7 7
8 8 /**
9 + * Option name used to store the time when auto redirected for welcome.
10 + */
11 + const REDIRECT_META_NAME = 'frm_activation_redirect';
12 +
13 + /**
9 14 * Handle name used for registering controller scripts and style.
10 15 */
11 16 const PAGE_SLUG = 'formidable-dashboard';
12 17
@@ -17,12 +22,15 @@
17 22
18 23 /**
19 24 * Register all of the hooks related to the welcome screen functionality
20 25 *
26 + * @access public
27 + *
21 28 * @return void
22 29 */
23 30 public static function load_admin_hooks() {
24 - add_action( 'admin_menu', self::class . '::menu', 9 );
31 + add_action( 'admin_init', __CLASS__ . '::redirect' );
32 + add_action( 'admin_menu', __CLASS__ . '::menu', 9 );
25 33 }
26 34
27 35 /**
28 36 * Register Dashboard page tp Formidable admin menu.
@@ -29,12 +37,64 @@
29 37 *
30 38 * @return void
31 39 */
32 40 public static function menu() {
33 - add_submenu_page( 'formidable', 'Formidable | ' . __( 'Dashboard', 'formidable' ), esc_html__( 'Dashboard', 'formidable' ) . wp_kses_post( FrmInboxController::get_notice_count() ), 'frm_view_forms', 'formidable-dashboard', 'FrmDashboardController::route' ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
41 + add_submenu_page( 'formidable', 'Formidable | ' . __( 'Dashboard', 'formidable' ), __( 'Dashboard', 'formidable' ) . FrmInboxController::get_notice_count(), 'frm_view_forms', 'formidable-dashboard', 'FrmDashboardController::route' );
34 42 }
35 43
36 44 /**
45 + * Performs a safe (local) redirect to the welcome screen
46 + * when the plugin is activated
47 + *
48 + * @return void
49 + */
50 + public static function redirect() {
51 + $current_page = FrmAppHelper::simple_get( 'page', 'sanitize_title' );
52 + if ( $current_page === self::PAGE_SLUG ) {
53 + // Prevent endless loop.
54 + return;
55 + }
56 +
57 + // phpcs:ignore WordPress.Security.NonceVerification
58 + if ( isset( $_GET['activate-multi'] ) || is_network_admin() ) {
59 + // Only do this for single site installs.
60 + return;
61 + }
62 +
63 + // Check if we should consider redirection.
64 + if ( ! self::is_dashboard_page() ) {
65 + return;
66 + }
67 +
68 + set_transient( self::REDIRECT_META_NAME, 'no', 60 );
69 +
70 + // Prevent redirect with every activation.
71 + if ( self::already_redirected() ) {
72 + return;
73 + }
74 +
75 + // Initial install.
76 + wp_safe_redirect( esc_url( admin_url( 'admin.php?page=' . self::PAGE_SLUG ) ) );
77 + exit;
78 + }
79 +
80 + /**
81 + * Don't redirect every time the plugin is activated.
82 + *
83 + * @return bool
84 + */
85 + private static function already_redirected() {
86 + $redirect_option = 'frm_welcome_redirect';
87 + $last_redirect = get_option( $redirect_option );
88 + if ( $last_redirect ) {
89 + return true;
90 + }
91 +
92 + update_option( $redirect_option, FrmAppHelper::plugin_version(), 'no' );
93 + return false;
94 + }
95 +
96 + /**
37 97 * Triggered from FrmAppController::load_page() with admin_init
38 98 *
39 99 * @since 6.8
40 100 *
@@ -43,11 +103,9 @@
43 103 public static function load_page() {
44 104 self::remove_admin_notices_on_dashboard();
45 105 self::load_assets();
46 106
47 - $unread_count = FrmEntriesHelper::get_visible_unread_inbox_count();
48 -
49 - add_filter( 'manage_' . sanitize_title( FrmAppHelper::get_menu_name() ) . ( $unread_count ? '-' . $unread_count : '' ) . '_page_formidable-dashboard_columns', 'FrmDashboardController::entries_columns' ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
107 + add_filter( 'manage_' . sanitize_title( FrmAppHelper::get_menu_name() ) . '_page_formidable-dashboard_columns', 'FrmDashboardController::entries_columns' );
50 108 add_filter( 'frm_show_footer_links', '__return_false' );
51 109 add_filter( 'screen_options_show_screen', '__return_false' );
52 110 }
53 111
@@ -63,30 +121,28 @@
63 121 self::enqueue_assets();
64 122 }
65 123
66 124 /**
67 - * Gets dashboard helper instance.
125 + * Init dashboard page.
68 126 *
69 - * @since 6.17
70 - *
71 - * @return FrmDashboardHelper
127 + * @return void
72 128 */
73 - public static function get_dashboard_helper() {
74 - $total_payments = self::view_args_payments();
75 - $counters_value = array(
129 + public static function route() {
130 + $latest_available_form = FrmForm::get_latest_form();
131 + $total_payments = self::view_args_payments();
132 + $counters_value = array(
76 133 'forms' => FrmForm::get_forms_count(),
77 134 'entries' => FrmEntry::get_entries_count(),
78 135 );
79 136
80 - return new FrmDashboardHelper(
137 + $dashboard_view = new FrmDashboardHelper(
81 138 array(
82 - 'counters' => array(
83 - 'counters' => self::view_args_counters( FrmForm::get_latest_form(), $counters_value ),
139 + 'counters' => array(
140 + 'counters' => self::view_args_counters( $latest_available_form, $counters_value ),
84 141 ),
85 - 'license' => array(),
86 - 'get_free_templates' => array(),
87 - 'inbox' => self::view_args_inbox(),
88 - 'entries' => array(
142 + 'license' => array(),
143 + 'inbox' => self::view_args_inbox(),
144 + 'entries' => array(
89 145 'widget-heading' => __( 'Latest Entries', 'formidable' ),
90 146 'cta' => array(
91 147 'label' => __( 'View All Entries', 'formidable' ),
92 148 'link' => admin_url( 'admin.php?page=formidable-entries' ),
@@ -94,10 +150,10 @@
94 150 'show-placeholder' => 0 >= (int) $counters_value['entries'],
95 151 'count' => $counters_value['entries'],
96 152 'placeholder' => self::view_args_entries_placeholder( $counters_value['forms'] ),
97 153 ),
98 - 'payments' => array(
99 - 'show-placeholder' => ! $total_payments,
154 + 'payments' => array(
155 + 'show-placeholder' => empty( $total_payments ),
100 156 'placeholder' => array(
101 157 'copy' => __( 'You don\'t have a payment form setup yet.', 'formidable' ),
102 158 'cta' => array(
103 159 'link' => admin_url( 'admin.php?page=formidable-form-templates' ),
@@ -111,22 +167,11 @@
111 167 'items' => $total_payments,
112 168 ),
113 169 ),
114 170 ),
115 - 'video' => array( 'id' => self::get_youtube_embed_video( $counters_value['entries'] ) ),
171 + 'video' => array( 'id' => self::get_youtube_embed_video( $counters_value['entries'] ) ),
116 172 )
117 173 );
118 - }
119 -
120 - /**
121 - * Init dashboard page.
122 - *
123 - * @return void
124 - */
125 - public static function route() {
126 - $dashboard_view = self::get_dashboard_helper();
127 - $should_display_videos = is_callable( 'FrmProDashboardHelper::should_display_videos' ) ? FrmProDashboardHelper::should_display_videos() : true;
128 -
129 174 require FrmAppHelper::plugin_path() . '/classes/views/dashboard/dashboard.php';
130 175 }
131 176
132 177 /**
@@ -131,15 +176,15 @@
131 176
132 177 /**
133 178 * Init top counters widgets view args used to construct FrmDashboardHelper.
134 179 *
135 - * @param false|object $latest_available_form If a form is available, we utilize its ID to direct the 'Create New Entry' link of the entries counter CTA when no entries exist.
180 + * @param object|false $latest_available_form If a form is availble, we utilize its ID to direct the 'Create New Entry' link of the entries counter CTA when no entries exist.
136 181 * @param array $counters_value The counter values for "Total Forms" & "Total Entries".
137 182 *
138 183 * @return array
139 184 */
140 185 private static function view_args_counters( $latest_available_form, $counters_value ) {
141 - $add_entry_cta_link = false !== $latest_available_form && isset( $latest_available_form->id ) ? admin_url( 'admin.php?page=formidable-entries&frm_action=new&form=' . $latest_available_form->id ) : ''; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
186 + $add_entry_cta_link = false !== $latest_available_form && isset( $latest_available_form->id ) ? admin_url( 'admin.php?page=formidable-entries&frm_action=new&form=' . $latest_available_form->id ) : '';
142 187
143 188 $lite_counters = array(
144 189 self::view_args_build_counter( __( 'Total Forms', 'formidable' ), array(), $counters_value['forms'] ),
145 190 self::view_args_build_counter(
@@ -175,8 +220,9 @@
175 220 return array_merge( $lite_counters, $pro_counters );
176 221 }
177 222
178 223 return array_merge( $lite_counters, $pro_counters_placeholder );
224 +
179 225 }
180 226
181 227 /**
182 228 * Build view args for counter widget.
@@ -188,15 +234,15 @@
188 234 *
189 235 * @return array
190 236 */
191 237 public static function view_args_build_counter( $heading, $cta = array(), $value = 0, $type = 'default' ) {
238 +
192 239 $counter_args = array(
193 240 'heading' => $heading,
194 241 'counter' => $value,
195 242 'type' => 'default',
196 243 );
197 -
198 - if ( $cta ) {
244 + if ( ! empty( $cta ) ) {
199 245 $counter_args['cta'] = $cta;
200 246 }
201 247
202 248 return $counter_args;
@@ -204,11 +250,11 @@
204 250
205 251 /**
206 252 * Build view args for cta.
207 253 *
208 - * @param string $title
209 - * @param string $link
210 - * @param bool $display
254 + * @param string $title
255 + * @param string $link
256 + * @param boolean $display
211 257 *
212 258 * @return array
213 259 */
214 260 public static function view_args_build_cta( $title, $link = '#', $display = true ) {
@@ -224,12 +270,13 @@
224 270 *
225 271 * @return array
226 272 */
227 273 private static function view_args_payments() {
228 - $prepared_data = array();
274 +
275 + $prepared_data = array();
276 +
229 277 $model_payments = new FrmTransLitePayment();
230 278 $payments = $model_payments->get_payments_stats();
231 -
232 279 foreach ( $payments['total'] as $currency => $total_payments ) {
233 280 if ( 0 < (int) $total_payments ) {
234 281 $prepared_data[] = array(
235 282 'counter_label' => FrmCurrencyHelper::get_currency( $currency ),
@@ -244,17 +291,17 @@
244 291 /**
245 292 * Init view args for entries placeholder.
246 293 *
247 294 * @param array $forms_count The total forms count. If there are no any forms yet, we'll have CTA pointing to creating a form.
248 - *
249 295 * @return array
250 296 */
251 297 private static function view_args_entries_placeholder( $forms_count ) {
298 +
252 299 if ( ! $forms_count ) {
253 300 $copy = sprintf(
254 301 /* translators: %1$s: HTML start of a tag, %2$s: HTML close a tag */
255 302 __( 'See the %1$sform documentation%2$s for instructions on publishing your form', 'formidable' ),
256 - '<a target="_blank" href="' . esc_url( FrmAppHelper::admin_upgrade_link( '', 'knowledgebase/publish-a-form/' ) ) . '">',
303 + '<a target="_blank" href="' . FrmAppHelper::admin_upgrade_link( '', 'knowledgebase/publish-a-form/' ) . '">',
257 304 '</a>'
258 305 );
259 306 return array(
260 307 'background' => 'entries-placeholder',
@@ -261,9 +308,9 @@
261 308 'heading' => __( 'You Have No Entries Yet', 'formidable' ),
262 309 'copy' => $copy,
263 310 'button' => array(
264 311 'label' => esc_html__( 'Add New Form', 'formidable' ),
265 - 'link' => admin_url( 'admin.php?page=' . FrmFormTemplatesController::PAGE_SLUG ),
312 + 'link' => admin_url( 'admin.php?page=formidable-form-templates' ),
266 313 ),
267 314 );
268 315 }
269 316
@@ -269,9 +316,9 @@
269 316
270 317 $copy = sprintf(
271 318 /* translators: %1$s: HTML start of a tag, %2$s: HTML close a tag */
272 319 __( 'See the %1$sform documentation%2$s for instructions on publishing a form. Once vou have at least one entry you\'ll see it here.', 'formidable' ),
273 - '<a target="_blank" href="' . esc_url( FrmAppHelper::admin_upgrade_link( '', 'knowledgebase/publish-a-form/' ) ) . '">',
320 + '<a target="_blank" href="' . FrmAppHelper::admin_upgrade_link( '', 'knowledgebase/publish-a-form/' ) . '">',
274 321 '</a>'
275 322 );
276 323 return array(
277 324 'background' => 'entries-placeholder',
@@ -285,15 +332,17 @@
285 332 * A function to handle the counters cta from the top: Total Forms, Total Entries, All Views, Installed Apps.
286 333 *
287 334 * @param string $counter_type
288 335 * @param int $counter_value
289 - * @param false|object $latest_available_form The form object of the latest form available. If there are at least one
290 - * form available we show "Add Entry" cta for entries counter.
291 - *
336 + * @param object|false $latest_available_form The form object of the latest form available. If there are at least one form available we show "Add Entry" cta for entries counter.
292 337 * @return array
293 338 */
294 339 public static function display_counter_cta( $counter_type, $counter_value, $latest_available_form = false ) {
295 - return $counter_value <= 0 && ! ( 'entries' === $counter_type && false === $latest_available_form );
340 + if ( $counter_value > 0 || ( 'entries' === $counter_type && false === $latest_available_form ) ) {
341 + return false;
342 + }
343 +
344 + return true;
296 345 }
297 346
298 347 /**
299 348 * Hide 3rd party notifications from dashboard
@@ -305,9 +354,8 @@
305 354 return;
306 355 }
307 356
308 357 global $wp_filter;
309 -
310 358 if ( isset( $wp_filter['admin_notices'] ) ) {
311 359 unset( $wp_filter['admin_notices'] );
312 360 }
313 361 }
@@ -328,9 +376,9 @@
328 376 wp_send_json_success();
329 377 break;
330 378
331 379 case 'save-subscribed-email':
332 - $email = FrmAppHelper::get_post_param( 'email', '', 'sanitize_email' );
380 + $email = FrmAppHelper::get_post_param( 'email' );
333 381 self::save_subscribed_email( $email );
334 382 wp_send_json_success();
335 383 break;
336 384 }
@@ -340,16 +388,24 @@
340 388 * Handle the entries list. Called via add_filter.
341 389 * Hook name: manage_formidable_page_formidable-dashboard_columns.
342 390 *
343 391 * @param array $columns An associative array of column headings.
344 - *
345 392 * @return array
346 393 */
347 394 public static function entries_columns( $columns = array() ) {
348 - $columns['0_form_id'] = esc_html__( 'Form', 'formidable' );
349 - $columns['0_name'] = esc_html__( 'Name', 'formidable' );
350 - $columns['0_user_id'] = esc_html__( 'Author', 'formidable' );
351 - $columns['0_created_at'] = esc_html__( 'Created on', 'formidable' );
395 +
396 + $form_id = FrmForm::get_current_form_id();
397 +
398 + if ( $form_id ) {
399 + self::get_columns_for_form( $form_id, $columns );
400 + } else {
401 + $columns[ $form_id . '_form_id' ] = __( 'Form', 'formidable' );
402 + $columns[ $form_id . '_name' ] = __( 'Name', 'formidable' );
403 + $columns[ $form_id . '_user_id' ] = __( 'Author', 'formidable' );
404 + }
405 +
406 + $columns[ $form_id . '_created_at' ] = __( 'Created on', 'formidable' );
407 +
352 408 return $columns;
353 409 }
354 410
355 411 /**
@@ -354,19 +410,24 @@
354 410
355 411 /**
356 412 * Check if user has closed the welcome banner. The status of banner is saved in db options.
357 413 *
358 - * @return bool
414 + * @return boolean
359 415 */
360 416 public static function welcome_banner_has_closed() {
417 + $user_id = get_current_user_id();
361 418 $banner_closed_by_users = self::get_closed_welcome_banner_user_ids();
362 - return $banner_closed_by_users && in_array( get_current_user_id(), $banner_closed_by_users, true );
419 +
420 + if ( ! empty( $banner_closed_by_users ) && false !== array_search( $user_id, $banner_closed_by_users, true ) ) {
421 + return true;
422 + }
423 + return false;
363 424 }
364 425
365 426 /**
366 427 * Check if is dashboard page.
367 428 *
368 - * @return bool
429 + * @return boolean
369 430 */
370 431 public static function is_dashboard_page() {
371 432 return FrmAppHelper::is_admin_page( 'formidable-dashboard' );
372 433 }
@@ -374,13 +435,13 @@
374 435 /**
375 436 * Detect if the logged user's email is subscribed. Used for inbox email subscribe.
376 437 *
377 438 * @param string $email The logged user's email.
378 - *
379 - * @return bool
439 + * @return boolean
380 440 */
381 441 public static function email_is_subscribed( $email ) {
382 - return in_array( $email, self::get_subscribed_emails(), true );
442 + $subscribed_emails = self::get_subscribed_emails();
443 + return false !== in_array( $email, $subscribed_emails, true );
383 444 }
384 445
385 446 /**
386 447 * Register controller assets.
@@ -397,13 +458,13 @@
397 458 *
398 459 * @return void
399 460 */
400 461 public static function enqueue_assets() {
462 +
401 463 if ( ! self::is_dashboard_page() ) {
402 464 return;
403 465 }
404 466
405 - wp_enqueue_style( 'formidable-animations' );
406 467 wp_enqueue_style( self::PAGE_SLUG );
407 468 wp_enqueue_script( self::PAGE_SLUG );
408 469 }
409 470
@@ -418,35 +479,24 @@
418 479
419 480 /**
420 481 * Prepare inbox messages data.
421 482 *
422 - * @param array $data
423 - *
424 483 * @return array
425 484 */
426 485 private static function inbox_prepare_messages( $data ) {
427 486 foreach ( $data as $key => $messages ) {
428 - if ( ! in_array( $key, array( 'unread', 'dismissed' ), true ) ) {
429 - continue;
487 + if ( in_array( $key, array( 'unread', 'dismissed' ), true ) ) {
488 + foreach ( $messages as $key_msg => $message ) {
489 + $data[ $key ][ $key_msg ]['cta'] = self::inbox_clean_messages_cta( $message['cta'] );
490 + }
430 491 }
431 -
432 - foreach ( $messages as $key_msg => $message ) {
433 - $data[ $key ][ $key_msg ]['cta'] = self::inbox_clean_messages_cta( $message['cta'] );
434 - }
435 492 }
436 -
437 493 return $data;
438 494 }
439 495
440 - /**
441 - * Clean messages CTA.
442 - *
443 - * @param string $cta
444 - *
445 - * @return string
446 - */
447 496 private static function inbox_clean_messages_cta( $cta ) {
448 - // Remove dismiss button
497 +
498 + // remove dismiss button
449 499 $pattern = '/<a[^>]*class="[^"]*frm_inbox_dismiss[^"]*"[^>]*>.*?<\/a>/is';
450 500 return preg_replace( $pattern, ' ', $cta );
451 501 }
452 502
@@ -452,10 +502,9 @@
452 502
453 503 /**
454 504 * Get the embed YouTube video from YouTube feed api. If there are 0 entries we show the welcome video otherwise latest video from FF YouTube channel is displayed.
455 505 *
456 - * @param int|string $entries_count The total entries available.
457 - *
506 + * @param int $entries_count The total entries available.
458 507 * @return string|null The YouTube video ID.
459 508 */
460 509 private static function get_youtube_embed_video( $entries_count ) {
461 510 $youtube_api = new FrmYoutubeFeedApi();
@@ -464,19 +513,16 @@
464 513
465 514 if ( 0 === (int) $entries_count && false === $welcome_video && false === $featured_video ) {
466 515 return null;
467 516 }
468 -
469 517 if ( 0 === (int) $entries_count && false !== $welcome_video ) {
470 - return $welcome_video['video-id'] ?? null;
518 + return isset( $welcome_video['video-id'] ) ? $welcome_video['video-id'] : null;
471 519 }
472 -
473 520 // We might receive the most recent video feed as the featured selection.
474 521 if ( isset( $featured_video[0] ) ) {
475 522 return $featured_video[0]['video-id'];
476 523 }
477 -
478 - return $featured_video['video-id'] ?? null;
524 + return isset( $featured_video['video-id'] ) ? $featured_video['video-id'] : null;
479 525 }
480 526
481 527 /**
482 528 * Save subscribed user's email to dashboard options.
@@ -482,20 +528,16 @@
482 528 * Save subscribed user's email to dashboard options.
483 529 * Used for Inbox widget - email subscribe.
484 530 *
485 531 * @param string $email The user email address.
486 - *
487 532 * @return void
488 533 */
489 534 private static function save_subscribed_email( $email ) {
490 535 $subscribed_emails = self::get_subscribed_emails();
491 -
492 - if ( in_array( $email, $subscribed_emails, true ) ) {
493 - return;
536 + if ( false === array_search( $email, $subscribed_emails, true ) ) {
537 + $subscribed_emails[] = $email;
538 + self::update_dashboard_options( $subscribed_emails, 'inbox-subscribed-emails' );
494 539 }
495 -
496 - $subscribed_emails[] = $email;
497 - self::update_dashboard_options( $subscribed_emails, 'inbox-subscribed-emails' );
498 540 }
499 541
500 542 /**
501 543 * Get list of users' ids that have closed the welcome banner.
@@ -518,19 +560,19 @@
518 560 /**
519 561 * Get the dashboard options from db.
520 562 *
521 563 * @param string|null $option_name The dashboard option name. If null it will return all dashboard options.
522 - *
523 564 * @return array
524 565 */
525 566 private static function get_dashboard_options( $option_name = null ) {
526 567 $options = get_option( self::OPTION_META_NAME, array() );
527 -
528 568 if ( null !== $option_name && ! isset( $options[ $option_name ] ) ) {
529 569 return array();
530 570 }
531 -
532 - return null !== $option_name ? $options[ $option_name ] : $options;
571 + if ( null !== $option_name ) {
572 + return $options[ $option_name ];
573 + }
574 + return $options;
533 575 }
534 576
535 577 /**
536 578 * Update the dashboard options to db.
@@ -542,9 +584,9 @@
542 584 */
543 585 private static function update_dashboard_options( $data, $option_name ) {
544 586 $options = self::get_dashboard_options();
545 587 $options[ $option_name ] = $data;
546 - update_option( self::OPTION_META_NAME, $options, false );
588 + update_option( self::OPTION_META_NAME, $options, 'no' );
547 589 }
548 590
549 591 /**
550 592 * Save user id to closed banner list.
@@ -553,13 +595,10 @@
553 595 */
554 596 private static function add_welcome_closed_banner_user_id() {
555 597 $users_list = self::get_closed_welcome_banner_user_ids();
556 598 $user_id = get_current_user_id();
557 -
558 - if ( in_array( $user_id, $users_list, true ) ) {
559 - return;
599 + if ( false === array_search( $user_id, $users_list, true ) ) {
600 + $users_list[] = $user_id;
601 + self::update_dashboard_options( $users_list, 'closed-welcome-banner-user-ids' );
560 602 }
561 -
562 - $users_list[] = $user_id;
563 - self::update_dashboard_options( $users_list, 'closed-welcome-banner-user-ids' );
564 603 }
565 604 }