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