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

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