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

553 lines 16.3 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' );
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' );
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
129 $should_display_videos = is_callable( 'FrmProDashboardHelper::should_display_videos' ) ? FrmProDashboardHelper::should_display_videos() : true;
130
131 require FrmAppHelper::plugin_path() . '/classes/views/dashboard/dashboard.php';
132 }
133
134 /**
135 * Init top counters widgets view args used to construct FrmDashboardHelper.
136 *
137 * @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.
138 * @param array $counters_value The counter values for "Total Forms" & "Total Entries".
139 *
140 * @return array
141 */
142 private static function view_args_counters( $latest_available_form, $counters_value ) {
143 $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 ) : '';
144
145 $lite_counters = array(
146 self::view_args_build_counter( __( 'Total Forms', 'formidable' ), array(), $counters_value['forms'] ),
147 self::view_args_build_counter(
148 __( 'Total Entries', 'formidable' ),
149 self::view_args_build_cta(
150 __( 'Add Entry', 'formidable' ),
151 $add_entry_cta_link,
152 self::display_counter_cta( 'entries', $counters_value['entries'], $latest_available_form )
153 ),
154 $counters_value['entries']
155 ),
156 );
157
158 $pro_counters_placeholder = array(
159 self::view_args_build_counter(
160 __( 'All Views', 'formidable' ),
161 self::view_args_build_cta(
162 __( 'Learn More', 'formidable' ),
163 admin_url( 'admin.php?page=formidable-views' )
164 )
165 ),
166 self::view_args_build_counter(
167 __( 'Installed Apps', 'formidable' ),
168 self::view_args_build_cta(
169 __( 'Learn More', 'formidable' ),
170 admin_url( 'admin.php?page=formidable-applications' )
171 )
172 ),
173 );
174
175 if ( class_exists( 'FrmProDashboardController' ) ) {
176 $pro_counters = FrmProDashboardController::get_counters();
177 return array_merge( $lite_counters, $pro_counters );
178 }
179
180 return array_merge( $lite_counters, $pro_counters_placeholder );
181 }
182
183 /**
184 * Build view args for counter widget.
185 *
186 * @param string $heading
187 * @param array $cta
188 * @param int $value
189 * @param string $type
190 *
191 * @return array
192 */
193 public static function view_args_build_counter( $heading, $cta = array(), $value = 0, $type = 'default' ) {
194
195 $counter_args = array(
196 'heading' => $heading,
197 'counter' => $value,
198 'type' => 'default',
199 );
200 if ( ! empty( $cta ) ) {
201 $counter_args['cta'] = $cta;
202 }
203
204 return $counter_args;
205 }
206
207 /**
208 * Build view args for cta.
209 *
210 * @param string $title
211 * @param string $link
212 * @param bool $display
213 *
214 * @return array
215 */
216 public static function view_args_build_cta( $title, $link = '#', $display = true ) {
217 return array(
218 'title' => $title,
219 'link' => $link,
220 'display' => $display,
221 );
222 }
223
224 /**
225 * Init total earnings widget view args to FrmDashboardHelper.
226 *
227 * @return array
228 */
229 private static function view_args_payments() {
230
231 $prepared_data = array();
232
233 $model_payments = new FrmTransLitePayment();
234 $payments = $model_payments->get_payments_stats();
235 foreach ( $payments['total'] as $currency => $total_payments ) {
236 if ( 0 < (int) $total_payments ) {
237 $prepared_data[] = array(
238 'counter_label' => FrmCurrencyHelper::get_currency( $currency ),
239 'counter' => (int) $total_payments,
240 );
241 }
242 }
243
244 return $prepared_data;
245 }
246
247 /**
248 * Init view args for entries placeholder.
249 *
250 * @param array $forms_count The total forms count. If there are no any forms yet, we'll have CTA pointing to creating a form.
251 * @return array
252 */
253 private static function view_args_entries_placeholder( $forms_count ) {
254
255 if ( ! $forms_count ) {
256 $copy = sprintf(
257 /* translators: %1$s: HTML start of a tag, %2$s: HTML close a tag */
258 __( 'See the %1$sform documentation%2$s for instructions on publishing your form', 'formidable' ),
259 '<a target="_blank" href="' . esc_url( FrmAppHelper::admin_upgrade_link( '', 'knowledgebase/publish-a-form/' ) ) . '">',
260 '</a>'
261 );
262 return array(
263 'background' => 'entries-placeholder',
264 'heading' => __( 'You Have No Entries Yet', 'formidable' ),
265 'copy' => $copy,
266 'button' => array(
267 'label' => esc_html__( 'Add New Form', 'formidable' ),
268 'link' => admin_url( 'admin.php?page=' . FrmFormTemplatesController::PAGE_SLUG ),
269 ),
270 );
271 }
272
273 $copy = sprintf(
274 /* translators: %1$s: HTML start of a tag, %2$s: HTML close a tag */
275 __( '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' ),
276 '<a target="_blank" href="' . FrmAppHelper::admin_upgrade_link( '', 'knowledgebase/publish-a-form/' ) . '">',
277 '</a>'
278 );
279 return array(
280 'background' => 'entries-placeholder',
281 'heading' => __( 'You Have No Entries Yet', 'formidable' ),
282 'copy' => $copy,
283 'button' => null,
284 );
285 }
286
287 /**
288 * A function to handle the counters cta from the top: Total Forms, Total Entries, All Views, Installed Apps.
289 *
290 * @param string $counter_type
291 * @param int $counter_value
292 * @param false|object $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.
293 * @return array
294 */
295 public static function display_counter_cta( $counter_type, $counter_value, $latest_available_form = false ) {
296 if ( $counter_value > 0 || ( 'entries' === $counter_type && false === $latest_available_form ) ) {
297 return false;
298 }
299
300 return true;
301 }
302
303 /**
304 * Hide 3rd party notifications from dashboard
305 *
306 * @return void
307 */
308 public static function remove_admin_notices_on_dashboard() {
309 if ( false === self::is_dashboard_page() ) {
310 return;
311 }
312
313 global $wp_filter;
314 if ( isset( $wp_filter['admin_notices'] ) ) {
315 unset( $wp_filter['admin_notices'] );
316 }
317 }
318
319 /**
320 * Handle dashboard AJAX requests. Used in FrmHooksController
321 * Action name: dashboard_ajax_action.
322 *
323 * @return void
324 */
325 public static function ajax_requests() {
326 $dashboard_action = FrmAppHelper::get_post_param( 'dashboard_action', '', 'sanitize_text_field' );
327
328 switch ( $dashboard_action ) {
329
330 case 'welcome-banner-has-closed':
331 self::add_welcome_closed_banner_user_id();
332 wp_send_json_success();
333 break;
334
335 case 'save-subscribed-email':
336 $email = FrmAppHelper::get_post_param( 'email', '', 'sanitize_email' );
337 self::save_subscribed_email( $email );
338 wp_send_json_success();
339 break;
340 }
341 }
342
343 /**
344 * Handle the entries list. Called via add_filter.
345 * Hook name: manage_formidable_page_formidable-dashboard_columns.
346 *
347 * @param array $columns An associative array of column headings.
348 * @return array
349 */
350 public static function entries_columns( $columns = array() ) {
351 $columns['0_form_id'] = esc_html__( 'Form', 'formidable' );
352 $columns['0_name'] = esc_html__( 'Name', 'formidable' );
353 $columns['0_user_id'] = esc_html__( 'Author', 'formidable' );
354 $columns['0_created_at'] = esc_html__( 'Created on', 'formidable' );
355 return $columns;
356 }
357
358 /**
359 * Check if user has closed the welcome banner. The status of banner is saved in db options.
360 *
361 * @return bool
362 */
363 public static function welcome_banner_has_closed() {
364 $user_id = get_current_user_id();
365 $banner_closed_by_users = self::get_closed_welcome_banner_user_ids();
366
367 if ( ! empty( $banner_closed_by_users ) && in_array( $user_id, $banner_closed_by_users, true ) ) {
368 return true;
369 }
370 return false;
371 }
372
373 /**
374 * Check if is dashboard page.
375 *
376 * @return bool
377 */
378 public static function is_dashboard_page() {
379 return FrmAppHelper::is_admin_page( 'formidable-dashboard' );
380 }
381
382 /**
383 * Detect if the logged user's email is subscribed. Used for inbox email subscribe.
384 *
385 * @param string $email The logged user's email.
386 * @return bool
387 */
388 public static function email_is_subscribed( $email ) {
389 $subscribed_emails = self::get_subscribed_emails();
390 return in_array( $email, $subscribed_emails, true );
391 }
392
393 /**
394 * Register controller assets.
395 *
396 * @return void
397 */
398 public static function register_assets() {
399 wp_register_script( self::PAGE_SLUG, FrmAppHelper::plugin_url() . '/js/formidable_dashboard.js', array( 'formidable_admin' ), FrmAppHelper::plugin_version(), true );
400 wp_register_style( self::PAGE_SLUG, FrmAppHelper::plugin_url() . '/css/admin/dashboard.css', array(), FrmAppHelper::plugin_version() );
401 }
402
403 /**
404 * Enqueue controller assets.
405 *
406 * @return void
407 */
408 public static function enqueue_assets() {
409
410 if ( ! self::is_dashboard_page() ) {
411 return;
412 }
413
414 wp_enqueue_style( 'formidable-animations' );
415 wp_enqueue_style( self::PAGE_SLUG );
416 wp_enqueue_script( self::PAGE_SLUG );
417 }
418
419 /**
420 * Init the view args for Inbox widget.
421 *
422 * @return array
423 */
424 private static function view_args_inbox() {
425 return self::inbox_prepare_messages( FrmInboxController::get_inbox_messages() );
426 }
427
428 /**
429 * Prepare inbox messages data.
430 *
431 * @return array
432 */
433 private static function inbox_prepare_messages( $data ) {
434 foreach ( $data as $key => $messages ) {
435 if ( in_array( $key, array( 'unread', 'dismissed' ), true ) ) {
436 foreach ( $messages as $key_msg => $message ) {
437 $data[ $key ][ $key_msg ]['cta'] = self::inbox_clean_messages_cta( $message['cta'] );
438 }
439 }
440 }
441 return $data;
442 }
443
444 private static function inbox_clean_messages_cta( $cta ) {
445
446 // remove dismiss button
447 $pattern = '/<a[^>]*class="[^"]*frm_inbox_dismiss[^"]*"[^>]*>.*?<\/a>/is';
448 return preg_replace( $pattern, ' ', $cta );
449 }
450
451 /**
452 * 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.
453 *
454 * @param int $entries_count The total entries available.
455 * @return string|null The YouTube video ID.
456 */
457 private static function get_youtube_embed_video( $entries_count ) {
458 $youtube_api = new FrmYoutubeFeedApi();
459 $welcome_video = $youtube_api->get_video();
460 $featured_video = $youtube_api->get_video( 'featured' );
461
462 if ( 0 === (int) $entries_count && false === $welcome_video && false === $featured_video ) {
463 return null;
464 }
465 if ( 0 === (int) $entries_count && false !== $welcome_video ) {
466 return $welcome_video['video-id'] ?? null;
467 }
468 // We might receive the most recent video feed as the featured selection.
469 if ( isset( $featured_video[0] ) ) {
470 return $featured_video[0]['video-id'];
471 }
472 return $featured_video['video-id'] ?? null;
473 }
474
475 /**
476 * Save subscribed user's email to dashboard options.
477 * Used for Inbox widget - email subscribe.
478 *
479 * @param string $email The user email address.
480 * @return void
481 */
482 private static function save_subscribed_email( $email ) {
483 $subscribed_emails = self::get_subscribed_emails();
484 if ( ! in_array( $email, $subscribed_emails, true ) ) {
485 $subscribed_emails[] = $email;
486 self::update_dashboard_options( $subscribed_emails, 'inbox-subscribed-emails' );
487 }
488 }
489
490 /**
491 * Get list of users' ids that have closed the welcome banner.
492 *
493 * @return array
494 */
495 private static function get_closed_welcome_banner_user_ids() {
496 return self::get_dashboard_options( 'closed-welcome-banner-user-ids' );
497 }
498
499 /**
500 * Get list of subscribed emails. The list will contain all emails subscribed via Inbox widget.
501 *
502 * @return array
503 */
504 private static function get_subscribed_emails() {
505 return self::get_dashboard_options( 'inbox-subscribed-emails' );
506 }
507
508 /**
509 * Get the dashboard options from db.
510 *
511 * @param string|null $option_name The dashboard option name. If null it will return all dashboard options.
512 * @return array
513 */
514 private static function get_dashboard_options( $option_name = null ) {
515 $options = get_option( self::OPTION_META_NAME, array() );
516 if ( null !== $option_name && ! isset( $options[ $option_name ] ) ) {
517 return array();
518 }
519 if ( null !== $option_name ) {
520 return $options[ $option_name ];
521 }
522 return $options;
523 }
524
525 /**
526 * Update the dashboard options to db.
527 *
528 * @param array $data
529 * @param string $option_name
530 *
531 * @return void
532 */
533 private static function update_dashboard_options( $data, $option_name ) {
534 $options = self::get_dashboard_options();
535 $options[ $option_name ] = $data;
536 update_option( self::OPTION_META_NAME, $options, 'no' );
537 }
538
539 /**
540 * Save user id to closed banner list.
541 *
542 * @return void
543 */
544 private static function add_welcome_closed_banner_user_id() {
545 $users_list = self::get_closed_welcome_banner_user_ids();
546 $user_id = get_current_user_id();
547 if ( ! in_array( $user_id, $users_list, true ) ) {
548 $users_list[] = $user_id;
549 self::update_dashboard_options( $users_list, 'closed-welcome-banner-user-ids' );
550 }
551 }
552 }
553