Passed
Push — master ( 6375bf...7ef419 )
by Paul
05:58
created

Upgrade_3_0_0::migrateRecaptchaSettings()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 4
nop 0
dl 0
loc 14
ccs 0
cts 14
cp 0
crap 12
rs 9.9332
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\Helper;
8
9
class Upgrade_3_0_0
10
{
11
	/**
12
	 * @var array
13
	 */
14
	protected $newSettings;
15
16
	/**
17
	 * @var array
18
	 */
19
	protected $oldSettings;
20
21
	public function __construct()
22
	{
23
		$this->newSettings = glsr( Helper::class )->flattenArray( glsr( OptionManager::class )->all() );
0 ignored issues
show
Bug introduced by
The type GeminiLabs\SiteReviews\M...\Upgrader\OptionManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
		$this->oldSettings = $this->getOldSettings();
25
		$this->migrateSettings();
26
		$this->setReviewCounts();
27
	}
28
29
	/**
30
	 * @return void
31
	 */
32
	public function migrateSettings()
33
	{
34
		$this->newSettings['settings.general.notification_slack'] = $this->oldSettings['settings.general.webhook_url'];
35
		$this->newSettings['settings.reviews.assigned_links'] = $this->oldSettings['settings.reviews.assigned_links.enabled'];
36
		$this->newSettings['settings.reviews.avatars'] = $this->oldSettings['settings.reviews.avatars.enabled'];
37
		$this->newSettings['settings.reviews.excerpts'] = $this->oldSettings['settings.reviews.excerpt.enabled'];
38
		$this->newSettings['settings.reviews.excerpts_length'] = $this->oldSettings['settings.reviews.excerpt.length'];
39
		$this->newSettings['settings.schema'] = $this->oldSettings['settings.reviews.schema'];
40
		$this->newSettings['settings.submissions'] = $this->oldSettings['settings.reviews-form'];
41
		$this->newSettings['settings.submissions'][] = 'rating';
42
		$this->newSettings['settings.submissions'][] = 'terms';
43
		$this->migrateNotificationSettings();
44
		$this->migrateRecaptchaSettings();
45
		glsr( OptionManager::class )->set( glsr( Helper::class )->convertDotNotationArray( $this->newSettings ));
46
	}
47
48
	/**
49
	 * @return void
50
	 */
51
	public function setReviewCounts()
52
	{
53
		add_action( 'admin_init', function() {
54
			glsr( AdminController::class )->routerCountReviews();
55
		});
56
	}
57
58
	/**
59
	 * @return array
60
	 */
61
	protected function getOldSettings()
62
	{
63
		$settings = glsr( Helper::class )->flattenArray( get_option( Application::ID.'-v2', [] ));
64
		return wp_parse_args( $settings, [
65
			'settings.general.notification' => '',
66
			'settings.general.webhook_url' => '',
67
			'settings.reviews.assigned_links.enabled' => 'no',
68
			'settings.reviews.avatars.enabled' => 'no',
69
			'settings.reviews.excerpt.enabled' => 'yes',
70
			'settings.reviews.excerpt.length' => 55,
71
			'settings.reviews.schema' => $this->newSettings['schema'],
72
			'settings.reviews-form' => $this->newSettings['submissions'],
73
			'settings.reviews-form.recaptcha.key' => $this->newSettings['submissions.recaptcha.key'],
74
			'settings.reviews-form.recaptcha.secret' => $this->newSettings['submissions.recaptcha.secret'],
75
			'settings.reviews-form.recaptcha.position' => $this->newSettings['submissions.recaptcha.position'],
76
		]);
77
	}
78
79
	/**
80
	 * @return void
81
	 */
82
	protected function migrateNotificationSettings()
83
	{
84
		$notifications = [
85
			'custom' => 'custom',
86
			'default' => 'admin',
87
			'webhook' => 'slack',
88
		];
89
		foreach( $notifications as $old => $new ) {
90
			if( $this->oldSettings['settings.general.notification'] != $old )continue;
91
			$this->newSettings['settings.general.notifications'][] = $new;
92
		}
93
	}
94
95
	/**
96
	 * @return void
97
	 */
98
	protected function migrateRecaptchaSettings()
99
	{
100
		if( in_array( $this->oldSettings['settings.reviews-form.recaptcha.integration'], ['custom', 'invisible-recaptcha'] )) {
101
			$this->newSettings['settings.submissions.recaptcha.integration'] == 'all';
102
		}
103
		if( $this->oldSettings['settings.reviews-form.recaptcha.integration'] == 'invisible-recaptcha' ) {
104
			$invisibleRecaptchaOptions = wp_parse_args( get_site_option( 'ic-settings', [] , false ). [
0 ignored issues
show
Bug introduced by
Are you sure get_site_option('ic-settings', array(), false) of type mixed|array can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

104
			$invisibleRecaptchaOptions = wp_parse_args( /** @scrutinizer ignore-type */ get_site_option( 'ic-settings', [] , false ). [
Loading history...
105
				'BadgePosition' => $this->oldSettings['settings.reviews-form.recaptcha.position'],
106
				'SecretKey' => $this->oldSettings['settings.reviews-form.recaptcha.secret'],
107
				'SiteKey' => $this->oldSettings['settings.reviews-form.recaptcha.key'],
108
			]);
109
			$this->newSettings['settings.submissions.recaptcha.key'] = $invisibleRecaptchaOptions['SiteKey'];
110
			$this->newSettings['settings.submissions.recaptcha.secret'] = $invisibleRecaptchaOptions['SecretKey'];
111
			$this->newSettings['settings.submissions.recaptcha.position'] = $invisibleRecaptchaOptions['BadgePosition'];
112
		}
113
	}
114
}
115