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

SettingsController::sanitizeSubmissions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 7
ccs 0
cts 5
cp 0
crap 6
rs 10
c 0
b 0
f 0
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
			glsr( Notice::class )->addSuccess( __( 'Settings updated.', 'site-reviews' ));
29
			return $options;
30
		}
31 1
		return $input;
32
	}
33
34
	/**
35
	 * @return void
36
	 * @action admin_init
37
	 */
38 1
	public function registerSettings()
39
	{
40 1
		register_setting( Application::ID.'-settings', OptionManager::databaseKey(), [
41 1
			'sanitize_callback' => [$this, 'callbackRegisterSettings'],
42
		]);
43 1
	}
44
45
	/**
46
	 * @return array
47
	 */
48
	protected function sanitizeGeneral( array $input, array $options )
49
	{
50
		$inputForm = $input['settings']['general'];
51
		if( $inputForm['support']['polylang'] == 'yes' ) {
52
			if( !glsr( Polylang::class )->isActive() ) {
53
				$options['settings']['general']['support']['polylang'] = 'no';
54
				glsr( Notice::class )->addError( __( 'Please install/activate the Polylang plugin to enable integration.', 'site-reviews' ));
55
			}
56
			else if( !glsr( Polylang::class )->isSupported() ) {
57
				$options['settings']['general']['support']['polylang'] = 'no';
58
				glsr( Notice::class )->addError( __( 'Please update the Polylang plugin to v2.3.0 or greater to enable integration.', 'site-reviews' ));
59
			}
60
		}
61
		if( !isset( $inputForm['notifications'] )) {
62
			$options['settings']['general']['notifications'] = [];
63
		}
64
		if( trim( $inputForm['notification_message'] ) == '' ) {
65
			$options['settings']['general']['notification_message'] = glsr()->defaults['settings']['general']['notification_message'];
66
		}
67
		return $options;
68
	}
69
70
	/**
71
	 * @return array
72
	 */
73
	protected function sanitizeSubmissions( array $input, array $options )
74
	{
75
		$inputForm = $input['settings']['submissions'];
76
		if( !isset( $inputForm['required'] )) {
77
			$options['settings']['submissions']['required'] = [];
78
		}
79
		return $options;
80
	}
81
82
	/**
83
	 * @return array
84
	 */
85
	protected function sanitizeTranslations( array $input, array $options )
86
	{
87
		if( isset( $input['settings']['strings'] )) {
88
			$options['settings']['strings'] = array_values( array_filter( $input['settings']['strings'] ));
89
			$allowedTags = ['a' => ['class' => [], 'href' => [], 'target' => []]];
90
			array_walk( $options['settings']['strings'], function( &$string ) use( $allowedTags ) {
91
				if( isset( $string['s2'] )) {
92
					$string['s2'] = wp_kses( $string['s2'], $allowedTags );
93
				}
94
				if( isset( $string['p2'] )) {
95
					$string['p2'] = wp_kses( $string['p2'], $allowedTags );
96
				}
97
			});
98
		}
99
		return $options;
100
	}
101
}
102