Admin
6 months ago
Compatibility
6 months ago
Helpers
6 months ago
Providers
6 months ago
Queue
6 months ago
Reports
6 months ago
Tasks
6 months ago
UsageTracking
6 months ago
AbstractConnection.php
6 months ago
Conflicts.php
6 months ago
Connect.php
6 months ago
Connection.php
6 months ago
ConnectionInterface.php
6 months ago
ConnectionsManager.php
6 months ago
Core.php
6 months ago
DBRepair.php
6 months ago
Debug.php
6 months ago
Geo.php
6 months ago
MailCatcher.php
6 months ago
MailCatcherInterface.php
6 months ago
MailCatcherTrait.php
6 months ago
MailCatcherV6.php
6 months ago
Migration.php
6 months ago
MigrationAbstract.php
6 months ago
Migrations.php
6 months ago
OptimizedEmailSending.php
6 months ago
Options.php
6 months ago
Processor.php
6 months ago
SiteHealth.php
6 months ago
Upgrade.php
6 months ago
Uploads.php
6 months ago
WP.php
6 months ago
WPMailArgs.php
6 months ago
WPMailInitiator.php
6 months ago
DBRepair.php
239 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPMailSMTP; |
| 4 | |
| 5 | use WPMailSMTP\Admin\Area; |
| 6 | use WPMailSMTP\Admin\DebugEvents\DebugEvents; |
| 7 | use WPMailSMTP\Admin\DebugEvents\Migration as DebugMigration; |
| 8 | use WPMailSMTP\Queue\Migration as QueueMigration; |
| 9 | use WPMailSMTP\Queue\Queue; |
| 10 | use WPMailSMTP\Tasks\Meta; |
| 11 | |
| 12 | /** |
| 13 | * Class DBRepair to fix the DB related issues. |
| 14 | * |
| 15 | * @since 3.6.0 |
| 16 | */ |
| 17 | class DBRepair { |
| 18 | |
| 19 | /** |
| 20 | * Hook all the functionality. |
| 21 | * |
| 22 | * @since 3.6.0 |
| 23 | */ |
| 24 | public function hooks() { |
| 25 | |
| 26 | add_action( 'admin_init', [ $this, 'fix_missing_db_tables' ] ); |
| 27 | add_action( 'admin_init', [ $this, 'verify_db_tables_after_fixing' ] ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Fixed the missing tables. |
| 32 | * |
| 33 | * @since 3.6.0 |
| 34 | */ |
| 35 | public function fix_missing_db_tables() { // phpcs:ignore Generic.Metrics.NestingLevel.MaxExceeded |
| 36 | |
| 37 | // Check if this is the request to create missing tables. |
| 38 | if ( |
| 39 | isset( $_GET['create-missing-db-tables'] ) && |
| 40 | $_GET['create-missing-db-tables'] === '1' && |
| 41 | wp_mail_smtp()->get_admin()->is_admin_page() && |
| 42 | current_user_can( wp_mail_smtp()->get_capability_manage_options() ) |
| 43 | ) { |
| 44 | check_admin_referer( Area::SLUG . '-create-missing-db-tables' ); |
| 45 | |
| 46 | $missing_tables = $this->get_missing_tables(); |
| 47 | |
| 48 | if ( ! empty( $missing_tables ) ) { |
| 49 | foreach ( $missing_tables as $missing_table ) { |
| 50 | $this->fix_missing_db_table( $missing_table ); |
| 51 | } |
| 52 | |
| 53 | $redirect_page = isset( $_GET['page'] ) ? sanitize_key( $_GET['page'] ) : Area::SLUG; |
| 54 | $redirect_tab = isset( $_GET['tab'] ) ? sanitize_key( $_GET['tab'] ) : ''; |
| 55 | $query_args = [ |
| 56 | 'check-db-tables' => 1, |
| 57 | ]; |
| 58 | |
| 59 | if ( ! empty( $redirect_tab ) ) { |
| 60 | $query_args['tab'] = $redirect_tab; |
| 61 | } |
| 62 | |
| 63 | $redirect_url = add_query_arg( |
| 64 | $query_args, |
| 65 | wp_mail_smtp()->get_admin()->get_admin_page_url( $redirect_page ) |
| 66 | ); |
| 67 | |
| 68 | wp_safe_redirect( $redirect_url ); |
| 69 | exit; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Update the Migration option to fix the missing table. |
| 76 | * |
| 77 | * @since 3.6.0 |
| 78 | * |
| 79 | * @param string $missing_table The name of the table. |
| 80 | */ |
| 81 | protected function fix_missing_db_table( $missing_table ) { |
| 82 | |
| 83 | if ( $missing_table === DebugEvents::get_table_name() ) { |
| 84 | update_option( DebugMigration::OPTION_NAME, 0 ); |
| 85 | } elseif ( $missing_table === Meta::get_table_name() ) { |
| 86 | update_option( Migration::OPTION_NAME, 1 ); |
| 87 | } elseif ( $missing_table === Queue::get_table_name() ) { |
| 88 | update_option( QueueMigration::OPTION_NAME, 0 ); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Default Unknown error message - If the table is not created. |
| 94 | * |
| 95 | * @since 3.6.0 |
| 96 | * |
| 97 | * @return string |
| 98 | */ |
| 99 | protected function get_missing_table_default_error_message() { |
| 100 | |
| 101 | $unknown_reason_msg = esc_html__( 'Unknown.', 'wp-mail-smtp' ); |
| 102 | |
| 103 | /** |
| 104 | * Filter the default error message for unknown reason. |
| 105 | * |
| 106 | * @since 3.6.0 |
| 107 | * |
| 108 | * @param string $unknown_reason_msg The default unknown reason message. |
| 109 | */ |
| 110 | return apply_filters( 'wp_mail_smtp_db_repair_get_missing_table_default_error_message', $unknown_reason_msg ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Get the error message (Reason) if the table is missing. |
| 115 | * |
| 116 | * @since 3.6.0 |
| 117 | * |
| 118 | * @param string $missing_table The table name that we are checking. |
| 119 | * @param array $reasons The array that holds all the error messages or reason. |
| 120 | */ |
| 121 | protected function get_error_message_for_missing_table( $missing_table, &$reasons ) { |
| 122 | |
| 123 | $reason = ''; |
| 124 | |
| 125 | if ( $missing_table === DebugEvents::get_table_name() ) { |
| 126 | $reason .= $this->get_reason_output_message( |
| 127 | $missing_table, |
| 128 | get_option( DebugMigration::ERROR_OPTION_NAME, $this->get_missing_table_default_error_message() ) |
| 129 | ); |
| 130 | } elseif ( $missing_table === Meta::get_table_name() ) { |
| 131 | $reason .= $this->get_reason_output_message( |
| 132 | $missing_table, |
| 133 | get_option( Migration::ERROR_OPTION_NAME, $this->get_missing_table_default_error_message() ) |
| 134 | ); |
| 135 | } elseif ( $missing_table === Queue::get_table_name() ) { |
| 136 | $reason .= $this->get_reason_output_message( |
| 137 | $missing_table, |
| 138 | get_option( QueueMigration::ERROR_OPTION_NAME, $this->get_missing_table_default_error_message() ) |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | $reasons[] = $reason; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Get the reason output message, why the DB table creation failed. |
| 147 | * |
| 148 | * @since 3.6.0 |
| 149 | * |
| 150 | * @param string $table The DB table name. |
| 151 | * @param string $error_message The error message. |
| 152 | * |
| 153 | * @return string |
| 154 | */ |
| 155 | protected function get_reason_output_message( $table, $error_message ) { |
| 156 | |
| 157 | return sprintf( |
| 158 | wp_kses( /* translators: %1$s - missing table name; %2$s - error message. */ |
| 159 | __( '<strong>Table:</strong> %1$s. <strong>Reason:</strong> %2$s', 'wp-mail-smtp' ), |
| 160 | [ |
| 161 | 'strong' => [], |
| 162 | ] |
| 163 | ), |
| 164 | esc_html( $table ), |
| 165 | esc_html( $error_message ) |
| 166 | ); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Verify the tables. |
| 171 | * If there is any missing table then display the Admin Notice of error type. |
| 172 | * Else display the success message (Success Admin Notice). |
| 173 | * |
| 174 | * @since 3.6.0 |
| 175 | */ |
| 176 | public function verify_db_tables_after_fixing() { |
| 177 | |
| 178 | // Display success or error message based on if there is any missing table available or not. |
| 179 | if ( |
| 180 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 181 | isset( $_GET['check-db-tables'] ) && $_GET['check-db-tables'] === '1' && |
| 182 | wp_mail_smtp()->get_admin()->is_admin_page() && |
| 183 | current_user_can( wp_mail_smtp()->get_capability_manage_options() ) |
| 184 | ) { |
| 185 | $missing_tables = $this->get_missing_tables(); |
| 186 | |
| 187 | if ( empty( $missing_tables ) ) { |
| 188 | WP::add_admin_notice( |
| 189 | esc_html__( 'Missing DB tables were created successfully.', 'wp-mail-smtp' ), |
| 190 | WP::ADMIN_NOTICE_SUCCESS |
| 191 | ); |
| 192 | |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | $reasons = []; |
| 197 | |
| 198 | foreach ( $missing_tables as $missing_table ) { |
| 199 | $this->get_error_message_for_missing_table( $missing_table, $reasons ); |
| 200 | } |
| 201 | |
| 202 | $reasons = array_filter( $reasons ); // Filtering out the empty values. |
| 203 | |
| 204 | if ( ! empty( $reasons ) ) { |
| 205 | $msg = sprintf( |
| 206 | wp_kses( |
| 207 | _n( 'The following DB table is still missing.', 'The following DB tables are still missing.', count( $missing_tables ), 'wp-mail-smtp' ) . '<br />%s', |
| 208 | [ |
| 209 | 'br' => [], |
| 210 | ] |
| 211 | ), |
| 212 | implode( '<br/>', $reasons ) |
| 213 | ); |
| 214 | } else { |
| 215 | $msg = esc_html__( 'Some DB Tables are still missing.', 'wp-mail-smtp' ); |
| 216 | } |
| 217 | |
| 218 | WP::add_admin_notice( |
| 219 | $msg, |
| 220 | WP::ADMIN_NOTICE_ERROR |
| 221 | ); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Get the missing tables. |
| 227 | * |
| 228 | * @since 3.6.0 |
| 229 | * |
| 230 | * @return array The array of the missing tables. |
| 231 | */ |
| 232 | protected function get_missing_tables() { |
| 233 | |
| 234 | $site_health = new SiteHealth(); |
| 235 | |
| 236 | return $site_health->get_missing_db_tables(); |
| 237 | } |
| 238 | } |
| 239 |