PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.28
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.28
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 / controllers / FrmDashboardController.php

FrmDashboardController.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.28, at classes/controllers/FrmDashboardController.php

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