Passed
Push — master ( 3e60cb...36c2a1 )
by Paul
07:09 queued 03:38
created

SettingsController   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Test Coverage

Coverage 15.69%

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 103
ccs 8
cts 51
cp 0.1569
rs 10
c 0
b 0
f 0
wmc 19

6 Methods

Rating   Name   Duplication   Size   Complexity  
A registerSettings() 0 4 1
A callbackRegisterSettings() 0 16 4
A sanitizeGeneral() 0 13 5
A sanitizeSubmissions() 0 7 2
A isPolylangActiveAndSupported() 0 11 3
A sanitizeTranslations() 0 18 4
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Controllers;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Controllers\Controller;
7
use GeminiLabs\SiteReviews\Database\OptionManager;
8
use GeminiLabs\SiteReviews\Modules\Notice;
9
use GeminiLabs\SiteReviews\Modules\Polylang;
10
11
class SettingsController extends Controller
12
{
13
	/**
14
	 * @param mixed $input
15
	 * @return array
16
	 * @callback register_setting
17
	 */
18 1
	public function callbackRegisterSettings( $input )
19
	{
20 1
		if( !is_array( $input )) {
21
			$input = ['settings' => []];
22
		}
23 1
		if( key( $input ) == 'settings' ) {
24
			$options = array_replace_recursive( glsr( OptionManager::class )->all(), $input );
25
			$options = $this->sanitizeGeneral( $input, $options );
26
			$options = $this->sanitizeSubmissions( $input, $options );
27
			$options = $this->sanitizeTranslations( $input, $options );
28
			if( filter_input( INPUT_POST, 'option_page' ) == Application::ID.'-settings' ) {
29
				glsr( Notice::class )->addSuccess( __( 'Settings updated.', 'site-reviews' ));
30
			}
31
			return $options;
32
		}
33 1
		return $input;
34
	}
35
36
	/**
37
	 * @return void
38
	 * @action admin_init
39
	 */
40 1
	public function registerSettings()
41
	{
42 1
		register_setting( Application::ID.'-settings', OptionManager::databaseKey(), [
43 1
			'sanitize_callback' => [$this, 'callbackRegisterSettings'],
44
		]);
45 1
	}
46
47
	/**
48
	 * @return array
49
	 */
50
	protected function sanitizeGeneral( array $input, array $options )
51
	{
52
		$inputForm = $input['settings']['general'];
53
		if( $inputForm['support']['polylang'] == 'yes' && !$this->isPolylangActiveAndSupported() ) {
54
			$options['settings']['general']['support']['polylang'] = 'no';
55
		}
56
		if( trim( $inputForm['notification_message'] ) == '' ) {
57
			$options['settings']['general']['notification_message'] = glsr()->defaults['settings']['general']['notification_message'];
58
		}
59
		$options['settings']['general']['notifications'] = isset( $inputForm['notifications'] )
60
			? $inputForm['notifications']
61
			: [];
62
		return $options;
63
	}
64
65
	/**
66
	 * @return array
67
	 */
68
	protected function sanitizeSubmissions( array $input, array $options )
69
	{
70
		$inputForm = $input['settings']['submissions'];
71
		$options['settings']['submissions']['required'] = isset( $inputForm['required'] )
72
			? $inputForm['required']
73
			: [];
74
		return $options;
75
	}
76
77
	/**
78
	 * @return array
79
	 */
80
	protected function sanitizeTranslations( array $input, array $options )
81
	{
82
		if( isset( $input['settings']['strings'] )) {
83
			$options['settings']['strings'] = array_values( array_filter( $input['settings']['strings'] ));
84
			$allowedTags = [
85
				'a' => ['class' => [], 'href' => [], 'target' => []],
86
				'span' => ['class' => []],
87
			];
88
			array_walk( $options['settings']['strings'], function( &$string ) use( $allowedTags ) {
89
				if( isset( $string['s2'] )) {
90
					$string['s2'] = wp_kses( $string['s2'], $allowedTags );
91
				}
92
				if( isset( $string['p2'] )) {
93
					$string['p2'] = wp_kses( $string['p2'], $allowedTags );
94
				}
95
			});
96
		}
97
		return $options;
98
	}
99
100
	/**
101
	 * @return bool
102
	 */
103
	protected function isPolylangActiveAndSupported()
104
	{
105
		if( !glsr( Polylang::class )->isActive() ) {
106
			glsr( Notice::class )->addError( __( 'Please install/activate the Polylang plugin to enable integration.', 'site-reviews' ));
107
			return false;
108
		}
109
		else if( !glsr( Polylang::class )->isSupported() ) {
110
			glsr( Notice::class )->addError( __( 'Please update the Polylang plugin to v2.3.0 or greater to enable integration.', 'site-reviews' ));
111
			return false;
112
		}
113
		return true;
114
	}
115
}
116