From 009e7783b780185a9f3411f16c36c6cacad36582 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 28 Feb 2025 15:00:56 -0400 Subject: [PATCH 01/10] Add inbox item to try setting up db again on failure --- classes/controllers/FrmAppController.php | 4 +++ classes/controllers/FrmFormsController.php | 29 +++++++++++++++++- classes/controllers/FrmHooksController.php | 2 +- classes/helpers/FrmXMLHelper.php | 2 +- classes/models/FrmInbox.php | 11 +++++++ classes/models/FrmMigrate.php | 34 ++++++++++++++++++++++ 6 files changed, 79 insertions(+), 3 deletions(-) diff --git a/classes/controllers/FrmAppController.php b/classes/controllers/FrmAppController.php index a2c71c5130..dc3f04fdee 100644 --- a/classes/controllers/FrmAppController.php +++ b/classes/controllers/FrmAppController.php @@ -566,6 +566,10 @@ public static function admin_init() { FrmFormsController::duplicate(); } + if ( FrmAppHelper::is_admin_page( 'formidable' ) && FrmAppHelper::simple_get( 'frm_add_tables' ) ) { + FrmFormsController::add_missing_tables(); + } + if ( FrmAppHelper::is_style_editor_page() && 'save' === FrmAppHelper::get_param( 'frm_action' ) ) { // Hook in earlier than FrmStylesController::route so we can redirect before the headers have been sent. FrmStylesController::save_style(); diff --git a/classes/controllers/FrmFormsController.php b/classes/controllers/FrmFormsController.php index c042050aeb..f600e7c5fc 100644 --- a/classes/controllers/FrmFormsController.php +++ b/classes/controllers/FrmFormsController.php @@ -1050,7 +1050,7 @@ public static function get_shortcode_opts() { * @param array $errors * @return void */ - public static function display_forms_list( $params = array(), $message = '', $errors = array() ) { + public static function display_forms_list( $params = array(), $message = '', $errors = array() ) { FrmAppHelper::permission_check( 'frm_view_forms' ); global $wpdb, $frm_vars; @@ -1077,6 +1077,14 @@ public static function display_forms_list( $params = array(), $message = '', $er die(); } + $inbox = new FrmInbox(); + $error = $inbox->check_for_error(); + if ( $error ) { + $show_messages = array( + $error['subject'] . '. ' . $error['message'], + ); + } + require FrmAppHelper::plugin_path() . '/classes/views/frm-forms/list.php'; } @@ -3288,6 +3296,25 @@ function () { ); } + /** + * @since x.x + */ + public static function add_missing_tables() { + FrmAppHelper::permission_check( 'frm_view_forms' ); + + $inbox = new FrmInbox(); + $error = $inbox->check_for_error(); + + if ( ! $error || 'failed-to-create-tables' !== $error['key'] ) { + wp_safe_redirect( admin_url( 'admin.php?page=formidable' ) ); + exit; + } + + delete_option( 'frm_db_version' ); + wp_safe_redirect( admin_url( 'admin.php?page=formidable' ) ); + exit; + } + /** * @deprecated 4.0 */ diff --git a/classes/controllers/FrmHooksController.php b/classes/controllers/FrmHooksController.php index ec99052bff..0724650c16 100644 --- a/classes/controllers/FrmHooksController.php +++ b/classes/controllers/FrmHooksController.php @@ -58,7 +58,7 @@ public static function trigger_load_form_hooks() { /** * @return void */ - public static function load_hooks() { + public static function load_hooks() { // Use 0 so this gets triggered before FrmFormActionsController::register_post_types. add_action( 'init', 'FrmAppController::load_lang', 0 ); diff --git a/classes/helpers/FrmXMLHelper.php b/classes/helpers/FrmXMLHelper.php index 88d7835b67..5967f2bfe1 100644 --- a/classes/helpers/FrmXMLHelper.php +++ b/classes/helpers/FrmXMLHelper.php @@ -934,7 +934,7 @@ public static function import_xml_views( $views, $imported ) { $post_id = false; if ( $post['post_type'] === $form_action_type ) { $action_control = FrmFormActionsController::get_form_actions( $post['post_excerpt'] ); - if ( $action_control && is_object( $action_control ) ) { + if ( $action_control && is_object( $action_control ) && isset( $imported['form_status'] ) ) { $post_id = $action_control->maybe_create_action( $post, $imported['form_status'] ); } unset( $action_control ); diff --git a/classes/models/FrmInbox.php b/classes/models/FrmInbox.php index 65ee29eceb..374bec4d03 100644 --- a/classes/models/FrmInbox.php +++ b/classes/models/FrmInbox.php @@ -516,4 +516,15 @@ function ( $total, $key ) use ( $message ) { public static function clear_cache() { delete_option( 'frm_inbox' ); } + + public static function check_for_error() { + $inbox = new self(); + $messages = $inbox->get_messages( 'filter' ); + foreach ( $messages as $message ) { + if ( is_array( $message ) && isset( $message['type'] ) && 'error' === $message['type'] ) { + return $message; + } + } + return false; + } } diff --git a/classes/models/FrmMigrate.php b/classes/models/FrmMigrate.php index 86830563d0..ebd59d893b 100644 --- a/classes/models/FrmMigrate.php +++ b/classes/models/FrmMigrate.php @@ -42,6 +42,7 @@ public function upgrade() { $this->create_tables(); $this->migrate_data( $old_db_version ); + $this->check_that_tables_exist(); // SAVE DB VERSION. update_option( 'frm_db_version', FrmAppHelper::plugin_version() . '-' . FrmAppHelper::$db_version ); @@ -68,6 +69,39 @@ public function upgrade() { } } + /** + * @since x.x + * + * @return void + */ + public function check_that_tables_exist() { + // Check the DB that the table $this->forms exists. + global $wpdb; + $exists = $wpdb->get_results( "SHOW TABLES LIKE '$this->forms'" ); + + if ( $exists ) { + $inbox = new FrmInbox(); + $inbox->dismiss( 'failed-to-create-tables' ); + return; + } + + $message = array( + 'key' => 'failed-to-create-tables', + 'subject' => 'Something went wrong setting up the database', + 'message' => 'For steps to continue, see our documentation. If you need assistance, we recommend that you reach out to your hosting provider. Then click here to try again.', + 'cta' => 'Learn More', + 'type' => 'error', + ); + + $inbox = new FrmInbox(); + $inbox->add_message( $message ); + } + + /** + * @since x.x + * + * @return string + */ public function collation() { global $wpdb; if ( ! $wpdb->has_cap( 'collation' ) ) { From 2d80c1ff8543f00218901616b5c69d779fb010e0 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 28 Feb 2025 15:06:38 -0400 Subject: [PATCH 02/10] Add comments --- classes/controllers/FrmFormsController.php | 13 +++++++++++++ classes/controllers/FrmHooksController.php | 2 +- classes/models/FrmInbox.php | 7 +++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/classes/controllers/FrmFormsController.php b/classes/controllers/FrmFormsController.php index f600e7c5fc..f94372a90f 100644 --- a/classes/controllers/FrmFormsController.php +++ b/classes/controllers/FrmFormsController.php @@ -3297,6 +3297,10 @@ function () { } /** + * In some cases, the DB tables may fail to install. + * This function tries to add them again when the user clicks the link to try again + * from the given inbox notice. + * * @since x.x */ public static function add_missing_tables() { @@ -3306,6 +3310,15 @@ public static function add_missing_tables() { $error = $inbox->check_for_error(); if ( ! $error || 'failed-to-create-tables' !== $error['key'] ) { + // Confirm the inbox item with this CTA exists. + wp_safe_redirect( admin_url( 'admin.php?page=formidable' ) ); + exit; + } + + global $wpdb; + $exists = $wpdb->get_results( "SHOW TABLES LIKE '$wpdb->prefix . 'frm_forms''" ); + if ( $exists ) { + // Exit early if the table already exists. wp_safe_redirect( admin_url( 'admin.php?page=formidable' ) ); exit; } diff --git a/classes/controllers/FrmHooksController.php b/classes/controllers/FrmHooksController.php index 0724650c16..ec99052bff 100644 --- a/classes/controllers/FrmHooksController.php +++ b/classes/controllers/FrmHooksController.php @@ -58,7 +58,7 @@ public static function trigger_load_form_hooks() { /** * @return void */ - public static function load_hooks() { + public static function load_hooks() { // Use 0 so this gets triggered before FrmFormActionsController::register_post_types. add_action( 'init', 'FrmAppController::load_lang', 0 ); diff --git a/classes/models/FrmInbox.php b/classes/models/FrmInbox.php index 374bec4d03..aaee8d88ff 100644 --- a/classes/models/FrmInbox.php +++ b/classes/models/FrmInbox.php @@ -517,6 +517,13 @@ public static function clear_cache() { delete_option( 'frm_inbox' ); } + /** + * Check inbox for an "error" type. This is displayed on the form list page if one exists. + * + * @since x.x + * + * @return array|false + */ public static function check_for_error() { $inbox = new self(); $messages = $inbox->get_messages( 'filter' ); From 7c674a3dc4ba3c48566baa17dc7a2fb1d979391f Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 28 Feb 2025 15:08:19 -0400 Subject: [PATCH 03/10] Clean up a bit --- classes/controllers/FrmFormsController.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/classes/controllers/FrmFormsController.php b/classes/controllers/FrmFormsController.php index f94372a90f..cbe396fea4 100644 --- a/classes/controllers/FrmFormsController.php +++ b/classes/controllers/FrmFormsController.php @@ -1050,7 +1050,7 @@ public static function get_shortcode_opts() { * @param array $errors * @return void */ - public static function display_forms_list( $params = array(), $message = '', $errors = array() ) { + public static function display_forms_list( $params = array(), $message = '', $errors = array() ) { FrmAppHelper::permission_check( 'frm_view_forms' ); global $wpdb, $frm_vars; @@ -1080,9 +1080,7 @@ public static function display_forms_list( $params = array(), $message = '', $er $inbox = new FrmInbox(); $error = $inbox->check_for_error(); if ( $error ) { - $show_messages = array( - $error['subject'] . '. ' . $error['message'], - ); + $show_messages = array( $error['subject'] . '. ' . $error['message'] ); } require FrmAppHelper::plugin_path() . '/classes/views/frm-forms/list.php'; From a8accbb352f6155502bd7e7e41e40e8a7c340e9a Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 28 Feb 2025 15:25:23 -0400 Subject: [PATCH 04/10] Fix phpcs issue --- classes/models/FrmMigrate.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/models/FrmMigrate.php b/classes/models/FrmMigrate.php index ebd59d893b..9c4dc3d65d 100644 --- a/classes/models/FrmMigrate.php +++ b/classes/models/FrmMigrate.php @@ -77,7 +77,7 @@ public function upgrade() { public function check_that_tables_exist() { // Check the DB that the table $this->forms exists. global $wpdb; - $exists = $wpdb->get_results( "SHOW TABLES LIKE '$this->forms'" ); + $exists = $wpdb->get_results( $wpdb->prepare( "SHOW TABLES LIKE %s", $this->forms ) ); if ( $exists ) { $inbox = new FrmInbox(); From 77e9f6cb25ca7102896a357bba9b5530352a0bb7 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 28 Feb 2025 15:28:40 -0400 Subject: [PATCH 05/10] Prepare another query --- classes/controllers/FrmFormsController.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/classes/controllers/FrmFormsController.php b/classes/controllers/FrmFormsController.php index cbe396fea4..0380fbbafd 100644 --- a/classes/controllers/FrmFormsController.php +++ b/classes/controllers/FrmFormsController.php @@ -3314,7 +3314,8 @@ public static function add_missing_tables() { } global $wpdb; - $exists = $wpdb->get_results( "SHOW TABLES LIKE '$wpdb->prefix . 'frm_forms''" ); + $exists = $wpdb->get_results( $wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->prefix . 'frm_forms' ) ); + if ( $exists ) { // Exit early if the table already exists. wp_safe_redirect( admin_url( 'admin.php?page=formidable' ) ); From 6cf1be9eb3f5c9cda6df71d77089ebbd6c3521b7 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 28 Feb 2025 15:30:35 -0400 Subject: [PATCH 06/10] Move function, make it private --- classes/controllers/FrmAppController.php | 35 +++++++++++++++++++++- classes/controllers/FrmFormsController.php | 33 -------------------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/classes/controllers/FrmAppController.php b/classes/controllers/FrmAppController.php index dc3f04fdee..8152dc6216 100644 --- a/classes/controllers/FrmAppController.php +++ b/classes/controllers/FrmAppController.php @@ -567,7 +567,7 @@ public static function admin_init() { } if ( FrmAppHelper::is_admin_page( 'formidable' ) && FrmAppHelper::simple_get( 'frm_add_tables' ) ) { - FrmFormsController::add_missing_tables(); + self::add_missing_tables(); } if ( FrmAppHelper::is_style_editor_page() && 'save' === FrmAppHelper::get_param( 'frm_action' ) ) { @@ -1386,4 +1386,37 @@ private static function is_our_callback_array( $callback ) { ! empty( $callback['function'][0] ) && self::is_our_callback_string( is_object( $callback['function'][0] ) ? get_class( $callback['function'][0] ) : $callback['function'][0] ); } + + /** + * In some cases, the DB tables may fail to install. + * This function tries to add them again when the user clicks the link to try again + * from the given inbox notice. + * + * @since x.x + */ + private static function add_missing_tables() { + FrmAppHelper::permission_check( 'frm_view_forms' ); + + $inbox = new FrmInbox(); + $error = $inbox->check_for_error(); + + if ( ! $error || 'failed-to-create-tables' !== $error['key'] ) { + // Confirm the inbox item with this CTA exists. + wp_safe_redirect( admin_url( 'admin.php?page=formidable' ) ); + exit; + } + + global $wpdb; + $exists = $wpdb->get_results( $wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->prefix . 'frm_forms' ) ); + + if ( $exists ) { + // Exit early if the table already exists. + wp_safe_redirect( admin_url( 'admin.php?page=formidable' ) ); + exit; + } + + delete_option( 'frm_db_version' ); + wp_safe_redirect( admin_url( 'admin.php?page=formidable' ) ); + exit; + } } diff --git a/classes/controllers/FrmFormsController.php b/classes/controllers/FrmFormsController.php index 0380fbbafd..e4bcf89e89 100644 --- a/classes/controllers/FrmFormsController.php +++ b/classes/controllers/FrmFormsController.php @@ -3294,39 +3294,6 @@ function () { ); } - /** - * In some cases, the DB tables may fail to install. - * This function tries to add them again when the user clicks the link to try again - * from the given inbox notice. - * - * @since x.x - */ - public static function add_missing_tables() { - FrmAppHelper::permission_check( 'frm_view_forms' ); - - $inbox = new FrmInbox(); - $error = $inbox->check_for_error(); - - if ( ! $error || 'failed-to-create-tables' !== $error['key'] ) { - // Confirm the inbox item with this CTA exists. - wp_safe_redirect( admin_url( 'admin.php?page=formidable' ) ); - exit; - } - - global $wpdb; - $exists = $wpdb->get_results( $wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->prefix . 'frm_forms' ) ); - - if ( $exists ) { - // Exit early if the table already exists. - wp_safe_redirect( admin_url( 'admin.php?page=formidable' ) ); - exit; - } - - delete_option( 'frm_db_version' ); - wp_safe_redirect( admin_url( 'admin.php?page=formidable' ) ); - exit; - } - /** * @deprecated 4.0 */ From 047620c72e51766efc4c08b32c4cc0b1c1173525 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 28 Feb 2025 15:32:54 -0400 Subject: [PATCH 07/10] Use single quotes (phpcs)g --- classes/controllers/FrmAppController.php | 2 +- classes/models/FrmMigrate.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/classes/controllers/FrmAppController.php b/classes/controllers/FrmAppController.php index 8152dc6216..f466f57ff0 100644 --- a/classes/controllers/FrmAppController.php +++ b/classes/controllers/FrmAppController.php @@ -1407,7 +1407,7 @@ private static function add_missing_tables() { } global $wpdb; - $exists = $wpdb->get_results( $wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->prefix . 'frm_forms' ) ); + $exists = $wpdb->get_results( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'frm_forms' ) ); if ( $exists ) { // Exit early if the table already exists. diff --git a/classes/models/FrmMigrate.php b/classes/models/FrmMigrate.php index 9c4dc3d65d..866a0f87de 100644 --- a/classes/models/FrmMigrate.php +++ b/classes/models/FrmMigrate.php @@ -77,7 +77,7 @@ public function upgrade() { public function check_that_tables_exist() { // Check the DB that the table $this->forms exists. global $wpdb; - $exists = $wpdb->get_results( $wpdb->prepare( "SHOW TABLES LIKE %s", $this->forms ) ); + $exists = $wpdb->get_results( $wpdb->prepare( 'SHOW TABLES LIKE %s', $this->forms ) ); if ( $exists ) { $inbox = new FrmInbox(); From 3ced3f2d0031d597c89ced6cad4a8ff1bfbaf03d Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 28 Feb 2025 15:45:12 -0400 Subject: [PATCH 08/10] Make function private --- classes/models/FrmMigrate.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/models/FrmMigrate.php b/classes/models/FrmMigrate.php index 866a0f87de..ee1e6146ff 100644 --- a/classes/models/FrmMigrate.php +++ b/classes/models/FrmMigrate.php @@ -74,7 +74,7 @@ public function upgrade() { * * @return void */ - public function check_that_tables_exist() { + private function check_that_tables_exist() { // Check the DB that the table $this->forms exists. global $wpdb; $exists = $wpdb->get_results( $wpdb->prepare( 'SHOW TABLES LIKE %s', $this->forms ) ); From ca3a925d5fd6879d9dacb3c5a204b2d645f87202 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 28 Feb 2025 15:50:29 -0400 Subject: [PATCH 09/10] Add a comment --- classes/models/FrmMigrate.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/classes/models/FrmMigrate.php b/classes/models/FrmMigrate.php index ee1e6146ff..689a1842ef 100644 --- a/classes/models/FrmMigrate.php +++ b/classes/models/FrmMigrate.php @@ -70,6 +70,9 @@ public function upgrade() { } /** + * If we fail to create the database tables, add an inbox notice. + * This informs the user that they need to correct the issue and try again. + * * @since x.x * * @return void From 54a08302cc7c6d7ffa25fd2aee7945e171b1c0dc Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 28 Feb 2025 15:51:15 -0400 Subject: [PATCH 10/10] Remove some code that is not correct --- classes/models/FrmMigrate.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/classes/models/FrmMigrate.php b/classes/models/FrmMigrate.php index 689a1842ef..38340235f6 100644 --- a/classes/models/FrmMigrate.php +++ b/classes/models/FrmMigrate.php @@ -101,8 +101,6 @@ private function check_that_tables_exist() { } /** - * @since x.x - * * @return string */ public function collation() {