Settings.php 15.6 KB
Newer Older
1
2
<?php

3
4
namespace Kudos\Helpers;

5
use Exception;
6
7
use Kudos\Entity\TransactionEntity;
use Kudos\Service\MapperService;
8

9
10
class Settings {

Michael Iseard's avatar
Michael Iseard committed
11
	const PREFIX = '_kudos_';
12
13

	/**
Michael Iseard's avatar
Michael Iseard committed
14
	 * Settings configuration.
15
	 *
16
17
	 * @var array
	 */
18
	private $settings;
19
20
21
22
	/**
	 * @var \Kudos\Service\MapperService
	 */
	private $mapper;
23
24

	/**
25
	 * Settings class constructor.
26
27
	 *
	 */
28
	public function __construct( MapperService $mapper ) {
29

30
		$this->mapper   = $mapper;
31
32
		$this->settings = apply_filters(
			'kudos_register_settings',
Michael Iseard's avatar
Michael Iseard committed
33
			[
34
				'show_intro'             => [
Michael Iseard's avatar
Michael Iseard committed
35
36
37
38
					'type'              => 'boolean',
					'show_in_rest'      => true,
					'default'           => true,
					'sanitize_callback' => 'rest_sanitize_boolean',
Michael Iseard's avatar
Michael Iseard committed
39
				],
40
				'vendor_mollie'          => [
41
42
					'type'              => 'object',
					'default'           => [
43
44
45
46
47
48
						'connected'       => false,
						'recurring'       => false,
						'mode'            => 'test',
						'payment_methods' => [],
						'test_key'        => '',
						'live_key'        => '',
49
					],
50
					'show_in_rest'      => [
51
52
53
						'schema' => [
							'type'       => 'object',
							'properties' => [
Michael Iseard's avatar
Michael Iseard committed
54
								'connected'       => [
55
56
									'type' => 'boolean',
								],
Michael Iseard's avatar
Michael Iseard committed
57
								'recurring'       => [
58
59
									'type' => 'boolean',
								],
Michael Iseard's avatar
Michael Iseard committed
60
								'mode'            => [
61
62
									'type' => 'string',
								],
Michael Iseard's avatar
Michael Iseard committed
63
								'test_key'        => [
64
65
									'type' => 'string',
								],
Michael Iseard's avatar
Michael Iseard committed
66
								'live_key'        => [
67
68
									'type' => 'string',
								],
Michael Iseard's avatar
Michael Iseard committed
69
70
71
72
73
								'payment_methods' => [
									'type'  => 'array',
									'items' => [
										'type'       => 'object',
										'properties' => [
74
											'id'            => [
Michael Iseard's avatar
Michael Iseard committed
75
76
												'type' => 'string',
											],
77
											'status'        => [
Michael Iseard's avatar
Michael Iseard committed
78
79
												'type' => 'string',
											],
80
81
82
83
84
85
86
87
88
89
90
											'maximumAmount' => [
												'type'       => 'object',
												'properties' => [
													'value'    => [
														'type' => 'string',
													],
													'currency' => [
														'type' => 'string',
													],
												],
											],
Michael Iseard's avatar
Michael Iseard committed
91
92
93
										],
									],
								],
94
95
96
							],
						],
					],
97
					'sanitize_callback' => [ $this, 'sanitize_vendor' ],
Michael Iseard's avatar
Michael Iseard committed
98
				],
99
				'email_receipt_enable'   => [
Michael Iseard's avatar
Michael Iseard committed
100
101
102
103
					'type'              => 'boolean',
					'show_in_rest'      => true,
					'default'           => false,
					'sanitize_callback' => 'rest_sanitize_boolean',
104
				],
105
				'email_bcc'              => [
Michael Iseard's avatar
Michael Iseard committed
106
107
108
109
					'type'              => 'string',
					'show_in_rest'      => true,
					'sanitize_callback' => 'sanitize_email',
				],
110
				'smtp_enable'            => [
Michael Iseard's avatar
Michael Iseard committed
111
112
113
114
					'type'              => 'boolean',
					'show_in_rest'      => true,
					'default'           => false,
					'sanitize_callback' => 'rest_sanitize_boolean',
Michael Iseard's avatar
Michael Iseard committed
115
				],
116
				'smtp_host'              => [
Michael Iseard's avatar
Michael Iseard committed
117
118
119
120
					'type'              => 'string',
					'show_in_rest'      => true,
					'sanitize_callback' => 'sanitize_text_field',
				],
121
				'smtp_encryption'        => [
Michael Iseard's avatar
Michael Iseard committed
122
123
124
125
					'type'              => 'string',
					'show_in_rest'      => true,
					'default'           => 'tls',
					'sanitize_callback' => 'sanitize_text_field',
Michael Iseard's avatar
Michael Iseard committed
126
				],
127
				'smtp_autotls'           => [
Michael Iseard's avatar
Michael Iseard committed
128
129
130
131
					'type'              => 'boolean',
					'show_in_rest'      => true,
					'default'           => true,
					'sanitize_callback' => 'rest_sanitize_boolean',
Michael Iseard's avatar
Michael Iseard committed
132
				],
133
				'smtp_from'              => [
Michael Iseard's avatar
Michael Iseard committed
134
135
					'type'              => 'string',
					'show_in_rest'      => true,
Michael Iseard's avatar
Michael Iseard committed
136
					'default'           => null,
Michael Iseard's avatar
Michael Iseard committed
137
138
					'sanitize_callback' => 'sanitize_email',
				],
139
				'smtp_username'          => [
Michael Iseard's avatar
Michael Iseard committed
140
141
142
143
					'type'              => 'string',
					'show_in_rest'      => true,
					'sanitize_callback' => 'sanitize_text_field',
				],
144
				'smtp_password'          => [
Michael Iseard's avatar
Michael Iseard committed
145
146
147
					'type'         => 'string',
					'show_in_rest' => true,
				],
148
				'smtp_port'              => [
149
					'type'              => 'number',
Michael Iseard's avatar
Michael Iseard committed
150
					'show_in_rest'      => true,
151
					'sanitize_callback' => 'intval',
Michael Iseard's avatar
Michael Iseard committed
152
				],
153
154
155
156
157
158
				'spam_protection'        => [
					'type'              => 'boolean',
					'show_in_rest'      => true,
					'default'           => true,
					'sanitize_callback' => 'rest_sanitize_boolean',
				],
159
				'theme_colors'           => [
160
161
					'type'              => 'object',
					'default'           => [
Michael Iseard's avatar
Michael Iseard committed
162
163
164
						'primary'   => '#ff9f1c',
						'secondary' => '#2ec4b6',
					],
165
					'show_in_rest'      => [
166
						'schema' => [
Michael Iseard's avatar
Michael Iseard committed
167
							'type'       => 'object',
168
							'properties' => [
Michael Iseard's avatar
Michael Iseard committed
169
170
								'primary'   => [
									'type' => 'string',
171
172
								],
								'secondary' => [
Michael Iseard's avatar
Michael Iseard committed
173
									'type' => 'string',
174
								],
Michael Iseard's avatar
Michael Iseard committed
175
176
							],
						],
Michael Iseard's avatar
Michael Iseard committed
177
					],
178
					'sanitize_callback' => [ $this, 'recursive_sanitize_text_field' ],
Michael Iseard's avatar
Michael Iseard committed
179
				],
180
				'terms_link'             => [
Michael Iseard's avatar
Michael Iseard committed
181
182
183
184
185
					'type'              => 'string',
					'show_in_rest'      => true,
					'default'           => null,
					'sanitize_callback' => 'esc_url_raw',
				],
186
				'privacy_link'           => [
187
188
189
190
191
					'type'              => 'string',
					'show_in_rest'      => true,
					'default'           => null,
					'sanitize_callback' => 'esc_url_raw',
				],
192
193
194
195
196
				'completed_payment'      => [
					'type'         => 'string',
					'default'      => 'message',
					'show_in_rest' => true,
				],
197
				'return_message_enable'  => [
Michael Iseard's avatar
Michael Iseard committed
198
199
200
201
					'type'              => 'boolean',
					'show_in_rest'      => true,
					'default'           => true,
					'sanitize_callback' => 'rest_sanitize_boolean',
Michael Iseard's avatar
Michael Iseard committed
202
				],
203
				'return_message_title'   => [
Michael Iseard's avatar
Michael Iseard committed
204
205
206
207
208
					'type'              => 'string',
					'show_in_rest'      => true,
					'default'           => __( 'Thank you!', 'kudos-donations' ),
					'sanitize_callback' => 'sanitize_text_field',
				],
209
				'return_message_text'    => [
Michael Iseard's avatar
Michael Iseard committed
210
211
					'type'              => 'string',
					'show_in_rest'      => true,
212
					'default'           => sprintf(
Michael Iseard's avatar
Michael Iseard committed
213
					/* translators: %s: Value of donation. */
214
215
216
						__( 'Many thanks for your donation of %s. We appreciate your support.', 'kudos-donations' ),
						'{{value}}'
					),
Michael Iseard's avatar
Michael Iseard committed
217
218
					'sanitize_callback' => 'sanitize_text_field',
				],
219
				'custom_return_enable'   => [
Michael Iseard's avatar
Michael Iseard committed
220
221
222
223
					'type'              => 'boolean',
					'show_in_rest'      => true,
					'default'           => false,
					'sanitize_callback' => 'rest_sanitize_boolean',
Michael Iseard's avatar
Michael Iseard committed
224
				],
225
				'custom_return_url'      => [
Michael Iseard's avatar
Michael Iseard committed
226
227
228
229
					'type'              => 'string',
					'show_in_rest'      => true,
					'sanitize_callback' => 'esc_url_raw',
				],
230
				'payment_vendor'         => [
Michael Iseard's avatar
Michael Iseard committed
231
232
233
					'type'    => 'string',
					'default' => 'mollie',
				],
234
				'debug_mode'             => [
Michael Iseard's avatar
Michael Iseard committed
235
236
237
238
					'type'              => 'boolean',
					'show_in_rest'      => true,
					'default'           => false,
					'sanitize_callback' => 'rest_sanitize_boolean',
Michael Iseard's avatar
Michael Iseard committed
239
				],
240
241
242
243
244
245
				'enable_shortcode'      => [
					'type'              => 'boolean',
					'show_in_rest'      => true,
					'default'           => true,
					'sanitize_callback' => 'rest_sanitize_boolean',
				],
246
				'donate_modal_in_footer' => [
247
248
249
250
251
					'type'              => 'boolean',
					'show_in_rest'      => true,
					'default'           => false,
					'sanitize_callback' => 'rest_sanitize_boolean',
				],
252
				'disable_object_cache'   => [
Michael Iseard's avatar
Michael Iseard committed
253
254
255
256
					'type'              => 'boolean',
					'show_in_rest'      => true,
					'default'           => false,
					'sanitize_callback' => 'rest_sanitize_boolean',
Michael Iseard's avatar
Michael Iseard committed
257
				],
258
				'campaigns'              => [
Michael Iseard's avatar
Michael Iseard committed
259
260
					'type'              => 'array',
					'show_in_rest'      => [
261
						'schema' => [
Michael Iseard's avatar
Michael Iseard committed
262
							'type'  => 'array',
263
							'items' => [
Michael Iseard's avatar
Michael Iseard committed
264
								'type'       => 'object',
Michael Iseard's avatar
Michael Iseard committed
265
								'properties' => [
Michael Iseard's avatar
Michael Iseard committed
266
									'id'               => [
267
										'type' => 'string',
Michael Iseard's avatar
Michael Iseard committed
268
									],
Michael Iseard's avatar
Michael Iseard committed
269
									'name'             => [
270
										'type' => 'string',
Michael Iseard's avatar
Michael Iseard committed
271
									],
272
									'campaign_goal'    => [
Michael Iseard's avatar
Michael Iseard committed
273
										'type' => 'string',
274
									],
Michael Iseard's avatar
Michael Iseard committed
275
276
277
									'additional_funds' => [
										'type' => 'string',
									],
Michael Iseard's avatar
Michael Iseard committed
278
279
									'modal_title'      => [
										'type' => 'string',
Michael Iseard's avatar
Michael Iseard committed
280
									],
Michael Iseard's avatar
Michael Iseard committed
281
									'welcome_text'     => [
Michael Iseard's avatar
Michael Iseard committed
282
283
284
285
286
287
288
289
										'type' => 'string',
									],
									'address_enabled'  => [
										'type' => 'boolean',
									],
									'address_required' => [
										'type' => 'boolean',
									],
290
291
292
									'message_enabled'  => [
										'type' => 'boolean',
									],
Michael Iseard's avatar
Michael Iseard committed
293
									'amount_type'      => [
Michael Iseard's avatar
Michael Iseard committed
294
										'type' => 'string',
Michael Iseard's avatar
Michael Iseard committed
295
									],
Michael Iseard's avatar
Michael Iseard committed
296
									'fixed_amounts'    => [
Michael Iseard's avatar
Michael Iseard committed
297
298
299
										'type' => 'string',
									],
									'donation_type'    => [
Michael Iseard's avatar
Michael Iseard committed
300
301
										'type' => 'string',
									],
302
									'show_progress'    => [
Michael Iseard's avatar
Michael Iseard committed
303
304
										'type' => 'boolean',
									],
305
306
									// Deprecated: do not use
									'protected'        => [
307
308
										'type' => 'boolean',
									],
Michael Iseard's avatar
Michael Iseard committed
309
								],
Michael Iseard's avatar
Michael Iseard committed
310
							],
311
312
						],
					],
Michael Iseard's avatar
Michael Iseard committed
313
					'default'           => [
Michael Iseard's avatar
Michael Iseard committed
314
315
316
317
318
319
						0 => [
							'id'               => 'default',
							'name'             => 'Default',
							'modal_title'      => __( 'Support us!', 'kudos-donations' ),
							'welcome_text'     => __( 'Your support is greatly appreciated and will help to keep us going.',
								'kudos-donations' ),
320
							'address_enabled'  => false,
Michael Iseard's avatar
Michael Iseard committed
321
							'address_required' => true,
322
							'message_enabled'  => false,
Michael Iseard's avatar
Michael Iseard committed
323
324
							'amount_type'      => 'both',
							'fixed_amounts'    => '1,5,20,50',
Michael Iseard's avatar
Michael Iseard committed
325
							'campaign_goal'    => '',
Michael Iseard's avatar
Michael Iseard committed
326
							'additional_funds' => '',
327
							'show_progress'    => false,
328
							'donation_type'    => 'oneoff',
Michael Iseard's avatar
Michael Iseard committed
329
330
						],
					],
331
					'sanitize_callback' => [ $this, 'sanitize_campaigns' ],
332
333
334
				],
			]
		);
335
336
	}

337
338
339
340
	/**
	 * Sanitize vendor settings.
	 *
	 * @param $settings
341
	 *
342
343
	 * @return mixed
	 */
344
	public function sanitize_vendor( $settings ) {
345

346
347
		foreach ( $settings as $setting => &$value ) {
			switch ( $setting ) {
348
349
				case 'connected':
				case 'recurring':
350
					$value = rest_sanitize_boolean( $value );
351
352
353
354
					break;
				case 'live_key':
				case 'test_key':
				case 'mode':
355
					$value = sanitize_text_field( $value );
356
357
					break;
				case 'payment_methods':
358
					$value = self::recursive_sanitize_text_field( $value );
359
360
361
					break;
			}
		}
362

363
364
365
		return $settings;
	}

366
	/**
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
	 * Sanitize the various setting fields in the donation form array.
	 *
	 * @param $campaigns
	 *
	 * @return array
	 */
	public function sanitize_campaigns( $campaigns ): array {

		// Loop through each of the campaigns.
		foreach ( $campaigns as &$form ) {

			// Generate a unique campaign ID if none yet.
			if ( ! isset( $form['id'] ) ) {
				$form['id'] = $this->generate_campaign_id( $form['name'] );
			}

			// Loop through fields and sanitize.
			foreach ( $form as $option => &$value ) {

				switch ( $option ) {
					case 'name':
					case 'modal_title':
					case 'welcome_text':
					case 'fixed_amounts':
						$value = sanitize_text_field( $value );
						break;
					case 'amount_type':
					case 'donation_type':
						$value = sanitize_key( $value );
						break;
					case 'address_enabled':
					case 'address_required':
					case 'show_progress':
					case 'message_enabled':
						$value = rest_sanitize_boolean( $value );
						break;
				}
			}
		}

		return $campaigns;
	}

	/**
	 * Gets the settings for the current vendor.
412
413
414
415
416
	 *
	 * @return mixed
	 */
	public static function get_current_vendor_settings() {

417
		return self::get_setting( 'vendor_' . self::get_setting( 'payment_vendor' ) );
418
419
	}

420
	/**
421
	 * Returns setting value.
422
	 *
423
	 * @param string $name Setting name without prefix.
Michael Iseard's avatar
Michael Iseard committed
424
	 *
425
	 * @return mixed
426
	 */
427
	public static function get_setting( string $name ) {
428

Michael Iseard's avatar
Michael Iseard committed
429
		return get_option( self::PREFIX . $name );
430
431
	}

Michael Iseard's avatar
Michael Iseard committed
432
	/**
433
	 * Update specified setting.
Michael Iseard's avatar
Michael Iseard committed
434
	 *
435
	 * @param string $name Setting name without prefix.
Michael Iseard's avatar
Michael Iseard committed
436
	 * @param mixed $value Setting value.
Michael Iseard's avatar
Michael Iseard committed
437
438
439
	 *
	 * @return bool
	 */
Michael Iseard's avatar
Michael Iseard committed
440
	public static function update_setting( string $name, $value ): bool {
Michael Iseard's avatar
Michael Iseard committed
441
442
443
444

		return update_option( self::PREFIX . $name, $value );
	}

445
	/**
Michael Iseard's avatar
Michael Iseard committed
446
447
	 * Updates specific values in serialized settings array.
	 * e.g update_array('my_setting', ['enabled' => false]).
448
	 *
449
450
	 * @param string $name Setting array name without prefix.
	 * @param array $value Array of name=>values in setting to update.
451
452
453
454
455
	 *
	 * @return bool
	 */
	public static function update_array( string $name, array $value ): bool {

456
		// Grab current data.
457
		$current = self::get_setting( $name );
458

459
		// Check if setting is either an array or null.
Michael Iseard's avatar
Michael Iseard committed
460
		if ( is_array( $current ) || ! null ) {
461
			// Merge provided data and current data then update setting.
462
			$new = wp_parse_args( $value, $current );
Michael Iseard's avatar
Michael Iseard committed
463

464
			return self::update_setting( $name, $new );
465
466
		}

467
		return false;
468
469
	}

470
	/**
Michael Iseard's avatar
Michael Iseard committed
471
	 * Register all the settings.
472
	 */
473
	public function register_settings() {
474

Michael Iseard's avatar
Michael Iseard committed
475
		foreach ( $this->settings as $name => $setting ) {
476
477
			register_setting(
				'kudos_donations',
Michael Iseard's avatar
Michael Iseard committed
478
479
				self::PREFIX . $name,
				$setting
480
481
482
483
			);
		}
	}

484
	/**
Michael Iseard's avatar
Michael Iseard committed
485
	 * Add the settings to the database.
486
	 */
487
	public function add_defaults() {
488

Michael Iseard's avatar
Michael Iseard committed
489
		foreach ( $this->settings as $name => $setting ) {
Michael Iseard's avatar
Michael Iseard committed
490
491
			if ( isset( $setting['default'] ) ) {
				add_option( self::PREFIX . $name, $setting['default'] );
Michael Iseard's avatar
Michael Iseard committed
492
			}
493
494
495
		}
	}

Michael Iseard's avatar
Michael Iseard committed
496
	/**
Michael Iseard's avatar
Michael Iseard committed
497
	 * Removes all settings from database.
Michael Iseard's avatar
Michael Iseard committed
498
	 */
Michael Iseard's avatar
Michael Iseard committed
499
	public function remove_settings() {
Michael Iseard's avatar
Michael Iseard committed
500

Michael Iseard's avatar
Michael Iseard committed
501
502
503
		foreach ( $this->settings as $key => $setting ) {
			self::remove_setting( $key );
		}
Michael Iseard's avatar
Michael Iseard committed
504
505
	}

506
	/**
Michael Iseard's avatar
Michael Iseard committed
507
	 * Remove specified setting from database.
508
	 *
Michael Iseard's avatar
Michael Iseard committed
509
510
511
	 * @param string $name
	 *
	 * @return bool
512
	 */
Michael Iseard's avatar
Michael Iseard committed
513
	public static function remove_setting( string $name ): bool {
514

Michael Iseard's avatar
Michael Iseard committed
515
		return delete_option( self::PREFIX . $name );
516
517
	}

518
519
520
	/**
	 * Method to recursively sanitize all text fields in an array.
	 *
Michael Iseard's avatar
Michael Iseard committed
521
	 * @param array $array Array of values to sanitize.
522
	 *
523
524
525
	 * @return mixed
	 * @source https://wordpress.stackexchange.com/questions/24736/wordpress-sanitize-array
	 */
526
	public function recursive_sanitize_text_field( array $array ): array {
527

Michael Iseard's avatar
Michael Iseard committed
528
		foreach ( $array as &$value ) {
529
530
531
532
533
534
			if ( is_array( $value ) ) {
				$value = self::recursive_sanitize_text_field( $value );
			} else {
				$value = sanitize_text_field( $value );
			}
		}
535

536
		return $array;
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
	}

	/**
	 * Generates a unique ID in the form of a slug for the campaign.
	 *
	 * @param $name string User provided name for the campaign.
	 *
	 * @return string
	 */
	private function generate_campaign_id( string $name ): string {

		$id        = sanitize_title( $name );
		$campaigns = $this->get_setting( 'campaigns' );
		$ids       = array_map( function ( $campaign ) {
			return $campaign['id'];
		},
			$campaigns );

		// If current id exists in array, iterate $n until it is unique.
		$n      = 1;
		$new_id = $id;
		while ( in_array( $new_id, $ids ) ) {
			$new_id = $id . '-' . $n;
			$n ++;
		}

		// Return new id.
		return $new_id;
	}

	/**
568
	 * Gets the campaign by specified column (e.g. id).
569
570
571
572
573
574
	 *
	 * @param string|null $value
	 *
	 * @return array|null
	 * @throws Exception
	 */
575
	public static function get_campaign( string $value ): ?array {
576
577
578
579
580
581
582
583
584
585
586

		$campaigns = self::get_setting( 'campaigns' );
		$key       = array_search( $value, array_column( (array) $campaigns, 'id' ) );

		// Check if key is an index and if so return index from forms.
		if ( is_int( $key ) ) {
			return $campaigns[ $key ];
		}

		/* translators: %s: Campaign id */
		throw new Exception( sprintf( __( 'Campaign "%s" not found.', 'kudos-donations' ), $value ) );
587
588
	}

589
590
591
592
593
594
595
	/**
	 * Gets transaction stats for campaign.
	 *
	 * @param string $campaign_id
	 *
	 * @return array
	 */
596
	public function get_campaign_stats( string $campaign_id ): ?array {
597

598
599
600
601
		$mapper       = $this->mapper;
		$transactions = $mapper
			->get_repository( TransactionEntity::class )
			->get_all_by( [ 'campaign_id' => $campaign_id, ] );
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624

		if ( $transactions ) {
			$values = array_map( function ( $transaction ) {
				if ( 'paid' === $transaction->status ) {
					$refunds = $transaction->get_refund();
					if ( $refunds ) {
						return $refunds->remaining;
					} else {
						return $transaction->value;
					}
				}

				return 0;
			},
				$transactions );

			return [
				'count'         => count( $values ),
				'total'         => array_sum( $values ),
				'last_donation' => end( $transactions )->created,
			];
		}

625
		// No transactions found.
626
627
628
629
630
631
632
		return [
			'count'         => 0,
			'total'         => 0,
			'last_donation' => '',
		];
	}

633
}