/var/www/html_uk/wp-content/plugins/wp-smtp/src/Mail/Migration/MigrationVer221.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php

namespace SolidWP\Mail\Migration;

use 
SolidWP\Mail\Connectors\ConnectorSMTP;
use 
SolidWP\Mail\Repository\ProvidersRepository;
use 
WPSMTP\Logger\Table;

/**
 * Class MigrationVer221
 *
 * Migration for version 2.2.1 to add the 'is_default' property to all providers.
 * The currently active provider will be set as default.
 *
 * @package SolidWP\Mail\Migration
 */
class MigrationVer221 {
    
/**
     * The repository for managing SMTP mailers.
     *
     * @var ProvidersRepository
     */
    
protected ProvidersRepository $providers_repository;

    
/**
     * Constructor for MigrationVer220.
     *
     * Initializes the migration class and sets up dependencies.
     *
     * @param ProvidersRepository $providers_repository The repository instance for managing providers.
     */
    
public function __constructProvidersRepository $providers_repository ) {
        
$this->providers_repository $providers_repository;
    }

    
/**
     * Migration logic for version 2.2.0.
     *
     * Adds 'is_default' property to all existing providers.
     * Sets is_default to true for the currently active provider, false for all others.
     *
     * @param string $version
     *
     * @return void
     */
    
public function migrationstring $version ): void {
        global 
$wpdb;

        if ( 
version_compare$version'2.2.1''>=' ) ) {
            return;
        }

        
// Upgrade logs table
        
Table::install();

        
$logsTableName Table::$name;

        
// phpcs:ignore WordPress.DB
        
$wpdb->query"UPDATE $logsTableName SET content_type = 'text/html' WHERE headers LIKE '%html%'" );

        
$default_provider $this->providers_repository->get_default_provider();
        if ( 
$default_provider instanceof ConnectorSMTP ) {
            
// Default provider has been set up by the user, no needs for programmatic changes
            
return;
        }

        
$active_provider    current$this->providers_repository->get_active_providers() );
        
$active_provider_id $active_provider $active_provider->get_id() : null;

        if ( ! 
$active_provider_id ) {
            
// No active providers, plugin is unused, or it's the first installation
            
return;
        }

        
$providers_data get_optionProvidersRepository::OPTION_NAME, [] );

        foreach ( 
$providers_data as $provider_id => &$provider_data ) {
            
// Set is_default to true only for the currently active provider
            
$provider_data['is_default'] = ( $provider_id === $active_provider_id );
        }
        unset( 
$provider_data );

        
update_optionProvidersRepository::OPTION_NAME$providers_data );
    }
}