Passed
Push — master ( a42385...9b46a7 )
by Paul
04:14
created

Upgrade_3_0_0::migrateRequiredSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Upgrader;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Controllers\AdminController;
7
use GeminiLabs\SiteReviews\Database\DefaultsManager;
8
use GeminiLabs\SiteReviews\Database\OptionManager;
9
use GeminiLabs\SiteReviews\Helper;
10
11
class Upgrade_3_0_0
12
{
13
	const MAPPED_SETTINGS = [
14
		'settings.general.notification' => 'settings.general.notifications', // array
15
		'settings.general.notification_email' => 'settings.general.notification_email',
16
		'settings.general.notification_message' => 'settings.general.notification_message',
17
		'settings.general.require.approval' => 'settings.general.require.approval',
18
		'settings.general.require.login' => 'settings.general.require.login',
19
		'settings.general.require.login_register' => 'settings.general.require.login_register',
20
		'settings.general.webhook_url' => 'settings.general.notification_slack',
21
		'settings.reviews-form.akismet' => 'settings.submissions.akismet',
22
		'settings.reviews-form.blacklist.action' => 'settings.submissions.blacklist.action',
23
		'settings.reviews-form.blacklist.entries' => 'settings.submissions.blacklist.entries',
24
		'settings.reviews-form.recaptcha.integration' => 'settings.submissions.recaptcha.integration',
25
		'settings.reviews-form.recaptcha.key' => 'settings.submissions.recaptcha.key',
26
		'settings.reviews-form.recaptcha.position' => 'settings.submissions.recaptcha.position',
27
		'settings.reviews-form.recaptcha.secret' => 'settings.submissions.recaptcha.secret',
28
		'settings.reviews-form.required' => 'settings.submissions.required', // array
29
		'settings.reviews.assigned_links.enabled' => 'settings.reviews.assigned_links',
30
		'settings.reviews.avatars.enabled' => 'settings.reviews.avatars',
31
		'settings.reviews.date.custom' => 'settings.reviews.date.custom',
32
		'settings.reviews.date.format' => 'settings.reviews.date.format',
33
		'settings.reviews.excerpt.enabled' => 'settings.reviews.excerpts',
34
		'settings.reviews.excerpt.length' => 'settings.reviews.excerpts_length',
35
		'settings.reviews.schema.address' => 'settings.schema.address',
36
		'settings.reviews.schema.description.custom' => 'settings.schema.description.custom',
37
		'settings.reviews.schema.description.default' => 'settings.schema.description.default',
38
		'settings.reviews.schema.highprice' => 'settings.schema.highprice',
39
		'settings.reviews.schema.image.custom' => 'settings.schema.image.custom',
40
		'settings.reviews.schema.image.default' => 'settings.schema.image.default',
41
		'settings.reviews.schema.lowprice' => 'settings.schema.lowprice',
42
		'settings.reviews.schema.name.custom' => 'settings.schema.name.custom',
43
		'settings.reviews.schema.name.default' => 'settings.schema.name.default',
44
		'settings.reviews.schema.pricecurrency' => 'settings.schema.pricecurrency',
45
		'settings.reviews.schema.pricerange' => 'settings.schema.pricerange',
46
		'settings.reviews.schema.telephone' => 'settings.schema.telephone',
47
		'settings.reviews.schema.type.custom' => 'settings.schema.type.custom',
48
		'settings.reviews.schema.type.default' => 'settings.schema.type.default',
49
		'settings.reviews.schema.url.custom' => 'settings.schema.url.custom',
50
		'settings.reviews.schema.url.default' => 'settings.schema.url.default',
51
		'version' => 'version_upgraded_from',
52
	];
53
54
	/**
55
	 * @var array
56
	 */
57
	protected $newSettings;
58
59
	/**
60
	 * @var array
61
	 */
62
	protected $oldSettings;
63
64 6
	public function __construct()
65
	{
66 6
		$this->migrateSettings();
67 6
		$this->setReviewCounts();
68 6
	}
69
70
	/**
71
	 * @return void
72
	 */
73 6
	public function migrateSettings()
74
	{
75 6
		$this->newSettings = $this->getNewSettings();
76 6
		$this->oldSettings = $this->getOldSettings();
77 6
		if( empty( $this->oldSettings ))return;
78
		foreach( static::MAPPED_SETTINGS as $old => $new ) {
79
			if( empty( $this->oldSettings[$old] ))continue;
80
			$this->newSettings[$new] = $this->oldSettings[$old];
81
		}
82
		$this->migrateNotificationSettings();
83
		$this->migrateRecaptchaSettings();
84
		$this->migrateRequiredSettings();
85
		$oldSettings = glsr( Helper::class )->convertDotNotationArray( $this->oldSettings );
86
		$newSettings = glsr( Helper::class )->convertDotNotationArray( $this->newSettings );
87
		if( isset( $oldSettings['settings']['strings'] ) && is_array( $oldSettings['settings']['strings'] )) {
88
			$newSettings['settings']['strings'] = $oldSettings['settings']['strings'];
89
		}
90
		glsr( OptionManager::class )->set( $newSettings );
91
	}
92
93
	/**
94
	 * @return void
95
	 */
96
	public function setReviewCounts()
97
	{
98 7
		add_action( 'admin_init', function() {
99 1
			glsr( AdminController::class )->routerCountReviews( false );
100 7
		});
101 6
	}
102
103
	/**
104
	 * @return array
105
	 */
106 6
	protected function getNewSettings()
107
	{
108 6
		return wp_parse_args(
109 6
			glsr( Helper::class )->flattenArray( glsr( OptionManager::class )->all() ),
110 6
			glsr( DefaultsManager::class )->defaults()
111
		);
112
	}
113
114
	/**
115
	 * @return array
116
	 */
117 6
	protected function getOldSettings()
118
	{
119 6
		$defaults = array_fill_keys( array_keys( static::MAPPED_SETTINGS ), '' );
120 6
		$settings = glsr( Helper::class )->flattenArray( get_option( 'geminilabs_site_reviews-v2', [] ));
121 6
		if( !empty( $settings )) {
122
			$settings = wp_parse_args( $settings, $defaults );
123
		}
124 6
		return $settings;
125
	}
126
127
	/**
128
	 * @return void
129
	 */
130
	protected function migrateNotificationSettings()
131
	{
132
		$notifications = [
133
			'custom' => 'custom',
134
			'default' => 'admin',
135
			'webhook' => 'slack',
136
		];
137
		$this->newSettings['settings.general.notifications'] = [];
138
		foreach( $notifications as $old => $new ) {
139
			if( $this->oldSettings['settings.general.notification'] != $old )continue;
140
			$this->newSettings['settings.general.notifications'][] = $new;
141
		}
142
	}
143
144
	/**
145
	 * @return void
146
	 */
147
	protected function migrateRecaptchaSettings()
148
	{
149
		$recaptcha = [
150
			'BadgePosition' => $this->oldSettings['settings.reviews-form.recaptcha.position'],
151
			'SecretKey' => $this->oldSettings['settings.reviews-form.recaptcha.secret'],
152
			'SiteKey' => $this->oldSettings['settings.reviews-form.recaptcha.key'],
153
		];
154
		if( in_array( $this->oldSettings['settings.reviews-form.recaptcha.integration'], ['custom', 'invisible-recaptcha'] )) {
155
			$this->newSettings['settings.submissions.recaptcha.integration'] = 'all';
156
		}
157
		if( $this->oldSettings['settings.reviews-form.recaptcha.integration'] == 'invisible-recaptcha' ) {
158
			$recaptcha = wp_parse_args( (array)get_site_option( 'ic-settings', [], false ), $recaptcha );
159
		}
160
		$this->newSettings['settings.submissions.recaptcha.key'] = $recaptcha['SiteKey'];
161
		$this->newSettings['settings.submissions.recaptcha.secret'] = $recaptcha['SecretKey'];
162
		$this->newSettings['settings.submissions.recaptcha.position'] = $recaptcha['BadgePosition'];
163
	}
164
165
	/**
166
	 * @return void
167
	 */
168
	protected function migrateRequiredSettings()
169
	{
170
		$this->newSettings['settings.submissions.required'] = array_filter( (array)$this->oldSettings['settings.reviews-form.required'] );
171
		$this->newSettings['settings.submissions.required'][] = 'rating';
172
		$this->newSettings['settings.submissions.required'][] = 'terms';
173
	}
174
}
175