Passed
Push — master ( 4562dd...74d0bd )
by Paul
04:12
created

SettingsController::isPolylangActiveAndSupported()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 0
dl 0
loc 11
ccs 0
cts 8
cp 0
crap 12
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
			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'] = glsr_get( $inputForm, 'notifications', [] );
60
		return $options;
61
	}
62
63
	/**
64
	 * @return array
65
	 */
66
	protected function sanitizeSubmissions( array $input, array $options )
67
	{
68
		$inputForm = $input['settings']['submissions'];
69
		$options['settings']['submissions']['required'] = isset( $inputForm['required'] )
70
			? $inputForm['required']
71
			: [];
72
		return $options;
73
	}
74
75
	/**
76
	 * @return array
77
	 */
78
	protected function sanitizeTranslations( array $input, array $options )
79
	{
80
		if( isset( $input['settings']['strings'] )) {
81
			$options['settings']['strings'] = array_values( array_filter( $input['settings']['strings'] ));
82
			$allowedTags = [
83
				'a' => ['class' => [], 'href' => [], 'target' => []],
84
				'span' => ['class' => []],
85
			];
86
			array_walk( $options['settings']['strings'], function( &$string ) use( $allowedTags ) {
87
				if( isset( $string['s2'] )) {
88
					$string['s2'] = wp_kses( $string['s2'], $allowedTags );
89
				}
90
				if( isset( $string['p2'] )) {
91
					$string['p2'] = wp_kses( $string['p2'], $allowedTags );
92
				}
93
			});
94
		}
95
		return $options;
96
	}
97
98
	/**
99
	 * @return bool
100
	 */
101
	protected function isPolylangActiveAndSupported()
102
	{
103
		if( !glsr( Polylang::class )->isActive() ) {
104
			glsr( Notice::class )->addError( __( 'Please install/activate the Polylang plugin to enable integration.', 'site-reviews' ));
105
			return false;
106
		}
107
		else if( !glsr( Polylang::class )->isSupported() ) {
108
			glsr( Notice::class )->addError( __( 'Please update the Polylang plugin to v2.3.0 or greater to enable integration.', 'site-reviews' ));
109
			return false;
110
		}
111
		return true;
112
	}
113
}
114