PluginProbe ʕ •ᴥ•ʔ
WP Mail SMTP by WPForms – The Most Popular SMTP and Email Log Plugin / 4.9.0
WP Mail SMTP by WPForms – The Most Popular SMTP and Email Log Plugin v4.9.0
4.9.0 0.9.6 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.6.0 1.6.2 1.7.0 1.7.1 1.8.0 1.8.1 1.9.0 2.0.0 2.0.1 2.1.1 2.2.1 2.3.1 2.4.0 2.5.0 2.5.1 2.6.0 2.7.0 2.8.0 2.9.0 3.0.1 3.0.2 3.0.3 3.1.0 3.10.0 3.11.0 3.11.1 3.2.0 3.2.1 3.3.0 3.4.0 3.5.0 3.5.1 3.5.2 3.6.1 3.7.0 3.8.0 3.8.2 3.9.0 4.0.1 4.1.0 4.1.1 4.2.0 4.3.0 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.8.0 trunk 0.10.0 0.10.1 0.11.1 0.11.2 0.3.1 0.3.2 0.4 0.4.1 0.4.2 0.5.0 0.5.1 0.5.2 0.6 0.7 0.8 0.8.2 0.8.3 0.8.4 0.8.5 0.8.6 0.8.7 0.9.0 0.9.1 0.9.2 0.9.3 0.9.4 0.9.5
wp-mail-smtp / src / DBRepair.php
wp-mail-smtp / src Last commit date
Abilities 5 days ago Admin 5 days ago Compatibility 5 days ago Helpers 5 days ago Integrations 5 days ago Providers 5 days ago Queue 5 days ago Reports 5 days ago Tasks 5 days ago TestEmail 5 days ago UsageTracking 5 days ago WPCLI 5 days ago AbstractConnection.php 5 days ago Conflicts.php 5 days ago Connect.php 5 days ago Connection.php 5 days ago ConnectionInterface.php 5 days ago ConnectionsManager.php 5 days ago Core.php 5 days ago DBRepair.php 5 days ago Debug.php 5 days ago EmailSendingDebug.php 5 days ago Geo.php 5 days ago MailCatcher.php 5 days ago MailCatcherInterface.php 5 days ago MailCatcherTrait.php 5 days ago MailCatcherV6.php 5 days ago Migration.php 5 days ago MigrationAbstract.php 5 days ago Migrations.php 5 days ago OptimizedEmailSending.php 5 days ago Options.php 5 days ago Processor.php 5 days ago SiteHealth.php 5 days ago Upgrade.php 5 days ago Uploads.php 5 days ago WP.php 5 days ago WPMailArgs.php 5 days ago WPMailInitiator.php 5 days ago
DBRepair.php
241 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 'nonce' => wp_create_nonce( 'wp-mail-smtp-check-db-tables' ),
58 ];
59
60 if ( ! empty( $redirect_tab ) ) {
61 $query_args['tab'] = $redirect_tab;
62 }
63
64 $redirect_url = add_query_arg(
65 $query_args,
66 wp_mail_smtp()->get_admin()->get_admin_page_url( $redirect_page )
67 );
68
69 wp_safe_redirect( $redirect_url );
70 exit;
71 }
72 }
73 }
74
75 /**
76 * Update the Migration option to fix the missing table.
77 *
78 * @since 3.6.0
79 *
80 * @param string $missing_table The name of the table.
81 */
82 protected function fix_missing_db_table( $missing_table ) {
83
84 if ( $missing_table === DebugEvents::get_table_name() ) {
85 update_option( DebugMigration::OPTION_NAME, 0 );
86 } elseif ( $missing_table === Meta::get_table_name() ) {
87 update_option( Migration::OPTION_NAME, 1 );
88 } elseif ( $missing_table === Queue::get_table_name() ) {
89 update_option( QueueMigration::OPTION_NAME, 0 );
90 }
91 }
92
93 /**
94 * Default Unknown error message - If the table is not created.
95 *
96 * @since 3.6.0
97 *
98 * @return string
99 */
100 protected function get_missing_table_default_error_message() {
101
102 $unknown_reason_msg = esc_html__( 'Unknown.', 'wp-mail-smtp' );
103
104 /**
105 * Filter the default error message for unknown reason.
106 *
107 * @since 3.6.0
108 *
109 * @param string $unknown_reason_msg The default unknown reason message.
110 */
111 return apply_filters( 'wp_mail_smtp_db_repair_get_missing_table_default_error_message', $unknown_reason_msg );
112 }
113
114 /**
115 * Get the error message (Reason) if the table is missing.
116 *
117 * @since 3.6.0
118 *
119 * @param string $missing_table The table name that we are checking.
120 * @param array $reasons The array that holds all the error messages or reason.
121 */
122 protected function get_error_message_for_missing_table( $missing_table, &$reasons ) {
123
124 $reason = '';
125
126 if ( $missing_table === DebugEvents::get_table_name() ) {
127 $reason .= $this->get_reason_output_message(
128 $missing_table,
129 get_option( DebugMigration::ERROR_OPTION_NAME, $this->get_missing_table_default_error_message() )
130 );
131 } elseif ( $missing_table === Meta::get_table_name() ) {
132 $reason .= $this->get_reason_output_message(
133 $missing_table,
134 get_option( Migration::ERROR_OPTION_NAME, $this->get_missing_table_default_error_message() )
135 );
136 } elseif ( $missing_table === Queue::get_table_name() ) {
137 $reason .= $this->get_reason_output_message(
138 $missing_table,
139 get_option( QueueMigration::ERROR_OPTION_NAME, $this->get_missing_table_default_error_message() )
140 );
141 }
142
143 $reasons[] = $reason;
144 }
145
146 /**
147 * Get the reason output message, why the DB table creation failed.
148 *
149 * @since 3.6.0
150 *
151 * @param string $table The DB table name.
152 * @param string $error_message The error message.
153 *
154 * @return string
155 */
156 protected function get_reason_output_message( $table, $error_message ) {
157
158 return sprintf(
159 wp_kses( /* translators: %1$s - missing table name; %2$s - error message. */
160 __( '<strong>Table:</strong> %1$s. <strong>Reason:</strong> %2$s', 'wp-mail-smtp' ),
161 [
162 'strong' => [],
163 ]
164 ),
165 esc_html( $table ),
166 esc_html( $error_message )
167 );
168 }
169
170 /**
171 * Verify the tables.
172 * If there is any missing table then display the Admin Notice of error type.
173 * Else display the success message (Success Admin Notice).
174 *
175 * @since 3.6.0
176 */
177 public function verify_db_tables_after_fixing() {
178
179 // Display success or error message based on if there is any missing table available or not.
180 if (
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 check_admin_referer( 'wp-mail-smtp-check-db-tables', 'nonce' );
186
187 $missing_tables = $this->get_missing_tables();
188
189 if ( empty( $missing_tables ) ) {
190 WP::add_admin_notice(
191 esc_html__( 'Missing DB tables were created successfully.', 'wp-mail-smtp' ),
192 WP::ADMIN_NOTICE_SUCCESS
193 );
194
195 return;
196 }
197
198 $reasons = [];
199
200 foreach ( $missing_tables as $missing_table ) {
201 $this->get_error_message_for_missing_table( $missing_table, $reasons );
202 }
203
204 $reasons = array_filter( $reasons ); // Filtering out the empty values.
205
206 if ( ! empty( $reasons ) ) {
207 $msg = sprintf(
208 wp_kses(
209 _n( 'The following DB table is still missing.', 'The following DB tables are still missing.', count( $missing_tables ), 'wp-mail-smtp' ) . '<br />%s',
210 [
211 'br' => [],
212 ]
213 ),
214 implode( '<br/>', $reasons )
215 );
216 } else {
217 $msg = esc_html__( 'Some DB Tables are still missing.', 'wp-mail-smtp' );
218 }
219
220 WP::add_admin_notice(
221 $msg,
222 WP::ADMIN_NOTICE_ERROR
223 );
224 }
225 }
226
227 /**
228 * Get the missing tables.
229 *
230 * @since 3.6.0
231 *
232 * @return array The array of the missing tables.
233 */
234 protected function get_missing_tables() {
235
236 $site_health = new SiteHealth();
237
238 return $site_health->get_missing_db_tables();
239 }
240 }
241