ActivatorService.php 8.11 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;
9
use Kudos\Helpers\WpDb;
Michael Iseard's avatar
Michael Iseard committed
10

Michael Iseard's avatar
Michael Iseard committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
 * 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
 */
25
class ActivatorService {
Michael Iseard's avatar
Michael Iseard committed
26

27
	/**
Michael Iseard's avatar
Michael Iseard committed
28
	 * @var LoggerService
29
30
31
	 */
	private $logger;
	/**
Michael Iseard's avatar
Michael Iseard committed
32
	 * @var TwigService
33
34
	 */
	private $twig;
35
	/**
Michael Iseard's avatar
Michael Iseard committed
36
	 * @var MapperService
37
38
39
	 */
	private $mapper;
	/**
Michael Iseard's avatar
Michael Iseard committed
40
	 * @var WpDb|\wpdb
41
42
	 */
	private $wpdb;
Michael Iseard's avatar
Michael Iseard committed
43
44
45
46
	/**
	 * @var Settings
	 */
	private $settings;
47

48
49
50
51
52
53
	public function __construct() {

		$this->logger = new LoggerService();
		$this->wpdb = new WpDb();
		$this->twig = new TwigService($this->logger);
		$this->mapper = new MapperService($this->logger, $this->wpdb);
Michael Iseard's avatar
Michael Iseard committed
54
55
		$this->settings = new Settings($this->mapper);

56
57
	}

Michael Iseard's avatar
Michael Iseard committed
58
	/**
59
	 * Runs all activation functions.
Michael Iseard's avatar
Michael Iseard committed
60
	 *
61
	 * @param string|null $old_version Previous version of plugin.
Michael Iseard's avatar
Michael Iseard committed
62
	 *
Michael Iseard's avatar
Michael Iseard committed
63
64
	 * @since    1.0.0
	 */
65
	public function activate( string $old_version = null ) {
66

67
		$logger = $this->logger;
Michael Iseard's avatar
Michael Iseard committed
68
		$logger->init();
69
		$twig = $this->twig;
Michael Iseard's avatar
Michael Iseard committed
70
		$twig->init();
Michael Iseard's avatar
Michael Iseard committed
71
		$settings = $this->settings;
Michael Iseard's avatar
Michael Iseard committed
72
		$settings->register_settings();
Michael Iseard's avatar
Michael Iseard committed
73

74
		if ( $old_version ) {
75
			self::run_migrations( $old_version );
76
		}
77

Michael Iseard's avatar
Michael Iseard committed
78
		$settings->add_defaults();
79
80
81
		self::create_donors_table();
		self::create_transactions_table();
		self::create_subscriptions_table();
Michael Iseard's avatar
Michael Iseard committed
82

Michael Iseard's avatar
Michael Iseard committed
83
		update_option( '_kudos_donations_version', KUDOS_VERSION );
Michael Iseard's avatar
Michael Iseard committed
84
		$logger->info( 'Kudos Donations plugin activated', ['version' => KUDOS_VERSION] );
85
86

	}
87

88
89
90
91
92
93
94
	/**
	 * Run migrations if upgrading
	 *
	 * @param string $old_version
	 *
	 * @since 2.3.2
	 */
95
96
97
98
	private function run_migrations( string $old_version ) {

		$logger = $this->logger;
		$wpdb = $this->wpdb;
Michael Iseard's avatar
Michael Iseard committed
99
		$settings = $this->settings;
100

Michael Iseard's avatar
Michael Iseard committed
101
102
103
		$logger->info( 'Upgrade detected, running migrations.',
			[ 'old_version' => $old_version, 'new_version' => KUDOS_VERSION ] );

104
		if ( version_compare( $old_version, '2.1.1', '<' ) ) {
Michael Iseard's avatar
Michael Iseard committed
105
			$settings::remove_setting( 'action_scheduler' );
106
107
108
		}

		if ( version_compare( $old_version, '2.2.0', '<' ) ) {
Michael Iseard's avatar
Michael Iseard committed
109
110
			$link = $settings::get_setting( 'privacy_link' );
			$settings::remove_setting( 'subscription_enabled' );
111
112

			if ( $link ) {
Michael Iseard's avatar
Michael Iseard committed
113
114
				$settings::update_setting( 'terms_link', $link );
				$settings::remove_setting( 'privacy_link' );
115
			}
116
		}
117

118
119
120
121
122
		if ( version_compare( $old_version, '2.3.0', '<' ) ) {

			// Rename setting
			$transaction_table = TransactionEntity::get_table_name();
			$wpdb->query( "ALTER TABLE $transaction_table RENAME COLUMN `campaign_label` TO `campaign_id`" );
Michael Iseard's avatar
Michael Iseard committed
123
			$settings::update_setting( 'show_intro', 1 );
124
125
126

			// Apply mode to Donors
			$donor_table = DonorEntity::get_table_name();
127
			$wpdb->query( "ALTER TABLE $donor_table ADD `mode` VARCHAR(45) NOT NULL" );
128
129
130
			$donors = $this->mapper
				->get_repository(DonorEntity::class)
				->get_all_by();
131
132
133
134
135
136
			/** @var DonorEntity $donor */
			foreach ( $donors as $donor ) {
				$transactions = $donor->get_transactions();
				if ( $transactions ) {
					$donor->set_fields( [ 'mode' => $transactions[0]->mode ] );
				}
137
				$this->mapper->save( $donor );
138
			}
139
140
		}

141
142
		if ( version_compare( $old_version, '2.3.2', '<' ) ) {
			// Setting now replaced by 'theme_colors'
Michael Iseard's avatar
Michael Iseard committed
143
144
			$old_color             = $settings::get_setting( 'theme_color' );
			$new_colors            = $settings::get_setting( 'theme_colors' );
145
			$new_colors['primary'] = $old_color;
Michael Iseard's avatar
Michael Iseard committed
146
147
			$settings::update_setting( 'theme_colors', $new_colors );
			$settings::remove_setting( 'theme_color' );
148
		}
Michael Iseard's avatar
Michael Iseard committed
149

150
151
152
153
154
155
		if ( version_compare( $old_version, '2.3.7', '<' ) ) {
			// Change business_name to allow NULL
			$donor_table = DonorEntity::get_table_name();
			$wpdb->query( "ALTER TABLE $donor_table MODIFY `business_name` VARCHAR(255)" );
		}

Michael Iseard's avatar
Michael Iseard committed
156
		if ( version_compare( $old_version, '2.4.0', '<' ) ) {
157
			// Setting now replaced by single 'vendor_mollie' setting.
Michael Iseard's avatar
Michael Iseard committed
158
159
			$connected = $settings::get_setting( 'mollie_connected' );
			$settings::update_array( 'vendor_mollie',
Michael Iseard's avatar
Michael Iseard committed
160
161
				[

162
					'connected' => (bool) $connected,
Michael Iseard's avatar
Michael Iseard committed
163
164
165
					'mode'      => ! empty( $settings::get_setting( 'mollie_api_mode' ) ) ? (string) $settings::get_setting( 'mollie_api_mode' ) : 'test',
					'test_key'  => (string) $settings::get_setting( 'mollie_test_api_key' ),
					'live_key'  => (string) $settings::get_setting( 'mollie_live_api_key' ),
Michael Iseard's avatar
Michael Iseard committed
166
167
168
169

				] );

			// Remove old settings fields.
Michael Iseard's avatar
Michael Iseard committed
170
171
172
173
174
			$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' );
175
176
		}

177
		if ( version_compare( $old_version, '2.4.1', '<' ) ) {
Michael Iseard's avatar
Michael Iseard committed
178
			// Cast connected variable as boolean.
Michael Iseard's avatar
Michael Iseard committed
179
			$vendor_settings = $settings::get_setting('vendor_mollie');
180
			$connected = ! empty( $vendor_settings['connected'] ) && $vendor_settings['connected'];
Michael Iseard's avatar
Michael Iseard committed
181
			$settings::update_array('vendor_mollie', [
182
183
184
185
				'connected' => $connected
			]);
		}

186
		if ( version_compare( $old_version, '2.5.0', '<' ) ) {
Michael Iseard's avatar
Michael Iseard committed
187
			// Add message field to transactions.
188
189
			$transaction_table = TransactionEntity::get_table_name();
			$wpdb->query( "ALTER TABLE $transaction_table ADD `message` VARCHAR(255)" );
Michael Iseard's avatar
Michael Iseard committed
190
191

			// Remove unused settings.
Michael Iseard's avatar
Michael Iseard committed
192
193
			$settings::remove_setting('address_enabled');
			$settings::remove_setting('address_required');
194
195
		}

196
197
198
	}

	/**
199
	 * Creates the donors table
200
	 *
201
	 * @since    1.1.0
202
	 */
203
	private function create_donors_table() {
204

205
		$wpdb = $this->wpdb;
Michael Iseard's avatar
Michael Iseard committed
206
207

		$charset_collate = $wpdb->get_charset_collate();
208
		$table_name      = DonorEntity::get_table_name();
Michael Iseard's avatar
Michael Iseard committed
209
210
211

		$sql = "CREATE TABLE $table_name (
		  id mediumint(9) NOT NULL AUTO_INCREMENT,
212
213
		  created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
		  last_updated datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
214
215
		  email VARCHAR(320) NOT NULL,
		  name VARCHAR(255) NOT NULL,
216
		  business_name VARCHAR(255),
217
218
219
220
		  street VARCHAR(255),
		  postcode VARCHAR(255),
		  city VARCHAR(255),
		  country VARCHAR(255),
Michael Iseard's avatar
Michael Iseard committed
221
		  customer_id VARCHAR(255),
222
		  secret VARCHAR(255),
223
		  mode VARCHAR(45) NOT NULL,
224
		  PRIMARY KEY (id)
225
		) $charset_collate";
Michael Iseard's avatar
Michael Iseard committed
226

227
		require_once ABSPATH . 'wp-admin/includes/upgrade.php';
Michael Iseard's avatar
Michael Iseard committed
228
		dbDelta( $sql );
229

Michael Iseard's avatar
Michael Iseard committed
230
231
	}

232
	/**
233
	 * Creates the transactions table
234
	 *
235
	 * @since    1.0.0
236
	 */
237
	private function create_transactions_table() {
238

239
		$wpdb = $this->wpdb;
240
241

		$charset_collate = $wpdb->get_charset_collate();
242
		$table_name      = TransactionEntity::get_table_name();
243
244
245

		$sql = "CREATE TABLE $table_name (
		  id mediumint(9) NOT NULL AUTO_INCREMENT,
246
247
		  created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
		  last_updated datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
248
249
250
251
252
253
		  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
254
		  customer_id varchar(255) NOT NULL,
255
256
257
258
		  order_id VARCHAR(255) NOT NULL,
		  transaction_id VARCHAR(255),
		  subscription_id VARCHAR(255),
		  refunds BLOB DEFAULT NULL,
259
		  campaign_id VARCHAR(255),
260
		  message VARCHAR(255),
261
		  secret VARCHAR(255),
262
		  PRIMARY KEY (id)
263
		) $charset_collate;";
264

265
		require_once ABSPATH . 'wp-admin/includes/upgrade.php';
266
		dbDelta( $sql );
267

268
269
	}

Michael Iseard's avatar
Michael Iseard committed
270
271
272
273
274
	/**
	 * Creates the subscription table
	 *
	 * @since    1.1.0
	 */
275
	private function create_subscriptions_table() {
276

277
		$wpdb = $this->wpdb;
Michael Iseard's avatar
Michael Iseard committed
278

Michael Iseard's avatar
Michael Iseard committed
279
		$charset_collate = $wpdb->get_charset_collate();
280
		$table_name      = SubscriptionEntity::get_table_name();
Michael Iseard's avatar
Michael Iseard committed
281

Michael Iseard's avatar
Michael Iseard committed
282
		$sql = "CREATE TABLE $table_name (
283
		  id MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
284
285
          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
286
287
288
		  value DECIMAL(7,2) NOT NULL,
		  currency VARCHAR(255),
		  frequency VARCHAR(255) NOT NULL,
289
		  years MEDIUMINT(2) NOT NULL,
Michael Iseard's avatar
Michael Iseard committed
290
291
292
293
		  customer_id VARCHAR(255),
		  transaction_id VARCHAR(255),
		  subscription_id VARCHAR(255),
		  status VARCHAR(255),
294
		  secret VARCHAR(255),		  
Michael Iseard's avatar
Michael Iseard committed
295
296
297
		  PRIMARY KEY (id)
		) $charset_collate";

298
		require_once ABSPATH . 'wp-admin/includes/upgrade.php';
Michael Iseard's avatar
Michael Iseard committed
299
		dbDelta( $sql );
300

Michael Iseard's avatar
Michael Iseard committed
301
	}
Michael Iseard's avatar
Michael Iseard committed
302
}