ActivatorService.php 7.15 KB
Newer Older
Michael Iseard's avatar
Michael Iseard committed
1
2
<?php

3
namespace Kudos\Service;
Michael Iseard's avatar
Michael Iseard committed
4

5
6
7
use Kudos\Entity\DonorEntity;
use Kudos\Entity\SubscriptionEntity;
use Kudos\Entity\TransactionEntity;
Michael Iseard's avatar
Michael Iseard committed
8
use Kudos\Helpers\Settings;
Michael Iseard's avatar
Michael Iseard committed
9

Michael Iseard's avatar
Michael Iseard committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * Fired during plugin activation
 *
 * @link       https://www.linkedin.com/in/michael-iseard/
 * @since      1.0.0
 */

/**
 * Fired during plugin activation.
 *
 * This class defines all code necessary to run during the plugin's activation.
 *
 * @since      1.0.0
 */
24
class ActivatorService {
Michael Iseard's avatar
Michael Iseard committed
25
26

	/**
27
	 * Runs all activation functions.
Michael Iseard's avatar
Michael Iseard committed
28
	 *
29
	 * @param string|null $old_version Previous version of plugin.
Michael Iseard's avatar
Michael Iseard committed
30
	 *
Michael Iseard's avatar
Michael Iseard committed
31
32
	 * @since    1.0.0
	 */
Michael Iseard's avatar
Michael Iseard committed
33
	public static function activate( $old_version = null ) {
34

Michael Iseard's avatar
Michael Iseard committed
35
36
		$logger = new LoggerService();
		$logger->init();
37
		$twig = new TwigService();
Michael Iseard's avatar
Michael Iseard committed
38
		$twig->init();
Michael Iseard's avatar
Michael Iseard committed
39
40
		$settings = new Settings();
		$settings->register_settings();
Michael Iseard's avatar
Michael Iseard committed
41

42
		if ( $old_version ) {
43
			self::run_migrations( $old_version );
44
		}
45

Michael Iseard's avatar
Michael Iseard committed
46
		$settings->add_defaults();
47
48
49
		self::create_donors_table();
		self::create_transactions_table();
		self::create_subscriptions_table();
Michael Iseard's avatar
Michael Iseard committed
50

Michael Iseard's avatar
Michael Iseard committed
51
		update_option( '_kudos_donations_version', KUDOS_VERSION );
52
53
54
		$logger->info( 'Kudos Donations plugin activated' );

	}
55

56
57
58
59
60
61
62
63
	/**
	 * Run migrations if upgrading
	 *
	 * @param string $old_version
	 *
	 * @since 2.3.2
	 */
	public static function run_migrations( string $old_version ) {
64

Michael Iseard's avatar
Michael Iseard committed
65
66
67
68
		$logger = new LoggerService();
		$logger->info( 'Upgrade detected, running migrations.',
			[ 'old_version' => $old_version, 'new_version' => KUDOS_VERSION ] );

69
70
71
72
73
74
75
76
77
78
79
		if ( version_compare( $old_version, '2.1.1', '<' ) ) {
			Settings::remove_setting( 'action_scheduler' );
		}

		if ( version_compare( $old_version, '2.2.0', '<' ) ) {
			$link = Settings::get_setting( 'privacy_link' );
			Settings::remove_setting( 'subscription_enabled' );

			if ( $link ) {
				Settings::update_setting( 'terms_link', $link );
				Settings::remove_setting( 'privacy_link' );
80
			}
81
		}
82

83
84
85
86
87
88
89
90
91
92
		if ( version_compare( $old_version, '2.3.0', '<' ) ) {
			global $wpdb;

			// Rename setting
			$transaction_table = TransactionEntity::get_table_name();
			$wpdb->query( "ALTER TABLE $transaction_table RENAME COLUMN `campaign_label` TO `campaign_id`" );
			Settings::update_setting( 'show_intro', 1 );

			// Apply mode to Donors
			$donor_table = DonorEntity::get_table_name();
93
			$wpdb->query( "ALTER TABLE $donor_table ADD `mode` VARCHAR(45) NOT NULL" );
94
95
96
97
98
99
100
101
102
103
			$mapper = new MapperService( DonorEntity::class );
			$donors = $mapper->get_all_by();
			/** @var DonorEntity $donor */
			foreach ( $donors as $donor ) {
				$transactions = $donor->get_transactions();
				if ( $transactions ) {
					$donor->set_fields( [ 'mode' => $transactions[0]->mode ] );
				}
				$mapper->save( $donor );
			}
104
105
		}

106
107
		if ( version_compare( $old_version, '2.3.2', '<' ) ) {
			// Setting now replaced by 'theme_colors'
108
109
			$old_color             = Settings::get_setting( 'theme_color' );
			$new_colors            = Settings::get_setting( 'theme_colors' );
110
			$new_colors['primary'] = $old_color;
111
			Settings::update_setting( 'theme_colors', $new_colors );
112
113
			Settings::remove_setting( 'theme_color' );
		}
Michael Iseard's avatar
Michael Iseard committed
114

115
116
117
118
119
120
121
		if ( version_compare( $old_version, '2.3.7', '<' ) ) {
			// Change business_name to allow NULL
			global $wpdb;
			$donor_table = DonorEntity::get_table_name();
			$wpdb->query( "ALTER TABLE $donor_table MODIFY `business_name` VARCHAR(255)" );
		}

Michael Iseard's avatar
Michael Iseard committed
122
		if ( version_compare( $old_version, '2.4.0', '<' ) ) {
123
			// Setting now replaced by single 'vendor_mollie' setting.
Michael Iseard's avatar
Michael Iseard committed
124
125
126
127
			$connected = Settings::get_setting( 'mollie_connected' );
			Settings::update_array( 'vendor_mollie',
				[

128
129
					'connected' => (bool) $connected,
					'mode'      => ! empty( Settings::get_setting( 'mollie_api_mode' ) ) ? (string) Settings::get_setting( 'mollie_api_mode' ) : 'test',
Michael Iseard's avatar
Michael Iseard committed
130
131
132
133
134
135
136
137
138
139
140
					'test_key'  => (string) Settings::get_setting( 'mollie_test_api_key' ),
					'live_key'  => (string) Settings::get_setting( 'mollie_live_api_key' ),

				] );

			// Remove old settings fields.
			Settings::remove_setting( 'mollie_connected' );
			Settings::remove_setting( 'mollie_api_mode' );
			Settings::remove_setting( 'mollie_test_api_key' );
			Settings::remove_setting( 'mollie_live_api_key' );
			Settings::remove_setting( 'campaign_labels' );
141
142
		}

143
144
145
146
147
148
149
150
151
		if ( version_compare( $old_version, '2.4.1', '<' ) ) {
			// Cast connected variable as boolean
			$vendor_settings = Settings::get_setting('vendor_mollie');
			$connected = !empty($vendor_settings['connected']) ? (bool) $vendor_settings['connected'] : false;
			Settings::update_array('vendor_mollie', [
				'connected' => $connected
			]);
		}

152
153
154
	}

	/**
155
	 * Creates the donors table
156
	 *
157
	 * @since    1.1.0
158
	 */
159
	private static function create_donors_table() {
160

Michael Iseard's avatar
Michael Iseard committed
161
162
163
		global $wpdb;

		$charset_collate = $wpdb->get_charset_collate();
164
		$table_name      = DonorEntity::get_table_name();
Michael Iseard's avatar
Michael Iseard committed
165
166
167

		$sql = "CREATE TABLE $table_name (
		  id mediumint(9) NOT NULL AUTO_INCREMENT,
168
169
		  created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
		  last_updated datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
170
171
		  email VARCHAR(320) NOT NULL,
		  name VARCHAR(255) NOT NULL,
172
		  business_name VARCHAR(255),
173
174
175
176
		  street VARCHAR(255),
		  postcode VARCHAR(255),
		  city VARCHAR(255),
		  country VARCHAR(255),
Michael Iseard's avatar
Michael Iseard committed
177
		  customer_id VARCHAR(255),
178
		  secret VARCHAR(255),
179
		  mode VARCHAR(45) NOT NULL,
180
		  PRIMARY KEY (id)
181
		) $charset_collate";
Michael Iseard's avatar
Michael Iseard committed
182

183
		require_once ABSPATH . 'wp-admin/includes/upgrade.php';
Michael Iseard's avatar
Michael Iseard committed
184
		dbDelta( $sql );
185

Michael Iseard's avatar
Michael Iseard committed
186
187
	}

188
	/**
189
	 * Creates the transactions table
190
	 *
191
	 * @since    1.0.0
192
	 */
193
	private static function create_transactions_table() {
194

195
196
197
		global $wpdb;

		$charset_collate = $wpdb->get_charset_collate();
198
		$table_name      = TransactionEntity::get_table_name();
199
200
201

		$sql = "CREATE TABLE $table_name (
		  id mediumint(9) NOT NULL AUTO_INCREMENT,
202
203
		  created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
		  last_updated datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
204
205
206
207
208
209
		  value DECIMAL(7,2) NOT NULL,
		  currency VARCHAR(255),
		  status VARCHAR(255) DEFAULT 'open' NOT NULL,
		  method VARCHAR(255),
		  mode VARCHAR(255) NOT NULL,
		  sequence_type VARCHAR(255) NOT NULL,
Michael Iseard's avatar
Michael Iseard committed
210
		  customer_id varchar(255) NOT NULL,
211
212
213
214
		  order_id VARCHAR(255) NOT NULL,
		  transaction_id VARCHAR(255),
		  subscription_id VARCHAR(255),
		  refunds BLOB DEFAULT NULL,
215
		  campaign_id VARCHAR(255),
216
		  secret VARCHAR(255),
217
		  PRIMARY KEY (id)
218
		) $charset_collate;";
219

220
		require_once ABSPATH . 'wp-admin/includes/upgrade.php';
221
		dbDelta( $sql );
222

223
224
	}

Michael Iseard's avatar
Michael Iseard committed
225
226
227
228
229
230
	/**
	 * Creates the subscription table
	 *
	 * @since    1.1.0
	 */
	private static function create_subscriptions_table() {
231

Michael Iseard's avatar
Michael Iseard committed
232
		global $wpdb;
Michael Iseard's avatar
Michael Iseard committed
233

Michael Iseard's avatar
Michael Iseard committed
234
		$charset_collate = $wpdb->get_charset_collate();
235
		$table_name      = SubscriptionEntity::get_table_name();
Michael Iseard's avatar
Michael Iseard committed
236

Michael Iseard's avatar
Michael Iseard committed
237
		$sql = "CREATE TABLE $table_name (
238
		  id MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
239
240
          created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
          last_updated datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
Michael Iseard's avatar
Michael Iseard committed
241
242
243
		  value DECIMAL(7,2) NOT NULL,
		  currency VARCHAR(255),
		  frequency VARCHAR(255) NOT NULL,
244
		  years MEDIUMINT(2) NOT NULL,
Michael Iseard's avatar
Michael Iseard committed
245
246
247
248
		  customer_id VARCHAR(255),
		  transaction_id VARCHAR(255),
		  subscription_id VARCHAR(255),
		  status VARCHAR(255),
249
		  secret VARCHAR(255),		  
Michael Iseard's avatar
Michael Iseard committed
250
251
252
		  PRIMARY KEY (id)
		) $charset_collate";

253
		require_once ABSPATH . 'wp-admin/includes/upgrade.php';
Michael Iseard's avatar
Michael Iseard committed
254
		dbDelta( $sql );
255

Michael Iseard's avatar
Michael Iseard committed
256
	}
Michael Iseard's avatar
Michael Iseard committed
257
}