Admin
1 year ago
Compatibility
1 year ago
Helpers
1 year ago
Providers
1 year ago
Queue
1 year ago
Reports
1 year ago
Tasks
1 year ago
UsageTracking
1 year ago
AbstractConnection.php
1 year ago
Conflicts.php
1 year ago
Connect.php
1 year ago
Connection.php
1 year ago
ConnectionInterface.php
1 year ago
ConnectionsManager.php
1 year ago
Core.php
1 year ago
DBRepair.php
1 year ago
Debug.php
1 year ago
Geo.php
1 year ago
MailCatcher.php
1 year ago
MailCatcherInterface.php
1 year ago
MailCatcherTrait.php
1 year ago
MailCatcherV6.php
1 year ago
Migration.php
1 year ago
MigrationAbstract.php
1 year ago
Migrations.php
1 year ago
OptimizedEmailSending.php
1 year ago
Options.php
1 year ago
Processor.php
1 year ago
SiteHealth.php
1 year ago
Upgrade.php
1 year ago
Uploads.php
1 year ago
WP.php
1 year ago
WPMailArgs.php
1 year ago
WPMailInitiator.php
1 year ago
Migrations.php
171 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPMailSMTP; |
| 4 | |
| 5 | use WP_Upgrader; |
| 6 | use WPMailSMTP\Admin\DebugEvents\Migration as DebugEventsMigration; |
| 7 | use WPMailSMTP\Queue\Migration as QueueMigration; |
| 8 | |
| 9 | /** |
| 10 | * Class Migrations. |
| 11 | * |
| 12 | * @since 4.0.0 |
| 13 | */ |
| 14 | class Migrations { |
| 15 | |
| 16 | /** |
| 17 | * Register hooks. |
| 18 | * |
| 19 | * @since 4.0.0 |
| 20 | */ |
| 21 | public function hooks() { |
| 22 | |
| 23 | // Initialize migrations during request in the admin panel only. |
| 24 | add_action( 'admin_init', [ $this, 'init_migrations_on_request' ] ); |
| 25 | |
| 26 | // Initialize migrations after plugin update. |
| 27 | add_action( 'upgrader_process_complete', [ $this, 'init_migrations_after_upgrade' ], PHP_INT_MAX, 2 ); |
| 28 | add_action( |
| 29 | 'wp_ajax_nopriv_wp_mail_smtp_init_migrations', |
| 30 | [ $this, 'init_migrations_ajax_handler' ] |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Initialize DB migrations during request. |
| 36 | * |
| 37 | * @since 4.0.0 |
| 38 | */ |
| 39 | public function init_migrations_on_request() { |
| 40 | |
| 41 | // Do not initialize migrations during AJAX and cron requests. |
| 42 | if ( WP::is_doing_ajax() || wp_doing_cron() ) { |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | $this->init_migrations(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Initialize DB migrations. |
| 51 | * |
| 52 | * @since 4.0.0 |
| 53 | */ |
| 54 | private function init_migrations() { |
| 55 | |
| 56 | $migrations = $this->get_migrations(); |
| 57 | |
| 58 | foreach ( $migrations as $migration ) { |
| 59 | if ( is_subclass_of( $migration, MigrationAbstract::class ) && $migration::is_enabled() ) { |
| 60 | ( new $migration() )->init(); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Get migrations classes. |
| 67 | * |
| 68 | * @since 4.0.0 |
| 69 | * |
| 70 | * @return array Migrations classes. |
| 71 | */ |
| 72 | private function get_migrations() { |
| 73 | |
| 74 | $migrations = [ |
| 75 | Migration::class, |
| 76 | DebugEventsMigration::class, |
| 77 | QueueMigration::class, |
| 78 | ]; |
| 79 | |
| 80 | /** |
| 81 | * Filters DB migrations classes. |
| 82 | * |
| 83 | * @deprecated 4.0.0 |
| 84 | * |
| 85 | * @since 3.0.0 |
| 86 | * |
| 87 | * @param array $migrations Migrations classes. |
| 88 | */ |
| 89 | $migrations = apply_filters_deprecated( |
| 90 | 'wp_mail_smtp_core_init_migrations', |
| 91 | [ $migrations ], |
| 92 | '3.10.0', |
| 93 | 'wp_mail_smtp_migrations_get_migrations' |
| 94 | ); |
| 95 | |
| 96 | /** |
| 97 | * Filters DB migrations classes. |
| 98 | * |
| 99 | * @since 4.0.0 |
| 100 | * |
| 101 | * @param array $migrations Migrations classes. |
| 102 | */ |
| 103 | return apply_filters( 'wp_mail_smtp_migrations_get_migrations', $migrations ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Initialize DB migrations after plugin update. |
| 108 | * Initiate ajax call to perform the migration with the new plugin version code. |
| 109 | * |
| 110 | * @since 4.0.0 |
| 111 | * |
| 112 | * @param WP_Upgrader $upgrader WP_Upgrader instance. |
| 113 | * @param array $options Array of update data. |
| 114 | */ |
| 115 | public function init_migrations_after_upgrade( $upgrader, $options ) { |
| 116 | |
| 117 | if ( |
| 118 | // Skip if in admin panel. |
| 119 | ( is_admin() && ! wp_doing_ajax() ) || |
| 120 | // Skip if it's update from plugins list page. |
| 121 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 122 | ( wp_doing_ajax() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] === 'update-plugin' ) |
| 123 | ) { |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | $plugins = []; |
| 128 | |
| 129 | if ( isset( $options['plugins'] ) && is_array( $options['plugins'] ) ) { |
| 130 | $plugins = $options['plugins']; |
| 131 | } elseif ( isset( $options['plugin'] ) && is_string( $options['plugin'] ) ) { |
| 132 | $plugins = [ $options['plugin'] ]; |
| 133 | } |
| 134 | |
| 135 | if ( |
| 136 | ! in_array( 'wp-mail-smtp/wp_mail_smtp.php', $plugins, true ) && |
| 137 | ! in_array( 'wp-mail-smtp-pro/wp_mail_smtp.php', $plugins, true ) |
| 138 | ) { |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | $url = add_query_arg( |
| 143 | [ |
| 144 | 'action' => 'wp_mail_smtp_init_migrations', |
| 145 | ], |
| 146 | admin_url( 'admin-ajax.php' ) |
| 147 | ); |
| 148 | |
| 149 | $timeout = (int) ini_get( 'max_execution_time' ); |
| 150 | |
| 151 | $args = [ |
| 152 | 'sslverify' => false, |
| 153 | 'timeout' => $timeout ? $timeout : 30, |
| 154 | ]; |
| 155 | |
| 156 | wp_remote_post( $url, $args ); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Initialize migrations via AJAX request. |
| 161 | * |
| 162 | * @since 4.0.0 |
| 163 | */ |
| 164 | public function init_migrations_ajax_handler() { |
| 165 | |
| 166 | $this->init_migrations(); |
| 167 | |
| 168 | wp_send_json_success(); |
| 169 | } |
| 170 | } |
| 171 |