PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.1.1
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.1.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 / FrmAppController.php

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

956 lines 26.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 FrmAppController {
7
8 public static function menu() {
9 FrmAppHelper::maybe_add_permissions();
10 if ( ! current_user_can( 'frm_view_forms' ) ) {
11 return;
12 }
13
14 $menu_name = FrmAppHelper::get_menu_name();
15 add_menu_page( 'Formidable', $menu_name, 'frm_view_forms', 'formidable', 'FrmFormsController::route', self::menu_icon(), self::get_menu_position() );
16 }
17
18 /**
19 * @return int
20 */
21 private static function get_menu_position() {
22 return (int) apply_filters( 'frm_menu_position', 29 );
23 }
24
25 /**
26 * @since 3.05
27 */
28 private static function menu_icon() {
29 $icon = FrmAppHelper::svg_logo(
30 array(
31 'fill' => '#a0a5aa',
32 'orange' => '#a0a5aa',
33 )
34 );
35 $icon = 'data:image/svg+xml;base64,' . base64_encode( $icon );
36
37 return apply_filters( 'frm_icon', $icon );
38 }
39
40 /**
41 * @since 3.0
42 */
43 public static function add_admin_class( $classes ) {
44 if ( self::is_white_page() ) {
45 $classes .= ' frm-white-body ';
46 $classes .= self::get_os();
47
48 $page = str_replace( 'formidable-', '', FrmAppHelper::simple_get( 'page', 'sanitize_title' ) );
49 if ( empty( $page ) || $page === 'formidable' ) {
50 $action = FrmAppHelper::simple_get( 'frm_action', 'sanitize_title' );
51 if ( in_array( $action, array( 'settings', 'edit', 'list' ) ) ) {
52 $page .= $action;
53 } else {
54 $page = $action;
55 }
56 }
57 if ( ! empty( $page ) ) {
58 $classes .= ' frm-admin-page-' . $page;
59 }
60 }
61
62 if ( FrmAppHelper::is_full_screen() ) {
63 $full_screen_on = self::get_full_screen_setting();
64 $add_class = '';
65 if ( $full_screen_on ) {
66 $add_class = ' frm-full-screen is-fullscreen-mode';
67 wp_enqueue_style( 'wp-edit-post' ); // Load the CSS for .is-fullscreen-mode.
68 }
69 $classes .= apply_filters( 'frm_admin_full_screen_class', $add_class );
70 }
71
72 if ( ! FrmAppHelper::pro_is_installed() ) {
73 $classes .= ' frm-lite ';
74 }
75
76 return $classes;
77 }
78
79 /**
80 * Get the full screen mode setting from the block editor.
81 *
82 * @since 6.1
83 *
84 * @return bool
85 */
86 private static function get_full_screen_setting() {
87 global $wpdb;
88 $meta_key = $wpdb->get_blog_prefix() . 'persisted_preferences';
89
90 $prefs = get_user_meta( get_current_user_id(), $meta_key, true );
91 if ( $prefs && isset( $prefs['core/edit-post']['fullscreenMode'] ) ) {
92 return $prefs['core/edit-post']['fullscreenMode'];
93 }
94
95 return true;
96 }
97
98 /**
99 * @since 4.0
100 */
101 private static function get_os() {
102 $agent = strtolower( FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' ) );
103 $os = '';
104 if ( strpos( $agent, 'mac' ) !== false ) {
105 $os = ' osx';
106 } elseif ( strpos( $agent, 'linux' ) !== false ) {
107 $os = ' linux';
108 } elseif ( strpos( $agent, 'windows' ) !== false ) {
109 $os = ' windows';
110 }
111 return $os;
112 }
113
114 /**
115 * @since 3.0
116 */
117 private static function is_white_page() {
118 $white_pages = array(
119 'formidable',
120 'formidable-entries',
121 'formidable-views',
122 'formidable-views-editor',
123 'formidable-pro-upgrade',
124 'formidable-addons',
125 'formidable-import',
126 'formidable-settings',
127 'formidable-styles',
128 'formidable-styles2',
129 'formidable-inbox',
130 'formidable-welcome',
131 'formidable-applications',
132 );
133
134 $get_page = FrmAppHelper::simple_get( 'page', 'sanitize_title' );
135 $is_white_page = in_array( $get_page, $white_pages, true );
136
137 if ( ! $is_white_page ) {
138 $screen = get_current_screen();
139 $is_white_page = ( $screen && strpos( $screen->id, 'frm_display' ) !== false );
140 }
141
142 /**
143 * Allow another add on to style a page as a Formidable "white page", which adds a white background color.
144 *
145 * @since 5.3
146 *
147 * @param bool $is_white_page
148 */
149 $is_white_page = apply_filters( 'frm_is_white_page', $is_white_page );
150
151 return $is_white_page;
152 }
153
154 public static function load_wp_admin_style() {
155 FrmAppHelper::load_font_style();
156 }
157
158 public static function get_form_nav( $form, $show_nav = false, $title = 'show' ) {
159 $show_nav = FrmAppHelper::get_param( 'show_nav', $show_nav, 'get', 'absint' );
160 if ( empty( $show_nav ) || ! $form ) {
161 return;
162 }
163
164 FrmForm::maybe_get_form( $form );
165 if ( ! is_object( $form ) ) {
166 return;
167 }
168
169 $id = $form->id;
170 $current_page = self::get_current_page();
171 $nav_items = self::get_form_nav_items( $form );
172
173 include( FrmAppHelper::plugin_path() . '/classes/views/shared/form-nav.php' );
174 }
175
176 private static function get_current_page() {
177 $page = FrmAppHelper::simple_get( 'page', 'sanitize_title' );
178 $post_type = FrmAppHelper::simple_get( 'post_type', 'sanitize_title', 'None' );
179 $current_page = isset( $_GET['page'] ) ? $page : $post_type;
180
181 if ( FrmAppHelper::is_view_builder_page() ) {
182 $current_page = 'frm_display';
183 }
184
185 return $current_page;
186 }
187
188 private static function get_form_nav_items( $form ) {
189 $id = $form->parent_form_id ? $form->parent_form_id : $form->id;
190
191 $nav_items = array(
192 array(
193 'link' => FrmForm::get_edit_link( $id ),
194 'label' => __( 'Build', 'formidable' ),
195 'current' => array( 'edit', 'new', 'duplicate' ),
196 'page' => 'formidable',
197 'permission' => 'frm_edit_forms',
198 ),
199 array(
200 'link' => FrmStylesHelper::get_list_url( $id ),
201 'label' => __( 'Style', 'formidable' ),
202 'current' => array(),
203 'page' => 'formidable-styles',
204 'permission' => 'frm_edit_forms',
205 ),
206 array(
207 'link' => admin_url( 'admin.php?page=formidable&frm_action=settings&id=' . absint( $id ) ),
208 'label' => __( 'Settings', 'formidable' ),
209 'current' => array( 'settings' ),
210 'page' => 'formidable',
211 'permission' => 'frm_edit_forms',
212 ),
213 array(
214 'link' => admin_url( 'admin.php?page=formidable-entries&frm_action=list&form=' . absint( $id ) ),
215 'label' => __( 'Entries', 'formidable' ),
216 'current' => array(),
217 'page' => 'formidable-entries',
218 'permission' => 'frm_view_entries',
219 ),
220 );
221
222 $views_installed = is_callable( 'FrmProAppHelper::views_is_installed' ) ? FrmProAppHelper::views_is_installed() : FrmAppHelper::pro_is_installed();
223
224 if ( ! $views_installed ) {
225 $nav_items[] = array(
226 'link' => admin_url( 'admin.php?page=formidable-views&form=' . absint( $id ) ),
227 'label' => __( 'Views', 'formidable' ),
228 'current' => array(),
229 'page' => 'formidable-views',
230 'permission' => 'frm_view_entries',
231 'atts' => array(
232 'class' => 'frm_noallow',
233 ),
234 );
235 }
236
237 // Let people know reports and views exist.
238 if ( ! FrmAppHelper::pro_is_installed() ) {
239 $nav_items[] = array(
240 'link' => admin_url( 'admin.php?page=formidable&frm_action=lite-reports&form=' . absint( $id ) ),
241 'label' => __( 'Reports', 'formidable' ),
242 'current' => array( 'reports' ),
243 'page' => 'formidable',
244 'permission' => 'frm_view_entries',
245 'atts' => array(
246 'class' => 'frm_noallow',
247 ),
248 );
249 }
250
251 $nav_args = array(
252 'form_id' => $id,
253 'form' => $form,
254 );
255
256 return apply_filters( 'frm_form_nav_list', $nav_items, $nav_args );
257 }
258
259 // Adds a settings link to the plugins page
260 public static function settings_link( $links ) {
261 $settings = array();
262
263 if ( ! FrmAppHelper::pro_is_installed() ) {
264 $settings[] = '<a href="' . esc_url( FrmAppHelper::admin_upgrade_link( 'plugin-row' ) ) . '" target="_blank" rel="noopener"><b>' . esc_html__( 'Upgrade to Pro', 'formidable' ) . '</b></a>';
265 }
266
267 $settings[] = '<a href="' . esc_url( admin_url( 'admin.php?page=formidable' ) ) . '">' . __( 'Build a Form', 'formidable' ) . '</a>';
268
269 return array_merge( $settings, $links );
270 }
271
272 public static function pro_get_started_headline() {
273 self::review_request();
274 FrmAppHelper::min_pro_version_notice( '4.0' );
275 }
276
277 /**
278 * Add admin notices as needed for reviews
279 *
280 * @since 3.04.03
281 */
282 private static function review_request() {
283 $reviews = new FrmReviews();
284 $reviews->review_request();
285 }
286
287 /**
288 * Save the request to hide the review
289 *
290 * @since 3.04.03
291 */
292 public static function dismiss_review() {
293 FrmAppHelper::permission_check( 'frm_change_settings' );
294 check_ajax_referer( 'frm_ajax', 'nonce' );
295
296 $reviews = new FrmReviews();
297 $reviews->dismiss_review();
298 }
299
300 /**
301 * @since 3.04.02
302 */
303 public static function include_upgrade_overlay() {
304 self::enqueue_dialog_assets();
305 add_action( 'admin_footer', 'FrmAppController::upgrade_overlay_html' );
306 }
307
308 /**
309 * Enqueue scripts and styles required for modals.
310 *
311 * @since 5.3
312 *
313 * @return void
314 */
315 public static function enqueue_dialog_assets() {
316 wp_enqueue_script( 'jquery-ui-dialog' );
317 wp_enqueue_style( 'jquery-ui-dialog' );
318 }
319
320 /**
321 * @since 3.06.03
322 */
323 public static function upgrade_overlay_html() {
324 $is_pro = FrmAppHelper::pro_is_installed();
325 $upgrade_link = array(
326 'medium' => 'builder',
327 'content' => 'upgrade',
328 );
329 $default_link = FrmAppHelper::admin_upgrade_link( $upgrade_link );
330 $plugin_path = FrmAppHelper::plugin_path();
331 $shared_path = $plugin_path . '/classes/views/shared/';
332
333 include $shared_path . 'upgrade_overlay.php';
334 include $shared_path . 'confirm-overlay.php';
335
336 if ( FrmAppHelper::is_admin_page( 'formidable-welcome' ) || FrmAppHelper::on_form_listing_page() ) {
337 self::new_form_overlay_html();
338 }
339 }
340
341 private static function new_form_overlay_html() {
342 FrmFormsController::before_list_templates();
343
344 $plugin_path = FrmAppHelper::plugin_path();
345 $path = $plugin_path . '/classes/views/frm-forms/';
346 $expired = FrmFormsController::expired();
347 $expiring = FrmAddonsController::is_license_expiring();
348 $user = wp_get_current_user(); // $user used in leave-email.php to determine a default value for field
349 $view_path = $path . 'new-form-overlay/';
350 $modal_class = '';
351 $upgrade_link = FrmAppHelper::admin_upgrade_link(
352 array(
353 'medium' => 'new-template',
354 'content' => 'upgrade',
355 )
356 );
357 $renew_link = FrmAppHelper::admin_upgrade_link(
358 array(
359 'medium' => 'new-template',
360 'content' => 'renew',
361 )
362 );
363 $blocks_to_render = array();
364
365 if ( ! FrmAppHelper::pro_is_installed() ) {
366 // avoid rendering the email and code blocks for users who have upgraded or have a free license already
367 $api = new FrmFormTemplateApi();
368 if ( ! $api->has_free_access() ) {
369 array_push( $blocks_to_render, 'email', 'code' );
370 }
371 }
372
373 // avoid rendering the upgrade block for users with elite
374 if ( 'elite' !== FrmAddonsController::license_type() ) {
375 $blocks_to_render[] = 'upgrade';
376 }
377
378 // avoid rendering the renew block for users who are not currently expired
379 if ( $expired ) {
380 $blocks_to_render[] = 'renew';
381 $modal_class = 'frm-expired';
382 } elseif ( $expiring ) {
383 $modal_class = 'frm-expiring';
384 }
385
386 include $path . 'new-form-overlay.php';
387 }
388
389 public static function include_info_overlay() {
390 self::enqueue_dialog_assets();
391 add_action( 'admin_footer', 'FrmAppController::info_overlay_html' );
392 }
393
394 public static function info_overlay_html() {
395 include FrmAppHelper::plugin_path() . '/classes/views/shared/info-overlay.php';
396 }
397
398 /**
399 * @since 3.04.02
400 */
401 public static function remove_upsells() {
402 remove_action( 'frm_before_settings', 'FrmSettingsController::license_box' );
403 remove_action( 'frm_after_settings', 'FrmSettingsController::settings_cta' );
404 remove_action( 'frm_add_form_style_tab_options', 'FrmFormsController::add_form_style_tab_options' );
405 remove_action( 'frm_after_field_options', 'FrmFormsController::logic_tip' );
406 }
407
408 /**
409 * If there are CURL problems on this server, wp_remote_post won't work for installing
410 * Use a javascript fallback instead.
411 *
412 * @since 2.0.3
413 */
414 public static function install_js_fallback() {
415 FrmAppHelper::load_admin_wide_js();
416 ?>
417 <div id="frm_install_message"></div>
418 <script>jQuery(document).ready( frm_install_now );</script>
419 <?php
420 }
421
422 /**
423 * Check if the database is outdated
424 *
425 * @since 2.0.1
426 * @return boolean
427 */
428 public static function needs_update() {
429 $needs_upgrade = self::compare_for_update(
430 array(
431 'option' => 'frm_db_version',
432 'new_db_version' => FrmAppHelper::$db_version,
433 'new_plugin_version' => FrmAppHelper::plugin_version(),
434 )
435 );
436
437 if ( ! $needs_upgrade ) {
438 $needs_upgrade = apply_filters( 'frm_db_needs_upgrade', $needs_upgrade );
439 }
440
441 return $needs_upgrade;
442 }
443
444 /**
445 * Check both version number and DB number for changes
446 *
447 * @since 3.0.04
448 */
449 public static function compare_for_update( $atts ) {
450 $db_version = get_option( $atts['option'] );
451
452 if ( strpos( $db_version, '-' ) === false ) {
453 $needs_upgrade = true;
454 } else {
455 $last_upgrade = explode( '-', $db_version );
456 $needs_db_upgrade = (int) $last_upgrade[1] < (int) $atts['new_db_version'];
457 $new_version = version_compare( $last_upgrade[0], $atts['new_plugin_version'], '<' );
458 $needs_upgrade = $needs_db_upgrade || $new_version;
459 }
460
461 return $needs_upgrade;
462 }
463
464 /**
465 * Check for database update and trigger js loading
466 *
467 * @since 2.0.1
468 */
469 public static function admin_init() {
470 if ( FrmAppHelper::is_admin_page( 'formidable' ) && 'duplicate' === FrmAppHelper::get_param( 'frm_action' ) ) {
471 FrmFormsController::duplicate();
472 }
473
474 if ( FrmAppHelper::is_style_editor_page() && 'save' === FrmAppHelper::get_param( 'frm_action' ) ) {
475 // Hook in earlier than FrmStylesController::route so we can redirect before the headers have been sent.
476 FrmStylesController::save_style();
477 }
478
479 new FrmPersonalData(); // register personal data hooks
480
481 if ( ! FrmAppHelper::doing_ajax() && self::needs_update() ) {
482 self::network_upgrade_site();
483 }
484
485 if ( ! FrmAppHelper::doing_ajax() ) {
486 // don't continue during ajax calls
487 self::admin_js();
488 }
489
490 if ( FrmAppHelper::is_admin_page( 'formidable' ) ) {
491 $action = FrmAppHelper::get_param( 'frm_action' );
492
493 if ( in_array( $action, array( 'add_new', 'list_templates' ), true ) ) {
494 wp_safe_redirect( admin_url( 'admin.php?page=formidable&triggerNewFormModal=1' ) );
495 exit;
496 }
497
498 FrmInbox::maybe_disable_screen_options();
499 }
500
501 self::maybe_add_ip_warning();
502 }
503
504 /**
505 * Show a warning for the IP address setting if it hasn't been set.
506 *
507 * @since 6.1
508 *
509 * @return void
510 */
511 private static function maybe_add_ip_warning() {
512 $settings = FrmAppHelper::get_settings();
513 if ( false !== $settings->custom_header_ip ) {
514 // The setting has been changed from the false default (to either 1 or 0), so stop showing the message.
515 return;
516 }
517
518 if ( ! self::is_behind_proxy() ) {
519 // This message is only applicable when where is a reverse proxy.
520 return;
521 }
522
523 if ( FrmAppHelper::get_post_param( 'frm_action', '', 'sanitize_text_field' ) ) {
524 // Avoid the message on a POST action. We don't want to show the message if we're saving global settings.
525 return;
526 }
527
528 add_filter(
529 'frm_message_list',
530 function( $show_messages ) {
531 $global_settings_link = admin_url( 'admin.php?page=formidable-settings' ) . '#frm_custom_header_ip';
532 $show_messages['ip_msg'] = 'IP addresses in form submissions may no longer be accurate! If you are experiencing issues, we recommend going to <a href="' . esc_url( $global_settings_link ) . '">Global Settings</a> and enabling the "Use custom headers when retrieving IPs with form submissions." setting.';
533 return $show_messages;
534 }
535 );
536 }
537
538 /**
539 * Check if any reverse proxy headers are set.
540 *
541 * @since 6.1
542 *
543 * @return bool
544 */
545 private static function is_behind_proxy() {
546 $custom_headers = FrmAppHelper::get_custom_header_keys_for_ip();
547 foreach ( $custom_headers as $header ) {
548 if ( 'REMOTE_ADDR' === $header ) {
549 // We want to check every key but REMOTE_ADDR. REMOTE_ATTR is not unique to reverse proxy servers.
550 continue;
551 }
552
553 $ip = trim( FrmAppHelper::get_server_value( $header ) );
554 // Return true for anything that isn't empty but ignoring values like ::1.
555 if ( $ip && 0 !== strpos( $ip, '::' ) ) {
556 return true;
557 }
558 }
559
560 return false;
561 }
562
563 /**
564 * @return void
565 */
566 public static function admin_js() {
567 $plugin_url = FrmAppHelper::plugin_url();
568 $version = FrmAppHelper::plugin_version();
569
570 FrmAppHelper::load_admin_wide_js();
571
572 wp_register_style( 'formidable_admin_global', $plugin_url . '/css/admin/frm_admin_global.css', array(), $version );
573 wp_enqueue_style( 'formidable_admin_global' );
574
575 wp_register_style( 'formidable-admin', $plugin_url . '/css/frm_admin.css', array(), $version );
576 wp_register_style( 'formidable-grids', $plugin_url . '/css/frm_grids.css', array(), $version );
577
578 wp_register_script( 'formidable_dom', $plugin_url . '/js/admin/dom.js', array( 'jquery', 'jquery-ui-dialog', 'wp-i18n' ), $version, true );
579 wp_register_script( 'formidable_embed', $plugin_url . '/js/admin/embed.js', array( 'formidable_dom', 'jquery-ui-autocomplete' ), $version, true );
580 self::register_popper1();
581 wp_register_script( 'bootstrap_tooltip', $plugin_url . '/js/bootstrap.min.js', array( 'jquery', 'popper' ), '4.6.1', true );
582 wp_register_script( 'formidable_settings', $plugin_url . '/js/admin/settings.js', array(), $version, true );
583
584 $page = FrmAppHelper::simple_get( 'page', 'sanitize_title' );
585
586 if ( 'formidable-applications' === $page ) {
587 FrmApplicationsController::load_assets();
588 return;
589 }
590
591 $dependencies = array(
592 'formidable_admin_global',
593 'jquery',
594 'jquery-ui-core',
595 'jquery-ui-draggable',
596 'jquery-ui-sortable',
597 'bootstrap_tooltip',
598 'bootstrap-multiselect',
599 'wp-i18n',
600 'wp-hooks', // Required in WP versions older than 5.7
601 'formidable_dom',
602 'formidable_embed',
603 );
604
605 if ( FrmAppHelper::is_style_editor_page( 'edit' ) ) {
606 // We only need to load the color picker when editing styles.
607 $dependencies[] = 'wp-color-picker';
608 }
609
610 wp_register_script( 'formidable_admin', $plugin_url . '/js/formidable_admin.js', $dependencies, $version, true );
611
612 if ( FrmAppHelper::on_form_listing_page() ) {
613 // For the existing page dropdown in the Form embed modal.
614 wp_enqueue_script( 'jquery-ui-autocomplete' );
615 }
616
617 wp_register_script( 'bootstrap-multiselect', $plugin_url . '/js/bootstrap-multiselect.js', array( 'jquery', 'bootstrap_tooltip', 'popper' ), '1.1.1', true );
618
619 $post_type = FrmAppHelper::simple_get( 'post_type', 'sanitize_title' );
620
621 global $pagenow;
622 if ( strpos( $page, 'formidable' ) === 0 || ( $pagenow === 'edit.php' && $post_type === 'frm_display' ) ) {
623
624 wp_enqueue_script( 'admin-widgets' );
625 wp_enqueue_style( 'widgets' );
626 self::maybe_deregister_popper2();
627 wp_enqueue_script( 'formidable_admin' );
628 wp_enqueue_script( 'formidable_embed' );
629 FrmAppHelper::localize_script( 'admin' );
630
631 wp_enqueue_style( 'formidable-admin' );
632 if ( 'formidable-styles' !== $page && 'formidable-styles2' !== $page ) {
633 wp_enqueue_style( 'formidable-grids' );
634 wp_enqueue_style( 'formidable-dropzone' );
635 } else {
636 wp_enqueue_style( 'formidable-grids' );
637 }
638
639 if ( 'formidable-entries' === $page ) {
640 // Load front end js for entries.
641 wp_enqueue_script( 'formidable' );
642 }
643
644 do_action( 'frm_enqueue_builder_scripts' );
645 self::include_upgrade_overlay();
646 self::include_info_overlay();
647 } elseif ( FrmAppHelper::is_view_builder_page() ) {
648 if ( isset( $_REQUEST['post_type'] ) ) {
649 $post_type = sanitize_title( wp_unslash( $_REQUEST['post_type'] ) );
650 } elseif ( isset( $_REQUEST['post'] ) && absint( $_REQUEST['post'] ) ) {
651 $post = get_post( absint( wp_unslash( $_REQUEST['post'] ) ) );
652 if ( ! $post ) {
653 return;
654 }
655 $post_type = $post->post_type;
656 } else {
657 return;
658 }
659
660 if ( $post_type === 'frm_display' ) {
661 wp_enqueue_style( 'formidable-grids' );
662 wp_enqueue_script( 'jquery-ui-draggable' );
663 self::maybe_deregister_popper2();
664 wp_enqueue_script( 'formidable_admin' );
665 wp_enqueue_style( 'formidable-admin' );
666 FrmAppHelper::localize_script( 'admin' );
667 self::include_info_overlay();
668 }
669 }
670
671 self::maybe_force_formidable_block_on_gutenberg_page();
672 }
673
674 /**
675 * Fix a Duplicator Pro conflict because it uses Popper 2. See issue #3459.
676 *
677 * @since 5.2.02.01
678 *
679 * @return void
680 */
681 private static function maybe_deregister_popper2() {
682 global $wp_scripts;
683
684 if ( ! array_key_exists( 'popper', $wp_scripts->registered ) ) {
685 return;
686 }
687
688 $popper = $wp_scripts->registered['popper'];
689 if ( version_compare( $popper->ver, '2.0', '>=' ) ) {
690 wp_deregister_script( 'popper' );
691 self::register_popper1();
692 }
693 }
694
695 /**
696 * Register Popper required for Bootstrap 4.
697 *
698 * @since 5.2.02.01
699 *
700 * @return void
701 */
702 private static function register_popper1() {
703 wp_register_script( 'popper', FrmAppHelper::plugin_url() . '/js/popper.min.js', array( 'jquery' ), '1.16.0', true );
704 }
705
706 /**
707 * Automatically insert a Formidable block when loading Gutenberg when $_GET['frmForm' is set.
708 *
709 * @since 5.2
710 *
711 * @return void
712 */
713 private static function maybe_force_formidable_block_on_gutenberg_page() {
714 global $pagenow;
715 if ( 'post.php' !== $pagenow ) {
716 return;
717 }
718
719 $form_id = FrmAppHelper::simple_get( 'frmForm', 'absint' );
720 if ( ! $form_id ) {
721 return;
722 }
723
724 self::add_js_to_inject_gutenberg_block( 'formidable/simple-form', 'formId', $form_id );
725 }
726
727 /**
728 * @since 5.3
729 *
730 * @return void
731 */
732 public static function add_js_to_inject_gutenberg_block( $block_name, $object_key, $object_id ) {
733 require FrmAppHelper::plugin_path() . '/classes/views/shared/edit-page-js.php';
734 }
735
736 public static function load_lang() {
737 load_plugin_textdomain( 'formidable', false, FrmAppHelper::plugin_folder() . '/languages/' );
738 }
739
740 /**
741 * Check if the styles are updated when a form is loaded on the front-end
742 *
743 * @since 3.0.1
744 */
745 public static function maybe_update_styles() {
746 if ( self::needs_update() ) {
747 self::network_upgrade_site();
748 }
749 }
750
751 /**
752 * @since 3.0
753 */
754 public static function create_rest_routes() {
755 $args = array(
756 'methods' => 'GET',
757 'callback' => 'FrmAppController::api_install',
758 'permission_callback' => __CLASS__ . '::can_update_db',
759 );
760
761 register_rest_route( 'frm-admin/v1', '/install', $args );
762
763 $args = array(
764 'methods' => 'GET',
765 'callback' => 'FrmAddonsController::install_addon_api',
766 'permission_callback' => 'FrmAddonsController::can_install_addon_api',
767 );
768
769 register_rest_route( 'frm-admin/v1', '/install-addon', $args );
770 }
771
772 /**
773 * Make sure the install is only being run when we tell it to.
774 * We don't want to run manually by people calling the API.
775 *
776 * @since 4.06.02
777 */
778 public static function can_update_db() {
779 return get_transient( 'frm_updating_api' );
780 }
781
782 /**
783 * Run silent upgrade on each site in the network during a network upgrade.
784 * Update database settings for all sites in a network during network upgrade process.
785 *
786 * @since 2.0.1
787 *
788 * @param int $blog_id Blog ID.
789 */
790 public static function network_upgrade_site( $blog_id = 0 ) {
791 // Flag to check if install is happening as intended.
792 set_transient( 'frm_updating_api', true, MINUTE_IN_SECONDS );
793 $request = new WP_REST_Request( 'GET', '/frm-admin/v1/install' );
794
795 self::maybe_add_wp_site_health();
796
797 if ( $blog_id ) {
798 switch_to_blog( $blog_id );
799 $response = rest_do_request( $request );
800 restore_current_blog();
801 } else {
802 $response = rest_do_request( $request );
803 }
804
805 if ( $response->is_error() ) {
806 // if the remove post fails, use javascript instead
807 add_action( 'admin_notices', 'FrmAppController::install_js_fallback' );
808 }
809 }
810
811 /**
812 * Make sure WP_Site_Health has been included because it is required when calling rest_do_request.
813 * Check first that the file exists because WP_Site_Health was only introduced in WordPress 5.2.
814 */
815 private static function maybe_add_wp_site_health() {
816 if ( ! class_exists( 'WP_Site_Health' ) ) {
817 $wp_site_health_path = ABSPATH . 'wp-admin/includes/class-wp-site-health.php';
818 if ( file_exists( $wp_site_health_path ) ) {
819 require_once $wp_site_health_path;
820 }
821 }
822 }
823
824 /**
825 * @since 3.0
826 */
827 public static function api_install() {
828 delete_transient( 'frm_updating_api' );
829 if ( self::needs_update() ) {
830 $running = get_option( 'frm_install_running' );
831 if ( false === $running || $running < strtotime( '-5 minutes' ) ) {
832 update_option( 'frm_install_running', time(), 'no' );
833 self::install();
834 delete_option( 'frm_install_running' );
835 }
836 }
837
838 return true;
839 }
840
841 /**
842 * Silent database upgrade (no redirect).
843 * Called via ajax request during network upgrade process.
844 *
845 * @since 2.0.1
846 */
847 public static function ajax_install() {
848 FrmAppHelper::permission_check( 'frm_change_settings' );
849 check_ajax_referer( 'frm_ajax', 'nonce' );
850 self::api_install();
851 wp_die();
852 }
853
854 public static function install() {
855 $frmdb = new FrmMigrate();
856 $frmdb->upgrade();
857 }
858
859 public static function uninstall() {
860 FrmAppHelper::permission_check( 'administrator' );
861 check_ajax_referer( 'frm_ajax', 'nonce' );
862
863 $frmdb = new FrmMigrate();
864 $frmdb->uninstall();
865
866 //disable the plugin and redirect after uninstall so the tables don't get added right back
867 $plugins = array( FrmAppHelper::plugin_folder() . '/formidable.php', 'formidable-pro/formidable-pro.php' );
868 deactivate_plugins( $plugins, false, false );
869 echo esc_url_raw( admin_url( 'plugins.php?deactivate=true' ) );
870
871 wp_die();
872 }
873
874 public static function drop_tables( $tables ) {
875 global $wpdb;
876 $tables[] = $wpdb->prefix . 'frm_fields';
877 $tables[] = $wpdb->prefix . 'frm_forms';
878 $tables[] = $wpdb->prefix . 'frm_items';
879 $tables[] = $wpdb->prefix . 'frm_item_metas';
880
881 return $tables;
882 }
883
884 public static function deauthorize() {
885 FrmAppHelper::permission_check( 'frm_change_settings' );
886 check_ajax_referer( 'frm_ajax', 'nonce' );
887
888 delete_option( 'frmpro-credentials' );
889 delete_option( 'frmpro-authorized' );
890 delete_site_option( 'frmpro-credentials' );
891 delete_site_option( 'frmpro-authorized' );
892 wp_die();
893 }
894
895 public static function set_footer_text( $text ) {
896 if ( FrmAppHelper::is_formidable_admin() ) {
897 $text = '';
898 }
899
900 return $text;
901 }
902
903 /**
904 * @deprecated 1.07.05
905 * @codeCoverageIgnore
906 */
907 public static function get_form_shortcode( $atts ) {
908 return FrmDeprecated::get_form_shortcode( $atts );
909 }
910
911 /**
912 * @deprecated 2.5.4
913 * @codeCoverageIgnore
914 */
915 public static function widget_text_filter( $content ) {
916 return FrmDeprecated::widget_text_filter( $content );
917 }
918
919 /**
920 * Deprecated in favor of wpmu_upgrade_site
921 *
922 * @deprecated 2.3
923 * @codeCoverageIgnore
924 */
925 public static function front_head() {
926 FrmDeprecated::front_head();
927 }
928
929 /**
930 * @deprecated 3.0.04
931 * @codeCoverageIgnore
932 */
933 public static function activation_install() {
934 FrmDeprecated::activation_install();
935 }
936
937 /**
938 * @deprecated 3.0
939 * @codeCoverageIgnore
940 */
941 public static function page_route( $content ) {
942 return FrmDeprecated::page_route( $content );
943 }
944
945 /**
946 * Include icons on page for Embed Form modal.
947 *
948 * @since 5.2
949 *
950 * @return void
951 */
952 public static function include_embed_form_icons() {
953 _deprecated_function( __METHOD__, '5.3' );
954 }
955 }
956