Passed
Push — master ( fe5b90...baa8e1 )
by Paul
05:38
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
10
class SettingsController extends Controller
11
{
12
	/**
13
	 * @param mixed $input
14
	 * @return array
15
	 * @callback register_setting
16
	 */
17 1
	public function callbackRegisterSettings( $input )
18
	{
19 1
		if( !is_array( $input )) {
20
			$input = ['settings' => []];
21
		}
22 1
		if( key( $input ) == 'settings' ) {
23
			$options = array_replace_recursive( glsr( OptionManager::class )->all(), $input );
24
			$options = $this->sanitizeGeneral( $input, $options );
25
			$options = $this->sanitizeSubmissions( $input, $options );
26
			$options = $this->sanitizeTranslations( $input, $options );
27
			glsr( Notice::class )->addSuccess( __( 'Settings updated.', 'site-reviews' ));
28
			return $options;
29
		}
30 1
		return $input;
31
	}
32
33
	/**
34
	 * @return void
35
	 * @action admin_init
36
	 */
37 1
	public function registerSettings()
38
	{
39 1
		register_setting( Application::ID.'-settings', OptionManager::databaseKey(), [
40 1
			'sanitize_callback' => [$this, 'callbackRegisterSettings'],
41
		]);
42 1
	}
43
44
	/**
45
	 * @return array
46
	 */
47
	protected function sanitizeGeneral( array $input, array $options )
48
	{
49
		$inputForm = $input['settings']['general'];
50
		if( !isset( $inputForm['notifications'] )) {
51
			$options['settings']['general']['notifications'] = [];
52
		}
53
		if( trim( $inputForm['notification_message'] ) == '' ) {
54
			$options['settings']['general']['notification_message'] = glsr()->defaults['settings']['general']['notification_message'];
55
		}
56
		return $options;
57
	}
58
59
	/**
60
	 * @return array
61
	 */
62
	protected function sanitizeSubmissions( array $input, array $options )
63
	{
64
		$inputForm = $input['settings']['submissions'];
65
		if( !isset( $inputForm['required'] )) {
66
			$options['settings']['submissions']['required'] = [];
67
		}
68
		return $options;
69
	}
70
71
	/**
72
	 * @return array
73
	 */
74
	protected function sanitizeTranslations( array $input, array $options )
75
	{
76
		if( isset( $input['settings']['strings'] )) {
77
			$options['settings']['strings'] = array_values( array_filter( $input['settings']['strings'] ));
78
			$allowedTags = ['a' => ['class' => [], 'href' => [], 'target' => []]];
79
			array_walk( $options['settings']['strings'], function( &$string ) use( $allowedTags ) {
80
				if( isset( $string['s2'] )) {
81
					$string['s2'] = wp_kses( $string['s2'], $allowedTags );
82
				}
83
				if( isset( $string['p2'] )) {
84
					$string['p2'] = wp_kses( $string['p2'], $allowedTags );
85
				}
86
			});
87
		}
88
		return $options;
89
	}
90
}
91