Passed
Push — master ( 9a7a6b...7de9ff )
by Paul
03:37
created

SettingsController   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 17.39%

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 91
ccs 8
cts 46
cp 0.1739
rs 10
c 0
b 0
f 0
wmc 17

5 Methods

Rating   Name   Duplication   Size   Complexity  
A sanitizeGeneral() 0 20 6
A registerSettings() 0 4 1
A sanitizeTranslations() 0 15 4
A callbackRegisterSettings() 0 16 4
A sanitizeSubmissions() 0 7 2
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' ) {
54
			if( !glsr( Polylang::class )->isActive() ) {
55
				$options['settings']['general']['support']['polylang'] = 'no';
56
				glsr( Notice::class )->addError( __( 'Please install/activate the Polylang plugin to enable integration.', 'site-reviews' ));
57
			}
58
			else if( !glsr( Polylang::class )->isSupported() ) {
59
				$options['settings']['general']['support']['polylang'] = 'no';
60
				glsr( Notice::class )->addError( __( 'Please update the Polylang plugin to v2.3.0 or greater to enable integration.', 'site-reviews' ));
61
			}
62
		}
63
		if( !isset( $inputForm['notifications'] )) {
64
			$options['settings']['general']['notifications'] = [];
65
		}
66
		if( trim( $inputForm['notification_message'] ) == '' ) {
67
			$options['settings']['general']['notification_message'] = glsr()->defaults['settings']['general']['notification_message'];
68
		}
69
		return $options;
70
	}
71
72
	/**
73
	 * @return array
74
	 */
75
	protected function sanitizeSubmissions( array $input, array $options )
76
	{
77
		$inputForm = $input['settings']['submissions'];
78
		if( !isset( $inputForm['required'] )) {
79
			$options['settings']['submissions']['required'] = [];
80
		}
81
		return $options;
82
	}
83
84
	/**
85
	 * @return array
86
	 */
87
	protected function sanitizeTranslations( array $input, array $options )
88
	{
89
		if( isset( $input['settings']['strings'] )) {
90
			$options['settings']['strings'] = array_values( array_filter( $input['settings']['strings'] ));
91
			$allowedTags = ['a' => ['class' => [], 'href' => [], 'target' => []]];
92
			array_walk( $options['settings']['strings'], function( &$string ) use( $allowedTags ) {
93
				if( isset( $string['s2'] )) {
94
					$string['s2'] = wp_kses( $string['s2'], $allowedTags );
95
				}
96
				if( isset( $string['p2'] )) {
97
					$string['p2'] = wp_kses( $string['p2'], $allowedTags );
98
				}
99
			});
100
		}
101
		return $options;
102
	}
103
}
104